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