]> git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
when choosing enclosures to embed or rewrite (af_zz_imgproxy) only use content type...
[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
22 $host->add_hook($host::HOOK_PREFS_TAB, $this);
23 }
24
25 function hook_enclosure_entry($enc) {
26 if (preg_match("/image/", $enc["content_type"])) {
27 $proxy_all = $this->host->get($this, "proxy_all");
28
29 $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
30 }
31
32 return $enc;
33 }
34
35 function hook_render_article($article) {
36 return $this->hook_render_article_cdm($article);
37 }
38
39 public function imgproxy() {
40
41 $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
42
43 // called without user context, let's just redirect to original URL
44 if (!$_SESSION["uid"]) {
45 header("Location: $url");
46 return;
47 }
48
49 $local_filename = CACHE_DIR . "/images/" . sha1($url);
50
51 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
52
53 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
54
55 if (file_exists($local_filename)) {
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
62 readfile($local_filename);
63 } else {
64 $data = fetch_file_contents(array("url" => $url));
65
66 if ($data) {
67 if (file_put_contents($local_filename, $data)) {
68 $mimetype = mime_content_type($local_filename);
69 header("Content-type: $mimetype");
70 }
71
72 print $data;
73 } else {
74 global $fetch_last_error;
75 global $fetch_last_error_code;
76 global $fetch_last_error_content;
77
78 if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
79 $img = imagecreate(450, 75);
80
81 $bg = imagecolorallocate($img, 255, 255, 255);
82 $textcolor = imagecolorallocate($img, 255, 0, 0);
83
84 imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
85
86 imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor);
87 imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor);
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 }
104 }
105 }
106 }
107
108 function rewrite_url_if_needed($url, $all_remote = false) {
109 $scheme = parse_url($url, PHP_URL_SCHEME);
110
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) {
122 $url = get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
123 urlencode($url);
124 }
125 }
126
127 return $url;
128 }
129
130 function hook_render_article_cdm($article, $api_mode = false) {
131
132 $need_saving = false;
133 $proxy_all = $this->host->get($this, "proxy_all");
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) {
141 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
142
143 if ($new_src != $img->getAttribute("src")) {
144 $img->setAttribute("src", $new_src);
145 $img->removeAttribute("srcset");
146
147 $need_saving = true;
148 }
149 }
150
151 $vids = $xpath->query("//video");
152
153 foreach ($vids as $vid) {
154 if ($vid->hasAttribute("poster")) {
155 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
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) {
167 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
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
183 function hook_prefs_tab($args) {
184 if ($args != "prefFeeds") return;
185
186 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
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
228 function api_version() {
229 return 2;
230 }
231 }