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