summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2015-07-08 10:30:35 +0300
committerAndrew Dolgov <[email protected]>2015-07-08 10:30:35 +0300
commit6475fc7e06121ff948264b990280b2f488a86aa8 (patch)
tree6984221f7f5b44f0a56e769016ed5da4a16be8ee /plugins
parentb7d1306b197bc7ae60df706f81d1f5665ee04bed (diff)
af_redditimgur: check if document is text/html before trying to readability parse it
Diffstat (limited to 'plugins')
-rw-r--r--plugins/af_redditimgur/init.php67
1 files changed, 43 insertions, 24 deletions
diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php
index 265999e2b..ae500c521 100644
--- a/plugins/af_redditimgur/init.php
+++ b/plugins/af_redditimgur/init.php
@@ -240,50 +240,69 @@ class Af_RedditImgur extends Plugin {
$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");
$content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
if ($content_link && strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
- $tmp = fetch_file_contents($content_link->getAttribute("href"));
+ /* 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 */
- if ($tmp) {
- $r = new Readability($tmp, $content_link->getAttribute("href"));
+ $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);
- if ($r->init()) {
- //$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
+ @$result = curl_exec($ch);
+ $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
- $tmpxpath = new DOMXPath($r->dom);
+ if ($content_type && strpos($content_type, "text/html") !== FALSE) {
- $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
+ $tmp = fetch_file_contents($content_link->getAttribute("href"));
- foreach ($entries as $entry) {
- if ($entry->hasAttribute("href")) {
- $entry->setAttribute("href",
- rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
+ if ($tmp) {
+ $r = new Readability($tmp, $content_link->getAttribute("href"));
- }
+ if ($r->init()) {
+ //$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
- if ($entry->hasAttribute("src")) {
- $entry->setAttribute("src",
- rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
+ $tmpxpath = new DOMXPath($r->dom);
- }
+ $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
- }
+ foreach ($entries as $entry) {
+ if ($entry->hasAttribute("href")) {
+ $entry->setAttribute("href",
+ rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
+
+ }
- $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
+ if ($entry->hasAttribute("src")) {
+ $entry->setAttribute("src",
+ rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
- $doc = new DOMDocument();
- @$doc->loadHTML($article["content"]);
- $xpath = new DOMXPath($doc);
+ }
- $found = $this->inline_stuff($article, $doc, $xpath);
+ }
+
+ $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
+
+ $doc = new DOMDocument();
+ @$doc->loadHTML($article["content"]);
+ $xpath = new DOMXPath($doc);
+
+ $found = $this->inline_stuff($article, $doc, $xpath);
+ }
}
}
-
}
}