]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
047f5a211ae57070d38b5068c660c88413f6ed88
[tt-rss.git] / plugins / af_zz_imgproxy / init.php
1 <?php
2 class Af_Zz_ImgProxy extends Plugin {
3         private $host;
4
5         function about() {
6                 return array(1.0,
7                         "Load insecure images via built-in proxy",
8                         "fox");
9         }
10
11         function is_public_method($method) {
12                 return $method === "imgproxy";
13         }
14
15         function init($host) {
16                 $this->host = $host;
17
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);
21
22                 $host->add_hook($host::HOOK_PREFS_TAB, $this);
23         }
24
25         function hook_enclosure_entry($enc) {
26                 if (preg_match("/image/", $enc["content_type"]) || preg_match("/\.(jpe?g|png|gif|bmp)$/i", $enc["filename"])) {
27                         $proxy_all = $this->host->get($this, "proxy_all");
28
29                         $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], 0, $proxy_all);
30                 }
31
32                 return $enc;
33         }
34
35         function hook_render_article($article) {
36                 return $this->hook_render_article_cdm($article);
37         }
38
39         public function imgproxy() {
40
41                 $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
42                 $kind = (int) $_REQUEST["kind"]; // 1 = video
43
44                 // called without user context, let's just redirect to original URL
45                 if (!$_SESSION["uid"]) {
46                         header("Location: $url");
47                         return;
48                 }
49
50                 $extension = $kind == 1 ? '.mp4' : '.png';
51                 $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension;
52
53                 if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
54
55                 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
56
57                 if (file_exists($local_filename)) {
58                         $mimetype = mime_content_type($local_filename);
59                         header("Content-type: $mimetype");
60
61                         $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
62                         header("Last-Modified: $stamp", true);
63
64                         readfile($local_filename);
65                 } else {
66                         $data = fetch_file_contents(array("url" => $url));
67
68                         if ($data) {
69                                 if (file_put_contents($local_filename, $data)) {
70                                         $mimetype = mime_content_type($local_filename);
71                                         header("Content-type: $mimetype");
72                                 }
73
74                                 print $data;
75                         }
76                 }
77         }
78
79         function rewrite_url_if_needed($url, $kind, $all_remote = false) {
80                 $scheme = parse_url($url, PHP_URL_SCHEME);
81
82                 if ($all_remote) {
83                         $host = parse_url($url, PHP_URL_HOST);
84                         $self_host = parse_url(SELF_URL_PATH, PHP_URL_HOST);
85
86                         $is_remote = $host != $self_host;
87                 } else {
88                         $is_remote = false;
89                 }
90
91                 if (($scheme != 'https' && $scheme != "") || $is_remote) {
92                         if (strpos($url, "data:") !== 0) {
93                                 $url = "public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
94                                         urlencode($url);
95                         }
96                 }
97
98                 return $url;
99         }
100
101         function hook_render_article_cdm($article, $api_mode = false) {
102
103                 $need_saving = false;
104                 $proxy_all = $this->host->get($this, "proxy_all");
105
106                 $doc = new DOMDocument();
107                 if (@$doc->loadHTML($article["content"])) {
108                         $xpath = new DOMXPath($doc);
109                         $imgs = $xpath->query("//img[@src]");
110
111                         foreach ($imgs as $img) {
112                                 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
113
114                                 if ($new_src != $img->getAttribute("src")) {
115                                         $img->setAttribute("src", $new_src);
116
117                                         $need_saving = true;
118                                 }
119                         }
120
121                         $vids = $xpath->query("//video");
122
123                         foreach ($vids as $vid) {
124                                 if ($vid->hasAttribute("poster")) {
125                                         $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
126
127                                         if ($new_src != $vid->getAttribute("poster")) {
128                                                 $vid->setAttribute("poster", $new_src);
129
130                                                 $need_saving = true;
131                                         }
132                                 }
133
134                                 $vsrcs = $xpath->query("source", $vid);
135
136                                 foreach ($vsrcs as $vsrc) {
137                                         $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
138
139                                         if ($new_src != $vsrc->getAttribute("src")) {
140                                                 $vid->setAttribute("src", $new_src);
141
142                                                 $need_saving = true;
143                                         }
144                                 }
145                         }
146                 }
147
148                 if ($need_saving) $article["content"] = $doc->saveXML();
149
150                 return $article;
151         }
152
153         function hook_prefs_tab($args) {
154                 if ($args != "prefFeeds") return;
155
156                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
157
158                 print "<form dojoType=\"dijit.form.Form\">";
159
160                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
161                         evt.preventDefault();
162                         if (this.validate()) {
163                                 console.log(dojo.objectToQuery(this.getValues()));
164                                 new Ajax.Request('backend.php', {
165                                         parameters: dojo.objectToQuery(this.getValues()),
166                                         onComplete: function(transport) {
167                                                 notify_info(transport.responseText);
168                                         }
169                                 });
170                                 //this.reset();
171                         }
172                         </script>";
173
174                 print_hidden("op", "pluginhandler");
175                 print_hidden("method", "save");
176                 print_hidden("plugin", "af_zz_imgproxy");
177
178                 $proxy_all = $this->host->get($this, "proxy_all");
179                 print_checkbox("proxy_all", $proxy_all);
180
181                 print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
182
183                 print "<p>"; print_button("submit", __("Save"));
184
185                 print "</form>";
186
187                 print "</div>";
188         }
189
190         function save() {
191                 $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
192
193                 $this->host->set($this, "proxy_all", $proxy_all);
194
195                 echo __("Configuration saved");
196         }
197
198         function api_version() {
199                 return 2;
200         }
201 }