]>
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])'); |
06a19166 | 82 | $img_entries = $xpath->query("(//img[@src])"); |
cc85704f | 83 | |
6322fc68 | 84 | $found = false; |
ce7d5e87 | 85 | |
6322fc68 AD |
86 | foreach ($entries as $entry) { |
87 | if ($entry->hasAttribute("href")) { | |
299aeb30 | 88 | |
ec482d28 AD |
89 | _debug("processing href: " . $entry->getAttribute("href"), $debug); |
90 | ||
6322fc68 | 91 | $matches = array(); |
5d429910 | 92 | |
8788698b AD |
93 | if (preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) { |
94 | _debug("handling as twitter: " . $matches[1] . " " . $matches[2], $debug); | |
95 | ||
96 | $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=" . urlencode($entry->getAttribute("href"))); | |
97 | ||
98 | if ($oembed_result) { | |
99 | $oembed_result = json_decode($oembed_result, true); | |
100 | ||
101 | if ($oembed_result && isset($oembed_result["html"])) { | |
102 | ||
103 | $tmp = new DOMDocument(); | |
48007463 | 104 | if ($tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) { |
8788698b AD |
105 | $p = $doc->createElement("p"); |
106 | ||
107 | $p->appendChild($doc->importNode( | |
108 | $tmp->getElementsByTagName("blockquote")->item(0), TRUE)); | |
109 | ||
110 | $br = $doc->createElement('br'); | |
111 | $entry->parentNode->insertBefore($p, $entry); | |
112 | $entry->parentNode->insertBefore($br, $entry); | |
113 | ||
114 | $found = 1; | |
115 | } | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
43db5b99 | 120 | if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
5f297a5c AD |
121 | $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]); |
122 | } | |
123 | ||
43db5b99 | 124 | if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
5d429910 | 125 | |
ec482d28 AD |
126 | _debug("Handling as Gfycat", $debug); |
127 | ||
6322fc68 | 128 | $tmp = fetch_file_contents($entry->getAttribute("href")); |
5d429910 | 129 | |
6322fc68 AD |
130 | if ($tmp) { |
131 | $tmpdoc = new DOMDocument(); | |
5d429910 | 132 | |
58a44ecb | 133 | if (@$tmpdoc->loadHTML($tmp)) { |
6322fc68 | 134 | $tmpxpath = new DOMXPath($tmpdoc); |
b8887ebb | 135 | |
46506d3f AD |
136 | $source_node = $tmpxpath->query("//video[contains(@class,'share-video')]//source[contains(@src, '.mp4')]")->item(0); |
137 | $poster_node = $tmpxpath->query("//video[contains(@class,'share-video') and @poster]")->item(0); | |
5d429910 | 138 | |
46506d3f AD |
139 | if ($source_node && $poster_node) { |
140 | $source_stream = $source_node->getAttribute("src"); | |
141 | $poster_url = $poster_node->getAttribute("poster"); | |
5d429910 | 142 | |
46506d3f AD |
143 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
144 | $found = 1; | |
5d429910 | 145 | } |
6322fc68 AD |
146 | } |
147 | } | |
6322fc68 | 148 | } |
98e20510 | 149 | |
06a19166 AD |
150 | if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry->getAttribute("href"), $matches)) { |
151 | ||
152 | _debug("Handling as reddit inline video", $debug); | |
153 | ||
154 | $img = $img_entries->item(0); | |
155 | ||
156 | if ($img) { | |
157 | $poster_url = $img->getAttribute("src"); | |
158 | } else { | |
159 | $poster_url = false; | |
160 | } | |
161 | ||
8352d7c6 | 162 | // Get original article URL from v.redd.it redirects |
163 | $source_article_url = $this->get_location($matches[0]); | |
164 | _debug("Resolved ".$matches[0]." to ".$source_article_url, $debug); | |
165 | ||
81d83a68 | 166 | $source_stream = false; |
8352d7c6 | 167 | |
168 | if ($source_article_url) { | |
169 | $j = json_decode(fetch_file_contents($source_article_url.".json"), true); | |
170 | ||
171 | if ($j) { | |
172 | foreach ($j as $listing) { | |
173 | foreach ($listing["data"]["children"] as $child) { | |
174 | if ($child["data"]["url"] == $matches[0]) { | |
175 | try { | |
176 | $source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"]; | |
177 | } | |
178 | catch (Exception $e) { | |
179 | } | |
180 | break 2; | |
81d83a68 | 181 | } |
81d83a68 | 182 | } |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | if (!$source_stream) { | |
188 | $source_stream = "https://v.redd.it/" . $matches[1] . "/DASH_600_K"; | |
189 | } | |
06a19166 AD |
190 | |
191 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); | |
192 | $found = 1; | |
193 | } | |
194 | ||
60e97d9e AD |
195 | if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) { |
196 | ||
197 | _debug("Handling as Streamable", $debug); | |
198 | ||
199 | $tmp = fetch_file_contents($entry->getAttribute("href")); | |
200 | ||
201 | if ($tmp) { | |
202 | $tmpdoc = new DOMDocument(); | |
203 | ||
204 | if (@$tmpdoc->loadHTML($tmp)) { | |
205 | $tmpxpath = new DOMXPath($tmpdoc); | |
206 | ||
207 | $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0); | |
208 | $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0); | |
209 | ||
210 | if ($source_node && $poster_node) { | |
211 | $source_stream = $source_node->getAttribute("src"); | |
212 | $poster_url = $poster_node->getAttribute("poster"); | |
213 | ||
214 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); | |
215 | $found = 1; | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
5f297a5c | 221 | // imgur .gif -> .gifv |
43db5b99 | 222 | if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) { |
ec482d28 AD |
223 | _debug("Handling as imgur gif (->gifv)", $debug); |
224 | ||
5f297a5c AD |
225 | $entry->setAttribute("href", |
226 | str_replace(".gif", ".gifv", $entry->getAttribute("href"))); | |
227 | } | |
228 | ||
8c395462 | 229 | if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) { |
ec482d28 | 230 | _debug("Handling as imgur gifv", $debug); |
5d429910 | 231 | |
6322fc68 | 232 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
b8887ebb | 233 | |
49048482 | 234 | if (strpos($source_stream, "imgur.com") !== FALSE) |
b8887ebb AD |
235 | $poster_url = str_replace(".mp4", "h.jpg", $source_stream); |
236 | ||
ec482d28 | 237 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug); |
98e20510 | 238 | |
6322fc68 AD |
239 | $found = true; |
240 | } | |
9875d717 | 241 | |
6322fc68 | 242 | $matches = array(); |
43db5b99 | 243 | if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
0a361964 AD |
244 | preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
245 | preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
6322fc68 | 246 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
3b9ca4e6 | 247 | |
6322fc68 | 248 | $vid_id = $matches[1]; |
3b9ca4e6 | 249 | |
ec482d28 AD |
250 | _debug("Handling as youtube: $vid_id", $debug); |
251 | ||
6322fc68 AD |
252 | $iframe = $doc->createElement("iframe"); |
253 | $iframe->setAttribute("class", "youtube-player"); | |
254 | $iframe->setAttribute("type", "text/html"); | |
255 | $iframe->setAttribute("width", "640"); | |
256 | $iframe->setAttribute("height", "385"); | |
257 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); | |
258 | $iframe->setAttribute("allowfullscreen", "1"); | |
259 | $iframe->setAttribute("frameborder", "0"); | |
3b9ca4e6 | 260 | |
6322fc68 AD |
261 | $br = $doc->createElement('br'); |
262 | $entry->parentNode->insertBefore($iframe, $entry); | |
263 | $entry->parentNode->insertBefore($br, $entry); | |
3b9ca4e6 | 264 | |
6322fc68 AD |
265 | $found = true; |
266 | } | |
3b9ca4e6 | 267 | |
43db5b99 | 268 | if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) || |
90e45935 AD |
269 | mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE || |
270 | mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) { | |
eb95d1bd | 271 | |
ec482d28 AD |
272 | _debug("Handling as a picture", $debug); |
273 | ||
6322fc68 AD |
274 | $img = $doc->createElement('img'); |
275 | $img->setAttribute("src", $entry->getAttribute("href")); | |
cc85704f | 276 | |
6322fc68 AD |
277 | $br = $doc->createElement('br'); |
278 | $entry->parentNode->insertBefore($img, $entry); | |
279 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 | 280 | |
6322fc68 AD |
281 | $found = true; |
282 | } | |
ce7d5e87 | 283 | |
d4ac4fc6 | 284 | // linked albums & pages |
35055d05 | 285 | |
43db5b99 | 286 | if (!$found && preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) || |
8b2a52c6 | 287 | preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { |
35055d05 | 288 | |
c80175bd | 289 | _debug("Handling as an imgur page/album/gallery", $debug); |
ec482d28 | 290 | |
6322fc68 AD |
291 | $album_content = fetch_file_contents($entry->getAttribute("href"), |
292 | false, false, false, false, 10); | |
35055d05 | 293 | |
6322fc68 AD |
294 | if ($album_content) { |
295 | $adoc = new DOMDocument(); | |
35055d05 | 296 | |
58a44ecb | 297 | if (@$adoc->loadHTML($album_content)) { |
6322fc68 | 298 | $axpath = new DOMXPath($adoc); |
ec482d28 | 299 | |
a6fde6c9 | 300 | $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)"); |
ec482d28 AD |
301 | $urls = []; |
302 | ||
303 | foreach ($aentries as $aentry) { | |
304 | ||
305 | $url = $aentry->getAttribute("src"); | |
306 | ||
307 | if (!in_array($url, $urls)) { | |
ec482d28 | 308 | |
a6fde6c9 | 309 | if ($aentry->tagName == "img") { |
ec482d28 | 310 | |
a6fde6c9 AD |
311 | $img = $doc->createElement('img'); |
312 | $img->setAttribute("src", $url); | |
313 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); | |
314 | ||
315 | $br = $doc->createElement('br'); | |
316 | ||
317 | $entry->parentNode->insertBefore($img, $entry); | |
318 | $entry->parentNode->insertBefore($br, $entry); | |
319 | } else if ($aentry->tagName == "source") { | |
320 | ||
49048482 | 321 | if (strpos($url, "imgur.com") !== FALSE) |
a6fde6c9 AD |
322 | $poster_url = str_replace(".mp4", "h.jpg", $url); |
323 | else | |
324 | $poster_url = ""; | |
325 | ||
326 | $this->handle_as_video($doc, $entry, $url, $poster_url); | |
327 | ||
328 | } | |
ec482d28 AD |
329 | |
330 | array_push($urls, $url); | |
331 | ||
332 | $found = true; | |
333 | } | |
334 | ||
35055d05 | 335 | } |
ec482d28 AD |
336 | |
337 | if ($debug) print_r($urls); | |
cc85704f | 338 | } |
cc85704f | 339 | } |
6322fc68 | 340 | } |
58a44ecb AD |
341 | |
342 | // wtf is this even | |
43db5b99 | 343 | if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
58a44ecb AD |
344 | $img_id = $matches[1]; |
345 | ||
ec482d28 AD |
346 | _debug("handling as gyazo: $img_id", $debug); |
347 | ||
58a44ecb AD |
348 | $img = $doc->createElement('img'); |
349 | $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); | |
350 | ||
351 | $br = $doc->createElement('br'); | |
352 | $entry->parentNode->insertBefore($img, $entry); | |
353 | $entry->parentNode->insertBefore($br, $entry); | |
354 | ||
355 | $found = true; | |
356 | } | |
6322fc68 AD |
357 | } |
358 | ||
359 | // remove tiny thumbnails | |
360 | if ($entry->hasAttribute("src")) { | |
361 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
362 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
363 | } | |
364 | } | |
365 | } | |
cc85704f | 366 | |
6322fc68 AD |
367 | return $found; |
368 | } | |
b90c4468 | 369 | |
6322fc68 | 370 | function hook_article_filter($article) { |
b90c4468 | 371 | |
6322fc68 AD |
372 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { |
373 | $doc = new DOMDocument(); | |
374 | @$doc->loadHTML($article["content"]); | |
375 | $xpath = new DOMXPath($doc); | |
99bb8b3b | 376 | |
582ff3cf AD |
377 | $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); |
378 | ||
891b77f9 | 379 | if ($this->host->get($this, "enable_content_dupcheck")) { |
902b1ac7 | 380 | |
891b77f9 AD |
381 | if ($content_link) { |
382 | $content_href = db_escape_string($content_link->getAttribute("href")); | |
383 | $entry_guid = db_escape_string($article["guid_hashed"]); | |
384 | $owner_uid = $article["owner_uid"]; | |
902b1ac7 | 385 | |
891b77f9 AD |
386 | if (DB_TYPE == "pgsql") { |
387 | $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'"; | |
388 | } else { | |
389 | $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)"; | |
390 | } | |
902b1ac7 | 391 | |
891b77f9 AD |
392 | $result = db_query("SELECT COUNT(id) AS cid |
393 | FROM ttrss_entries, ttrss_user_entries WHERE | |
394 | ref_id = id AND | |
395 | $interval_qpart AND | |
396 | guid != '$entry_guid' AND | |
397 | owner_uid = '$owner_uid' AND | |
398 | content LIKE '%href=\"$content_href\">[link]%'"); | |
902b1ac7 | 399 | |
891b77f9 AD |
400 | if ($result) { |
401 | $num_found = db_fetch_result($result, 0, "cid"); | |
902b1ac7 | 402 | |
891b77f9 AD |
403 | if ($num_found > 0) $article["force_catchup"] = true; |
404 | } | |
902b1ac7 | 405 | } |
891b77f9 | 406 | } |
4f5204dd | 407 | |
4533b3ef | 408 | $found = $this->inline_stuff($article, $doc, $xpath); |
b90c4468 | 409 | |
6322fc68 AD |
410 | $node = $doc->getElementsByTagName('body')->item(0); |
411 | ||
412 | if ($node && $found) { | |
f3774b9d | 413 | $article["content"] = $doc->saveHTML($node); |
e487e92d AD |
414 | } else if ($content_link) { |
415 | $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath); | |
6322fc68 | 416 | } |
cc85704f AD |
417 | } |
418 | ||
419 | return $article; | |
420 | } | |
106a3de9 AD |
421 | |
422 | function api_version() { | |
423 | return 2; | |
424 | } | |
425 | ||
ec482d28 AD |
426 | private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) { |
427 | ||
428 | _debug("handle_as_video: $source_stream", $debug); | |
5d429910 AD |
429 | |
430 | $video = $doc->createElement('video'); | |
431 | $video->setAttribute("autoplay", "1"); | |
5dcc7bf1 | 432 | $video->setAttribute("controls", "1"); |
5d429910 AD |
433 | $video->setAttribute("loop", "1"); |
434 | ||
b8887ebb AD |
435 | if ($poster_url) $video->setAttribute("poster", $poster_url); |
436 | ||
5d429910 AD |
437 | $source = $doc->createElement('source'); |
438 | $source->setAttribute("src", $source_stream); | |
439 | $source->setAttribute("type", "video/mp4"); | |
440 | ||
441 | $video->appendChild($source); | |
442 | ||
443 | $br = $doc->createElement('br'); | |
444 | $entry->parentNode->insertBefore($video, $entry); | |
445 | $entry->parentNode->insertBefore($br, $entry); | |
446 | ||
447 | $img = $doc->createElement('img'); | |
448 | $img->setAttribute("src", | |
449 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
450 | ||
451 | $entry->parentNode->insertBefore($img, $entry); | |
452 | } | |
ec482d28 AD |
453 | |
454 | function testurl() { | |
455 | $url = htmlspecialchars($_REQUEST["url"]); | |
456 | ||
457 | header("Content-type: text/plain"); | |
458 | ||
459 | print "URL: $url\n"; | |
460 | ||
461 | $doc = new DOMDocument(); | |
462 | @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>"); | |
463 | $xpath = new DOMXPath($doc); | |
464 | ||
e487e92d AD |
465 | $found = $this->inline_stuff([], $doc, $xpath, true); |
466 | ||
467 | print "Inline result: $found\n"; | |
468 | ||
469 | if (!$found) { | |
470 | print "\nReadability result:\n"; | |
ec482d28 | 471 | |
e487e92d | 472 | $article = $this->readability([], $url, $doc, $xpath, true); |
ec482d28 | 473 | |
e487e92d AD |
474 | print_r($article); |
475 | } else { | |
476 | print "\nResulting HTML:\n"; | |
ec482d28 | 477 | |
e487e92d AD |
478 | print $doc->saveHTML(); |
479 | } | |
ec482d28 | 480 | } |
90e45935 AD |
481 | |
482 | private function get_content_type($url, $useragent = SELF_USER_AGENT) { | |
483 | $content_type = false; | |
484 | ||
485 | if (function_exists("curl_init") && !defined("NO_CURL")) { | |
486 | $ch = curl_init($url); | |
487 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
488 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
489 | curl_setopt($ch, CURLOPT_HEADER, true); | |
490 | curl_setopt($ch, CURLOPT_NOBODY, true); | |
491 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir")); | |
492 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent); | |
493 | ||
21ce7d9e | 494 | @curl_exec($ch); |
90e45935 AD |
495 | $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
496 | } | |
497 | ||
498 | return $content_type; | |
499 | } | |
e487e92d | 500 | |
8352d7c6 | 501 | private function get_location($url, $useragent = SELF_USER_AGENT) { |
502 | $location = false; | |
503 | ||
504 | if (function_exists("curl_init") && !defined("NO_CURL")) { | |
505 | $ch = curl_init($url); | |
506 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
507 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
508 | curl_setopt($ch, CURLOPT_HEADER, true); | |
509 | curl_setopt($ch, CURLOPT_NOBODY, true); | |
510 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir")); | |
511 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent); | |
512 | ||
513 | @curl_exec($ch); | |
514 | $location = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); | |
515 | } | |
516 | ||
517 | return $location; | |
518 | } | |
519 | ||
21ce7d9e AD |
520 | /** |
521 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
522 | */ | |
e487e92d AD |
523 | private function readability($article, $url, $doc, $xpath, $debug = false) { |
524 | ||
525 | if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") && | |
526 | mb_strlen(strip_tags($article["content"])) <= 150) { | |
527 | ||
528 | if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php"); | |
529 | ||
530 | if ($url && | |
531 | strpos($url, "twitter.com") === FALSE && | |
532 | strpos($url, "youtube.com") === FALSE && | |
533 | strpos($url, "reddit.com") === FALSE) { | |
534 | ||
535 | /* link may lead to a huge video file or whatever, we need to check content type before trying to | |
536 | parse it which p much requires curl */ | |
537 | ||
538 | $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; | |
539 | ||
540 | $content_type = $this->get_content_type($url, $useragent_compat); | |
541 | ||
542 | if ($content_type && strpos($content_type, "text/html") !== FALSE) { | |
543 | ||
544 | $tmp = fetch_file_contents(array("url" => $url, | |
545 | "useragent" => $useragent_compat)); | |
546 | ||
547 | if ($debug) _debug("tmplen: " . mb_strlen($tmp)); | |
548 | ||
549 | if ($tmp && mb_strlen($tmp) < 1024 * 500) { | |
550 | ||
551 | $r = new Readability($tmp, $url); | |
552 | ||
553 | if ($r->init()) { | |
554 | ||
555 | $tmpxpath = new DOMXPath($r->dom); | |
556 | ||
557 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); | |
558 | ||
559 | foreach ($entries as $entry) { | |
560 | if ($entry->hasAttribute("href")) { | |
561 | $entry->setAttribute("href", | |
562 | rewrite_relative_url($url, $entry->getAttribute("href"))); | |
563 | ||
564 | } | |
565 | ||
566 | if ($entry->hasAttribute("src")) { | |
567 | $entry->setAttribute("src", | |
568 | rewrite_relative_url($url, $entry->getAttribute("src"))); | |
569 | ||
570 | } | |
571 | ||
572 | } | |
573 | ||
574 | $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
575 | } | |
576 | } | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
581 | return $article; | |
582 | } | |
cc85704f | 583 | } |