]>
Commit | Line | Data |
---|---|---|
cc85704f | 1 | <?php |
0862a602 | 2 | class Af_RedditImgur extends Plugin { |
19c73507 AD |
3 | private $host; |
4 | ||
d2a421e3 | 5 | function about() { |
7a866114 | 6 | return array(1.0, |
a95fb169 | 7 | "Inline images (and other content) in Reddit RSS feeds", |
7a866114 AD |
8 | "fox"); |
9 | } | |
10 | ||
41245888 AD |
11 | function flags() { |
12 | return array("needs_curl" => true); | |
13 | } | |
14 | ||
d2a421e3 | 15 | function init($host) { |
19c73507 AD |
16 | $this->host = $host; |
17 | ||
18 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); | |
b90c4468 AD |
19 | $host->add_hook($host::HOOK_PREFS_TAB, $this); |
20 | } | |
21 | ||
22 | function hook_prefs_tab($args) { | |
23 | if ($args != "prefFeeds") return; | |
24 | ||
dc8bd8a6 | 25 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Reddit content settings (af_redditimgur)')."\">"; |
b90c4468 AD |
26 | |
27 | $enable_readability = $this->host->get($this, "enable_readability"); | |
891b77f9 | 28 | $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck"); |
8788698b | 29 | |
b90c4468 AD |
30 | print "<form dojoType=\"dijit.form.Form\">"; |
31 | ||
32 | print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> | |
33 | evt.preventDefault(); | |
34 | if (this.validate()) { | |
35 | console.log(dojo.objectToQuery(this.getValues())); | |
36 | new Ajax.Request('backend.php', { | |
37 | parameters: dojo.objectToQuery(this.getValues()), | |
38 | onComplete: function(transport) { | |
39 | notify_info(transport.responseText); | |
40 | } | |
41 | }); | |
42 | //this.reset(); | |
43 | } | |
44 | </script>"; | |
45 | ||
328118d1 AD |
46 | print_hidden("op", "pluginhandler"); |
47 | print_hidden("method", "save"); | |
48 | print_hidden("plugin", "af_redditimgur"); | |
b90c4468 | 49 | |
73dfda1d | 50 | print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>"); |
b90c4468 AD |
51 | print "<p/>"; |
52 | ||
dc8bd8a6 AD |
53 | print_checkbox("enable_readability", $enable_readability); |
54 | print " <label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>"; | |
b90c4468 | 55 | |
891b77f9 | 56 | print "<br/>"; |
8788698b | 57 | |
dc8bd8a6 AD |
58 | print_checkbox("enable_content_dupcheck", $enable_content_dupcheck); |
59 | print " <label for=\"enable_content_dupcheck\">" . __("Enable additional duplicate checking") . "</label>"; | |
60 | print "<p>"; print_button("submit", __("Save")); | |
b90c4468 AD |
61 | print "</form>"; |
62 | ||
63 | print "</div>"; | |
64 | } | |
65 | ||
66 | function save() { | |
67 | $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true"; | |
891b77f9 | 68 | $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]) == "true"; |
4533b3ef | 69 | |
891b77f9 AD |
70 | $this->host->set($this, "enable_readability", $enable_readability, false); |
71 | $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck); | |
b90c4468 AD |
72 | |
73 | echo __("Configuration saved"); | |
19c73507 AD |
74 | } |
75 | ||
21ce7d9e AD |
76 | /** |
77 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
78 | */ | |
ec482d28 | 79 | private function inline_stuff($article, &$doc, $xpath, $debug = false) { |
cc85704f | 80 | |
6322fc68 | 81 | $entries = $xpath->query('(//a[@href]|//img[@src])'); |
cc85704f | 82 | |
6322fc68 | 83 | $found = false; |
ce7d5e87 | 84 | |
6322fc68 AD |
85 | foreach ($entries as $entry) { |
86 | if ($entry->hasAttribute("href")) { | |
299aeb30 | 87 | |
ec482d28 AD |
88 | _debug("processing href: " . $entry->getAttribute("href"), $debug); |
89 | ||
6322fc68 | 90 | $matches = array(); |
5d429910 | 91 | |
8788698b AD |
92 | if (preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) { |
93 | _debug("handling as twitter: " . $matches[1] . " " . $matches[2], $debug); | |
94 | ||
95 | $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=" . urlencode($entry->getAttribute("href"))); | |
96 | ||
97 | if ($oembed_result) { | |
98 | $oembed_result = json_decode($oembed_result, true); | |
99 | ||
100 | if ($oembed_result && isset($oembed_result["html"])) { | |
101 | ||
102 | $tmp = new DOMDocument(); | |
48007463 | 103 | if ($tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) { |
8788698b AD |
104 | $p = $doc->createElement("p"); |
105 | ||
106 | $p->appendChild($doc->importNode( | |
107 | $tmp->getElementsByTagName("blockquote")->item(0), TRUE)); | |
108 | ||
109 | $br = $doc->createElement('br'); | |
110 | $entry->parentNode->insertBefore($p, $entry); | |
111 | $entry->parentNode->insertBefore($br, $entry); | |
112 | ||
113 | $found = 1; | |
114 | } | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
43db5b99 | 119 | if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
5f297a5c AD |
120 | $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]); |
121 | } | |
122 | ||
43db5b99 | 123 | if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
5d429910 | 124 | |
ec482d28 AD |
125 | _debug("Handling as Gfycat", $debug); |
126 | ||
6322fc68 | 127 | $tmp = fetch_file_contents($entry->getAttribute("href")); |
5d429910 | 128 | |
6322fc68 AD |
129 | if ($tmp) { |
130 | $tmpdoc = new DOMDocument(); | |
5d429910 | 131 | |
58a44ecb | 132 | if (@$tmpdoc->loadHTML($tmp)) { |
6322fc68 | 133 | $tmpxpath = new DOMXPath($tmpdoc); |
b8887ebb | 134 | |
46506d3f AD |
135 | $source_node = $tmpxpath->query("//video[contains(@class,'share-video')]//source[contains(@src, '.mp4')]")->item(0); |
136 | $poster_node = $tmpxpath->query("//video[contains(@class,'share-video') and @poster]")->item(0); | |
5d429910 | 137 | |
46506d3f AD |
138 | if ($source_node && $poster_node) { |
139 | $source_stream = $source_node->getAttribute("src"); | |
140 | $poster_url = $poster_node->getAttribute("poster"); | |
5d429910 | 141 | |
46506d3f AD |
142 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
143 | $found = 1; | |
5d429910 | 144 | } |
6322fc68 AD |
145 | } |
146 | } | |
6322fc68 | 147 | } |
98e20510 | 148 | |
60e97d9e AD |
149 | if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) { |
150 | ||
151 | _debug("Handling as Streamable", $debug); | |
152 | ||
153 | $tmp = fetch_file_contents($entry->getAttribute("href")); | |
154 | ||
155 | if ($tmp) { | |
156 | $tmpdoc = new DOMDocument(); | |
157 | ||
158 | if (@$tmpdoc->loadHTML($tmp)) { | |
159 | $tmpxpath = new DOMXPath($tmpdoc); | |
160 | ||
161 | $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0); | |
162 | $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0); | |
163 | ||
164 | if ($source_node && $poster_node) { | |
165 | $source_stream = $source_node->getAttribute("src"); | |
166 | $poster_url = $poster_node->getAttribute("poster"); | |
167 | ||
168 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); | |
169 | $found = 1; | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
5f297a5c | 175 | // imgur .gif -> .gifv |
43db5b99 | 176 | if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) { |
ec482d28 AD |
177 | _debug("Handling as imgur gif (->gifv)", $debug); |
178 | ||
5f297a5c AD |
179 | $entry->setAttribute("href", |
180 | str_replace(".gif", ".gifv", $entry->getAttribute("href"))); | |
181 | } | |
182 | ||
8c395462 | 183 | if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) { |
ec482d28 | 184 | _debug("Handling as imgur gifv", $debug); |
5d429910 | 185 | |
6322fc68 | 186 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
b8887ebb | 187 | |
49048482 | 188 | if (strpos($source_stream, "imgur.com") !== FALSE) |
b8887ebb AD |
189 | $poster_url = str_replace(".mp4", "h.jpg", $source_stream); |
190 | ||
ec482d28 | 191 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug); |
98e20510 | 192 | |
6322fc68 AD |
193 | $found = true; |
194 | } | |
9875d717 | 195 | |
6322fc68 | 196 | $matches = array(); |
43db5b99 | 197 | if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
0a361964 AD |
198 | preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
199 | preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
6322fc68 | 200 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
3b9ca4e6 | 201 | |
6322fc68 | 202 | $vid_id = $matches[1]; |
3b9ca4e6 | 203 | |
ec482d28 AD |
204 | _debug("Handling as youtube: $vid_id", $debug); |
205 | ||
6322fc68 AD |
206 | $iframe = $doc->createElement("iframe"); |
207 | $iframe->setAttribute("class", "youtube-player"); | |
208 | $iframe->setAttribute("type", "text/html"); | |
209 | $iframe->setAttribute("width", "640"); | |
210 | $iframe->setAttribute("height", "385"); | |
211 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); | |
212 | $iframe->setAttribute("allowfullscreen", "1"); | |
213 | $iframe->setAttribute("frameborder", "0"); | |
3b9ca4e6 | 214 | |
6322fc68 AD |
215 | $br = $doc->createElement('br'); |
216 | $entry->parentNode->insertBefore($iframe, $entry); | |
217 | $entry->parentNode->insertBefore($br, $entry); | |
3b9ca4e6 | 218 | |
6322fc68 AD |
219 | $found = true; |
220 | } | |
3b9ca4e6 | 221 | |
43db5b99 | 222 | if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) || |
90e45935 AD |
223 | mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE || |
224 | mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) { | |
eb95d1bd | 225 | |
ec482d28 AD |
226 | _debug("Handling as a picture", $debug); |
227 | ||
6322fc68 AD |
228 | $img = $doc->createElement('img'); |
229 | $img->setAttribute("src", $entry->getAttribute("href")); | |
cc85704f | 230 | |
6322fc68 AD |
231 | $br = $doc->createElement('br'); |
232 | $entry->parentNode->insertBefore($img, $entry); | |
233 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 | 234 | |
6322fc68 AD |
235 | $found = true; |
236 | } | |
ce7d5e87 | 237 | |
d4ac4fc6 | 238 | // linked albums & pages |
35055d05 | 239 | |
43db5b99 | 240 | if (!$found && preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) || |
8b2a52c6 | 241 | preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { |
35055d05 | 242 | |
c80175bd | 243 | _debug("Handling as an imgur page/album/gallery", $debug); |
ec482d28 | 244 | |
6322fc68 AD |
245 | $album_content = fetch_file_contents($entry->getAttribute("href"), |
246 | false, false, false, false, 10); | |
35055d05 | 247 | |
6322fc68 AD |
248 | if ($album_content) { |
249 | $adoc = new DOMDocument(); | |
35055d05 | 250 | |
58a44ecb | 251 | if (@$adoc->loadHTML($album_content)) { |
6322fc68 | 252 | $axpath = new DOMXPath($adoc); |
ec482d28 | 253 | |
a6fde6c9 | 254 | $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)"); |
ec482d28 AD |
255 | $urls = []; |
256 | ||
257 | foreach ($aentries as $aentry) { | |
258 | ||
259 | $url = $aentry->getAttribute("src"); | |
260 | ||
261 | if (!in_array($url, $urls)) { | |
ec482d28 | 262 | |
a6fde6c9 | 263 | if ($aentry->tagName == "img") { |
ec482d28 | 264 | |
a6fde6c9 AD |
265 | $img = $doc->createElement('img'); |
266 | $img->setAttribute("src", $url); | |
267 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); | |
268 | ||
269 | $br = $doc->createElement('br'); | |
270 | ||
271 | $entry->parentNode->insertBefore($img, $entry); | |
272 | $entry->parentNode->insertBefore($br, $entry); | |
273 | } else if ($aentry->tagName == "source") { | |
274 | ||
49048482 | 275 | if (strpos($url, "imgur.com") !== FALSE) |
a6fde6c9 AD |
276 | $poster_url = str_replace(".mp4", "h.jpg", $url); |
277 | else | |
278 | $poster_url = ""; | |
279 | ||
280 | $this->handle_as_video($doc, $entry, $url, $poster_url); | |
281 | ||
282 | } | |
ec482d28 AD |
283 | |
284 | array_push($urls, $url); | |
285 | ||
286 | $found = true; | |
287 | } | |
288 | ||
35055d05 | 289 | } |
ec482d28 AD |
290 | |
291 | if ($debug) print_r($urls); | |
cc85704f | 292 | } |
cc85704f | 293 | } |
6322fc68 | 294 | } |
58a44ecb AD |
295 | |
296 | // wtf is this even | |
43db5b99 | 297 | if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
58a44ecb AD |
298 | $img_id = $matches[1]; |
299 | ||
ec482d28 AD |
300 | _debug("handling as gyazo: $img_id", $debug); |
301 | ||
58a44ecb AD |
302 | $img = $doc->createElement('img'); |
303 | $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); | |
304 | ||
305 | $br = $doc->createElement('br'); | |
306 | $entry->parentNode->insertBefore($img, $entry); | |
307 | $entry->parentNode->insertBefore($br, $entry); | |
308 | ||
309 | $found = true; | |
310 | } | |
6322fc68 AD |
311 | } |
312 | ||
313 | // remove tiny thumbnails | |
314 | if ($entry->hasAttribute("src")) { | |
315 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
316 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
317 | } | |
318 | } | |
319 | } | |
cc85704f | 320 | |
6322fc68 AD |
321 | return $found; |
322 | } | |
b90c4468 | 323 | |
6322fc68 | 324 | function hook_article_filter($article) { |
b90c4468 | 325 | |
6322fc68 AD |
326 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { |
327 | $doc = new DOMDocument(); | |
328 | @$doc->loadHTML($article["content"]); | |
329 | $xpath = new DOMXPath($doc); | |
99bb8b3b | 330 | |
582ff3cf AD |
331 | $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); |
332 | ||
891b77f9 | 333 | if ($this->host->get($this, "enable_content_dupcheck")) { |
902b1ac7 | 334 | |
891b77f9 AD |
335 | if ($content_link) { |
336 | $content_href = db_escape_string($content_link->getAttribute("href")); | |
337 | $entry_guid = db_escape_string($article["guid_hashed"]); | |
338 | $owner_uid = $article["owner_uid"]; | |
902b1ac7 | 339 | |
891b77f9 AD |
340 | if (DB_TYPE == "pgsql") { |
341 | $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'"; | |
342 | } else { | |
343 | $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)"; | |
344 | } | |
902b1ac7 | 345 | |
891b77f9 AD |
346 | $result = db_query("SELECT COUNT(id) AS cid |
347 | FROM ttrss_entries, ttrss_user_entries WHERE | |
348 | ref_id = id AND | |
349 | $interval_qpart AND | |
350 | guid != '$entry_guid' AND | |
351 | owner_uid = '$owner_uid' AND | |
352 | content LIKE '%href=\"$content_href\">[link]%'"); | |
902b1ac7 | 353 | |
891b77f9 AD |
354 | if ($result) { |
355 | $num_found = db_fetch_result($result, 0, "cid"); | |
902b1ac7 | 356 | |
891b77f9 AD |
357 | if ($num_found > 0) $article["force_catchup"] = true; |
358 | } | |
902b1ac7 | 359 | } |
891b77f9 | 360 | } |
4f5204dd | 361 | |
4533b3ef | 362 | $found = $this->inline_stuff($article, $doc, $xpath); |
b90c4468 | 363 | |
6322fc68 AD |
364 | $node = $doc->getElementsByTagName('body')->item(0); |
365 | ||
366 | if ($node && $found) { | |
f3774b9d | 367 | $article["content"] = $doc->saveHTML($node); |
e487e92d AD |
368 | } else if ($content_link) { |
369 | $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath); | |
6322fc68 | 370 | } |
cc85704f AD |
371 | } |
372 | ||
373 | return $article; | |
374 | } | |
106a3de9 AD |
375 | |
376 | function api_version() { | |
377 | return 2; | |
378 | } | |
379 | ||
ec482d28 AD |
380 | private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) { |
381 | ||
382 | _debug("handle_as_video: $source_stream", $debug); | |
5d429910 AD |
383 | |
384 | $video = $doc->createElement('video'); | |
385 | $video->setAttribute("autoplay", "1"); | |
5dcc7bf1 | 386 | $video->setAttribute("controls", "1"); |
5d429910 AD |
387 | $video->setAttribute("loop", "1"); |
388 | ||
b8887ebb AD |
389 | if ($poster_url) $video->setAttribute("poster", $poster_url); |
390 | ||
5d429910 AD |
391 | $source = $doc->createElement('source'); |
392 | $source->setAttribute("src", $source_stream); | |
393 | $source->setAttribute("type", "video/mp4"); | |
394 | ||
395 | $video->appendChild($source); | |
396 | ||
397 | $br = $doc->createElement('br'); | |
398 | $entry->parentNode->insertBefore($video, $entry); | |
399 | $entry->parentNode->insertBefore($br, $entry); | |
400 | ||
401 | $img = $doc->createElement('img'); | |
402 | $img->setAttribute("src", | |
403 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
404 | ||
405 | $entry->parentNode->insertBefore($img, $entry); | |
406 | } | |
ec482d28 AD |
407 | |
408 | function testurl() { | |
409 | $url = htmlspecialchars($_REQUEST["url"]); | |
410 | ||
411 | header("Content-type: text/plain"); | |
412 | ||
413 | print "URL: $url\n"; | |
414 | ||
415 | $doc = new DOMDocument(); | |
416 | @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>"); | |
417 | $xpath = new DOMXPath($doc); | |
418 | ||
e487e92d AD |
419 | $found = $this->inline_stuff([], $doc, $xpath, true); |
420 | ||
421 | print "Inline result: $found\n"; | |
422 | ||
423 | if (!$found) { | |
424 | print "\nReadability result:\n"; | |
ec482d28 | 425 | |
e487e92d | 426 | $article = $this->readability([], $url, $doc, $xpath, true); |
ec482d28 | 427 | |
e487e92d AD |
428 | print_r($article); |
429 | } else { | |
430 | print "\nResulting HTML:\n"; | |
ec482d28 | 431 | |
e487e92d AD |
432 | print $doc->saveHTML(); |
433 | } | |
ec482d28 | 434 | } |
90e45935 AD |
435 | |
436 | private function get_content_type($url, $useragent = SELF_USER_AGENT) { | |
437 | $content_type = false; | |
438 | ||
439 | if (function_exists("curl_init") && !defined("NO_CURL")) { | |
440 | $ch = curl_init($url); | |
441 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
442 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
443 | curl_setopt($ch, CURLOPT_HEADER, true); | |
444 | curl_setopt($ch, CURLOPT_NOBODY, true); | |
445 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir")); | |
446 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent); | |
447 | ||
21ce7d9e | 448 | @curl_exec($ch); |
90e45935 AD |
449 | $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
450 | } | |
451 | ||
452 | return $content_type; | |
453 | } | |
e487e92d | 454 | |
21ce7d9e AD |
455 | /** |
456 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
457 | */ | |
e487e92d AD |
458 | private function readability($article, $url, $doc, $xpath, $debug = false) { |
459 | ||
460 | if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") && | |
461 | mb_strlen(strip_tags($article["content"])) <= 150) { | |
462 | ||
463 | if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php"); | |
464 | ||
465 | if ($url && | |
466 | strpos($url, "twitter.com") === FALSE && | |
467 | strpos($url, "youtube.com") === FALSE && | |
468 | strpos($url, "reddit.com") === FALSE) { | |
469 | ||
470 | /* link may lead to a huge video file or whatever, we need to check content type before trying to | |
471 | parse it which p much requires curl */ | |
472 | ||
473 | $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; | |
474 | ||
475 | $content_type = $this->get_content_type($url, $useragent_compat); | |
476 | ||
477 | if ($content_type && strpos($content_type, "text/html") !== FALSE) { | |
478 | ||
479 | $tmp = fetch_file_contents(array("url" => $url, | |
480 | "useragent" => $useragent_compat)); | |
481 | ||
482 | if ($debug) _debug("tmplen: " . mb_strlen($tmp)); | |
483 | ||
484 | if ($tmp && mb_strlen($tmp) < 1024 * 500) { | |
485 | ||
486 | $r = new Readability($tmp, $url); | |
487 | ||
488 | if ($r->init()) { | |
489 | ||
490 | $tmpxpath = new DOMXPath($r->dom); | |
491 | ||
492 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); | |
493 | ||
494 | foreach ($entries as $entry) { | |
495 | if ($entry->hasAttribute("href")) { | |
496 | $entry->setAttribute("href", | |
497 | rewrite_relative_url($url, $entry->getAttribute("href"))); | |
498 | ||
499 | } | |
500 | ||
501 | if ($entry->hasAttribute("src")) { | |
502 | $entry->setAttribute("src", | |
503 | rewrite_relative_url($url, $entry->getAttribute("src"))); | |
504 | ||
505 | } | |
506 | ||
507 | } | |
508 | ||
509 | $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
510 | } | |
511 | } | |
512 | } | |
513 | } | |
514 | } | |
515 | ||
516 | return $article; | |
517 | } | |
cc85704f | 518 | } |