]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
remove $link
[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) {
a16d7a5d 18 $owner_uid = $article["owner_uid"];
cc85704f 19
0b5ef30d
AD
20 $force = false;
21
cc85704f 22 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
0b5ef30d 23 if (strpos($article["plugin_data"], "redditimgur,$owner_uid:") === FALSE || $force) {
a16d7a5d
AD
24 $doc = new DOMDocument();
25 @$doc->loadHTML($article["content"]);
cc85704f 26
a16d7a5d
AD
27 if ($doc) {
28 $xpath = new DOMXPath($doc);
29 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 30
ce7d5e87
AD
31 $found = false;
32
a16d7a5d
AD
33 foreach ($entries as $entry) {
34 if ($entry->hasAttribute("href")) {
35 if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
cc85704f 36
a16d7a5d
AD
37 $img = $doc->createElement('img');
38 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 39
7ff4d1fa
AD
40 $br = $doc->createElement('br');
41 $entry->parentNode->insertBefore($img, $entry);
42 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87
AD
43
44 $found = true;
45 }
46
47 // links to imgur pages
48 $matches = array();
35055d05 49 if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
ce7d5e87
AD
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"));
7ff4d1fa
AD
68
69 $br = $doc->createElement('br');
70
ce7d5e87 71 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
72 $entry->parentNode->insertBefore($br, $entry);
73
ce7d5e87 74 $found = true;
35055d05
AD
75
76 break;
ce7d5e87
AD
77 }
78 }
79 }
80 }
a16d7a5d 81 }
35055d05
AD
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"));
0b5ef30d 100 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa
AD
101
102 $br = $doc->createElement('br');
103
35055d05 104 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
105 $entry->parentNode->insertBefore($br, $entry);
106
35055d05
AD
107 $found = true;
108 }
109 }
110 }
111 }
cc85704f
AD
112 }
113
a16d7a5d
AD
114 // remove tiny thumbnails
115 if ($entry->hasAttribute("src")) {
116 if ($entry->parentNode && $entry->parentNode->parentNode) {
117 $entry->parentNode->parentNode->removeChild($entry->parentNode);
118 }
cc85704f
AD
119 }
120 }
121
a16d7a5d 122 $node = $doc->getElementsByTagName('body')->item(0);
cc85704f 123
ce7d5e87 124 if ($node && $found) {
cc38c8e5 125 $article["content"] = $doc->saveXML($node);
0b5ef30d 126 if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"];
a16d7a5d 127 }
cc85704f 128 }
3cb9cd6e
AD
129 } else if (isset($article["stored"]["content"])) {
130 $article["content"] = $article["stored"]["content"];
cc85704f
AD
131 }
132 }
133
134 return $article;
135 }
136}
137?>