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