]>
Commit | Line | Data |
---|---|---|
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_SUBSCRIBE_FEED, $this); | |
18 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); | |
19 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
20 | ||
21 | require_once __DIR__ . "/filter_base.php"; | |
22 | ||
23 | $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php")); | |
24 | $names = []; | |
25 | ||
26 | foreach ($filters as $file) { | |
27 | $filter_name = preg_replace("/\..*$/", "", basename($file)); | |
28 | ||
29 | if (array_search($filter_name, $names) === FALSE) { | |
30 | if (!class_exists($filter_name)) { | |
31 | require_once $file; | |
32 | } | |
33 | ||
34 | array_push($names, $filter_name); | |
35 | ||
36 | $filter = new $filter_name(); | |
37 | ||
38 | if (is_subclass_of($filter, "Af_ComicFilter")) { | |
39 | array_push($this->filters, $filter); | |
40 | array_push($names, $filter_name); | |
41 | } | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | function hook_prefs_tab($args) { | |
47 | if ($args != "prefFeeds") return; | |
48 | ||
49 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">"; | |
50 | ||
51 | print "<p>" . __("The following comics are currently supported:") . "</p>"; | |
52 | ||
53 | $comics = array("GoComics"); | |
54 | ||
55 | foreach ($this->filters as $f) { | |
56 | foreach ($f->supported() as $comic) { | |
57 | array_push($comics, $comic); | |
58 | } | |
59 | } | |
60 | ||
61 | asort($comics); | |
62 | ||
63 | print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">"; | |
64 | foreach ($comics as $comic) { | |
65 | print "<li>$comic</li>"; | |
66 | } | |
67 | print "</ul>"; | |
68 | ||
69 | print "<p>".__("To subscribe to GoComics use the comic's regular web page as the feed URL (e.g. for the <em>Garfield</em> comic use <code>http://www.gocomics.com/garfield</code>).")."</p>"; | |
70 | ||
71 | print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>"; | |
72 | ||
73 | print "</div>"; | |
74 | } | |
75 | ||
76 | function hook_article_filter($article) { | |
77 | foreach ($this->filters as $f) { | |
78 | if ($f->process($article)) | |
79 | break; | |
80 | } | |
81 | ||
82 | return $article; | |
83 | } | |
84 | ||
85 | // GoComics dropped feed support so it needs to be handled when fetching the feed. | |
86 | /** | |
87 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
88 | */ | |
89 | function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) { | |
90 | if ($auth_login || $auth_pass) | |
91 | return $feed_data; | |
92 | ||
93 | if (preg_match('#^https?://(?:feeds\.feedburner\.com/uclick|www\.gocomics\.com)/([-a-z0-9]+)$#i', $fetch_url, $comic)) { | |
94 | $site_url = 'http://www.gocomics.com/' . $comic[1]; | |
95 | ||
96 | $article_link = $site_url . date('/Y/m/d'); | |
97 | ||
98 | $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false)); | |
99 | ||
100 | require_once 'lib/MiniTemplator.class.php'; | |
101 | ||
102 | $feed_title = htmlspecialchars($comic[1]); | |
103 | $site_url = htmlspecialchars($site_url); | |
104 | $article_link = htmlspecialchars($article_link); | |
105 | ||
106 | $tpl = new MiniTemplator(); | |
107 | ||
108 | $tpl->readTemplateFromFile('templates/generated_feed.txt'); | |
109 | ||
110 | $tpl->setVariable('FEED_TITLE', $feed_title, true); | |
111 | $tpl->setVariable('VERSION', VERSION, true); | |
112 | $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true); | |
113 | $tpl->setVariable('SELF_URL', $site_url, true); | |
114 | ||
115 | $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true); | |
116 | $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true); | |
117 | ||
118 | if ($body) { | |
119 | $doc = new DOMDocument(); | |
120 | ||
121 | if (@$doc->loadHTML($body)) { | |
122 | $xpath = new DOMXPath($doc); | |
123 | ||
124 | $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0); | |
125 | ||
126 | if ($node) { | |
127 | $node->removeAttribute("width"); | |
128 | ||
129 | if ($node->hasAttribute("srcset") && preg_match("|/transparent\.png$|", $node->getAttribute("srcset"))) { | |
130 | if ($node->hasAttribute("data-srcset")) { | |
131 | $node->setAttribute("srcset", $node->getAttribute("data-srcset")); | |
132 | $node->removeAttribute("data-srcset"); | |
133 | } elseif ($node->hasAttribute("src")) { | |
134 | $node->removeAttribute("srcset"); | |
135 | } | |
136 | } | |
137 | ||
138 | $tpl->setVariable('ARTICLE_ID', $article_link, true); | |
139 | $tpl->setVariable('ARTICLE_LINK', $article_link, true); | |
140 | $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true); | |
141 | $tpl->setVariable('ARTICLE_EXCERPT', '', true); | |
142 | $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true); | |
143 | ||
144 | $tpl->setVariable('ARTICLE_AUTHOR', '', true); | |
145 | $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true); | |
146 | $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true); | |
147 | ||
148 | $tpl->addBlock('entry'); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | $tpl->addBlock('feed'); | |
154 | ||
155 | $tmp_data = ''; | |
156 | ||
157 | if ($tpl->generateOutputToString($tmp_data)) | |
158 | $feed_data = $tmp_data; | |
159 | } | |
160 | ||
161 | return $feed_data; | |
162 | } | |
163 | ||
164 | function hook_subscribe_feed($contents, $url, $auth_login, $auth_pass) { | |
165 | if ($auth_login || $auth_pass) | |
166 | return $contents; | |
167 | ||
168 | if (preg_match('#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i', $url)) | |
169 | return '<?xml version="1.0" encoding="utf-8"?>'; // Get is_html() to return false. | |
170 | ||
171 | return $contents; | |
172 | } | |
173 | ||
174 | function api_version() { | |
175 | return 2; | |
176 | } | |
177 | ||
178 | } |