]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
2 class Af_Zz_ImgProxy
extends Plugin
{
7 "Load insecure images via built-in proxy",
11 function is_public_method($method) {
12 return $method === "imgproxy";
15 function init($host) {
18 $host->add_hook($host::HOOK_RENDER_ARTICLE
, $this);
19 $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM
, $this);
20 $host->add_hook($host::HOOK_RENDER_ARTICLE_API
, $this);
21 $host->add_hook($host::HOOK_ENCLOSURE_ENTRY
, $this);
23 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
26 function hook_enclosure_entry($enc) {
27 if (preg_match("/image/", $enc["content_type"]) ||
preg_match("/\.(jpe?g|png|gif|bmp)$/i", $enc["filename"])) {
28 $proxy_all = $this->host
->get($this, "proxy_all");
30 $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
36 function hook_render_article($article) {
37 return $this->hook_render_article_cdm($article);
40 function hook_render_article_api($headline) {
41 return $this->hook_render_article_cdm($headline["headline"], true);
44 public function imgproxy() {
46 $url = rewrite_relative_url(SELF_URL_PATH
, $_REQUEST["url"]);
47 $kind = (int) $_REQUEST["kind"]; // 1 = video
49 // called without user context, let's just redirect to original URL
50 if (!$_SESSION["uid"]) {
51 header("Location: $url");
55 $extension = $kind == 1 ?
'.mp4' : '.png';
56 $local_filename = CACHE_DIR
. "/images/" . sha1($url) . $extension;
58 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
60 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
62 if (file_exists($local_filename)) {
63 $mimetype = mime_content_type($local_filename);
64 header("Content-type: $mimetype");
66 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
67 header("Last-Modified: $stamp", true);
69 readfile($local_filename);
71 $data = fetch_file_contents(array("url" => $url));
74 if (file_put_contents($local_filename, $data)) {
75 $mimetype = mime_content_type($local_filename);
76 header("Content-type: $mimetype");
84 function rewrite_url_if_needed($url, $kind, $all_remote = false) {
85 $scheme = parse_url($url, PHP_URL_SCHEME
);
88 $host = parse_url($url, PHP_URL_HOST
);
89 $self_host = parse_url(SELF_URL_PATH
, PHP_URL_HOST
);
91 $is_remote = $host != $self_host;
96 if (($scheme != 'https' && $scheme != "") ||
$is_remote) {
97 if (strpos($url, "data:") !== 0) {
98 $url = "public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
106 function hook_render_article_cdm($article, $api_mode = false) {
108 $need_saving = false;
109 $proxy_all = $this->host
->get($this, "proxy_all");
111 $doc = new DOMDocument();
112 if (@$doc->loadHTML($article["content"])) {
113 $xpath = new DOMXPath($doc);
114 $imgs = $xpath->query("//img[@src]");
116 foreach ($imgs as $img) {
117 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
119 if ($new_src != $img->getAttribute("src")) {
120 $img->setAttribute("src", $new_src);
126 $vids = $xpath->query("//video");
128 foreach ($vids as $vid) {
129 if ($vid->hasAttribute("poster")) {
130 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
132 if ($new_src != $vid->getAttribute("poster")) {
133 $vid->setAttribute("poster", $new_src);
139 $vsrcs = $xpath->query("source", $vid);
141 foreach ($vsrcs as $vsrc) {
142 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
144 if ($new_src != $vsrc->getAttribute("src")) {
145 $vid->setAttribute("src", $new_src);
153 if ($need_saving) $article["content"] = $doc->saveXML();
158 function hook_prefs_tab($args) {
159 if ($args != "prefFeeds") return;
161 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
163 print "<form dojoType=\"dijit.form.Form\">";
165 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
166 evt.preventDefault();
167 if (this.validate()) {
168 console.log(dojo.objectToQuery(this.getValues()));
169 new Ajax.Request('backend.php', {
170 parameters: dojo.objectToQuery(this.getValues()),
171 onComplete: function(transport) {
172 notify_info(transport.responseText);
179 print_hidden("op", "pluginhandler");
180 print_hidden("method", "save");
181 print_hidden("plugin", "af_zz_imgproxy");
183 $proxy_all = $this->host
->get($this, "proxy_all");
184 print_checkbox("proxy_all", $proxy_all);
186 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
188 print "<p>"; print_button("submit", __("Save"));
196 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
198 $this->host
->set($this, "proxy_all", $proxy_all);
200 echo __("Configuration saved");
203 function api_version() {