]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
remove LIBXML_NOEMPTYTAG because of double <br/>s - the #357 issue with
[tt-rss.git] / plugins / af_redditimgur / init.php
1 <?php
2 class Af_RedditImgur extends Plugin {
3
4 private $link;
5 private $host;
6
7 function about() {
8 return array(1.0,
9 "Inline image links in Reddit RSS feeds",
10 "fox");
11 }
12
13 function init($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) {
21 $owner_uid = $article["owner_uid"];
22
23 $force = false;
24
25 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
26 if (strpos($article["plugin_data"], "redditimgur,$owner_uid:") === FALSE || $force) {
27 $doc = new DOMDocument();
28 @$doc->loadHTML($article["content"]);
29
30 if ($doc) {
31 $xpath = new DOMXPath($doc);
32 $entries = $xpath->query('(//a[@href]|//img[@src])');
33
34 $found = false;
35
36 foreach ($entries as $entry) {
37 if ($entry->hasAttribute("href")) {
38 if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
39
40 $img = $doc->createElement('img');
41 $img->setAttribute("src", $entry->getAttribute("href"));
42
43 $entry->parentNode->replaceChild($img, $entry);
44
45 $found = true;
46 }
47
48 // links to imgur pages
49 $matches = array();
50 if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
51
52 $token = $matches[1];
53
54 $album_content = fetch_file_contents($entry->getAttribute("href"),
55 false, false, false, false, 10);
56
57 if ($album_content && $token) {
58 $adoc = new DOMDocument();
59 @$adoc->loadHTML($album_content);
60
61 if ($adoc) {
62 $axpath = new DOMXPath($adoc);
63 $aentries = $axpath->query('(//img[@src])');
64
65 foreach ($aentries as $aentry) {
66 if (preg_match("/^http:\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
67 $img = $doc->createElement('img');
68 $img->setAttribute("src", $aentry->getAttribute("src"));
69 $entry->parentNode->insertBefore($img, $entry);
70 $found = true;
71
72 break;
73 }
74 }
75 }
76 }
77 }
78
79 // linked albums, ffs
80 if (preg_match("/^http:\/\/imgur.com\/a\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
81
82 $album_content = fetch_file_contents($entry->getAttribute("href"),
83 false, false, false, false, 10);
84
85 if ($album_content) {
86 $adoc = new DOMDocument();
87 @$adoc->loadHTML($album_content);
88
89 if ($adoc) {
90 $axpath = new DOMXPath($adoc);
91 $aentries = $axpath->query("//div[@class='image']//a[@href and @class='zoom']");
92
93 foreach ($aentries as $aentry) {
94 $img = $doc->createElement('img');
95 $img->setAttribute("src", $aentry->getAttribute("href"));
96 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
97 $entry->parentNode->insertBefore($img, $entry);
98 $found = true;
99 }
100 }
101 }
102 }
103 }
104
105 // remove tiny thumbnails
106 if ($entry->hasAttribute("src")) {
107 if ($entry->parentNode && $entry->parentNode->parentNode) {
108 $entry->parentNode->parentNode->removeChild($entry->parentNode);
109 }
110 }
111 }
112
113 $node = $doc->getElementsByTagName('body')->item(0);
114
115 if ($node && $found) {
116 $article["content"] = $doc->saveXML($node);
117 if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"];
118 }
119 }
120 } else if (isset($article["stored"]["content"])) {
121 $article["content"] = $article["stored"]["content"];
122 }
123 }
124
125 return $article;
126 }
127 }
128 ?>