215 lines
4.6 KiB
PHP
215 lines
4.6 KiB
PHP
<?php
|
|
/*
|
|
Atom and RSS Extractor and Displayer
|
|
(c) 2007 Scriptol.com - Licence Mozilla 1.1.
|
|
commonlib.php
|
|
*/
|
|
class atomRss {
|
|
|
|
public $codeErreurHttp=0;
|
|
private $Common_Content = array();
|
|
|
|
function RSS_Tags($item, $type)
|
|
{
|
|
$y = array();
|
|
$y["title"] = $item->getElementsByTagName("title")->item(0)->firstChild->data;
|
|
$y["link"] = $item->getElementsByTagName("link")->item(0)->firstChild->data;
|
|
$y["description"] = $item->getElementsByTagName("description")->item(0)->firstChild->data;
|
|
|
|
$tnl = $item->getElementsByTagName("pubDate");
|
|
if($tnl->length == 0)
|
|
{
|
|
$tnl = $item->getElementsByTagName("lastBuildDate");
|
|
}
|
|
if($tnl->length != 0)
|
|
{
|
|
$tnl =$tnl->item(0)->firstChild->data;
|
|
}
|
|
else
|
|
$tnl = false;
|
|
|
|
$y["updated"] = $tnl;
|
|
$y["type"] = $type;
|
|
|
|
array_push($this->Common_Content, $y);
|
|
}
|
|
|
|
|
|
function RSS_Channel($channel)
|
|
{
|
|
$items = $channel->getElementsByTagName("item");
|
|
|
|
// Processing channel
|
|
|
|
$this->RSS_Tags($channel, 0); // get description of channel, type 0
|
|
|
|
// Processing articles
|
|
|
|
foreach($items as $item)
|
|
{
|
|
$this->RSS_Tags($item, 1); // get description of article, type 1
|
|
}
|
|
}
|
|
|
|
function RSS_Retrieve($url)
|
|
{
|
|
$doc = new DOMDocument();
|
|
$ret = $doc->load($url);
|
|
if ($ret===false) {
|
|
$page=getUrl($url,'','','',false,'','',3);
|
|
$this->codeErreurHtml=$page['code'];
|
|
if ($this->codeErreurHttp==302) {
|
|
$url=$page['header']['Location'];
|
|
$ret = $doc->load($url);
|
|
}
|
|
}
|
|
|
|
$channels = $doc->getElementsByTagName("channel");
|
|
|
|
$this->Common_Content = array();
|
|
|
|
foreach($channels as $channel)
|
|
{
|
|
$this->RSS_Channel($channel);
|
|
}
|
|
|
|
return ( count($this->Common_Content) > 0);
|
|
}
|
|
|
|
|
|
|
|
function Atom_Tags($item)
|
|
{
|
|
$y = array();
|
|
$y["title"] = $item->getElementsByTagName("title")->item(0)->firstChild->data;
|
|
$y["link"] = $item->getElementsByTagName("link")->item(0)->getAttribute("href");
|
|
$y["description"] = $item->getElementsByTagName("summary")->item(0)->firstChild->data;
|
|
$y["updated"] = $item->getElementsByTagName("updated")->item(0)->firstChild->data;
|
|
$y["type"] = 1;
|
|
|
|
array_push($this->Common_Content, $y);
|
|
}
|
|
|
|
function Atom_Feed($doc)
|
|
{
|
|
$entries = $doc->getElementsByTagName("entry");
|
|
|
|
if($entries->length == 0) return false;
|
|
|
|
// Processing feed
|
|
|
|
$y = array();
|
|
$y["title"] = $doc->getElementsByTagName("title")->item(0)->firstChild->data;
|
|
$y["link"] = $doc->getElementsByTagName("link")->item(0)->getAttribute("href");
|
|
$y["description"] = $doc->getElementsByTagName("subtitle")->item(0)->firstChild->data;
|
|
$y["updated"] = $doc->getElementsByTagName("updated")->item(0)->firstChild->data;
|
|
$y["type"] = 0;
|
|
|
|
array_push($this->Common_Content, $y);
|
|
|
|
// Processing articles
|
|
|
|
foreach($entries as $entry)
|
|
{
|
|
$this->Atom_Tags($entry); // get description of article, type 1
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
function Atom_Retrieve($url)
|
|
{
|
|
$doc = new DOMDocument();
|
|
$doc->load($url);
|
|
|
|
$this->Common_Content = array();
|
|
|
|
return $this->Atom_Feed($doc);
|
|
|
|
}
|
|
|
|
|
|
function loadRss($url, $size=200, $chanopt=true, $descopt=true, $dateopt=true)
|
|
{
|
|
$opened = false;
|
|
$page = "";
|
|
|
|
if($this->Atom_Retrieve($url) === false)
|
|
{
|
|
if($this->RSS_Retrieve($url) === false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
if($size > 0)
|
|
{
|
|
$size += 1; // add one for the channel
|
|
$recents = array_slice($this->Common_Content, 0, $size);
|
|
}
|
|
//$tabFlux=array();
|
|
return $recents;
|
|
/*
|
|
|
|
foreach($recents as $article)
|
|
{
|
|
$type = $article["type"];
|
|
|
|
if($type == 0)
|
|
{
|
|
if($chanopt != true) continue;
|
|
if($opened == true)
|
|
{
|
|
$page .="</ul>\n";
|
|
$opened = false;
|
|
}
|
|
//$page .="<b>";
|
|
}
|
|
else
|
|
{
|
|
if($opened == false && $chanopt == true)
|
|
{
|
|
$page .= "<ul>\n";
|
|
$opened = true;
|
|
}
|
|
}
|
|
$title = $article["title"];
|
|
$link = $article["link"];
|
|
$page .= "<".$Common_Style."><a href=\"$link\">$title</a>";
|
|
|
|
if($descopt != false)
|
|
{
|
|
$description = $article["description"];
|
|
if($description != false)
|
|
{
|
|
$page .= "<br>$description";
|
|
}
|
|
}
|
|
if($dateopt != false)
|
|
{
|
|
$updated = $article["updated"];
|
|
if($updated != false)
|
|
{
|
|
$page .= "<br /><font $Common_Date_Font>$updated</font>";
|
|
}
|
|
}
|
|
$page .= "</".$Common_Style.">\n";
|
|
|
|
/*
|
|
if($type == 0)
|
|
{
|
|
$page .="<br />";
|
|
}
|
|
*
|
|
}
|
|
|
|
if($opened == true)
|
|
{
|
|
$page .="</ul>\n";
|
|
}
|
|
return $page."\n";
|
|
*/
|
|
}
|
|
}
|
|
|
|
?>
|