<?php
class DOMrssFeed {
   
// required
   
public $title='';
   public 
$link='';
   public 
$desc='';

   
// recommended
   
public $atomLink='';
   
   
// optional
   
public $stylesheet='';
   public 
$language='en-us';
   public 
$copyright='';
   public 
$managingEditor='';
   public 
$webMaster='';
   public 
$category='';
   public 
$generator='';
   public 
$ttl='';
   public 
$skipHours = Array();
   public 
$skipDays = Array();
   
   
//for debug
   
public $debug false;
   
   private 
$docs='http://validator.w3.org/feed/docs/rss2.html';
   private 
$atomNS 'http://www.w3.org/2005/Atom';

   public function 
DOMrssFeed($dom) {
      
$this->dom $dom;
      }
      
   public function 
makeChannel($pubdate='',$lastbuild='') {
      if (
strlen($this->stylesheet) > 0) {
         
$instructions 'type="text/css" href="' $this->stylesheet '"';
         
$style $this->dom->createProcessingInstruction('xml-stylesheet',$instructions);
         
$this->dom->appendChild($style);
         }
      
$root $this->dom->createElement('rss');
      
$root->setAttribute('version','2.0');
      if (
strlen($this->atomLink) > 0) {
         
$root->setAttribute('xmlns:atom',$this->atomNS);
         }
      
$this->dom->appendChild($root);
      
$this->channel $this->dom->createElement('channel');
      
$root->appendChild($this->channel);
      if (
strlen($this->atomLink) > 0) {
         
$atom $this->dom->createElement('atom:link');
         
$atom->setAttribute('href',$this->atomLink);
         
$atom->setAttribute('rel','self');
         
$atom->setAttribute('type','application/rss+xml');
         
$this->channel->appendChild($atom);
         }
      
      
$node $this->dom->createElement('title',$this->title);
      
$this->channel->appendChild($node);
      
$node $this->dom->createElement('link',$this->link);
      
$this->channel->appendChild($node);
      
$node $this->dom->createElement('description',$this->desc);
      
$this->channel->appendChild($node);
      
$node $this->dom->createElement('language',$this->language);
      
$this->channel->appendChild($node);
      
      
putenv('TZ=GMT');
      if (
strlen($pubdate) > 0) {
         
$node $this->dom->createElement('pubDate',date('D, d M Y H:i:s T',$pubdate));
         
$this->channel->appendChild($node);
         }
      if (
strlen($lastbuild) > 0) {
         
$node $this->dom->createElement('lastBuildDate',date('D, d M Y H:i:s T',$lastbuild));
         
$this->channel->appendChild($node);
         }    
      
$this->optElements();
      }
      
   public function 
addImage($url,$title='',$link='',$width='',$height='',$description='') {
      
$image $this->dom->createElement('image');
      
$this->channel->appendChild($image);
      
$node $this->dom->createElement('url',$url);
      
$image->appendChild($node);
      if (
strlen($title) > 0) {
         
$imtitle $title;
         } else {
         
$imtitle $this->title;
         }
      
$node $this->dom->createElement('title',$imtitle);
      
$image->appendChild($node);
      if (
strlen($link) > 0) {
         
$imlink $link;
         } else {
         
$imlink $this->link;
         }
      
$node $this->dom->createElement('link',$imlink);
      
$image->appendChild($node);
      if (
strlen($Width) > 0) {
         
$node $this->dom->createElement('width',$width);
         
$image->appendChild($node);
         }
      if (
strlen($Height) > 0) {
         
$node $this->dom->createElement('height',$height);
         
$image->appendChild($node);
         }
      if (
strlen($Description) > 0) {
         
$node $this->dom->createElement('description',$description);
         
$image->appendChild($node);
         }
      }
      
   public function 
cloud($domain,$port,$path,$registerProcedure,$protocol) {
      
$node $this->dom->createElement('cloud');
      
$this->channel->appendChild($node);
      
$node->setAttribute('domain',$domain);
      
$node->setAttribute('port',$port);
      
$node->setAttribute('path',$path);
      
$node->setAttribute('registerProcedure',$registerProcedure);
      
$node->setAttribute('protocol',$protocol);
      }
      
   public function 
textInput($name,$description,$cgi,$submit='Submit') {
      
$ti $this->dom->createElement('textInput');
      
$this->channel->appendChild($textInput);
      
$node $this->dom->createElement('name',$name);
      
$ti->appendChild($node);
      
$node $this->dom->createElement('description',$description);
      
$ti->appendChild($node);
      
$node $this->dom->createElement('link',$cgi);
      
$ti->appendChild($node);
      
$node $this->dom->createElement('title',$submit);
      
$ti->appendChild($node);
      }
      
   public function 
newItem($title='',$desc='',$link='',$pubdate='') {
      if (isset(
$this->item)) {
         
$this->channel->appendChild($this->item);
         }
      
$this->item $this->dom->createElement('item');
      if (
strlen($title) > 0) {
         
$node $this->dom->createElement('title',$title);
         
$this->item->appendChild($node);
         }
      if (
strlen($desc) > 0) {
         
$node $this->dom->createElement('description',$desc);
         
$this->item->appendChild($node);
         }
      if (
strlen($link) > 0) {
         
$node $this->dom->createElement('link',$link);
         
$this->item->appendChild($node);
         }
      if (
strlen($pubdate) > 0) {
         
putenv('TZ=GMT');
         
$node $this->dom->createElement('pubDate',date('D, d M Y H:i:s T',$pubdate));
         
$this->item->appendChild($node);
         }
      }
      
   public function 
addCategory($cat,$domain='') {
      
$node $this->dom->createElement('category',$cat);
      
$this->item->appendChild($node);
      if (
strlen($domain) > 0) {
         
$node->setAttribute('domain',$domain);
         }
      }
      
   public function 
addMedia($url,$size,$mime) {
      
$node $this->dom->createElement('enclosure');
      
$this->item->appendChild($node);
      
$node->setAttribute('url',$url);
      
$node->setAttribute('length',$size);
      
$node->setAttribute('type',$mime);
      }
      
   public function 
addGuid($guid,$permalink=true) {
      
$node $this->dom->createElement('guid',$guid);
      
$this->item->appendChild($node);
      if (
$permalink) {
         
$node->setAttribute('isPermaLink','true');
         } else {
         
$node->setAttribute('isPermaLink','false');
         }
      }
      
   public function 
addExtraData($author='',$comments='') {
      if (
strlen($author) > 0) {
         
$node $this->dom->createElement('author',$author);
         
$this->item->appendChild($node);
         }
      if (
strlen($comments) > 0) {
         
$node $this->dom->createElement('comments',$comments);
         
$this->item->appendChild($node);
         }
      }
      
   public function 
itemSource($source,$url) {
      
$node $this->dom->createElement('source',$source);
      
$this->item->appendChild($node);
      
$node->setAttribute('url',$url);
      }
      
   public function 
addCdata($string) {
      
$cdata $this->dom->createCDATASection($string);
      
$this->item->appendChild($cdata);
      }

   public function 
sendRss() {
      if (isset(
$this->item)) {
         
$this->channel->appendChild($this->item);
         }
      if (
$this->debug) {
         
header('Content-type: text/plain');
         } else {
         
header('Content-type: application/rss+xml');
         }
      print 
$this->dom->saveXML();
      }
      
   private function 
optElements() {
      if (
strlen($this->copyright) > 0) {
         
$node $this->dom->createElement('copyright',$this->copyright);
         
$this->channel->appendChild($node);
         }
      if (
strlen($this->managingEditor) > 0) {
         
$node $this->dom->createElement('managingEditor',$this->managingEditor);
         
$this->channel->appendChild($node);
         }
      if (
strlen($this->webMaster) > 0) {
         
$node $this->dom->createElement('webMaster',$this->webMaster);
         
$this->channel->appendChild($node);
         }
      if (
strlen($this->category) > 0) {
         
$node $this->dom->createElement('category',$this->category);
         
$this->channel->appendChild($node);
         }
      if (
strlen($this->generator) > 0) {
         
$gen $this->generator;
         } else {
         
$gen 'PHP ' phpversion() . ' DOMDocument / libxml ' LIBXML_DOTTED_VERSION;
         }
      
$node $this->dom->createElement('generator',$gen);
      
$this->channel->appendChild($node);
      if (
strlen($this->docs) > 0) {
         
$node $this->dom->createElement('docs',$this->docs);
         
$this->channel->appendChild($node);
         }
      if (
strlen($this->ttl) > 0) {
         
$node $this->dom->createElement('ttl',$this->ttl);
         
$this->channel->appendChild($node);
         }
      if (
sizeof($this->skipHours) > 0) {
         
$sh $this->dom->createElement('skipHours');
         
$this->channel->appendChild($sh);
         for (
$i=0;$i<sizeof($this->skipHours);$i++) {
            
$node $this->dom->createElement('hour',$this->skipHours[$i]);
            
$sh->appendChild($node);
            }
         }
      if (
sizeof($this->skipDays) > 0) {
         
$sd $this->dom->createElement('skipDays');
         
$this->channel->appendChild($sd);
         for (
$i=0;$i<sizeof($this->skipDays);$i++) {
            
$node $this->dom->createElement('day',$this->skipDays[$i]);
            
$sh->appendChild($node);
            }
         }
      }
      
   } 
// end of class
?>