2 class Af_Comics extends Plugin {
5 private $filters = array();
9 "Fixes RSS feeds of assorted comic strips",
13 function init($host) {
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);
21 require_once __DIR__ . "/filter_base.php";
23 $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
26 foreach ($filters as $file) {
27 $filter_name = preg_replace("/\..*$/", "", basename($file));
29 if (array_search($filter_name, $names) === FALSE) {
30 if (!class_exists($filter_name)) {
34 array_push($names, $filter_name);
36 $filter = new $filter_name();
38 if (is_subclass_of($filter, "Af_ComicFilter")) {
39 array_push($this->filters, $filter);
40 array_push($names, $filter_name);
46 function hook_prefs_tab($args) {
47 if ($args != "prefFeeds") return;
49 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
51 print "<p>" . __("The following comics are currently supported:") . "</p>";
53 $comics = array("GoComics");
55 foreach ($this->filters as $f) {
56 foreach ($f->supported() as $comic) {
57 array_push($comics, $comic);
63 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
64 foreach ($comics as $comic) {
65 print "<li>$comic</li>";
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>";
71 print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>";
76 function hook_article_filter($article) {
77 foreach ($this->filters as $f) {
78 if ($f->process($article))
85 // GoComics dropped feed support so it needs to be handled when fetching the feed.
87 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
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)
93 if (preg_match('#^https?://(?:feeds\.feedburner\.com/uclick|www\.gocomics\.com)/([-a-z0-9]+)$#i', $fetch_url, $comic)) {
94 $site_url = 'https://www.gocomics.com/' . $comic[1];
96 $article_link = $site_url . date('/Y/m/d');
98 $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false));
100 require_once 'lib/MiniTemplator.class.php';
102 $feed_title = htmlspecialchars($comic[1]);
103 $site_url = htmlspecialchars($site_url);
104 $article_link = htmlspecialchars($article_link);
106 $tpl = new MiniTemplator();
108 $tpl->readTemplateFromFile('templates/generated_feed.txt');
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);
115 $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true);
116 $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true);
119 $doc = new DOMDocument();
121 if (@$doc->loadHTML($body)) {
122 $xpath = new DOMXPath($doc);
124 $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0);
127 $node->removeAttribute("width");
128 $node->removeAttribute("data-srcset");
129 $node->removeAttribute("srcset");
131 $tpl->setVariable('ARTICLE_ID', $article_link, true);
132 $tpl->setVariable('ARTICLE_LINK', $article_link, true);
133 $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true);
134 $tpl->setVariable('ARTICLE_EXCERPT', '', true);
135 $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true);
137 $tpl->setVariable('ARTICLE_AUTHOR', '', true);
138 $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true);
139 $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true);
141 $tpl->addBlock('entry');
147 $tpl->addBlock('feed');
151 if ($tpl->generateOutputToString($tmp_data))
152 $feed_data = $tmp_data;
158 function hook_subscribe_feed($contents, $url, $auth_login, $auth_pass) {
159 if ($auth_login || $auth_pass)
162 if (preg_match('#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i', $url))
163 return '<?xml version="1.0" encoding="utf-8"?>'; // Get is_html() to return false.
168 function api_version() {