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