]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
Merge pull request #454 from aliz27/patch-2
[tt-rss.git] / plugins / af_redditimgur / init.php
CommitLineData
cc85704f 1<?php
0862a602 2class Af_RedditImgur extends Plugin {
19c73507
AD
3 private $host;
4
d2a421e3 5 function about() {
7a866114
AD
6 return array(1.0,
7 "Inline image links in Reddit RSS feeds",
8 "fox");
9 }
10
d2a421e3 11 function init($host) {
19c73507
AD
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
15 }
16
17 function hook_article_filter($article) {
0b5ef30d 18
cc85704f 19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
a16d7a5d
AD
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
cc85704f 22
a16d7a5d
AD
23 if ($doc) {
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 26
ce7d5e87
AD
27 $found = false;
28
a16d7a5d
AD
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
9875d717 31
32 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
33
34 $gifv_meta = fetch_file_contents($entry->getAttribute("href"),
35 false, false, false, false, 10);
36
37 if ($gifv_meta) {
38 $adoc = new DOMDocument();
39 @$adoc->loadHTML($gifv_meta);
40
41 if ($adoc) {
42 $axpath = new DOMXPath($adoc);
43 $aentries = $axpath->query('(//meta)');
44
45 $width = false;
46 $height = false;
47
48 foreach ($aentries as $aentry) {
49 if (strpos($aentry->getAttribute("property"), "og:image:width") !== FALSE) {
50 $width = $aentry->getAttribute("content");
51 }
52 if (strpos($aentry->getAttribute("property"), "og:image:height") !== FALSE) {
53 $height = $aentry->getAttribute("content");
54 }
55 }
56 }
57 }
58
59 if ($width && $height) {
60
61 $iframe = $doc->createElement('iframe');
62 $iframe->setAttribute("src", str_replace("http:", "", $entry->getAttribute("href")));
63 $iframe->setAttribute("frameborder", "0");
64 $iframe->setAttribute("width", $width);
65 $iframe->setAttribute("height", $height);
66
67 $br = $doc->createElement('br');
68 $entry->parentNode->insertBefore($iframe, $entry);
69 $entry->parentNode->insertBefore($br, $entry);
70
71 // add empty img tag to disable display of attachment
72 $img = $doc->createElement('img');
73 $img->setAttribute("src", "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
74 $img->setAttribute("width", "0");
75 $img->setAttribute("height", "0");
76 $entry->parentNode->insertBefore($img, $entry);
77 $found = true;
78 }
79 }
80
7adf9556 81 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
cc85704f 82
a16d7a5d
AD
83 $img = $doc->createElement('img');
84 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 85
7ff4d1fa
AD
86 $br = $doc->createElement('br');
87 $entry->parentNode->insertBefore($img, $entry);
88 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87
AD
89
90 $found = true;
91 }
92
93 // links to imgur pages
94 $matches = array();
248c5a6a 95 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
ce7d5e87 96
1af9f54f 97 $token = $matches[2];
ce7d5e87
AD
98
99 $album_content = fetch_file_contents($entry->getAttribute("href"),
100 false, false, false, false, 10);
101
102 if ($album_content && $token) {
103 $adoc = new DOMDocument();
104 @$adoc->loadHTML($album_content);
105
106 if ($adoc) {
107 $axpath = new DOMXPath($adoc);
108 $aentries = $axpath->query('(//img[@src])');
109
110 foreach ($aentries as $aentry) {
e44ea762 111 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
ce7d5e87
AD
112 $img = $doc->createElement('img');
113 $img->setAttribute("src", $aentry->getAttribute("src"));
7ff4d1fa
AD
114
115 $br = $doc->createElement('br');
116
ce7d5e87 117 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
118 $entry->parentNode->insertBefore($br, $entry);
119
ce7d5e87 120 $found = true;
35055d05
AD
121
122 break;
ce7d5e87
AD
123 }
124 }
125 }
126 }
a16d7a5d 127 }
35055d05
AD
128
129 // linked albums, ffs
47cdc58c 130 if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05
AD
131
132 $album_content = fetch_file_contents($entry->getAttribute("href"),
133 false, false, false, false, 10);
134
135 if ($album_content) {
136 $adoc = new DOMDocument();
137 @$adoc->loadHTML($album_content);
138
139 if ($adoc) {
140 $axpath = new DOMXPath($adoc);
47cdc58c 141 $aentries = $axpath->query("//meta[@property='og:image']");
35055d05
AD
142
143 foreach ($aentries as $aentry) {
144 $img = $doc->createElement('img');
47cdc58c 145 $img->setAttribute("src", $aentry->getAttribute("content"));
0b5ef30d 146 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa
AD
147
148 $br = $doc->createElement('br');
149
35055d05 150 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
151 $entry->parentNode->insertBefore($br, $entry);
152
35055d05
AD
153 $found = true;
154 }
155 }
156 }
157 }
cc85704f
AD
158 }
159
a16d7a5d
AD
160 // remove tiny thumbnails
161 if ($entry->hasAttribute("src")) {
162 if ($entry->parentNode && $entry->parentNode->parentNode) {
163 $entry->parentNode->parentNode->removeChild($entry->parentNode);
164 }
cc85704f
AD
165 }
166 }
167
a16d7a5d 168 $node = $doc->getElementsByTagName('body')->item(0);
cc85704f 169
ce7d5e87 170 if ($node && $found) {
cc38c8e5 171 $article["content"] = $doc->saveXML($node);
a16d7a5d 172 }
cc85704f 173 }
cc85704f
AD
174 }
175
176 return $article;
177 }
106a3de9
AD
178
179 function api_version() {
180 return 2;
181 }
182
cc85704f
AD
183}
184?>