]> git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
53d3bf3ed08a30f347cc445ec42a4dcc740ae165
[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 function is_public_method($method) {
12 return $method === "imgproxy";
13 }
14
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);
20 $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
21 //$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
22
23 $host->add_hook($host::HOOK_PREFS_TAB, $this);
24 }
25
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");
29
30 $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], 0, $proxy_all);
31 }
32
33 return $enc;
34 }
35
36 function hook_article_filter($article) {
37 return $this->hook_render_article_cdm($article);
38 }
39
40 function hook_render_article($article) {
41 return $this->hook_render_article_cdm($article);
42 }
43
44 public function imgproxy() {
45
46 $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
47 $kind = (int) $_REQUEST["kind"]; // 1 = video
48
49 // called without user context, let's just redirect to original URL
50 if (!$_SESSION["uid"]) {
51 header("Location: $url");
52 return;
53 }
54
55 $extension = $kind == 1 ? '.mp4' : '.png';
56 $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension;
57
58 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
59
60 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
61
62 if (file_exists($local_filename)) {
63 $mimetype = mime_content_type($local_filename);
64 header("Content-type: $mimetype");
65
66 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
67 header("Last-Modified: $stamp", true);
68
69 readfile($local_filename);
70 } else {
71 $data = fetch_file_contents(array("url" => $url));
72
73 if ($data) {
74 if (file_put_contents($local_filename, $data)) {
75 $mimetype = mime_content_type($local_filename);
76 header("Content-type: $mimetype");
77 }
78
79 print $data;
80 } else {
81 global $fetch_last_error;
82 global $fetch_last_error_code;
83 global $fetch_last_error_content;
84
85 if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
86 $img = imagecreate(450, 75);
87
88 $bg = imagecolorallocate($img, 255, 255, 255);
89 $textcolor = imagecolorallocate($img, 255, 0, 0);
90
91 imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
92
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);
96
97 header("Content-type: image/png");
98 print imagepng($img);
99 imagedestroy($img);
100
101 } else {
102 header("Content-type: text/html");
103
104 http_response_code(400);
105
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>";
110 }
111 }
112 }
113 }
114
115 function rewrite_url_if_needed($url, $kind, $all_remote = false) {
116 $scheme = parse_url($url, PHP_URL_SCHEME);
117
118 if ($all_remote) {
119 $host = parse_url($url, PHP_URL_HOST);
120 $self_host = parse_url(SELF_URL_PATH, PHP_URL_HOST);
121
122 $is_remote = $host != $self_host;
123 } else {
124 $is_remote = false;
125 }
126
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=" .
130 urlencode($url);
131 }
132 }
133
134 return $url;
135 }
136
137 function hook_render_article_cdm($article, $api_mode = false) {
138
139 $need_saving = false;
140 $proxy_all = $this->host->get($this, "proxy_all");
141
142 $doc = new DOMDocument();
143 if (@$doc->loadHTML($article["content"])) {
144 $xpath = new DOMXPath($doc);
145 $imgs = $xpath->query("//img[@src]");
146
147 foreach ($imgs as $img) {
148 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
149
150 if ($new_src != $img->getAttribute("src")) {
151 $img->setAttribute("src", $new_src);
152 $img->removeAttribute("srcset");
153
154 $need_saving = true;
155 }
156 }
157
158 $vids = $xpath->query("//video");
159
160 foreach ($vids as $vid) {
161 if ($vid->hasAttribute("poster")) {
162 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
163
164 if ($new_src != $vid->getAttribute("poster")) {
165 $vid->setAttribute("poster", $new_src);
166
167 $need_saving = true;
168 }
169 }
170
171 $vsrcs = $xpath->query("source", $vid);
172
173 foreach ($vsrcs as $vsrc) {
174 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
175
176 if ($new_src != $vsrc->getAttribute("src")) {
177 $vid->setAttribute("src", $new_src);
178
179 $need_saving = true;
180 }
181 }
182 }
183 }
184
185 if ($need_saving) $article["content"] = $doc->saveXML();
186
187 return $article;
188 }
189
190 function hook_prefs_tab($args) {
191 if ($args != "prefFeeds") return;
192
193 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
194
195 print "<form dojoType=\"dijit.form.Form\">";
196
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);
205 }
206 });
207 //this.reset();
208 }
209 </script>";
210
211 print_hidden("op", "pluginhandler");
212 print_hidden("method", "save");
213 print_hidden("plugin", "af_zz_imgproxy");
214
215 $proxy_all = $this->host->get($this, "proxy_all");
216 print_checkbox("proxy_all", $proxy_all);
217
218 print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
219
220 print "<p>"; print_button("submit", __("Save"));
221
222 print "</form>";
223
224 print "</div>";
225 }
226
227 function save() {
228 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
229
230 $this->host->set($this, "proxy_all", $proxy_all);
231
232 echo __("Configuration saved");
233 }
234
235 function api_version() {
236 return 2;
237 }
238 }