]>
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_RENDER_ARTICLE_API
, $this);
21 $host->add_hook($host::HOOK_ENCLOSURE_ENTRY
, $this);
23 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
26 function hook_enclosure_entry($enc) {
27 $proxy_all = $this->host
->get($this, "proxy_all");
29 $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
34 function hook_render_article($article) {
35 return $this->hook_render_article_cdm($article);
38 function hook_render_article_api($headline) {
39 return $this->hook_render_article_cdm($headline["headline"], true);
42 public function imgproxy() {
44 $url = rewrite_relative_url(SELF_URL_PATH
, $_REQUEST["url"]);
45 $kind = (int) $_REQUEST["kind"]; // 1 = video
47 // called without user context, let's just redirect to original URL
48 if (!$_SESSION["uid"]) {
49 header("Location: $url");
53 $extension = $kind == 1 ?
'.mp4' : '.png';
54 $local_filename = CACHE_DIR
. "/images/" . sha1($url) . $extension;
56 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
58 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
60 if (file_exists($local_filename)) {
61 $mimetype = mime_content_type($local_filename);
62 header("Content-type: $mimetype");
64 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
65 header("Last-Modified: $stamp", true);
67 readfile($local_filename);
69 $data = fetch_file_contents(array("url" => $url));
72 if (file_put_contents($local_filename, $data)) {
73 $mimetype = mime_content_type($local_filename);
74 header("Content-type: $mimetype");
82 function rewrite_url_if_needed($url, $kind, $all_remote = false) {
83 $scheme = parse_url($url, PHP_URL_SCHEME
);
86 $host = parse_url($url, PHP_URL_HOST
);
87 $self_host = parse_url(SELF_URL_PATH
, PHP_URL_HOST
);
89 $is_remote = $host != $self_host;
94 if (($scheme != 'https' && $scheme != "") ||
$is_remote) {
95 if (strpos($url, "data:") !== 0) {
96 $url = "public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
104 function hook_render_article_cdm($article, $api_mode = false) {
106 $need_saving = false;
107 $proxy_all = $this->host
->get($this, "proxy_all");
109 $doc = new DOMDocument();
110 if (@$doc->loadHTML($article["content"])) {
111 $xpath = new DOMXPath($doc);
112 $imgs = $xpath->query("//img[@src]");
114 foreach ($imgs as $img) {
115 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
117 if ($new_src != $img->getAttribute("src")) {
118 $img->setAttribute("src", $new_src);
124 $vids = $xpath->query("//video");
126 foreach ($vids as $vid) {
127 if ($vid->hasAttribute("poster")) {
128 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
130 if ($new_src != $vid->getAttribute("poster")) {
131 $vid->setAttribute("poster", $new_src);
137 $vsrcs = $xpath->query("source", $vid);
139 foreach ($vsrcs as $vsrc) {
140 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
142 if ($new_src != $vsrc->getAttribute("src")) {
143 $vid->setAttribute("src", $new_src);
151 if ($need_saving) $article["content"] = $doc->saveXML();
156 function hook_prefs_tab($args) {
157 if ($args != "prefFeeds") return;
159 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
161 print "<form dojoType=\"dijit.form.Form\">";
163 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
164 evt.preventDefault();
165 if (this.validate()) {
166 console.log(dojo.objectToQuery(this.getValues()));
167 new Ajax.Request('backend.php', {
168 parameters: dojo.objectToQuery(this.getValues()),
169 onComplete: function(transport) {
170 notify_info(transport.responseText);
177 print_hidden("op", "pluginhandler");
178 print_hidden("method", "save");
179 print_hidden("plugin", "af_zz_imgproxy");
181 $proxy_all = $this->host
->get($this, "proxy_all");
182 print_checkbox("proxy_all", $proxy_all);
184 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
186 print "<p>"; print_button("submit", __("Save"));
194 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
196 $this->host
->set($this, "proxy_all", $proxy_all);
198 echo __("Configuration saved");
201 function api_version() {