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