]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | class Af_RedditImgur extends Plugin { | |
3 | private $host; | |
4 | ||
5 | function about() { | |
6 | return array(1.0, | |
7 | "Inline images (and other content) in Reddit RSS feeds", | |
8 | "fox"); | |
9 | } | |
10 | ||
11 | function init($host) { | |
12 | $this->host = $host; | |
13 | ||
14 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); | |
15 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
16 | } | |
17 | ||
18 | function hook_prefs_tab($args) { | |
19 | if ($args != "prefFeeds") return; | |
20 | ||
21 | print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">"; | |
22 | ||
23 | $enable_readability = $this->host->get($this, "enable_readability"); | |
24 | $enable_readability_checked = $enable_readability ? "checked" : ""; | |
25 | ||
26 | print "<form dojoType=\"dijit.form.Form\">"; | |
27 | ||
28 | print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> | |
29 | evt.preventDefault(); | |
30 | if (this.validate()) { | |
31 | console.log(dojo.objectToQuery(this.getValues())); | |
32 | new Ajax.Request('backend.php', { | |
33 | parameters: dojo.objectToQuery(this.getValues()), | |
34 | onComplete: function(transport) { | |
35 | notify_info(transport.responseText); | |
36 | } | |
37 | }); | |
38 | //this.reset(); | |
39 | } | |
40 | </script>"; | |
41 | ||
42 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">"; | |
43 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">"; | |
44 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">"; | |
45 | ||
46 | print "<h3>" . __("Global settings") . "</h3>"; | |
47 | ||
48 | print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>"); | |
49 | print "<p/>"; | |
50 | ||
51 | print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\" | |
52 | $enable_readability_checked name=\"enable_readability\"> "; | |
53 | ||
54 | print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>"; | |
55 | ||
56 | print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">". | |
57 | __("Save")."</button>"; | |
58 | ||
59 | print "</form>"; | |
60 | ||
61 | print "</div>"; | |
62 | } | |
63 | ||
64 | function save() { | |
65 | $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true"; | |
66 | ||
67 | $this->host->set($this, "enable_readability", $enable_readability); | |
68 | ||
69 | echo __("Configuration saved"); | |
70 | } | |
71 | ||
72 | function hook_article_filter($article) { | |
73 | ||
74 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { | |
75 | $doc = new DOMDocument(); | |
76 | @$doc->loadHTML($article["content"]); | |
77 | ||
78 | if ($doc) { | |
79 | $xpath = new DOMXPath($doc); | |
80 | $entries = $xpath->query('(//a[@href]|//img[@src])'); | |
81 | ||
82 | $found = false; | |
83 | ||
84 | foreach ($entries as $entry) { | |
85 | if ($entry->hasAttribute("href")) { | |
86 | ||
87 | $matches = array(); | |
88 | ||
89 | if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { | |
90 | ||
91 | $tmp = fetch_file_contents($entry->getAttribute("href")); | |
92 | ||
93 | if ($tmp) { | |
94 | $tmpdoc = new DOMDocument(); | |
95 | @$tmpdoc->loadHTML($tmp); | |
96 | ||
97 | if ($tmpdoc) { | |
98 | $tmpxpath = new DOMXPath($tmpdoc); | |
99 | $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0); | |
100 | ||
101 | if ($source_meta) { | |
102 | $source_stream = $source_meta->getAttribute("content"); | |
103 | ||
104 | if ($source_stream) { | |
105 | $this->handle_as_video($doc, $entry, $source_stream); | |
106 | $found = 1; | |
107 | } | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | } | |
113 | ||
114 | if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) { | |
115 | ||
116 | /*$video = $doc->createElement('video'); | |
117 | $video->setAttribute("autoplay", "1"); | |
118 | $video->setAttribute("loop", "1"); | |
119 | ||
120 | $source = $doc->createElement('source'); | |
121 | $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href"))); | |
122 | $source->setAttribute("type", "video/mp4"); | |
123 | ||
124 | $video->appendChild($source); | |
125 | ||
126 | $br = $doc->createElement('br'); | |
127 | $entry->parentNode->insertBefore($video, $entry); | |
128 | $entry->parentNode->insertBefore($br, $entry); | |
129 | ||
130 | $img = $doc->createElement('img'); | |
131 | $img->setAttribute("src", | |
132 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
133 | ||
134 | $entry->parentNode->insertBefore($img, $entry);*/ | |
135 | ||
136 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); | |
137 | $this->handle_as_video($doc, $entry, $source_stream); | |
138 | ||
139 | $found = true; | |
140 | } | |
141 | ||
142 | $matches = array(); | |
143 | if (preg_match("/\/\/www\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
144 | preg_match("/\/\/www\.youtube\.com\/watch?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
145 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { | |
146 | ||
147 | $vid_id = $matches[1]; | |
148 | ||
149 | $iframe = $doc->createElement("iframe"); | |
150 | $iframe->setAttribute("class", "youtube-player"); | |
151 | $iframe->setAttribute("type", "text/html"); | |
152 | $iframe->setAttribute("width", "640"); | |
153 | $iframe->setAttribute("height", "385"); | |
154 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); | |
155 | $iframe->setAttribute("allowfullscreen", "1"); | |
156 | $iframe->setAttribute("frameborder", "0"); | |
157 | ||
158 | $br = $doc->createElement('br'); | |
159 | $entry->parentNode->insertBefore($iframe, $entry); | |
160 | $entry->parentNode->insertBefore($br, $entry); | |
161 | ||
162 | $found = true; | |
163 | } | |
164 | ||
165 | if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) { | |
166 | $img = $doc->createElement('img'); | |
167 | $img->setAttribute("src", $entry->getAttribute("href")); | |
168 | ||
169 | $br = $doc->createElement('br'); | |
170 | $entry->parentNode->insertBefore($img, $entry); | |
171 | $entry->parentNode->insertBefore($br, $entry); | |
172 | ||
173 | $found = true; | |
174 | } | |
175 | ||
176 | // links to imgur pages | |
177 | $matches = array(); | |
178 | if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { | |
179 | ||
180 | $token = $matches[2]; | |
181 | ||
182 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
183 | false, false, false, false, 10); | |
184 | ||
185 | if ($album_content && $token) { | |
186 | $adoc = new DOMDocument(); | |
187 | @$adoc->loadHTML($album_content); | |
188 | ||
189 | if ($adoc) { | |
190 | $axpath = new DOMXPath($adoc); | |
191 | $aentries = $axpath->query('(//img[@src])'); | |
192 | ||
193 | foreach ($aentries as $aentry) { | |
194 | if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) { | |
195 | $img = $doc->createElement('img'); | |
196 | $img->setAttribute("src", $aentry->getAttribute("src")); | |
197 | ||
198 | $br = $doc->createElement('br'); | |
199 | ||
200 | $entry->parentNode->insertBefore($img, $entry); | |
201 | $entry->parentNode->insertBefore($br, $entry); | |
202 | ||
203 | $found = true; | |
204 | ||
205 | break; | |
206 | } | |
207 | } | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | // linked albums, ffs | |
213 | if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { | |
214 | ||
215 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
216 | false, false, false, false, 10); | |
217 | ||
218 | if ($album_content) { | |
219 | $adoc = new DOMDocument(); | |
220 | @$adoc->loadHTML($album_content); | |
221 | ||
222 | if ($adoc) { | |
223 | $axpath = new DOMXPath($adoc); | |
224 | $aentries = $axpath->query("//meta[@property='og:image']"); | |
225 | $urls = array(); | |
226 | ||
227 | foreach ($aentries as $aentry) { | |
228 | ||
229 | if (!in_array($aentry->getAttribute("content"), $urls)) { | |
230 | $img = $doc->createElement('img'); | |
231 | $img->setAttribute("src", $aentry->getAttribute("content")); | |
232 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); | |
233 | ||
234 | $br = $doc->createElement('br'); | |
235 | ||
236 | $entry->parentNode->insertBefore($img, $entry); | |
237 | $entry->parentNode->insertBefore($br, $entry); | |
238 | ||
239 | array_push($urls, $aentry->getAttribute("content")); | |
240 | ||
241 | $found = true; | |
242 | } | |
243 | } | |
244 | } | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | // remove tiny thumbnails | |
250 | if ($entry->hasAttribute("src")) { | |
251 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
252 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
253 | } | |
254 | } | |
255 | } | |
256 | ||
257 | if (!$found && $this->host->get($this, "enable_readability") && mb_strlen(strip_tags($article["content"])) <= 150) { | |
258 | if (!class_exists("Readability")) require_once(__DIR__ . "/classes/Readability.php"); | |
259 | ||
260 | $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); | |
261 | ||
262 | if ($content_link) { | |
263 | $tmp = fetch_file_contents($content_link->getAttribute("href")); | |
264 | ||
265 | if ($tmp) { | |
266 | $r = new Readability($tmp, $content_link->getAttribute("href")); | |
267 | ||
268 | if ($r->init()) { | |
269 | //$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
270 | ||
271 | $tmpxpath = new DOMXPath($r->dom); | |
272 | ||
273 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); | |
274 | ||
275 | foreach ($entries as $entry) { | |
276 | if ($entry->hasAttribute("href")) { | |
277 | $entry->setAttribute("href", | |
278 | rewrite_relative_url($content_link, $entry->getAttribute("href"))); | |
279 | ||
280 | } | |
281 | ||
282 | if ($entry->hasAttribute("src")) { | |
283 | $entry->setAttribute("src", | |
284 | rewrite_relative_url($content_link, $entry->getAttribute("src"))); | |
285 | ||
286 | } | |
287 | ||
288 | } | |
289 | ||
290 | $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
291 | } | |
292 | } | |
293 | ||
294 | } | |
295 | ||
296 | } | |
297 | ||
298 | $node = $doc->getElementsByTagName('body')->item(0); | |
299 | ||
300 | if ($node && $found) { | |
301 | $article["content"] = $doc->saveXML($node); | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
306 | return $article; | |
307 | } | |
308 | ||
309 | function api_version() { | |
310 | return 2; | |
311 | } | |
312 | ||
313 | private function handle_as_video($doc, $entry, $source_stream) { | |
314 | ||
315 | $video = $doc->createElement('video'); | |
316 | $video->setAttribute("autoplay", "1"); | |
317 | $video->setAttribute("controls", "1"); | |
318 | $video->setAttribute("loop", "1"); | |
319 | ||
320 | $source = $doc->createElement('source'); | |
321 | $source->setAttribute("src", $source_stream); | |
322 | $source->setAttribute("type", "video/mp4"); | |
323 | ||
324 | $video->appendChild($source); | |
325 | ||
326 | $br = $doc->createElement('br'); | |
327 | $entry->parentNode->insertBefore($video, $entry); | |
328 | $entry->parentNode->insertBefore($br, $entry); | |
329 | ||
330 | $img = $doc->createElement('img'); | |
331 | $img->setAttribute("src", | |
332 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
333 | ||
334 | $entry->parentNode->insertBefore($img, $entry); | |
335 | } | |
336 | } | |
337 | ?> |