]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
9449a518b0800e3d228f2be855715f57376d659b
2 class Af_Zz_ImgProxy
extends Plugin
{
7 "Load insecure images via built-in proxy",
11 function init($host) {
14 $host->add_hook($host::HOOK_RENDER_ARTICLE
, $this);
15 $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM
, $this);
16 $host->add_hook($host::HOOK_RENDER_ARTICLE_API
, $this);
18 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
21 function hook_render_article($article) {
22 return $this->hook_render_article_cdm($article);
25 function hook_render_article_api($headline) {
26 return $this->hook_render_article_cdm($headline["headline"], true);
29 public function imgproxy() {
30 $url = rewrite_relative_url(SELF_URL_PATH
, $_REQUEST["url"]);
31 $kind = (int) $_REQUEST["kind"]; // 1 = video
33 $extension = $kind == 1 ?
'.mp4' : '.png';
34 $local_filename = CACHE_DIR
. "/images/" . sha1($url) . $extension;
36 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
38 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
40 if (file_exists($local_filename)) {
41 $mimetype = mime_content_type($local_filename);
42 header("Content-type: $mimetype");
44 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
45 header("Last-Modified: $stamp", true);
47 readfile($local_filename);
49 $data = fetch_file_contents(array("url" => $url));
51 global $fetch_last_error;
52 print $fetch_last_error;
55 if (file_put_contents($local_filename, $data)) {
56 $mimetype = mime_content_type($local_filename);
57 header("Content-type: $mimetype");
65 function rewrite_url_if_needed($url, $kind, $all_remote = false) {
66 $scheme = parse_url($url, PHP_URL_SCHEME
);
69 $host = parse_url($url, PHP_URL_HOST
);
70 $self_host = parse_url(SELF_URL_PATH
, PHP_URL_HOST
);
72 $is_remote = $host != $self_host;
77 if (($scheme != 'https' && $scheme != "") ||
$is_remote) {
78 if (strpos($url, "data:") !== 0) {
79 $url = "backend.php?op=pluginhandler&plugin=af_zz_imgproxy&method=imgproxy&kind=$kind&url=" .
87 function hook_render_article_cdm($article, $api_mode = false) {
90 $proxy_all = $this->host
->get($this, "proxy_all");
92 $doc = new DOMDocument();
93 if (@$doc->loadHTML($article["content"])) {
94 $xpath = new DOMXPath($doc);
95 $imgs = $xpath->query("//img[@src]");
97 foreach ($imgs as $img) {
98 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
100 if ($new_src != $img->getAttribute("src")) {
101 $img->setAttribute("src", $new_src);
107 $vids = $xpath->query("//video");
109 foreach ($vids as $vid) {
110 if ($vid->hasAttribute("poster")) {
111 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
113 if ($new_src != $vid->getAttribute("poster")) {
114 $vid->setAttribute("poster", $new_src);
120 $vsrcs = $xpath->query("source", $vid);
122 foreach ($vsrcs as $vsrc) {
123 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
125 if ($new_src != $vsrc->getAttribute("src")) {
126 $vid->setAttribute("src", $new_src);
134 if ($need_saving) $article["content"] = $doc->saveXML();
139 function hook_prefs_tab($args) {
140 if ($args != "prefFeeds") return;
142 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
144 print "<form dojoType=\"dijit.form.Form\">";
146 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
147 evt.preventDefault();
148 if (this.validate()) {
149 console.log(dojo.objectToQuery(this.getValues()));
150 new Ajax.Request('backend.php', {
151 parameters: dojo.objectToQuery(this.getValues()),
152 onComplete: function(transport) {
153 notify_info(transport.responseText);
160 print_hidden("op", "pluginhandler");
161 print_hidden("method", "save");
162 print_hidden("plugin", "af_zz_imgproxy");
164 $proxy_all = $this->host
->get($this, "proxy_all");
165 print_checkbox("proxy_all", $proxy_all);
167 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
169 print "<p>"; print_button("submit", __("Save"));
177 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
179 $this->host
->set($this, "proxy_all", $proxy_all);
181 echo __("Configuration saved");
184 function api_version() {