%s
%s
%s
http://pas-bien.net/divers/rss-filter/
';
/*
';
const ITEM = ' -
%s
%s
%s
%s
';
// Attributes
private $outputList = array();
private $feeds;
private $includes;
private $excludes;
private $debug = false;
private $errors = array();
private $replacementPatterns = null;
private $replacementStrings = null;
// Constructors
public function __construct()
{
set_error_handler(array($this,'errorHandler'));
}
// GETers & SETers
public function setReplacementFilters($replacementPatterns, $replacementStrings)
{
$this->replacementPatterns = $replacementPatterns;
$this->replacementStrings = $replacementStrings;
}
// Public functions
public function process()
{
$this->init();
foreach ($this->feeds as $feed)
{
if($data = file_get_contents($feed))
{
if(isset($this->replacementPatterns, $this->replacementStrings))
{
$data = preg_replace($this->replacementPatterns,
$this->replacementStrings, $data);
}
if($xml = simplexml_load_string($data))
{
foreach ($xml->channel->item as $item)
{
if(RSSfilter::preg_match_any($this->includes,$item->title))
{
if( ! RSSfilter::preg_match_any($this->excludes,$item->title))
{
$this->outputList[] = $item->asXML();
}
}
}
}
else
{
$this->addErrorItem("Failed parsing feed",$feed);
}
}
else
{
$this->addErrorItem("Failed fetching feed",$feed);
}
}
$this->terminate();
}
public function errorHandler( $errno , $errstr , $errfile , $errline , $errcontext )
{
if($this->debug)
$this->errors[] = new ErrorItem( $errno , $errstr , $errfile , $errline );
return true;
}
// Private functions
public function init()
{
if(isset($_GET['debug']) && $_GET['debug'] == '1')
{
$this->debug = true;
error_reporting(E_ALL);
}
else
{
$this->debug = true;
error_reporting(0);
}
if(isset($_GET['file']))
{
$this->initXML();
}
else if (isset($_GET['feeds'], $_GET['filters']))
{
$this->initGET();
}
else
{
$this->outputList[] = sprintf(RSSfilter::HEADER,
"No parameter found !",
htmlentities('Examples : [ ?file=toto | ?feeds=http://tata.com/rss.xml,http://toto.com/rss.xml&include=tutu&exclude=titi,lala ]'),
date('r')
);
}
}
private function initXML()
{
$this->feeds = array();
$this->includes = array();
$this->excludes = array();
if($config = simplexml_load_file("files/{$_GET['file']}.xml"))
{
$this->outputList[] = sprintf(RSSfilter::HEADER,
$config->title,
$config->description,
date('r')
);
foreach ($config->feeds->feed as $feed)
$this->feeds[] = "{$feed}";
foreach ($config->includes->filter as $filter)
$this->includes[] = "{$filter}";
foreach ($config->excludes->filter as $filter)
$this->excludes[] = "{$filter}";
}
else
{
$this->outputList[] = sprintf(RSSfilter::HEADER,
"Failed reading config : ".$_GET['file'],
'Error feed !',
date('r')
);
$this->addErrorItem('Failed reading config !', '');
}
}
private function initGET()
{
$this->feeds = split(',', $_GET['feeds']);
$this->includes = split(',', $_GET['filters']);
for($i = 0; $i < count($this->includes) ; $i++ )
$this->includes[$i] = "/\b".$this->includes[$i]."\b/i";
if(isset($_GET['excludes']))
{
$this->excludes = split(',', $_GET['excludes']);
for($i = 0; $i < count($this->excludes) ; $i++ )
$this->includes[$i] = "/\b".$this->includes[$i]."\b/i";
}
else
$this->excludes = array();
$this->outputList[] = sprintf(RSSfilter::HEADER,
'Custom filtered feed',
'This feed is a concatenation of '.count($this->feeds).' feeds filtered by '.count($this->includes).' filters.',
date('r')
);
}
public function terminate()
{
if(count($this->errors) > 0)
$this->addErrorItem('Unhandled errors occured','');
$this->outputList[] = RSSfilter::FOOTER;
foreach ($this->outputList as $output)
{
echo $output;
}
}
private function addErrorItem($title, $link)
{
$errorText = 'Error list :
';
while($errorItem = array_shift($this->errors))
{
$errorText .= $errorItem->toString();
}
if($this->debug)
{
$this->outputList[] = sprintf(RSSfilter::ITEM,
$title,
htmlspecialchars($errorText),
date('r'),
$link
);
}
else
{
die($errorText);
}
}
// private static function
private static function preg_match_any($patterns, $subject)
{
foreach ($patterns as $pattern)
if(preg_match($pattern, $subject))
return true;
return false;
}
}
class ErrorItem
{
public $errno;
public $errstr;
public $errfile;
public $errline;
public function __construct( $errno , $errstr , $errfile , $errline )
{
$this->errno = $errno;
$this->errstr = $errstr;
$this->errfile = $errfile;
$this->errline = $errline;
}
public function toString()
{
switch ($this->errno)
{
case E_ERROR : $errorLabel = 'E_ERROR (1)'; break;
case E_WARNING : $errorLabel = 'E_WARNING (2)'; break;
case E_PARSE : $errorLabel = 'E_PARSE (4)'; break;
case E_NOTICE : $errorLabel = 'E_NOTICE (8)'; break;
case E_CORE_ERROR : $errorLabel = 'E_CORE_ERROR (16)'; break;
case E_CORE_WARNING : $errorLabel = 'E_CORE_WARNING (32)'; break;
case E_COMPILE_ERROR : $errorLabel = 'E_COMPILE_ERROR (64)'; break;
case E_COMPILE_WARNING : $errorLabel = 'E_COMPILE_WARNING (128)'; break;
case E_USER_ERROR : $errorLabel = 'E_USER_ERROR (256)'; break;
case E_USER_WARNING : $errorLabel = 'E_USER_WARNING (512)'; break;
case E_USER_NOTICE : $errorLabel = 'E_USER_NOTICE (1024)'; break;
case E_STRICT : $errorLabel = 'E_STRICT (2048)'; break;
case E_RECOVERABLE_ERROR : $errorLabel = 'E_RECOVERABLE_ERROR (4096)'; break;
case E_DEPRECATED : $errorLabel = 'E_DEPRECATED (8192)'; break;
case E_USER_DEPRECATED : $errorLabel = 'E_USER_DEPRECATED (16384)'; break;
default : $errorLabel = "unknown type ({$this->errno})";
}
return sprintf('Error : %s
Description : %s
File : %s
Line : %s
',
$errorLabel,
$this->errstr,
$this->errfile,
$this->errline
);
}
}
$generator = new RSSfilter();
$generator->process();
?>