2014-01-09 10:08:39 +00:00
< ? php
class Af_Comics extends Plugin {
private $host ;
2014-02-17 09:00:25 +00:00
private $filters = array ();
2014-01-09 10:08:39 +00:00
function about () {
2017-01-22 07:14:02 +00:00
return array ( 2.0 ,
2014-01-09 10:08:39 +00:00
" Fixes RSS feeds of assorted comic strips " ,
" fox " );
}
function init ( $host ) {
$this -> host = $host ;
2017-01-22 07:14:02 +00:00
$host -> add_hook ( $host :: HOOK_FETCH_FEED , $this );
2019-03-12 17:16:24 +00:00
$host -> add_hook ( $host :: HOOK_FEED_BASIC_INFO , $this );
2018-02-07 06:52:36 +00:00
$host -> add_hook ( $host :: HOOK_SUBSCRIBE_FEED , $this );
2014-01-09 10:08:39 +00:00
$host -> add_hook ( $host :: HOOK_ARTICLE_FILTER , $this );
$host -> add_hook ( $host :: HOOK_PREFS_TAB , $this );
2014-02-17 09:00:25 +00:00
require_once __DIR__ . " /filter_base.php " ;
2014-01-09 10:08:39 +00:00
2017-06-05 09:53:06 +00:00
$filters = array_merge ( glob ( __DIR__ . " /filters.local/*.php " ), glob ( __DIR__ . " /filters/*.php " ));
$names = [];
2014-01-09 10:08:39 +00:00
2014-02-17 09:00:25 +00:00
foreach ( $filters as $file ) {
$filter_name = preg_replace ( " / \ ..* $ / " , " " , basename ( $file ));
2014-01-09 10:08:39 +00:00
2017-06-05 09:53:06 +00:00
if ( array_search ( $filter_name , $names ) === FALSE ) {
if ( ! class_exists ( $filter_name )) {
require_once $file ;
}
2014-01-09 10:08:39 +00:00
2017-06-05 09:53:06 +00:00
array_push ( $names , $filter_name );
$filter = new $filter_name ();
if ( is_subclass_of ( $filter , " Af_ComicFilter " )) {
array_push ( $this -> filters , $filter );
array_push ( $names , $filter_name );
}
2014-02-17 08:26:46 +00:00
}
}
2014-02-17 09:00:25 +00:00
}
2014-01-09 10:08:39 +00:00
2014-02-17 09:00:25 +00:00
function hook_prefs_tab ( $args ) {
2017-06-05 09:53:06 +00:00
if ( $args != " prefFeeds " ) return ;
2014-01-09 10:08:39 +00:00
2018-12-06 05:56:28 +00:00
print " <div dojoType= \" dijit.layout.AccordionPane \"
title = \ " <i class='material-icons'>photo</i> " . __ ( 'Feeds supported by af_comics' ) . " \" > " ;
2014-01-09 10:08:39 +00:00
2014-02-17 09:00:25 +00:00
print " <p> " . __ ( " The following comics are currently supported: " ) . " </p> " ;
2014-01-09 10:08:39 +00:00
2017-01-22 07:14:02 +00:00
$comics = array ( " GoComics " );
2014-01-09 10:08:39 +00:00
2014-02-17 09:00:25 +00:00
foreach ( $this -> filters as $f ) {
foreach ( $f -> supported () as $comic ) {
array_push ( $comics , $comic );
2014-01-09 10:08:39 +00:00
}
}
2014-02-17 09:00:25 +00:00
asort ( $comics );
2014-02-14 12:44:39 +00:00
2018-12-07 11:03:33 +00:00
print " <ul class='panel panel-scrollable list list-unstyled'> " ;
2014-02-17 09:00:25 +00:00
foreach ( $comics as $comic ) {
print " <li> $comic </li> " ;
2014-01-09 10:08:39 +00:00
}
2014-02-17 09:00:25 +00:00
print " </ul> " ;
2014-01-09 10:08:39 +00:00
2017-11-16 17:14:20 +00:00
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> " ;
2017-01-22 18:52:25 +00:00
2017-06-05 09:53:06 +00:00
print " <p> " . __ ( 'Drop any updated filters into <code>filters.local</code> in plugin directory.' ) . " </p> " ;
2014-02-17 09:00:25 +00:00
print " </div> " ;
}
2014-01-09 10:08:39 +00:00
2014-02-17 09:00:25 +00:00
function hook_article_filter ( $article ) {
foreach ( $this -> filters as $f ) {
if ( $f -> process ( $article ))
break ;
2014-01-09 10:08:39 +00:00
}
return $article ;
}
2017-01-22 07:14:02 +00:00
// GoComics dropped feed support so it needs to be handled when fetching the feed.
2017-04-26 17:57:36 +00:00
/**
* @ SuppressWarnings ( PHPMD . UnusedFormalParameter )
*/
2017-01-22 07:14:02 +00:00
function hook_fetch_feed ( $feed_data , $fetch_url , $owner_uid , $feed , $last_article_timestamp , $auth_login , $auth_pass ) {
if ( $auth_login || $auth_pass )
return $feed_data ;
2017-11-16 17:14:20 +00:00
if ( preg_match ( '#^https?://(?:feeds\.feedburner\.com/uclick|www\.gocomics\.com)/([-a-z0-9]+)$#i' , $fetch_url , $comic )) {
2018-05-12 17:31:52 +00:00
$site_url = 'https://www.gocomics.com/' . $comic [ 1 ];
2017-01-22 07:14:02 +00:00
$article_link = $site_url . date ( '/Y/m/d' );
$body = fetch_file_contents ( array ( 'url' => $article_link , 'type' => 'text/html' , 'followlocation' => false ));
require_once 'lib/MiniTemplator.class.php' ;
$feed_title = htmlspecialchars ( $comic [ 1 ]);
$site_url = htmlspecialchars ( $site_url );
$article_link = htmlspecialchars ( $article_link );
$tpl = new MiniTemplator ();
$tpl -> readTemplateFromFile ( 'templates/generated_feed.txt' );
$tpl -> setVariable ( 'FEED_TITLE' , $feed_title , true );
$tpl -> setVariable ( 'VERSION' , VERSION , true );
$tpl -> setVariable ( 'FEED_URL' , htmlspecialchars ( $fetch_url ), true );
$tpl -> setVariable ( 'SELF_URL' , $site_url , true );
if ( $body ) {
$doc = new DOMDocument ();
if ( @ $doc -> loadHTML ( $body )) {
$xpath = new DOMXPath ( $doc );
$node = $xpath -> query ( '//picture[contains(@class, "item-comic-image")]/img' ) -> item ( 0 );
if ( $node ) {
2019-10-06 20:17:58 +00:00
$title = $xpath -> query ( '//h1' ) -> item ( 0 );
if ( $title ) {
$title = clean ( trim ( $title -> nodeValue ));
} else {
$title = date ( 'l, F d, Y' );
}
foreach ([ 'srcset' , 'sizes' , 'data-srcset' , 'width' ] as $attr ) {
$node -> removeAttribute ( $attr );
}
2018-02-07 16:57:32 +00:00
2017-01-22 07:14:02 +00:00
$tpl -> setVariable ( 'ARTICLE_ID' , $article_link , true );
$tpl -> setVariable ( 'ARTICLE_LINK' , $article_link , true );
2019-10-06 20:17:58 +00:00
$tpl -> setVariable ( 'ARTICLE_UPDATED_ATOM' , date ( 'c' , mktime ( 11 , 0 , 0 )), true );
$tpl -> setVariable ( 'ARTICLE_TITLE' , htmlspecialchars ( $title ), true );
2017-01-22 07:14:02 +00:00
$tpl -> setVariable ( 'ARTICLE_EXCERPT' , '' , true );
2017-09-09 18:51:59 +00:00
$tpl -> setVariable ( 'ARTICLE_CONTENT' , $doc -> saveHTML ( $node ), true );
2017-01-22 07:14:02 +00:00
$tpl -> setVariable ( 'ARTICLE_AUTHOR' , '' , true );
$tpl -> setVariable ( 'ARTICLE_SOURCE_LINK' , $site_url , true );
$tpl -> setVariable ( 'ARTICLE_SOURCE_TITLE' , $feed_title , true );
$tpl -> addBlock ( 'entry' );
}
}
}
$tpl -> addBlock ( 'feed' );
if ( $tpl -> generateOutputToString ( $tmp_data ))
$feed_data = $tmp_data ;
}
return $feed_data ;
}
2018-02-07 06:52:36 +00:00
function hook_subscribe_feed ( $contents , $url , $auth_login , $auth_pass ) {
if ( $auth_login || $auth_pass )
return $contents ;
if ( preg_match ( '#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i' , $url ))
return '<?xml version="1.0" encoding="utf-8"?>' ; // Get is_html() to return false.
return $contents ;
}
2019-03-12 17:16:24 +00:00
function hook_feed_basic_info ( $basic_info , $fetch_url , $owner_uid , $feed , $auth_login , $auth_pass ) {
if ( $auth_login || $auth_pass )
return $basic_info ;
if ( preg_match ( '#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i' , $fetch_url , $matches ))
$basic_info = array ( 'title' => ucfirst ( $matches [ 1 ]), 'site_url' => $matches [ 0 ]);
return $basic_info ;
}
2014-01-09 10:08:39 +00:00
function api_version () {
return 2 ;
}
2017-09-09 18:51:59 +00:00
}