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