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