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