]> git.wh0rd.org - tt-rss.git/blame - plugins/redditimgur/redditimgur.php
experimental support for per-user plugins (bump schema)
[tt-rss.git] / plugins / redditimgur / redditimgur.php
CommitLineData
cc85704f 1<?php
5a0e0392 2class RedditImgur extends Plugin {
cc85704f 3
19c73507
AD
4 private $link;
5 private $host;
6
7a866114
AD
7 function _about() {
8 return array(1.0,
9 "Inline image links in Reddit RSS feeds",
10 "fox");
11 }
12
19c73507
AD
13 function __construct($host) {
14 $this->link = $host->get_link();
15 $this->host = $host;
16
17 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
18 }
19
20 function hook_article_filter($article) {
cc85704f
AD
21
22 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
dad075f0
AD
23 $doc = new DOMDocument();
24 @$doc->loadHTML($article["content"]);
cc85704f 25
dad075f0
AD
26 if ($doc) {
27 $xpath = new DOMXPath($doc);
28 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 29
dad075f0
AD
30 foreach ($entries as $entry) {
31 if ($entry->hasAttribute("href")) {
32 if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
cc85704f 33
dad075f0
AD
34 $img = $doc->createElement('img');
35 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 36
dad075f0 37 $entry->parentNode->replaceChild($img, $entry);
cc85704f 38 }
dad075f0 39 }
cc85704f 40
dad075f0
AD
41 // remove tiny thumbnails
42 if ($entry->hasAttribute("src")) {
43 if ($entry->parentNode && $entry->parentNode->parentNode) {
44 $entry->parentNode->parentNode->removeChild($entry->parentNode);
cc85704f
AD
45 }
46 }
dad075f0 47 }
cc85704f 48
dad075f0 49 $node = $doc->getElementsByTagName('body')->item(0);
cc85704f 50
dad075f0
AD
51 if ($node) {
52 $article["content"] = $doc->saveXML($node, LIBXML_NOEMPTYTAG);
cc85704f
AD
53 }
54 }
55 }
56
57 return $article;
58 }
59}
60?>