]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
plugins: remove obsolete plugin_data/stored stuff
[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
19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
22
23 if ($doc) {
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
26
27 $found = false;
28
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
31 if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
32
33 $img = $doc->createElement('img');
34 $img->setAttribute("src", $entry->getAttribute("href"));
35
36 $br = $doc->createElement('br');
37 $entry->parentNode->insertBefore($img, $entry);
38 $entry->parentNode->insertBefore($br, $entry);
39
40 $found = true;
41 }
42
43 // links to imgur pages
44 $matches = array();
45 if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
46
47 $token = $matches[1];
48
49 $album_content = fetch_file_contents($entry->getAttribute("href"),
50 false, false, false, false, 10);
51
52 if ($album_content && $token) {
53 $adoc = new DOMDocument();
54 @$adoc->loadHTML($album_content);
55
56 if ($adoc) {
57 $axpath = new DOMXPath($adoc);
58 $aentries = $axpath->query('(//img[@src])');
59
60 foreach ($aentries as $aentry) {
61 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
62 $img = $doc->createElement('img');
63 $img->setAttribute("src", $aentry->getAttribute("src"));
64
65 $br = $doc->createElement('br');
66
67 $entry->parentNode->insertBefore($img, $entry);
68 $entry->parentNode->insertBefore($br, $entry);
69
70 $found = true;
71
72 break;
73 }
74 }
75 }
76 }
77 }
78
79 // linked albums, ffs
80 if (preg_match("/^http:\/\/imgur.com\/(a|album)\/[^\.]+$/", $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
98 $br = $doc->createElement('br');
99
100 $entry->parentNode->insertBefore($img, $entry);
101 $entry->parentNode->insertBefore($br, $entry);
102
103 $found = true;
104 }
105 }
106 }
107 }
108 }
109
110 // remove tiny thumbnails
111 if ($entry->hasAttribute("src")) {
112 if ($entry->parentNode && $entry->parentNode->parentNode) {
113 $entry->parentNode->parentNode->removeChild($entry->parentNode);
114 }
115 }
116 }
117
118 $node = $doc->getElementsByTagName('body')->item(0);
119
120 if ($node && $found) {
121 $article["content"] = $doc->saveXML($node);
122 }
123 }
124 }
125
126 return $article;
127 }
128
129 function api_version() {
130 return 2;
131 }
132
133 }
134 ?>