]>
Commit | Line | Data |
---|---|---|
de0d8d10 AD |
1 | <?php |
2 | class Af_Comics extends Plugin { | |
3 | ||
4 | private $host; | |
901dd67a | 5 | private $filters = array(); |
de0d8d10 AD |
6 | |
7 | function about() { | |
5800d3d5 | 8 | return array(2.0, |
de0d8d10 AD |
9 | "Fixes RSS feeds of assorted comic strips", |
10 | "fox"); | |
11 | } | |
12 | ||
13 | function init($host) { | |
14 | $this->host = $host; | |
15 | ||
5800d3d5 | 16 | $host->add_hook($host::HOOK_FETCH_FEED, $this); |
de0d8d10 AD |
17 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
18 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
de0d8d10 | 19 | |
901dd67a | 20 | require_once __DIR__ . "/filter_base.php"; |
de0d8d10 | 21 | |
6f398dcf AD |
22 | $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php")); |
23 | $names = []; | |
de0d8d10 | 24 | |
901dd67a | 25 | foreach ($filters as $file) { |
901dd67a | 26 | $filter_name = preg_replace("/\..*$/", "", basename($file)); |
de0d8d10 | 27 | |
6f398dcf AD |
28 | if (array_search($filter_name, $names) === FALSE) { |
29 | if (!class_exists($filter_name)) { | |
30 | require_once $file; | |
31 | } | |
de0d8d10 | 32 | |
6f398dcf AD |
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 | } | |
52265e19 AD |
41 | } |
42 | } | |
901dd67a | 43 | } |
de0d8d10 | 44 | |
901dd67a | 45 | function hook_prefs_tab($args) { |
6f398dcf | 46 | if ($args != "prefFeeds") return; |
de0d8d10 | 47 | |
901dd67a | 48 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">"; |
de0d8d10 | 49 | |
901dd67a | 50 | print "<p>" . __("The following comics are currently supported:") . "</p>"; |
de0d8d10 | 51 | |
5800d3d5 | 52 | $comics = array("GoComics"); |
de0d8d10 | 53 | |
901dd67a AD |
54 | foreach ($this->filters as $f) { |
55 | foreach ($f->supported() as $comic) { | |
56 | array_push($comics, $comic); | |
de0d8d10 AD |
57 | } |
58 | } | |
59 | ||
901dd67a | 60 | asort($comics); |
3df1a8b8 | 61 | |
901dd67a AD |
62 | print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">"; |
63 | foreach ($comics as $comic) { | |
64 | print "<li>$comic</li>"; | |
de0d8d10 | 65 | } |
901dd67a | 66 | print "</ul>"; |
de0d8d10 | 67 | |
a25c3c29 J |
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 | ||
6f398dcf AD |
70 | print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>"; |
71 | ||
901dd67a AD |
72 | print "</div>"; |
73 | } | |
de0d8d10 | 74 | |
901dd67a | 75 | function hook_article_filter($article) { |
901dd67a AD |
76 | foreach ($this->filters as $f) { |
77 | if ($f->process($article)) | |
78 | break; | |
de0d8d10 AD |
79 | } |
80 | ||
81 | return $article; | |
82 | } | |
83 | ||
5800d3d5 | 84 | // GoComics dropped feed support so it needs to be handled when fetching the feed. |
21ce7d9e AD |
85 | /** |
86 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
87 | */ | |
5800d3d5 J |
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 | ||
051737e9 | 92 | if (preg_match('#^https?://feeds.feedburner.com/uclick/([-a-z]+)#', $fetch_url, $comic)) { |
5800d3d5 J |
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); | |
f3774b9d | 130 | $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true); |
5800d3d5 J |
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 | ||
de0d8d10 AD |
152 | function api_version() { |
153 | return 2; | |
154 | } | |
155 | ||
f3774b9d | 156 | } |