]>
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);
22 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
25 function hook_render_article($article) {
26 return $this->hook_render_article_cdm($article);
29 function hook_render_article_api($headline) {
30 return $this->hook_render_article_cdm($headline["headline"], true);
33 public function imgproxy() {
35 $url = rewrite_relative_url(SELF_URL_PATH
, $_REQUEST["url"]);
36 $kind = (int) $_REQUEST["kind"]; // 1 = video
38 $extension = $kind == 1 ?
'.mp4' : '.png';
39 $local_filename = CACHE_DIR
. "/images/" . sha1($url) . $extension;
41 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
43 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
45 if (file_exists($local_filename)) {
46 $mimetype = mime_content_type($local_filename);
47 header("Content-type: $mimetype");
49 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
50 header("Last-Modified: $stamp", true);
52 readfile($local_filename);
54 $data = fetch_file_contents(array("url" => $url));
57 if (file_put_contents($local_filename, $data)) {
58 $mimetype = mime_content_type($local_filename);
59 header("Content-type: $mimetype");
67 function rewrite_url_if_needed($url, $kind, $all_remote = false) {
68 $scheme = parse_url($url, PHP_URL_SCHEME
);
71 $host = parse_url($url, PHP_URL_HOST
);
72 $self_host = parse_url(SELF_URL_PATH
, PHP_URL_HOST
);
74 $is_remote = $host != $self_host;
79 if (($scheme != 'https' && $scheme != "") ||
$is_remote) {
80 if (strpos($url, "data:") !== 0) {
81 $url = "public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
89 function hook_render_article_cdm($article, $api_mode = false) {
92 $proxy_all = $this->host
->get($this, "proxy_all");
94 $doc = new DOMDocument();
95 if (@$doc->loadHTML($article["content"])) {
96 $xpath = new DOMXPath($doc);
97 $imgs = $xpath->query("//img[@src]");
99 foreach ($imgs as $img) {
100 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
102 if ($new_src != $img->getAttribute("src")) {
103 $img->setAttribute("src", $new_src);
109 $vids = $xpath->query("//video");
111 foreach ($vids as $vid) {
112 if ($vid->hasAttribute("poster")) {
113 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
115 if ($new_src != $vid->getAttribute("poster")) {
116 $vid->setAttribute("poster", $new_src);
122 $vsrcs = $xpath->query("source", $vid);
124 foreach ($vsrcs as $vsrc) {
125 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
127 if ($new_src != $vsrc->getAttribute("src")) {
128 $vid->setAttribute("src", $new_src);
136 if ($need_saving) $article["content"] = $doc->saveXML();
141 function hook_prefs_tab($args) {
142 if ($args != "prefFeeds") return;
144 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
146 print "<form dojoType=\"dijit.form.Form\">";
148 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
149 evt.preventDefault();
150 if (this.validate()) {
151 console.log(dojo.objectToQuery(this.getValues()));
152 new Ajax.Request('backend.php', {
153 parameters: dojo.objectToQuery(this.getValues()),
154 onComplete: function(transport) {
155 notify_info(transport.responseText);
162 print_hidden("op", "pluginhandler");
163 print_hidden("method", "save");
164 print_hidden("plugin", "af_zz_imgproxy");
166 $proxy_all = $this->host
->get($this, "proxy_all");
167 print_checkbox("proxy_all", $proxy_all);
169 print " <label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
171 print "<p>"; print_button("submit", __("Save"));
179 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
181 $this->host
->set($this, "proxy_all", $proxy_all);
183 echo __("Configuration saved");
186 function api_version() {