]>
Commit | Line | Data |
---|---|---|
c4ebf01e AD |
1 | <?php |
2 | class Af_Zz_ImgProxy extends Plugin { | |
10bf7aa1 AD |
3 | |
4 | /* @var PluginHost $host */ | |
c4ebf01e AD |
5 | private $host; |
6 | ||
7 | function about() { | |
8 | return array(1.0, | |
c93d43c6 | 9 | "Load insecure images via built-in proxy", |
c4ebf01e AD |
10 | "fox"); |
11 | } | |
12 | ||
78f1116d | 13 | private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com"; |
ecab4354 | 14 | |
4daaf234 AD |
15 | function is_public_method($method) { |
16 | return $method === "imgproxy"; | |
17 | } | |
18 | ||
c4ebf01e AD |
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); | |
58210301 | 24 | $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this); |
8cf37284 AD |
25 | |
26 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
c4ebf01e AD |
27 | } |
28 | ||
58210301 | 29 | function hook_enclosure_entry($enc) { |
dc2c4b13 | 30 | if (preg_match("/image/", $enc["content_type"])) { |
bc83dcb3 | 31 | $proxy_all = $this->host->get($this, "proxy_all"); |
58210301 | 32 | |
41bead9b | 33 | $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all); |
bc83dcb3 | 34 | } |
58210301 AD |
35 | |
36 | return $enc; | |
37 | } | |
38 | ||
c4ebf01e AD |
39 | function hook_render_article($article) { |
40 | return $this->hook_render_article_cdm($article); | |
41 | } | |
42 | ||
c93d43c6 | 43 | public function imgproxy() { |
4daaf234 | 44 | |
b2d42e96 | 45 | $url = rewrite_relative_url(get_self_url_prefix(), $_REQUEST["url"]); |
c4ebf01e | 46 | |
2187322c AD |
47 | // called without user context, let's just redirect to original URL |
48 | if (!$_SESSION["uid"]) { | |
49 | header("Location: $url"); | |
50 | return; | |
51 | } | |
52 | ||
41bead9b | 53 | $local_filename = CACHE_DIR . "/images/" . sha1($url); |
c4ebf01e | 54 | |
51198e7e | 55 | if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; } |
c4ebf01e | 56 | |
38b3998b | 57 | header("Content-Disposition: inline; filename=\"".basename($local_filename)."\""); |
c4ebf01e | 58 | |
c93d43c6 | 59 | if (file_exists($local_filename)) { |
38b3998b | 60 | |
8b73bd28 | 61 | send_local_file($local_filename); |
38b3998b | 62 | |
c4ebf01e | 63 | } else { |
c55fb22b | 64 | $data = fetch_file_contents(array("url" => $url)); |
51198e7e | 65 | |
c93d43c6 | 66 | if ($data) { |
7611f0c0 AD |
67 | |
68 | $disable_cache = $this->host->get($this, "disable_cache"); | |
69 | ||
6fd03996 | 70 | if (!$disable_cache && strlen($data) > MIN_CACHE_FILE_SIZE) { |
7611f0c0 AD |
71 | if (file_put_contents($local_filename, $data)) { |
72 | $mimetype = mime_content_type($local_filename); | |
73 | header("Content-type: $mimetype"); | |
74 | } | |
38b3998b AD |
75 | } |
76 | ||
c93d43c6 | 77 | print $data; |
bf639865 AD |
78 | } else { |
79 | global $fetch_last_error; | |
80 | global $fetch_last_error_code; | |
81 | global $fetch_last_error_content; | |
82 | ||
c55fb22b | 83 | if (function_exists("imagecreate") && !isset($_REQUEST["text"])) { |
093d4633 | 84 | $img = imagecreate(450, 75); |
bf639865 | 85 | |
21ce7d9e | 86 | /*$bg =*/ imagecolorallocate($img, 255, 255, 255); |
bf639865 AD |
87 | $textcolor = imagecolorallocate($img, 255, 0, 0); |
88 | ||
093d4633 | 89 | imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor); |
bf639865 AD |
90 | |
91 | imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor); | |
093d4633 | 92 | imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor); |
bf639865 AD |
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 | } | |
c93d43c6 | 109 | } |
c4ebf01e | 110 | } |
c4ebf01e AD |
111 | } |
112 | ||
41bead9b | 113 | function rewrite_url_if_needed($url, $all_remote = false) { |
c4ebf01e AD |
114 | $scheme = parse_url($url, PHP_URL_SCHEME); |
115 | ||
8cf37284 AD |
116 | if ($all_remote) { |
117 | $host = parse_url($url, PHP_URL_HOST); | |
b2d42e96 | 118 | $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST); |
8cf37284 AD |
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) { | |
ecab4354 AD |
127 | $parts = parse_url($url); |
128 | ||
129 | foreach (explode(" " , $this->ssl_known_whitelist) as $host) { | |
f2fbb4ee | 130 | if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) { |
ecab4354 | 131 | $parts['scheme'] = 'https'; |
c7360f4a | 132 | $url = build_url($parts); |
133 | if ($all_remote && $is_remote) { | |
134 | break; | |
135 | } else { | |
136 | return $url; | |
137 | } | |
ecab4354 AD |
138 | } |
139 | } | |
140 | ||
141 | return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" . | |
51198e7e | 142 | urlencode($url); |
8cf37284 | 143 | } |
c4ebf01e AD |
144 | } |
145 | ||
146 | return $url; | |
147 | } | |
148 | ||
21ce7d9e AD |
149 | /** |
150 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
151 | */ | |
c4ebf01e AD |
152 | function hook_render_article_cdm($article, $api_mode = false) { |
153 | ||
154 | $need_saving = false; | |
8cf37284 | 155 | $proxy_all = $this->host->get($this, "proxy_all"); |
c4ebf01e AD |
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) { | |
41bead9b | 163 | $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all); |
c4ebf01e AD |
164 | |
165 | if ($new_src != $img->getAttribute("src")) { | |
166 | $img->setAttribute("src", $new_src); | |
c55fb22b | 167 | $img->removeAttribute("srcset"); |
c4ebf01e AD |
168 | |
169 | $need_saving = true; | |
170 | } | |
171 | } | |
172 | ||
173 | $vids = $xpath->query("//video"); | |
174 | ||
175 | foreach ($vids as $vid) { | |
176 | if ($vid->hasAttribute("poster")) { | |
41bead9b | 177 | $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all); |
c4ebf01e AD |
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) { | |
41bead9b | 189 | $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all); |
c4ebf01e AD |
190 | |
191 | if ($new_src != $vsrc->getAttribute("src")) { | |
192 | $vid->setAttribute("src", $new_src); | |
193 | ||
194 | $need_saving = true; | |
195 | } | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
f3774b9d | 200 | if ($need_saving) $article["content"] = $doc->saveHTML(); |
c4ebf01e AD |
201 | |
202 | return $article; | |
203 | } | |
204 | ||
8cf37284 AD |
205 | function hook_prefs_tab($args) { |
206 | if ($args != "prefFeeds") return; | |
207 | ||
dc8bd8a6 | 208 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">"; |
8cf37284 AD |
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); | |
7611f0c0 | 232 | print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label><br/>"; |
8cf37284 | 233 | |
7611f0c0 AD |
234 | $disable_cache = $this->host->get($this, "disable_cache"); |
235 | print_checkbox("disable_cache", $disable_cache); | |
236 | print " <label for=\"disable_cache\">" . __("Don't cache files locally.") . "</label>"; | |
8cf37284 AD |
237 | |
238 | print "<p>"; print_button("submit", __("Save")); | |
239 | ||
240 | print "</form>"; | |
241 | ||
242 | print "</div>"; | |
243 | } | |
244 | ||
245 | function save() { | |
da9ea57d AD |
246 | $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]); |
247 | $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]); | |
8cf37284 | 248 | |
7611f0c0 AD |
249 | $this->host->set($this, "proxy_all", $proxy_all, false); |
250 | $this->host->set($this, "disable_cache", $disable_cache); | |
8cf37284 AD |
251 | |
252 | echo __("Configuration saved"); | |
253 | } | |
254 | ||
c4ebf01e AD |
255 | function api_version() { |
256 | return 2; | |
257 | } | |
f3774b9d | 258 | } |