]> git.wh0rd.org - tt-rss.git/blob - plugins/af_comics/init.php
09e9033794875c1f7ce3b60b58c24ebcb08809df
[tt-rss.git] / plugins / af_comics / init.php
1 <?php
2 class Af_Comics extends Plugin {
3
4 private $host;
5 private $filters = array();
6
7 function about() {
8 return array(2.0,
9 "Fixes RSS feeds of assorted comic strips",
10 "fox");
11 }
12
13 function init($host) {
14 $this->host = $host;
15
16 $host->add_hook($host::HOOK_FETCH_FEED, $this);
17 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
18 $host->add_hook($host::HOOK_PREFS_TAB, $this);
19
20 require_once __DIR__ . "/filter_base.php";
21
22 $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
23 $names = [];
24
25 foreach ($filters as $file) {
26 $filter_name = preg_replace("/\..*$/", "", basename($file));
27
28 if (array_search($filter_name, $names) === FALSE) {
29 if (!class_exists($filter_name)) {
30 require_once $file;
31 }
32
33 array_push($names, $filter_name);
34
35 $filter = new $filter_name();
36
37 if (is_subclass_of($filter, "Af_ComicFilter")) {
38 array_push($this->filters, $filter);
39 array_push($names, $filter_name);
40 }
41 }
42 }
43 }
44
45 function hook_prefs_tab($args) {
46 if ($args != "prefFeeds") return;
47
48 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
49
50 print "<p>" . __("The following comics are currently supported:") . "</p>";
51
52 $comics = array("GoComics");
53
54 foreach ($this->filters as $f) {
55 foreach ($f->supported() as $comic) {
56 array_push($comics, $comic);
57 }
58 }
59
60 asort($comics);
61
62 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
63 foreach ($comics as $comic) {
64 print "<li>$comic</li>";
65 }
66 print "</ul>";
67
68 print "<p>".__('GoComics requires a specific URL to workaround their lack of feed support: <code>http://feeds.feedburner.com/uclick/<em>comic_name</em></code> (e.g. <code>http://www.gocomics.com/garfield</code> uses <code>http://feeds.feedburner.com/uclick/garfield</code>).')."</p>";
69
70 print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>";
71
72 print "</div>";
73 }
74
75 function hook_article_filter($article) {
76 foreach ($this->filters as $f) {
77 if ($f->process($article))
78 break;
79 }
80
81 return $article;
82 }
83
84 // GoComics dropped feed support so it needs to be handled when fetching the feed.
85 /**
86 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
87 */
88 function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) {
89 if ($auth_login || $auth_pass)
90 return $feed_data;
91
92 if (preg_match('#^https?://feeds.feedburner.com/uclick/([-a-z]+)#', $fetch_url, $comic)) {
93 $site_url = 'http://www.gocomics.com/' . $comic[1];
94
95 $article_link = $site_url . date('/Y/m/d');
96
97 $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false));
98
99 require_once 'lib/MiniTemplator.class.php';
100
101 $feed_title = htmlspecialchars($comic[1]);
102 $site_url = htmlspecialchars($site_url);
103 $article_link = htmlspecialchars($article_link);
104
105 $tpl = new MiniTemplator();
106
107 $tpl->readTemplateFromFile('templates/generated_feed.txt');
108
109 $tpl->setVariable('FEED_TITLE', $feed_title, true);
110 $tpl->setVariable('VERSION', VERSION, true);
111 $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true);
112 $tpl->setVariable('SELF_URL', $site_url, true);
113
114 $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true);
115 $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true);
116
117 if ($body) {
118 $doc = new DOMDocument();
119
120 if (@$doc->loadHTML($body)) {
121 $xpath = new DOMXPath($doc);
122
123 $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0);
124
125 if ($node) {
126 $tpl->setVariable('ARTICLE_ID', $article_link, true);
127 $tpl->setVariable('ARTICLE_LINK', $article_link, true);
128 $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true);
129 $tpl->setVariable('ARTICLE_EXCERPT', '', true);
130 $tpl->setVariable('ARTICLE_CONTENT', $doc->saveXML($node), true);
131
132 $tpl->setVariable('ARTICLE_AUTHOR', '', true);
133 $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true);
134 $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true);
135
136 $tpl->addBlock('entry');
137 }
138 }
139 }
140
141 $tpl->addBlock('feed');
142
143 $tmp_data = '';
144
145 if ($tpl->generateOutputToString($tmp_data))
146 $feed_data = $tmp_data;
147 }
148
149 return $feed_data;
150 }
151
152 function api_version() {
153 return 2;
154 }
155
156 }