]>
Commit | Line | Data |
---|---|---|
cc85704f | 1 | <?php |
0862a602 | 2 | class 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")) { | |
299aeb30 | 31 | |
9875d717 | 32 | if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) { |
33 | ||
1ae46c50 AD |
34 | $video = $doc->createElement('video'); |
35 | $video->setAttribute("autoplay", "1"); | |
36 | $video->setAttribute("loop", "1"); | |
299aeb30 | 37 | |
1ae46c50 AD |
38 | $source = $doc->createElement('source'); |
39 | $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href"))); | |
40 | $source->setAttribute("type", "video/mp4"); | |
299aeb30 | 41 | |
1ae46c50 AD |
42 | $video->appendChild($source); |
43 | ||
44 | $br = $doc->createElement('br'); | |
45 | $entry->parentNode->insertBefore($video, $entry); | |
46 | $entry->parentNode->insertBefore($br, $entry); | |
47 | ||
48 | $found = true; | |
299aeb30 | 49 | } |
9875d717 | 50 | |
7adf9556 | 51 | if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) { |
cc85704f | 52 | |
a16d7a5d AD |
53 | $img = $doc->createElement('img'); |
54 | $img->setAttribute("src", $entry->getAttribute("href")); | |
cc85704f | 55 | |
7ff4d1fa AD |
56 | $br = $doc->createElement('br'); |
57 | $entry->parentNode->insertBefore($img, $entry); | |
58 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 AD |
59 | |
60 | $found = true; | |
61 | } | |
62 | ||
63 | // links to imgur pages | |
64 | $matches = array(); | |
248c5a6a | 65 | if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
ce7d5e87 | 66 | |
1af9f54f | 67 | $token = $matches[2]; |
ce7d5e87 AD |
68 | |
69 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
70 | false, false, false, false, 10); | |
71 | ||
72 | if ($album_content && $token) { | |
73 | $adoc = new DOMDocument(); | |
74 | @$adoc->loadHTML($album_content); | |
75 | ||
76 | if ($adoc) { | |
77 | $axpath = new DOMXPath($adoc); | |
78 | $aentries = $axpath->query('(//img[@src])'); | |
79 | ||
80 | foreach ($aentries as $aentry) { | |
e44ea762 | 81 | if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) { |
ce7d5e87 AD |
82 | $img = $doc->createElement('img'); |
83 | $img->setAttribute("src", $aentry->getAttribute("src")); | |
7ff4d1fa AD |
84 | |
85 | $br = $doc->createElement('br'); | |
86 | ||
ce7d5e87 | 87 | $entry->parentNode->insertBefore($img, $entry); |
7ff4d1fa AD |
88 | $entry->parentNode->insertBefore($br, $entry); |
89 | ||
ce7d5e87 | 90 | $found = true; |
35055d05 AD |
91 | |
92 | break; | |
ce7d5e87 AD |
93 | } |
94 | } | |
95 | } | |
96 | } | |
a16d7a5d | 97 | } |
35055d05 AD |
98 | |
99 | // linked albums, ffs | |
47cdc58c | 100 | if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { |
35055d05 AD |
101 | |
102 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
103 | false, false, false, false, 10); | |
104 | ||
105 | if ($album_content) { | |
106 | $adoc = new DOMDocument(); | |
107 | @$adoc->loadHTML($album_content); | |
108 | ||
109 | if ($adoc) { | |
110 | $axpath = new DOMXPath($adoc); | |
47cdc58c | 111 | $aentries = $axpath->query("//meta[@property='og:image']"); |
35055d05 AD |
112 | |
113 | foreach ($aentries as $aentry) { | |
114 | $img = $doc->createElement('img'); | |
47cdc58c | 115 | $img->setAttribute("src", $aentry->getAttribute("content")); |
0b5ef30d | 116 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); |
7ff4d1fa AD |
117 | |
118 | $br = $doc->createElement('br'); | |
119 | ||
35055d05 | 120 | $entry->parentNode->insertBefore($img, $entry); |
7ff4d1fa AD |
121 | $entry->parentNode->insertBefore($br, $entry); |
122 | ||
35055d05 AD |
123 | $found = true; |
124 | } | |
125 | } | |
126 | } | |
127 | } | |
cc85704f AD |
128 | } |
129 | ||
a16d7a5d AD |
130 | // remove tiny thumbnails |
131 | if ($entry->hasAttribute("src")) { | |
132 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
133 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
134 | } | |
cc85704f AD |
135 | } |
136 | } | |
137 | ||
a16d7a5d | 138 | $node = $doc->getElementsByTagName('body')->item(0); |
cc85704f | 139 | |
ce7d5e87 | 140 | if ($node && $found) { |
cc38c8e5 | 141 | $article["content"] = $doc->saveXML($node); |
a16d7a5d | 142 | } |
cc85704f | 143 | } |
cc85704f AD |
144 | } |
145 | ||
146 | return $article; | |
147 | } | |
106a3de9 AD |
148 | |
149 | function api_version() { | |
150 | return 2; | |
151 | } | |
152 | ||
cc85704f AD |
153 | } |
154 | ?> |