]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
a6f0083245673ea8b1bf26f9f4e876e663fb6f2b
[tt-rss.git] / plugins / af_zz_imgproxy / init.php
1 <?php
2 class Af_Zz_ImgProxy extends Plugin {
3         private $host;
4
5         function about() {
6                 return array(1.0,
7                         "Load insecure images via built-in proxy",
8                         "fox");
9         }
10
11         private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com";
12
13         function is_public_method($method) {
14                 return $method === "imgproxy";
15         }
16
17         function init($host) {
18                 $this->host = $host;
19
20                 $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
21                 $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
22                 $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
23
24                 $host->add_hook($host::HOOK_PREFS_TAB, $this);
25         }
26
27         function hook_enclosure_entry($enc) {
28                 if (preg_match("/image/", $enc["content_type"])) {
29                         $proxy_all = $this->host->get($this, "proxy_all");
30
31                         $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
32                 }
33
34                 return $enc;
35         }
36
37         function hook_render_article($article) {
38                 return $this->hook_render_article_cdm($article);
39         }
40
41         public function imgproxy() {
42
43                 $url = rewrite_relative_url(get_self_url_prefix(), $_REQUEST["url"]);
44
45                 // called without user context, let's just redirect to original URL
46                 if (!$_SESSION["uid"]) {
47                         header("Location: $url");
48                         return;
49                 }
50
51                 $local_filename = CACHE_DIR . "/images/" . sha1($url);
52
53                 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
54
55                 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
56
57                 if (file_exists($local_filename)) {
58
59                         send_local_file($local_filename);
60
61                 } else {
62                         $data = fetch_file_contents(array("url" => $url));
63
64                         if ($data) {
65
66                                 $disable_cache = $this->host->get($this, "disable_cache");
67
68                                 if (!$disable_cache && strlen($data) > MIN_CACHE_FILE_SIZE) {
69                                         if (file_put_contents($local_filename, $data)) {
70                                                 $mimetype = mime_content_type($local_filename);
71                                                 header("Content-type: $mimetype");
72                                         }
73                                 }
74
75                                 print $data;
76                         } else {
77                                 global $fetch_last_error;
78                                 global $fetch_last_error_code;
79                                 global $fetch_last_error_content;
80
81                                 if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
82                                         $img = imagecreate(450, 75);
83
84                                         /*$bg =*/ imagecolorallocate($img, 255, 255, 255);
85                                         $textcolor = imagecolorallocate($img, 255, 0, 0);
86
87                                         imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
88
89                                         imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor);
90                                         imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor);
91                                         imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor);
92
93                                         header("Content-type: image/png");
94                                         print imagepng($img);
95                                         imagedestroy($img);
96
97                                 } else {
98                                         header("Content-type: text/html");
99
100                                         http_response_code(400);
101
102                                         print "<h1>Proxy request failed.</h1>";
103                                         print "<p>Fetch error $fetch_last_error ($fetch_last_error_code)</p>";
104                                         print "<p>URL: $url</p>";
105                                         print "<textarea cols='80' rows='25'>" . htmlspecialchars($fetch_last_error_content) . "</textarea>";
106                                 }
107                         }
108                 }
109         }
110
111         function rewrite_url_if_needed($url, $all_remote = false) {
112                 $scheme = parse_url($url, PHP_URL_SCHEME);
113
114                 if ($all_remote) {
115                         $host = parse_url($url, PHP_URL_HOST);
116                         $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST);
117
118                         $is_remote = $host != $self_host;
119                 } else {
120                         $is_remote = false;
121                 }
122
123                 if (($scheme != 'https' && $scheme != "") || $is_remote) {
124                         if (strpos($url, "data:") !== 0) {
125                                 $parts = parse_url($url);
126
127                                 foreach (explode(" " , $this->ssl_known_whitelist) as $host) {
128                                         if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) {
129                                                 $parts['scheme'] = 'https';
130                                                 $url = build_url($parts);
131                                                 if ($all_remote && $is_remote) {
132                                                         break;
133                                                 } else {
134                                                         return $url;
135                                                 }
136                                         }
137                                 }
138
139                                 return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
140                                         urlencode($url);
141                         }
142                 }
143
144                 return $url;
145         }
146
147         /**
148          * @SuppressWarnings(PHPMD.UnusedFormalParameter)
149          */
150         function hook_render_article_cdm($article, $api_mode = false) {
151
152                 $need_saving = false;
153                 $proxy_all = $this->host->get($this, "proxy_all");
154
155                 $doc = new DOMDocument();
156                 if (@$doc->loadHTML($article["content"])) {
157                         $xpath = new DOMXPath($doc);
158                         $imgs = $xpath->query("//img[@src]");
159
160                         foreach ($imgs as $img) {
161                                 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
162
163                                 if ($new_src != $img->getAttribute("src")) {
164                                         $img->setAttribute("src", $new_src);
165                                         $img->removeAttribute("srcset");
166
167                                         $need_saving = true;
168                                 }
169                         }
170
171                         $vids = $xpath->query("//video");
172
173                         foreach ($vids as $vid) {
174                                 if ($vid->hasAttribute("poster")) {
175                                         $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
176
177                                         if ($new_src != $vid->getAttribute("poster")) {
178                                                 $vid->setAttribute("poster", $new_src);
179
180                                                 $need_saving = true;
181                                         }
182                                 }
183
184                                 $vsrcs = $xpath->query("source", $vid);
185
186                                 foreach ($vsrcs as $vsrc) {
187                                         $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
188
189                                         if ($new_src != $vsrc->getAttribute("src")) {
190                                                 $vid->setAttribute("src", $new_src);
191
192                                                 $need_saving = true;
193                                         }
194                                 }
195                         }
196                 }
197
198                 if ($need_saving) $article["content"] = $doc->saveHTML();
199
200                 return $article;
201         }
202
203         function hook_prefs_tab($args) {
204                 if ($args != "prefFeeds") return;
205
206                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
207
208                 print "<form dojoType=\"dijit.form.Form\">";
209
210                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
211                         evt.preventDefault();
212                         if (this.validate()) {
213                                 console.log(dojo.objectToQuery(this.getValues()));
214                                 new Ajax.Request('backend.php', {
215                                         parameters: dojo.objectToQuery(this.getValues()),
216                                         onComplete: function(transport) {
217                                                 notify_info(transport.responseText);
218                                         }
219                                 });
220                                 //this.reset();
221                         }
222                         </script>";
223
224                 print_hidden("op", "pluginhandler");
225                 print_hidden("method", "save");
226                 print_hidden("plugin", "af_zz_imgproxy");
227
228                 $proxy_all = $this->host->get($this, "proxy_all");
229                 print_checkbox("proxy_all", $proxy_all);
230                 print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label><br/>";
231
232                 $disable_cache = $this->host->get($this, "disable_cache");
233                 print_checkbox("disable_cache", $disable_cache);
234                 print "&nbsp;<label for=\"disable_cache\">" . __("Don't cache files locally.") . "</label>";
235
236                 print "<p>"; print_button("submit", __("Save"));
237
238                 print "</form>";
239
240                 print "</div>";
241         }
242
243         function save() {
244                 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]);
245                 $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]);
246
247                 $this->host->set($this, "proxy_all", $proxy_all, false);
248                 $this->host->set($this, "disable_cache", $disable_cache);
249
250                 echo __("Configuration saved");
251         }
252
253         function api_version() {
254                 return 2;
255         }
256 }