]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_comics/init.php
Merge branch 'master' of github.com:gothfox/Tiny-Tiny-RSS
[tt-rss.git] / plugins / af_comics / init.php
1 <?php
2 class Af_Comics extends Plugin {
3
4         private $host;
5
6         function about() {
7                 return array(1.0,
8                         "Fixes RSS feeds of assorted comic strips",
9                         "fox");
10         }
11
12         function init($host) {
13                 $this->host = $host;
14
15                 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
16                 $host->add_hook($host::HOOK_PREFS_TAB, $this);
17         }
18
19         function hook_prefs_tab($args) {
20                 if ($args != "prefPrefs") return;
21
22                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
23
24                 print_notice("This plugin supports the following comics:");
25
26                 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
27                 print "<li>Buni</li>
28                 <li>Buttersafe</li>
29                 <li>CSection</li>
30                 <li>Ctrl+Alt+Del</li>
31                 <li>Dilbert</li>
32                 <li>Explosm</li>
33                 <li>GoComics</li>
34                 <li>Happy Jar</li>
35                 <li>Penny Arcade</li>
36                 <li>Three word phrase</li>
37                 <li>Whomp</li>";
38                 print "</ul>";
39
40                 print "</div>";
41         }
42
43         function hook_article_filter($article) {
44                 $owner_uid = $article["owner_uid"];
45
46                 $found = false;
47
48                 # div#comic - comicpress?
49
50                 if (strpos($article["guid"], "bunicomic.com") !== FALSE ||
51                                 strpos($article["guid"], "buttersafe.com") !== FALSE ||
52                                 strpos($article["guid"], "whompcomic.com") !== FALSE ||
53                                 strpos($article["guid"], "happyjar.com") !== FALSE ||
54                                 strpos($article["guid"], "csectioncomics.com") !== FALSE) {
55
56                          if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
57
58
59                                 // lol at people who block clients by user agent
60                                 // oh noes my ad revenue Q_Q
61
62                                 $res = fetch_file_contents($article["link"], false, false, false,
63                                          false, false, 0,
64                                          "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)");
65
66                                 $doc = new DOMDocument();
67                                 @$doc->loadHTML($res);
68
69                                 $basenode = false;
70
71                                 if ($doc) {
72                                         $xpath = new DOMXPath($doc);
73                                         $basenode = $xpath->query('//div[@id="comic"]')->item(0);
74
75                                         if ($basenode) {
76                                                 $article["content"] = $doc->saveXML($basenode);
77                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
78                                         }
79                                 }
80                         } else if (isset($article["stored"]["content"])) {
81                                 $article["content"] = $article["stored"]["content"];
82                         }
83                 }
84
85                 if (strpos($article["guid"], "dilbert.com") !== FALSE) {
86                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
87                                 $doc = new DOMDocument();
88                                 @$doc->loadHTML(fetch_file_contents($article["link"]));
89
90                                 $basenode = false;
91
92                                 if ($doc) {
93                                         $xpath = new DOMXPath($doc);
94                                         $entries = $xpath->query('(//img[@src])'); // we might also check for img[@class='strip'] I guess...
95
96                                         $matches = array();
97
98                                         foreach ($entries as $entry) {
99
100                                                 if (preg_match("/dyn\/str_strip\/.*zoom\.gif$/", $entry->getAttribute("src"), $matches)) {
101
102                                                         $entry->setAttribute("src",
103                                                                 rewrite_relative_url("http://dilbert.com/",
104                                                                 $matches[0]));
105
106                                                         $basenode = $entry;
107                                                         break;
108                                                 }
109                                         }
110
111                                         if ($basenode) {
112                                                 $article["content"] = $doc->saveXML($basenode);
113                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
114                                         }
115                                 }
116                         } else if (isset($article["stored"]["content"])) {
117                                 $article["content"] = $article["stored"]["content"];
118                         }
119                 }
120
121                 if (strpos($article["link"], "explosm.net/comics") !== FALSE) {
122                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
123
124                                 $doc = new DOMDocument();
125                                 @$doc->loadHTML(fetch_file_contents($article["link"]));
126
127                                 $basenode = false;
128
129                                 if ($doc) {
130                                         $xpath = new DOMXPath($doc);
131                                         $entries = $xpath->query('(//img[@src])'); // we might also check for img[@class='strip'] I guess...
132
133                                         $matches = array();
134
135                                         foreach ($entries as $entry) {
136
137                                                 if (preg_match("/(http:\/\/.*\/db\/files\/Comics\/.*)/i", $entry->getAttribute("src"), $matches)) {
138
139                                                         $basenode = $entry;
140                                                         break;
141                                                 }
142                                         }
143
144                                         if ($basenode) {
145                                                 $article["content"] = $doc->saveXML($basenode);
146                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
147                                         }
148                                 }
149                         } else if (isset($article["stored"]["content"])) {
150                                 $article["content"] = $article["stored"]["content"];
151                         }
152                 }
153
154                 if (strpos($article["link"], "cad-comic.com/cad/") !== FALSE) {
155                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
156
157                                 $doc = new DOMDocument();
158                                 @$doc->loadHTML(fetch_file_contents($article["link"]));
159
160                                 $basenode = false;
161
162                                 if ($doc) {
163                                         $xpath = new DOMXPath($doc);
164                                         $basenode = $xpath->query('(//img[contains(@src, "/comics/cad-")])')->item(0);
165
166                                         if ($basenode) {
167                                                 $article["content"] = $doc->saveXML($basenode);
168                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
169                                         }
170                                 }
171
172                         } else if (isset($article["stored"]["content"])) {
173                                 $article["content"] = $article["stored"]["content"];
174                         }
175                 }
176
177                 if (strpos($article["guid"], "gocomics.com") !== FALSE) {
178                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
179                                 $doc = new DOMDocument();
180                                 @$doc->loadHTML(fetch_file_contents($article["link"]));
181
182                                 $basenode = false;
183
184                                 if ($doc) {
185                                         $xpath = new DOMXPath($doc);
186                                         $entries = $xpath->query('(//img[@src])'); // we might also check for img[@class='strip'] I guess...
187
188                                         $matches = array();
189
190                                         foreach ($entries as $entry) {
191
192                                                 if (preg_match("/(http:\/\/assets.amuniversal.com\/.*width.*)/i", $entry->getAttribute("src"), $matches)) {
193
194                                                         $entry->setAttribute("src", $matches[0]);
195                                                         $basenode = $entry;
196                                                         break;
197                                                 }
198                                         }
199
200                     if (!$basenode) {
201                         // fallback on the smaller version
202                         foreach ($entries as $entry) {
203
204                             if (preg_match("/(http:\/\/assets.amuniversal.com\/.*)/i", $entry->getAttribute("src"), $matches)) {
205
206                                 $entry->setAttribute("src", $matches[0]);
207                                 $basenode = $entry;
208                                 break;
209                             }
210                         }
211                     }
212
213                                         if ($basenode) {
214                                                 $article["content"] = $doc->saveXML($basenode);
215                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
216                                         }
217                                 }
218                         } else if (isset($article["stored"]["content"])) {
219                                 $article["content"] = $article["stored"]["content"];
220                         }
221                 }
222
223                 if (strpos($article["link"], "penny-arcade.com") !== FALSE && strpos($article["title"], "Comic:") !== FALSE) {
224                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
225
226                                 if ($debug_enabled) {
227                                         _debug("af_pennyarcade: Processing comic");
228                                 }
229
230                                 $doc = new DOMDocument();
231                                 $doc->loadHTML(fetch_file_contents($article["link"]));
232
233                                 $basenode = false;
234
235                                 if ($doc) {
236                                         $xpath = new DOMXPath($doc);
237                                         $basenode = $xpath->query('(//div[@id="comicFrame"])')->item(0);
238
239                                         if ($basenode) {
240                                                 $article["content"] = $doc->saveXML($basenode);
241                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
242                                         }
243                                 }
244                         } else if (isset($article["stored"]["content"])) {
245                                 $article["content"] = $article["stored"]["content"];
246                         }
247                 }
248
249                 if (strpos($article["link"], "penny-arcade.com") !== FALSE && strpos($article["title"], "News Post:") !== FALSE) {
250                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
251                                 if ($debug_enabled) {
252                                         _debug("af_pennyarcade: Processing news post");
253                                 }
254                                 $doc = new DOMDocument();
255                                 $doc->loadHTML(fetch_file_contents($article["link"]));
256
257                                 if ($doc) {
258                                         $xpath = new DOMXPath($doc);
259                                         $entries = $xpath->query('(//div[@class="post"])');
260
261                                         $basenode = false;
262
263                                         foreach ($entries as $entry) {
264                                                 $basenode = $entry;
265                                         }
266
267                                         $meta = $xpath->query('(//div[@class="meta"])')->item(0);
268                                         if ($meta->parentNode) { $meta->parentNode->removeChild($meta); }
269
270                                         $header = $xpath->query('(//div[@class="postBody"]/h2)')->item(0);
271                                         if ($header->parentNode) { $header->parentNode->removeChild($header); }
272
273                                         $header = $xpath->query('(//div[@class="postBody"]/div[@class="comicPost"])')->item(0);
274                                         if ($header->parentNode) { $header->parentNode->removeChild($header); }
275
276                                         $avatar = $xpath->query('(//div[@class="avatar"]//img)')->item(0);
277                                         $basenode->insertBefore($avatar, $basenode->firstChild);
278
279                                         $uninteresting = $xpath->query('(//div[@class="avatar"])');
280                                         foreach ($uninteresting as $i) {
281                                                 $i->parentNode->removeChild($i);
282                                         }
283
284                                         if ($basenode){
285                                                 $article["content"] = $doc->saveXML($basenode);
286                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
287                                         }
288                                 }
289                         } else if (isset($article["stored"]["content"])) {
290                                 $article["content"] = $article["stored"]["content"];
291                         }
292                 }
293
294                 if (strpos($article["link"], "threewordphrase.com") !== FALSE) {
295                         if (strpos($article["plugin_data"], "af_comics,$owner_uid:") === FALSE) {
296
297                                 $doc = new DOMDocument();
298                                 @$doc->loadHTML(fetch_file_contents($article["link"]));
299
300                                 $basenode = false;
301
302                                 if ($doc) {
303                                         $xpath = new DOMXpath($doc);
304
305                                         $basenode = $xpath->query("//td/center/img")->item(0);
306
307                                         if ($basenode) {
308                                                 $article["content"] = $doc->saveXML($basenode);
309                                                 $article["plugin_data"] = "af_comics,$owner_uid:" . $article["plugin_data"];
310                                         }
311                                 }
312                         } else if (isset($article["stored"]["content"])) {
313                                 $article["content"] = $article["stored"]["content"];
314                         }
315                 }
316
317                 return $article;
318         }
319
320         function api_version() {
321                 return 2;
322         }
323
324 }
325 ?>