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