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