]>
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); |
c55fb22b | 21 | //$host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
8cf37284 AD |
22 | |
23 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
c4ebf01e AD |
24 | } |
25 | ||
58210301 | 26 | function hook_enclosure_entry($enc) { |
046a0cc7 | 27 | if (preg_match("/image/", $enc["content_type"]) || preg_match("/\.(jpe?g|png|gif|bmp)$/i", $enc["filename"])) { |
bc83dcb3 | 28 | $proxy_all = $this->host->get($this, "proxy_all"); |
58210301 | 29 | |
ff4f2b1e | 30 | $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], 0, $proxy_all); |
bc83dcb3 | 31 | } |
58210301 AD |
32 | |
33 | return $enc; | |
34 | } | |
35 | ||
c55fb22b AD |
36 | function hook_article_filter($article) { |
37 | return $this->hook_render_article_cdm($article); | |
38 | } | |
39 | ||
c4ebf01e AD |
40 | function hook_render_article($article) { |
41 | return $this->hook_render_article_cdm($article); | |
42 | } | |
43 | ||
c93d43c6 | 44 | public function imgproxy() { |
4daaf234 | 45 | |
c93d43c6 AD |
46 | $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]); |
47 | $kind = (int) $_REQUEST["kind"]; // 1 = video | |
c4ebf01e | 48 | |
2187322c AD |
49 | // called without user context, let's just redirect to original URL |
50 | if (!$_SESSION["uid"]) { | |
51 | header("Location: $url"); | |
52 | return; | |
53 | } | |
54 | ||
c93d43c6 AD |
55 | $extension = $kind == 1 ? '.mp4' : '.png'; |
56 | $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension; | |
c4ebf01e | 57 | |
51198e7e | 58 | if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; } |
c4ebf01e | 59 | |
38b3998b | 60 | header("Content-Disposition: inline; filename=\"".basename($local_filename)."\""); |
c4ebf01e | 61 | |
c93d43c6 | 62 | if (file_exists($local_filename)) { |
38b3998b AD |
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 | ||
c93d43c6 | 69 | readfile($local_filename); |
c4ebf01e | 70 | } else { |
c55fb22b | 71 | $data = fetch_file_contents(array("url" => $url)); |
51198e7e | 72 | |
c93d43c6 | 73 | if ($data) { |
38b3998b AD |
74 | if (file_put_contents($local_filename, $data)) { |
75 | $mimetype = mime_content_type($local_filename); | |
76 | header("Content-type: $mimetype"); | |
77 | } | |
78 | ||
c93d43c6 | 79 | print $data; |
bf639865 AD |
80 | } else { |
81 | global $fetch_last_error; | |
82 | global $fetch_last_error_code; | |
83 | global $fetch_last_error_content; | |
84 | ||
c55fb22b | 85 | if (function_exists("imagecreate") && !isset($_REQUEST["text"])) { |
093d4633 | 86 | $img = imagecreate(450, 75); |
bf639865 AD |
87 | |
88 | $bg = imagecolorallocate($img, 255, 255, 255); | |
89 | $textcolor = imagecolorallocate($img, 255, 0, 0); | |
90 | ||
093d4633 | 91 | imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor); |
bf639865 AD |
92 | |
93 | imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor); | |
093d4633 | 94 | imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor); |
bf639865 AD |
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 | } | |
c93d43c6 | 111 | } |
c4ebf01e | 112 | } |
c4ebf01e AD |
113 | } |
114 | ||
8cf37284 | 115 | function rewrite_url_if_needed($url, $kind, $all_remote = false) { |
c4ebf01e AD |
116 | $scheme = parse_url($url, PHP_URL_SCHEME); |
117 | ||
8cf37284 AD |
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) { | |
c55fb22b | 129 | $url = get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" . |
51198e7e | 130 | urlencode($url); |
8cf37284 | 131 | } |
c4ebf01e AD |
132 | } |
133 | ||
134 | return $url; | |
135 | } | |
136 | ||
137 | function hook_render_article_cdm($article, $api_mode = false) { | |
138 | ||
139 | $need_saving = false; | |
8cf37284 | 140 | $proxy_all = $this->host->get($this, "proxy_all"); |
c4ebf01e AD |
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) { | |
8cf37284 | 148 | $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all); |
c4ebf01e AD |
149 | |
150 | if ($new_src != $img->getAttribute("src")) { | |
151 | $img->setAttribute("src", $new_src); | |
c55fb22b | 152 | $img->removeAttribute("srcset"); |
c4ebf01e AD |
153 | |
154 | $need_saving = true; | |
155 | } | |
156 | } | |
157 | ||
158 | $vids = $xpath->query("//video"); | |
159 | ||
160 | foreach ($vids as $vid) { | |
161 | if ($vid->hasAttribute("poster")) { | |
8cf37284 | 162 | $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all); |
c4ebf01e AD |
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) { | |
8cf37284 | 174 | $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all); |
c4ebf01e AD |
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 | ||
8cf37284 AD |
190 | function hook_prefs_tab($args) { |
191 | if ($args != "prefFeeds") return; | |
192 | ||
dc8bd8a6 | 193 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">"; |
8cf37284 AD |
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 " <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 | ||
c4ebf01e AD |
235 | function api_version() { |
236 | return 2; | |
237 | } | |
238 | } |