af_redditimgur: check if document is text/html before trying to readability parse it

This commit is contained in:
Andrew Dolgov 2015-07-08 10:30:35 +03:00
parent b7d1306b19
commit 6475fc7e06
1 changed files with 44 additions and 25 deletions

View File

@ -240,13 +240,32 @@ class Af_RedditImgur extends Plugin {
$found = $this->inline_stuff($article, $doc, $xpath); $found = $this->inline_stuff($article, $doc, $xpath);
if (!$found && $this->host->get($this, "enable_readability") && mb_strlen(strip_tags($article["content"])) <= 150) { if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
mb_strlen(strip_tags($article["content"])) <= 150) {
if (!class_exists("Readability")) require_once(__DIR__ . "/classes/Readability.php"); if (!class_exists("Readability")) require_once(__DIR__ . "/classes/Readability.php");
$content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
if ($content_link && strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) { if ($content_link && strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
/* link may lead to a huge video file or whatever, we need to check content type before trying to
parse it which p much requires curl */
$ch = curl_init($content_link->getAttribute("href"));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
!ini_get("safe_mode") && !ini_get("open_basedir"));
curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
@$result = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if ($content_type && strpos($content_type, "text/html") !== FALSE) {
$tmp = fetch_file_contents($content_link->getAttribute("href")); $tmp = fetch_file_contents($content_link->getAttribute("href"));
if ($tmp) { if ($tmp) {
@ -283,7 +302,7 @@ class Af_RedditImgur extends Plugin {
$found = $this->inline_stuff($article, $doc, $xpath); $found = $this->inline_stuff($article, $doc, $xpath);
} }
} }
}
} }
} }