]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
2 class Af_Zz_ImgProxy
extends Plugin
{
4 /* @var PluginHost $host */
9 "Load insecure images via built-in proxy",
13 private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com";
15 function is_public_method($method) {
16 return $method === "imgproxy";
19 function init($host) {
22 $host->add_hook($host::HOOK_RENDER_ARTICLE
, $this);
23 $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM
, $this);
24 $host->add_hook($host::HOOK_ENCLOSURE_ENTRY
, $this);
26 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
29 function hook_enclosure_entry($enc) {
30 if (preg_match("/image/", $enc["content_type"])) {
31 $proxy_all = $this->host
->get($this, "proxy_all");
33 $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
39 function hook_render_article($article) {
40 return $this->hook_render_article_cdm($article);
43 public function imgproxy() {
45 $url = rewrite_relative_url(get_self_url_prefix(), $_REQUEST["url"]);
47 // called without user context, let's just redirect to original URL
48 if (!$_SESSION["uid"]) {
49 header("Location: $url");
53 $local_filename = CACHE_DIR
. "/images/" . sha1($url);
55 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
57 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
59 if (file_exists($local_filename)) {
61 send_local_file($local_filename);
64 $data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE
]);
68 $disable_cache = $this->host
->get($this, "disable_cache");
70 if (!$disable_cache && strlen($data) > MIN_CACHE_FILE_SIZE
) {
71 if (file_put_contents($local_filename, $data)) {
72 $mimetype = mime_content_type($local_filename);
73 header("Content-type: $mimetype");
79 global $fetch_last_error;
80 global $fetch_last_error_code;
81 global $fetch_last_error_content;
83 if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
84 $img = imagecreate(450, 75);
86 /*$bg =*/ imagecolorallocate($img, 255, 255, 255);
87 $textcolor = imagecolorallocate($img, 255, 0, 0);
89 imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
91 imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor);
92 imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor);
93 imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor);
95 header("Content-type: image/png");
100 header("Content-type: text/html");
102 http_response_code(400);
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>";
113 function rewrite_url_if_needed($url, $all_remote = false) {
114 $scheme = parse_url($url, PHP_URL_SCHEME
);
117 $host = parse_url($url, PHP_URL_HOST
);
118 $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST
);
120 $is_remote = $host != $self_host;
125 if (($scheme != 'https' && $scheme != "") ||
$is_remote) {
126 if (strpos($url, "data:") !== 0) {
127 $parts = parse_url($url);
129 foreach (explode(" " , $this->ssl_known_whitelist
) as $host) {
130 if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) {
131 $parts['scheme'] = 'https';
132 $url = build_url($parts);
133 if ($all_remote && $is_remote) {
141 return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
150 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
152 function hook_render_article_cdm($article, $api_mode = false) {
154 $need_saving = false;
155 $proxy_all = $this->host
->get($this, "proxy_all");
157 $doc = new DOMDocument();
158 if (@$doc->loadHTML($article["content"])) {
159 $xpath = new DOMXPath($doc);
160 $imgs = $xpath->query("//img[@src]");
162 foreach ($imgs as $img) {
163 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
165 if ($new_src != $img->getAttribute("src")) {
166 $img->setAttribute("src", $new_src);
167 $img->removeAttribute("srcset");
173 $vids = $xpath->query("//video");
175 foreach ($vids as $vid) {
176 if ($vid->hasAttribute("poster")) {
177 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
179 if ($new_src != $vid->getAttribute("poster")) {
180 $vid->setAttribute("poster", $new_src);
186 $vsrcs = $xpath->query("source", $vid);
188 foreach ($vsrcs as $vsrc) {
189 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
191 if ($new_src != $vsrc->getAttribute("src")) {
192 $vid->setAttribute("src", $new_src);
200 if ($need_saving) $article["content"] = $doc->saveHTML();
205 function hook_prefs_tab($args) {
206 if ($args != "prefFeeds") return;
208 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
210 print "<form dojoType=\"dijit.form.Form\">";
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);
226 print_hidden("op", "pluginhandler");
227 print_hidden("method", "save");
228 print_hidden("plugin", "af_zz_imgproxy");
230 $proxy_all = $this->host
->get($this, "proxy_all");
231 print_checkbox("proxy_all", $proxy_all);
232 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label><br/>";
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>";
238 print "<p>"; print_button("submit", __("Save"));
246 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]);
247 $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]);
249 $this->host
->set($this, "proxy_all", $proxy_all, false);
250 $this->host
->set($this, "disable_cache", $disable_cache);
252 echo __("Configuration saved");
255 function api_version() {