codysnider /
tt-rss
| 1 | <?php |
||||||||
| 2 | class Af_Comics extends Plugin { |
||||||||
| 3 | |||||||||
| 4 | private $host; |
||||||||
| 5 | private $filters = array(); |
||||||||
| 6 | |||||||||
| 7 | public function about() { |
||||||||
| 8 | return array(2.0, |
||||||||
| 9 | "Fixes RSS feeds of assorted comic strips", |
||||||||
| 10 | "fox"); |
||||||||
| 11 | } |
||||||||
| 12 | |||||||||
| 13 | public function init($host) { |
||||||||
| 14 | $this->host = $host; |
||||||||
| 15 | |||||||||
| 16 | $host->add_hook($host::HOOK_FETCH_FEED, $this); |
||||||||
| 17 | $host->add_hook($host::HOOK_FEED_BASIC_INFO, $this); |
||||||||
| 18 | $host->add_hook($host::HOOK_SUBSCRIBE_FEED, $this); |
||||||||
| 19 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
||||||||
| 20 | $host->add_hook($host::HOOK_PREFS_TAB, $this); |
||||||||
| 21 | |||||||||
| 22 | require_once __DIR__."/filter_base.php"; |
||||||||
| 23 | |||||||||
| 24 | $filters = array_merge(glob(__DIR__."/filters.local/*.php"), glob(__DIR__."/filters/*.php")); |
||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
glob(__DIR__ . '/filters.local/*.php') can also be of type false; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 25 | $names = []; |
||||||||
| 26 | |||||||||
| 27 | foreach ($filters as $file) { |
||||||||
| 28 | $filter_name = preg_replace("/\..*$/", "", basename($file)); |
||||||||
| 29 | |||||||||
| 30 | if (array_search($filter_name, $names) === false) { |
||||||||
| 31 | if (!class_exists($filter_name)) { |
||||||||
| 32 | require_once $file; |
||||||||
| 33 | } |
||||||||
| 34 | |||||||||
| 35 | array_push($names, $filter_name); |
||||||||
| 36 | |||||||||
| 37 | $filter = new $filter_name(); |
||||||||
| 38 | |||||||||
| 39 | if (is_subclass_of($filter, "Af_ComicFilter")) { |
||||||||
| 40 | array_push($this->filters, $filter); |
||||||||
| 41 | array_push($names, $filter_name); |
||||||||
| 42 | } |
||||||||
| 43 | } |
||||||||
| 44 | } |
||||||||
| 45 | } |
||||||||
| 46 | |||||||||
| 47 | public function hook_prefs_tab($args) { |
||||||||
| 48 | if ($args != "prefFeeds") { |
||||||||
| 49 | return; |
||||||||
| 50 | } |
||||||||
| 51 | |||||||||
| 52 | print "<div dojoType=\"dijit.layout.AccordionPane\" |
||||||||
| 53 | title=\"<i class='material-icons'>photo</i> ".__('Feeds supported by af_comics')."\">"; |
||||||||
| 54 | |||||||||
| 55 | print "<p>".__("The following comics are currently supported:")."</p>"; |
||||||||
| 56 | |||||||||
| 57 | $comics = array("GoComics"); |
||||||||
| 58 | |||||||||
| 59 | foreach ($this->filters as $f) { |
||||||||
| 60 | foreach ($f->supported() as $comic) { |
||||||||
| 61 | array_push($comics, $comic); |
||||||||
| 62 | } |
||||||||
| 63 | } |
||||||||
| 64 | |||||||||
| 65 | asort($comics); |
||||||||
| 66 | |||||||||
| 67 | print "<ul class='panel panel-scrollable list list-unstyled'>"; |
||||||||
| 68 | foreach ($comics as $comic) { |
||||||||
| 69 | print "<li>$comic</li>"; |
||||||||
| 70 | } |
||||||||
| 71 | print "</ul>"; |
||||||||
| 72 | |||||||||
| 73 | 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>"; |
||||||||
| 74 | |||||||||
| 75 | print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>"; |
||||||||
| 76 | |||||||||
| 77 | print "</div>"; |
||||||||
| 78 | } |
||||||||
| 79 | |||||||||
| 80 | public function hook_article_filter($article) { |
||||||||
| 81 | foreach ($this->filters as $f) { |
||||||||
| 82 | if ($f->process($article)) { |
||||||||
| 83 | break; |
||||||||
| 84 | } |
||||||||
| 85 | } |
||||||||
| 86 | |||||||||
| 87 | return $article; |
||||||||
| 88 | } |
||||||||
| 89 | |||||||||
| 90 | // GoComics dropped feed support so it needs to be handled when fetching the feed. |
||||||||
| 91 | /** |
||||||||
| 92 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||||||||
| 93 | */ |
||||||||
| 94 | public function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) { |
||||||||
|
0 ignored issues
–
show
The parameter
$last_article_timestamp is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$feed is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$owner_uid is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 95 | if ($auth_login || $auth_pass) { |
||||||||
| 96 | return $feed_data; |
||||||||
| 97 | } |
||||||||
| 98 | |||||||||
| 99 | if (preg_match('#^https?://(?:feeds\.feedburner\.com/uclick|www\.gocomics\.com)/([-a-z0-9]+)$#i', $fetch_url, $comic)) { |
||||||||
| 100 | $site_url = 'https://www.gocomics.com/'.$comic[1]; |
||||||||
| 101 | |||||||||
| 102 | $article_link = $site_url.date('/Y/m/d'); |
||||||||
| 103 | |||||||||
| 104 | $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false)); |
||||||||
| 105 | |||||||||
| 106 | require_once 'lib/MiniTemplator.class.php'; |
||||||||
| 107 | |||||||||
| 108 | $feed_title = htmlspecialchars($comic[1]); |
||||||||
| 109 | $site_url = htmlspecialchars($site_url); |
||||||||
| 110 | $article_link = htmlspecialchars($article_link); |
||||||||
| 111 | |||||||||
| 112 | $tpl = new MiniTemplator(); |
||||||||
| 113 | |||||||||
| 114 | $tpl->readTemplateFromFile('templates/generated_feed.txt'); |
||||||||
| 115 | |||||||||
| 116 | $tpl->setVariable('FEED_TITLE', $feed_title, true); |
||||||||
| 117 | $tpl->setVariable('VERSION', get_version(), true); |
||||||||
| 118 | $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true); |
||||||||
| 119 | $tpl->setVariable('SELF_URL', $site_url, true); |
||||||||
| 120 | |||||||||
| 121 | if ($body) { |
||||||||
| 122 | $doc = new DOMDocument(); |
||||||||
| 123 | |||||||||
| 124 | if (@$doc->loadHTML($body)) { |
||||||||
|
0 ignored issues
–
show
It seems like
$body can also be of type true; however, parameter $source of DOMDocument::loadHTML() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 125 | $xpath = new DOMXPath($doc); |
||||||||
| 126 | |||||||||
| 127 | $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0); |
||||||||
| 128 | |||||||||
| 129 | if ($node) { |
||||||||
| 130 | $title = $xpath->query('//h1')->item(0); |
||||||||
| 131 | |||||||||
| 132 | if ($title) { |
||||||||
| 133 | $title = clean(trim($title->nodeValue)); |
||||||||
| 134 | } else { |
||||||||
| 135 | $title = date('l, F d, Y'); |
||||||||
| 136 | } |
||||||||
| 137 | |||||||||
| 138 | foreach (['srcset', 'sizes', 'data-srcset', 'width'] as $attr) { |
||||||||
| 139 | $node->removeAttribute($attr); |
||||||||
| 140 | } |
||||||||
| 141 | |||||||||
| 142 | $tpl->setVariable('ARTICLE_ID', $article_link, true); |
||||||||
| 143 | $tpl->setVariable('ARTICLE_LINK', $article_link, true); |
||||||||
| 144 | $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c', mktime(11, 0, 0)), true); |
||||||||
| 145 | $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($title), true); |
||||||||
| 146 | $tpl->setVariable('ARTICLE_EXCERPT', '', true); |
||||||||
| 147 | $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true); |
||||||||
| 148 | |||||||||
| 149 | $tpl->setVariable('ARTICLE_AUTHOR', '', true); |
||||||||
| 150 | $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true); |
||||||||
| 151 | $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true); |
||||||||
| 152 | |||||||||
| 153 | $tpl->addBlock('entry'); |
||||||||
| 154 | } |
||||||||
| 155 | } |
||||||||
| 156 | } |
||||||||
| 157 | |||||||||
| 158 | $tpl->addBlock('feed'); |
||||||||
| 159 | |||||||||
| 160 | if ($tpl->generateOutputToString($tmp_data)) { |
||||||||
| 161 | $feed_data = $tmp_data; |
||||||||
| 162 | } |
||||||||
| 163 | } |
||||||||
| 164 | |||||||||
| 165 | return $feed_data; |
||||||||
| 166 | } |
||||||||
| 167 | |||||||||
| 168 | public function hook_subscribe_feed($contents, $url, $auth_login, $auth_pass) { |
||||||||
| 169 | if ($auth_login || $auth_pass) { |
||||||||
| 170 | return $contents; |
||||||||
| 171 | } |
||||||||
| 172 | |||||||||
| 173 | if (preg_match('#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i', $url)) { |
||||||||
| 174 | return '<?xml version="1.0" encoding="utf-8"?>'; |
||||||||
| 175 | } |
||||||||
| 176 | // Get is_html() to return false. |
||||||||
| 177 | |||||||||
| 178 | return $contents; |
||||||||
| 179 | } |
||||||||
| 180 | |||||||||
| 181 | public function hook_feed_basic_info($basic_info, $fetch_url, $owner_uid, $feed, $auth_login, $auth_pass) { |
||||||||
|
0 ignored issues
–
show
The parameter
$feed is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$owner_uid is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 182 | if ($auth_login || $auth_pass) { |
||||||||
| 183 | return $basic_info; |
||||||||
| 184 | } |
||||||||
| 185 | |||||||||
| 186 | if (preg_match('#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i', $fetch_url, $matches)) { |
||||||||
| 187 | $basic_info = array('title' => ucfirst($matches[1]), 'site_url' => $matches[0]); |
||||||||
| 188 | } |
||||||||
| 189 | |||||||||
| 190 | return $basic_info; |
||||||||
| 191 | } |
||||||||
| 192 | |||||||||
| 193 | public function api_version() { |
||||||||
| 194 | return 2; |
||||||||
| 195 | } |
||||||||
| 196 | |||||||||
| 197 | } |
||||||||
| 198 |