]> git.wh0rd.org - tt-rss.git/blame - plugins/af_comics/init.php
af_comics: Go Comics now uses HTTPS.
[tt-rss.git] / plugins / af_comics / init.php
CommitLineData
de0d8d10
AD
1<?php
2class 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);
fc756975 17 $host->add_hook($host::HOOK_SUBSCRIBE_FEED, $this);
de0d8d10
AD
18 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
19 $host->add_hook($host::HOOK_PREFS_TAB, $this);
de0d8d10 20
901dd67a 21 require_once __DIR__ . "/filter_base.php";
de0d8d10 22
6f398dcf
AD
23 $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
24 $names = [];
de0d8d10 25
901dd67a 26 foreach ($filters as $file) {
901dd67a 27 $filter_name = preg_replace("/\..*$/", "", basename($file));
de0d8d10 28
6f398dcf
AD
29 if (array_search($filter_name, $names) === FALSE) {
30 if (!class_exists($filter_name)) {
31 require_once $file;
32 }
de0d8d10 33
6f398dcf
AD
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 }
52265e19
AD
42 }
43 }
901dd67a 44 }
de0d8d10 45
901dd67a 46 function hook_prefs_tab($args) {
6f398dcf 47 if ($args != "prefFeeds") return;
de0d8d10 48
901dd67a 49 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
de0d8d10 50
901dd67a 51 print "<p>" . __("The following comics are currently supported:") . "</p>";
de0d8d10 52
5800d3d5 53 $comics = array("GoComics");
de0d8d10 54
901dd67a
AD
55 foreach ($this->filters as $f) {
56 foreach ($f->supported() as $comic) {
57 array_push($comics, $comic);
de0d8d10
AD
58 }
59 }
60
901dd67a 61 asort($comics);
3df1a8b8 62
901dd67a
AD
63 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
64 foreach ($comics as $comic) {
65 print "<li>$comic</li>";
de0d8d10 66 }
901dd67a 67 print "</ul>";
de0d8d10 68
d1d05f7a 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>";
a25c3c29 70
6f398dcf
AD
71 print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>";
72
901dd67a
AD
73 print "</div>";
74 }
de0d8d10 75
901dd67a 76 function hook_article_filter($article) {
901dd67a
AD
77 foreach ($this->filters as $f) {
78 if ($f->process($article))
79 break;
de0d8d10
AD
80 }
81
82 return $article;
83 }
84
5800d3d5 85 // GoComics dropped feed support so it needs to be handled when fetching the feed.
21ce7d9e
AD
86 /**
87 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
88 */
5800d3d5
J
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
d1d05f7a 93 if (preg_match('#^https?://(?:feeds\.feedburner\.com/uclick|www\.gocomics\.com)/([-a-z0-9]+)$#i', $fetch_url, $comic)) {
aedcbfd9 94 $site_url = 'https://www.gocomics.com/' . $comic[1];
5800d3d5
J
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) {
e0383746
AD
127 $node->removeAttribute("width");
128
d92fcaa9
D
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")) {
3f5f5bc7 134 $node->removeAttribute("srcset");
d92fcaa9
D
135 }
136 }
137
5800d3d5
J
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);
f3774b9d 142 $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true);
5800d3d5
J
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
fc756975
D
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
de0d8d10
AD
174 function api_version() {
175 return 2;
176 }
177
f3774b9d 178}