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