]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
53d3bf3ed08a30f347cc445ec42a4dcc740ae165
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_ENCLOSURE_ENTRY
, $this);
21 //$host->add_hook($host::HOOK_ARTICLE_FILTER, $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"], 0, $proxy_all);
36 function hook_article_filter($article) {
37 return $this->hook_render_article_cdm($article);
40 function hook_render_article($article) {
41 return $this->hook_render_article_cdm($article);
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");
81 global $fetch_last_error;
82 global $fetch_last_error_code;
83 global $fetch_last_error_content;
85 if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
86 $img = imagecreate(450, 75);
88 $bg = imagecolorallocate($img, 255, 255, 255);
89 $textcolor = imagecolorallocate($img, 255, 0, 0);
91 imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
93 imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor);
94 imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor);
95 imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor);
97 header("Content-type: image/png");
102 header("Content-type: text/html");
104 http_response_code(400);
106 print "<h1>Proxy request failed.</h1>";
107 print "<p>Fetch error $fetch_last_error ($fetch_last_error_code)</p>";
108 print "<p>URL: $url</p>";
109 print "<textarea cols='80' rows='25'>" . htmlspecialchars($fetch_last_error_content) . "</textarea>";
115 function rewrite_url_if_needed($url, $kind, $all_remote = false) {
116 $scheme = parse_url($url, PHP_URL_SCHEME
);
119 $host = parse_url($url, PHP_URL_HOST
);
120 $self_host = parse_url(SELF_URL_PATH
, PHP_URL_HOST
);
122 $is_remote = $host != $self_host;
127 if (($scheme != 'https' && $scheme != "") ||
$is_remote) {
128 if (strpos($url, "data:") !== 0) {
129 $url = get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
137 function hook_render_article_cdm($article, $api_mode = false) {
139 $need_saving = false;
140 $proxy_all = $this->host
->get($this, "proxy_all");
142 $doc = new DOMDocument();
143 if (@$doc->loadHTML($article["content"])) {
144 $xpath = new DOMXPath($doc);
145 $imgs = $xpath->query("//img[@src]");
147 foreach ($imgs as $img) {
148 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
150 if ($new_src != $img->getAttribute("src")) {
151 $img->setAttribute("src", $new_src);
152 $img->removeAttribute("srcset");
158 $vids = $xpath->query("//video");
160 foreach ($vids as $vid) {
161 if ($vid->hasAttribute("poster")) {
162 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
164 if ($new_src != $vid->getAttribute("poster")) {
165 $vid->setAttribute("poster", $new_src);
171 $vsrcs = $xpath->query("source", $vid);
173 foreach ($vsrcs as $vsrc) {
174 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
176 if ($new_src != $vsrc->getAttribute("src")) {
177 $vid->setAttribute("src", $new_src);
185 if ($need_saving) $article["content"] = $doc->saveXML();
190 function hook_prefs_tab($args) {
191 if ($args != "prefFeeds") return;
193 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
195 print "<form dojoType=\"dijit.form.Form\">";
197 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
198 evt.preventDefault();
199 if (this.validate()) {
200 console.log(dojo.objectToQuery(this.getValues()));
201 new Ajax.Request('backend.php', {
202 parameters: dojo.objectToQuery(this.getValues()),
203 onComplete: function(transport) {
204 notify_info(transport.responseText);
211 print_hidden("op", "pluginhandler");
212 print_hidden("method", "save");
213 print_hidden("plugin", "af_zz_imgproxy");
215 $proxy_all = $this->host
->get($this, "proxy_all");
216 print_checkbox("proxy_all", $proxy_all);
218 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
220 print "<p>"; print_button("submit", __("Save"));
228 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
230 $this->host
->set($this, "proxy_all", $proxy_all);
232 echo __("Configuration saved");
235 function api_version() {