]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
2 class Af_Zz_ImgProxy
extends Plugin
{
7 "Load insecure images via built-in proxy",
11 function is_public_method($method) {
12 return $method === "imgproxy";
15 function init($host) {
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);
22 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
25 function hook_enclosure_entry($enc) {
26 if (preg_match("/image/", $enc["content_type"])) {
27 $proxy_all = $this->host
->get($this, "proxy_all");
29 $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
35 function hook_render_article($article) {
36 return $this->hook_render_article_cdm($article);
39 public function imgproxy() {
41 $url = rewrite_relative_url(SELF_URL_PATH
, $_REQUEST["url"]);
43 // called without user context, let's just redirect to original URL
44 if (!$_SESSION["uid"]) {
45 header("Location: $url");
49 $local_filename = CACHE_DIR
. "/images/" . sha1($url);
51 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
53 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
55 if (file_exists($local_filename)) {
56 $mimetype = mime_content_type($local_filename);
57 header("Content-type: $mimetype");
59 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
60 header("Last-Modified: $stamp", true);
62 readfile($local_filename);
64 $data = fetch_file_contents(array("url" => $url));
67 if (file_put_contents($local_filename, $data)) {
68 $mimetype = mime_content_type($local_filename);
69 header("Content-type: $mimetype");
74 global $fetch_last_error;
75 global $fetch_last_error_code;
76 global $fetch_last_error_content;
78 if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
79 $img = imagecreate(450, 75);
81 $bg = imagecolorallocate($img, 255, 255, 255);
82 $textcolor = imagecolorallocate($img, 255, 0, 0);
84 imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
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);
90 header("Content-type: image/png");
95 header("Content-type: text/html");
97 http_response_code(400);
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>";
108 function rewrite_url_if_needed($url, $all_remote = false) {
109 $scheme = parse_url($url, PHP_URL_SCHEME
);
112 $host = parse_url($url, PHP_URL_HOST
);
113 $self_host = parse_url(SELF_URL_PATH
, PHP_URL_HOST
);
115 $is_remote = $host != $self_host;
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=" .
130 function hook_render_article_cdm($article, $api_mode = false) {
132 $need_saving = false;
133 $proxy_all = $this->host
->get($this, "proxy_all");
135 $doc = new DOMDocument();
136 if (@$doc->loadHTML($article["content"])) {
137 $xpath = new DOMXPath($doc);
138 $imgs = $xpath->query("//img[@src]");
140 foreach ($imgs as $img) {
141 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
143 if ($new_src != $img->getAttribute("src")) {
144 $img->setAttribute("src", $new_src);
145 $img->removeAttribute("srcset");
151 $vids = $xpath->query("//video");
153 foreach ($vids as $vid) {
154 if ($vid->hasAttribute("poster")) {
155 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
157 if ($new_src != $vid->getAttribute("poster")) {
158 $vid->setAttribute("poster", $new_src);
164 $vsrcs = $xpath->query("source", $vid);
166 foreach ($vsrcs as $vsrc) {
167 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
169 if ($new_src != $vsrc->getAttribute("src")) {
170 $vid->setAttribute("src", $new_src);
178 if ($need_saving) $article["content"] = $doc->saveXML();
183 function hook_prefs_tab($args) {
184 if ($args != "prefFeeds") return;
186 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
188 print "<form dojoType=\"dijit.form.Form\">";
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);
204 print_hidden("op", "pluginhandler");
205 print_hidden("method", "save");
206 print_hidden("plugin", "af_zz_imgproxy");
208 $proxy_all = $this->host
->get($this, "proxy_all");
209 print_checkbox("proxy_all", $proxy_all);
211 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
213 print "<p>"; print_button("submit", __("Save"));
221 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
223 $this->host
->set($this, "proxy_all", $proxy_all);
225 echo __("Configuration saved");
228 function api_version() {