]>
Commit | Line | Data |
---|---|---|
c4ebf01e AD |
1 | <?php |
2 | class Af_Zz_ImgProxy extends Plugin { | |
3 | private $host; | |
4 | ||
5 | function about() { | |
6 | return array(1.0, | |
c93d43c6 | 7 | "Load insecure images via built-in proxy", |
c4ebf01e AD |
8 | "fox"); |
9 | } | |
10 | ||
c4ebf01e AD |
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); | |
8cf37284 AD |
17 | |
18 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
c4ebf01e AD |
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 | ||
c93d43c6 AD |
29 | public function imgproxy() { |
30 | $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]); | |
31 | $kind = (int) $_REQUEST["kind"]; // 1 = video | |
c4ebf01e | 32 | |
c93d43c6 AD |
33 | $extension = $kind == 1 ? '.mp4' : '.png'; |
34 | $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension; | |
c4ebf01e | 35 | |
51198e7e | 36 | if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; } |
c4ebf01e | 37 | |
38b3998b | 38 | header("Content-Disposition: inline; filename=\"".basename($local_filename)."\""); |
c4ebf01e | 39 | |
c93d43c6 | 40 | if (file_exists($local_filename)) { |
38b3998b AD |
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 | ||
c93d43c6 | 47 | readfile($local_filename); |
c4ebf01e | 48 | } else { |
c93d43c6 | 49 | $data = fetch_file_contents(array("url" => $url)); |
51198e7e AD |
50 | |
51 | global $fetch_last_error; | |
52 | print $fetch_last_error; | |
53 | ||
c93d43c6 | 54 | if ($data) { |
38b3998b AD |
55 | if (file_put_contents($local_filename, $data)) { |
56 | $mimetype = mime_content_type($local_filename); | |
57 | header("Content-type: $mimetype"); | |
58 | } | |
59 | ||
c93d43c6 AD |
60 | print $data; |
61 | } | |
c4ebf01e | 62 | } |
c4ebf01e AD |
63 | } |
64 | ||
8cf37284 | 65 | function rewrite_url_if_needed($url, $kind, $all_remote = false) { |
c4ebf01e AD |
66 | $scheme = parse_url($url, PHP_URL_SCHEME); |
67 | ||
8cf37284 AD |
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=" . | |
51198e7e | 80 | urlencode($url); |
8cf37284 | 81 | } |
c4ebf01e AD |
82 | } |
83 | ||
84 | return $url; | |
85 | } | |
86 | ||
87 | function hook_render_article_cdm($article, $api_mode = false) { | |
88 | ||
89 | $need_saving = false; | |
8cf37284 | 90 | $proxy_all = $this->host->get($this, "proxy_all"); |
c4ebf01e AD |
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) { | |
8cf37284 | 98 | $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all); |
c4ebf01e AD |
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")) { | |
8cf37284 | 111 | $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all); |
c4ebf01e AD |
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) { | |
8cf37284 | 123 | $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all); |
c4ebf01e AD |
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 | ||
8cf37284 AD |
139 | function hook_prefs_tab($args) { |
140 | if ($args != "prefFeeds") return; | |
141 | ||
142 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_zz_imgproxy Settings')."\">"; | |
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 " <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 | ||
c4ebf01e AD |
184 | function api_version() { |
185 | return 2; | |
186 | } | |
187 | } |