]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
f2d5c7b674906712e634e7f6d1ad8f72b1abfda4
[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 $br = $doc->createElement('br');
44 $entry->parentNode->insertBefore($img, $entry);
45 $entry->parentNode->insertBefore($br, $entry);
46
47 $found = true;
48 }
49
50 // links to imgur pages
51 $matches = array();
52 if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
53
54 $token = $matches[1];
55
56 $album_content = fetch_file_contents($entry->getAttribute("href"),
57 false, false, false, false, 10);
58
59 if ($album_content && $token) {
60 $adoc = new DOMDocument();
61 @$adoc->loadHTML($album_content);
62
63 if ($adoc) {
64 $axpath = new DOMXPath($adoc);
65 $aentries = $axpath->query('(//img[@src])');
66
67 foreach ($aentries as $aentry) {
68 if (preg_match("/^http:\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
69 $img = $doc->createElement('img');
70 $img->setAttribute("src", $aentry->getAttribute("src"));
71
72 $br = $doc->createElement('br');
73
74 $entry->parentNode->insertBefore($img, $entry);
75 $entry->parentNode->insertBefore($br, $entry);
76
77 $found = true;
78
79 break;
80 }
81 }
82 }
83 }
84 }
85
86 // linked albums, ffs
87 if (preg_match("/^http:\/\/imgur.com\/a\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
88
89 $album_content = fetch_file_contents($entry->getAttribute("href"),
90 false, false, false, false, 10);
91
92 if ($album_content) {
93 $adoc = new DOMDocument();
94 @$adoc->loadHTML($album_content);
95
96 if ($adoc) {
97 $axpath = new DOMXPath($adoc);
98 $aentries = $axpath->query("//div[@class='image']//a[@href and @class='zoom']");
99
100 foreach ($aentries as $aentry) {
101 $img = $doc->createElement('img');
102 $img->setAttribute("src", $aentry->getAttribute("href"));
103 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
104
105 $br = $doc->createElement('br');
106
107 $entry->parentNode->insertBefore($img, $entry);
108 $entry->parentNode->insertBefore($br, $entry);
109
110 $found = true;
111 }
112 }
113 }
114 }
115 }
116
117 // remove tiny thumbnails
118 if ($entry->hasAttribute("src")) {
119 if ($entry->parentNode && $entry->parentNode->parentNode) {
120 $entry->parentNode->parentNode->removeChild($entry->parentNode);
121 }
122 }
123 }
124
125 $node = $doc->getElementsByTagName('body')->item(0);
126
127 if ($node && $found) {
128 $article["content"] = $doc->saveXML($node);
129 if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"];
130 }
131 }
132 } else if (isset($article["stored"]["content"])) {
133 $article["content"] = $article["stored"]["content"];
134 }
135 }
136
137 return $article;
138 }
139 }
140 ?>