/*
$Id: rss2writer.class.php,v 1.1 2006/07/24 21:14:12 cvsuser Exp $
RSS2Writer a php class to generate in a simple way RSS 2.0 feeds
by Maurizio Giunti (http://www.mauriziogiunti.it)
This software is distributed under the LGPL license:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge,
MA 02139, USA.
Although it is in no way required by the GNU Library General
Public License, it is the author's desire that software designed
around this library remain public as per the intent of the GPL. The
author did not license this software under the GPL but rather the
LGPL so as to give others more freedom in how this software is used.
*/
class RSS2Item {
// Mandatory items
var $title;
var $link;
var $description;
// Optional items
var $author;
var $category;
var $comments;
// var $enclosure;
var $guid;
var $pubDate;
// var $source;
function RSS2Item($sTitle,$slink,$sDescription)
{
$this->title=$sTitle;
$this->link=$slink;
$this->description=$sDescription;
$this->author='';
$this->category='';
$this->comments='';
//$this->enclosure=''; not yet supported
$this->guid='';
$this->pubDate='';
//$this->source=''; not yet supported
}
function setAuthor($sEmail,$sName)
{
$this->author="$sEmail ($sName)";
}
function setCategory($sCategory)
{
$this->category=$sCategory;
}
function setComments($sComments)
{
$this->comments=$sComments;
}
function setGuid($sGuid)
{
$this->guid=$sGuid;
}
function setPubDate($sPubDate)
{
$dt=date("D, j M Y H:i:s T",strtotime($sPubDate));
$this->pubDate=$dt;
}
function getAsString()
{
$ret='';
$ret.="\n";
$ret.=''.encodexmlchars($this->title).''."\n";
$ret.=''.encodexmlchars($this->link).''."\n";
$ret.=''.encodexmlchars($this->description).''."\n";
if($this->author!='') {
$ret.=''.encodexmlchars($this->author).''."\n";
}
if($this->category!='') {
$ret.=''.encodexmlchars($this->category).''."\n";
}
if($this->comments!='') {
$ret.=''.encodexmlchars($this->comments).''."\n";
}
if($this->guid!='') {
$ret.=''.encodexmlchars($this->guid).''."\n";
}
if($this->pubDate!='') {
$ret.=''.encodexmlchars($this->pubDate).''."\n";
}
$ret.="\n";
return $ret;
}
}
class RSS2Writer {
// Mandatory items
var $title;
var $link;
var $description;
// Optional items
var $language;
var $copyright;
var $managingEditor;
var $webMaster;
var $pubDate;
var $lastBuildDate;
var $category; // Partially supported
var $generator;
var $docs;
// var $cloud
var $ttl='';
var $image;
// var $rating='';
// var $textinput;
// var $skipHours
// var $skipDays
// Items list
var $items;
function RSS2Writer($sTitle,$sLink,$sDescription)
{
$this->title=$sTitle;;
$this->link=$sLink;
$this->description=$sDescription;
// Optional items
$this->language='';
$this->copyright='';
$this->managingEditor='';
$this->webMaster='';
$this->pubDate='';
$this->lastBuildDate='';
$this->category='';
$this->generator='RSS2Writer PHP class by Maurizio Giunti http://www.mauriziogiunti.it';
$this->docs='';
$this->ttl='';
// Reset image and items
$this->image=array();
$this->items=array();
}
function setLanguage($sLanguage)
{
$this->language=$sLanguage;
}
function setCopyright($sCopyright)
{
$this->copyright=$sCopyright;
}
function setManagingEditor($sEmail,$sName)
{
$this->managingEditor="$sEmail ($sName)";
}
function setWebMaster($sEmail,$sName)
{
$this->webMaster="$sEmail ($sName)";
}
function setPubDate($sPubDate)
{
$dt=date("D, j M Y H:i:s T",strtotime($sPubDate));
$this->pubDate=$dt;
}
function setLastBuildDate($sLastBuildDate)
{
$dt=date("D, j M Y H:i:s T",strtotime($sLastBuildDate));
$this->lastBuildDate=$dt;
}
function setCategory($sCategory)
{
$this->category=$sCategory;
}
function setGenerator($sGenerator)
{
$this->generator=$sGenerator;
}
function setDocs($sDocs)
{
$this->docs=$sDocs;
}
function setTtl($sTtl)
{
$this->ttl=$sTtl;
}
function setImage($url,$title,$link,$width,$height)
{
$img['url']=$url;
$img['title']=$title;
$img['link']=$link;
if($width>0) {
$img['width']=$width;
}
if($height>0) {
$img['height']=$height;
}
$this->image=$img;
}
function addItem($rss2item)
{
$this->items[]=$rss2item;
}
function getHeaderAsString()
{
$ret='';
$ret.=''."\n";
$ret.=''."\n";
$ret.=''."\n";
$ret.=''.encodexmlchars($this->title).''."\n";
$ret.=''.encodexmlchars($this->link).''."\n";
$ret.=''.encodexmlchars($this->description).''."\n";
if($this->language!='') {
$ret.=''.encodexmlchars($this->language).''."\n";
}
if($this->copyright!='') {
$ret.=''.encodexmlchars($this->copyright).''."\n";
}
if($this->managingEditor !='') {
$ret.=''.encodexmlchars($this->managingEditor).''."\n";
}
if($this->webMaster !='') {
$ret.=''.encodexmlchars($this->webMaster).''."\n";
}
if($this->pubDate !='') {
$ret.=''.encodexmlchars($this->pubDate).''."\n";
}
if($this->lastBuildDate !='') {
$ret.=''.encodexmlchars($this->lastBuildDate).''."\n";
}
if($this->category !='') {
$ret.=''.encodexmlchars($this->category).''."\n";
}
if($this->generator !='') {
$ret.=''.encodexmlchars($this->generator).''."\n";
}
if($this->docs !='') {
$ret.=''.encodexmlchars($this->docs).''."\n";
}
if($this->ttl !='') {
$ret.=''.encodexmlchars($this->ttl).''."\n";
}
// Feed image
if(count($this->image)>0) {
$img=$this->image;
$ret.=''."\n";
$ret.=''.encodexmlchars($img['url']).''."\n";
$ret.=''.encodexmlchars($img['title']).''."\n";
$ret.=''.encodexmlchars($img['link']).''."\n";
if($img['width']>0) {
$ret.=''.encodexmlchars($img['width']).''."\n";
}
if($img['height']>0) {
$ret.=''.encodexmlchars($img['height']).''."\n";
}
$ret.=''."\n";
}
return $ret;
}
function getFooterAsString()
{
$ret='';
$ret.=''."\n";
$ret.=''."\n";
return $ret;
}
function getAsString()
{
$ret='';
$ret.=$this->getHeaderAsString();
// Items
foreach($this->items as $item) {
$ret.=$item->getAsString();
}
$ret.=$this->getFooterAsString();
return $ret;
}
function doPrint()
{
print $this->getHeaderAsString();
// Items
foreach($this->items as $item) {
print $item->getAsString();
}
print $this->getFooterAsString();
}
}
/*
Common method to encode special XML chars
*/
function encodexmlchars($str)
{
$str=str_replace('&','&',$str);
$str=str_replace('<','<',$str);
$str=str_replace('>','>',$str);
$str=str_replace('"','"',$str);
$str=str_replace("'",''',$str);
return $str;
}
?>