]>
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 | |
901dd67a | 22 | $filters = glob(__DIR__ . "/filters/*.php"); |
de0d8d10 | 23 | |
901dd67a AD |
24 | foreach ($filters as $file) { |
25 | require_once $file; | |
26 | $filter_name = preg_replace("/\..*$/", "", basename($file)); | |
de0d8d10 | 27 | |
901dd67a | 28 | $filter = new $filter_name(); |
de0d8d10 | 29 | |
901dd67a AD |
30 | if (is_subclass_of($filter, "Af_ComicFilter")) { |
31 | array_push($this->filters, $filter); | |
52265e19 AD |
32 | } |
33 | } | |
34 | ||
901dd67a | 35 | } |
de0d8d10 | 36 | |
901dd67a AD |
37 | function hook_prefs_tab($args) { |
38 | if ($args != "prefPrefs") return; | |
de0d8d10 | 39 | |
901dd67a | 40 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">"; |
de0d8d10 | 41 | |
901dd67a | 42 | print "<p>" . __("The following comics are currently supported:") . "</p>"; |
de0d8d10 | 43 | |
5800d3d5 | 44 | $comics = array("GoComics"); |
de0d8d10 | 45 | |
901dd67a AD |
46 | foreach ($this->filters as $f) { |
47 | foreach ($f->supported() as $comic) { | |
48 | array_push($comics, $comic); | |
de0d8d10 AD |
49 | } |
50 | } | |
51 | ||
901dd67a | 52 | asort($comics); |
3df1a8b8 | 53 | |
901dd67a AD |
54 | print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">"; |
55 | foreach ($comics as $comic) { | |
56 | print "<li>$comic</li>"; | |
de0d8d10 | 57 | } |
901dd67a | 58 | print "</ul>"; |
de0d8d10 | 59 | |
a25c3c29 J |
60 | 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>"; |
61 | ||
901dd67a AD |
62 | print "</div>"; |
63 | } | |
de0d8d10 | 64 | |
901dd67a | 65 | function hook_article_filter($article) { |
901dd67a AD |
66 | foreach ($this->filters as $f) { |
67 | if ($f->process($article)) | |
68 | break; | |
de0d8d10 AD |
69 | } |
70 | ||
71 | return $article; | |
72 | } | |
73 | ||
5800d3d5 | 74 | // GoComics dropped feed support so it needs to be handled when fetching the feed. |
21ce7d9e AD |
75 | /** |
76 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
77 | */ | |
5800d3d5 J |
78 | function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) { |
79 | if ($auth_login || $auth_pass) | |
80 | return $feed_data; | |
81 | ||
051737e9 | 82 | if (preg_match('#^https?://feeds.feedburner.com/uclick/([-a-z]+)#', $fetch_url, $comic)) { |
5800d3d5 J |
83 | $site_url = 'http://www.gocomics.com/' . $comic[1]; |
84 | ||
85 | $article_link = $site_url . date('/Y/m/d'); | |
86 | ||
87 | $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false)); | |
88 | ||
89 | require_once 'lib/MiniTemplator.class.php'; | |
90 | ||
91 | $feed_title = htmlspecialchars($comic[1]); | |
92 | $site_url = htmlspecialchars($site_url); | |
93 | $article_link = htmlspecialchars($article_link); | |
94 | ||
95 | $tpl = new MiniTemplator(); | |
96 | ||
97 | $tpl->readTemplateFromFile('templates/generated_feed.txt'); | |
98 | ||
99 | $tpl->setVariable('FEED_TITLE', $feed_title, true); | |
100 | $tpl->setVariable('VERSION', VERSION, true); | |
101 | $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true); | |
102 | $tpl->setVariable('SELF_URL', $site_url, true); | |
103 | ||
104 | $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true); | |
105 | $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true); | |
106 | ||
107 | if ($body) { | |
108 | $doc = new DOMDocument(); | |
109 | ||
110 | if (@$doc->loadHTML($body)) { | |
111 | $xpath = new DOMXPath($doc); | |
112 | ||
113 | $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0); | |
114 | ||
115 | if ($node) { | |
116 | $tpl->setVariable('ARTICLE_ID', $article_link, true); | |
117 | $tpl->setVariable('ARTICLE_LINK', $article_link, true); | |
118 | $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true); | |
119 | $tpl->setVariable('ARTICLE_EXCERPT', '', true); | |
120 | $tpl->setVariable('ARTICLE_CONTENT', $doc->saveXML($node), true); | |
121 | ||
122 | $tpl->setVariable('ARTICLE_AUTHOR', '', true); | |
123 | $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true); | |
124 | $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true); | |
125 | ||
126 | $tpl->addBlock('entry'); | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | $tpl->addBlock('feed'); | |
132 | ||
133 | $tmp_data = ''; | |
134 | ||
135 | if ($tpl->generateOutputToString($tmp_data)) | |
136 | $feed_data = $tmp_data; | |
137 | } | |
138 | ||
139 | return $feed_data; | |
140 | } | |
141 | ||
de0d8d10 AD |
142 | function api_version() { |
143 | return 2; | |
144 | } | |
145 | ||
21ce7d9e | 146 | } |