<?php
/**
* cat-rss.inc.php (File per creare i feeds del sito)
* +----------------------------------------------------------------------+
* |                                                                      |
* | web-sytem 2.0rc : Web Site Management System                         |
* |                                                                      |
* +----------------------------------------------------------------------+
* | Copyright (c) davcaffa@gmail.com                                     |
* +----------------------------------------------------------------------+
* | Authors: Davide Caffaratti <davcaffa@gmail.com>                      |
* |                                                                      |
* +----------------------------------------------------------------------+
*
*
*/

/**
* @desc classe per creare gli rss del sistema
*
* @package Cat_Rss
* @author Davide Caffaratti <davcaffa@gmail.com>
* @version 1.0
**/
class Cat_Rss
{
    var 
$generator 'Created by Cat-Rss @version 1.0';
    var 
$Articles = array();

    
// Channel info
    
var $title '';
    var 
$link '';
    var 
$description '';
    var 
$optional = array();
    var 
$image = array('url'=>'','title'=>'','link'=>'','description'=>'','w'=>0,'h'=> 0);

    
/**
    * @desc Costruttore della classe
    * @param string $title Titolo del feed
    * @param string $link Link al feed
    * @param $description Descrizione del feed
    * @param array $optional
    * $optional = array(
    *             'generator'=>'http://miosito.com',
    *             'language'=>'en',
    *             'copyright'=>'Copyright 2007 Davide Caffaratti',
    *             'managingEditor'=>'davcaffa@gmail.com (Davide Caffaratti)',
    *             'webMaster'=>'davcaffa@gmail.com (Davide Caffaratti)',
    *             'lastBuildDate'=>'Sat, 07 Sep 2002 09:42:31 GMT',
    *             'category'=>'Nome categoria'
    *             );
    */
    
function Cat_Rss($title$link$description$optional '')
    {
        
$this->title $this->_set_text($title);
        
$this->link $link;
        
$this->description $this->_set_text($description);

        if (
is_array($optional) && count($optional)) {
            
$this->optional $optional;
        }
    }
    
/**
    * @desc distruttore della classe
    */
    
function __destruct()
    {
        
$this->generator null;
        
$this->title null;
        
$this->link null;
        
$this->description null;
        
$this->optional null;
        
$this->image null;
    }
    
/**
    * @desc Aggiunge immagine del feed al file rss
    * @access public
    * @param string $title Titolo immagine
    * @param string url Url immagine
    * @param string $link link alla pagina del feed
    * @param string $description Descrizione immagine (tag alt)
    * @return void
    */
    
function add_image($title$url$link$description='')
    {
        
// Controllo elementi indispensabili
        
if (empty($title) || empty($url) && empty($link)) {
            return;
        }
        
$this->image['title'] = $this->_set_text($title);
        
$this->image['url'] = $url;
        
$this->image['link'] = $link;
        
$this->image['description'] = $this->_set_text($description);

        if (
$tmp = @getimagesize($url)) {
            
$this->image['w'] = ($tmp[0] > 144) ? 144 $tmp[0];
            
$this->image['h'] = ($tmp[1] > 400) ? 400 $tmp[1];
        }
    }
    
/**
    * @desc Aggiunge un articolo al file rss
    * @access public
    * @param string $title
    * @param string $link link all'articolo
    * @param string $description Articolo
    * @param string $pubdate Data di pubblicazione articolo
    * @param array $optional
    * $optional = array(
    *             'author'=>'davcaffa@gmail.com (Davide Caffaratti)',
    *             'category'=>'Nome categoria',
    *             'comments'=>'http://sito.com/id=1&comment=yes',
    *             'guid'=>'http://sito.com/news.php?id=1'
    *             );
    * @return void
    */
    
function add_article($title$link$description$pubdate$optional=false)
    {
        
$item = array(
                
'title'=>$this->_set_text($title),
                
'link'=>$link,
                
'description'=>$this->_set_text($description),
                
'pubDate'=>$this->_set_date($pubdate)
                );
        
// inserisce un nuovo articolo
        
$i array_push($this->Articles$item);

        
// aggiunge le coppie chiave valore opzionali
        // all'articolo appena inserito
        
if ($optional && is_array($optional) && count($optional)) {
            --
$i;
            while(list(
$k$v) = each($optional)) {
              
$this->Articles[$i][$k] = $v;
            }
        }
    }
    
/**
    * @desc Ritorna il file xml
    * @access public
    * @param integer $ttl Numero di minuti che indica per quanto tempo un canale può essere mantenuto in cache prima di rieseguire un aggiornamento dalla sorgente
    * @return string
    */
    
function get_rss($ttl=60)
    {
        
$ttl = (empty($ttl)) ? 60 $ttl;
        
$out =
        
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" .
        
"<rss version=\"2.0\">\n" .
        
"<channel>\n".
        
"<title>".$this->title."</title>\n" .
        
"<link>".$this->link."</link>\n" .
        
"<description>".$this->description."</description>\n".
        
"<pubDate>".$this->_set_date()."</pubDate>\n".
        
"<generator>".$this->generator."</generator>\n".
        
"<ttl>".$ttl."</ttl>\n";

        
// Channel optionals
        
if (is_array($this->optional) && count($this->optional)) {
            while(list(
$k$v) = each($this->optional)) {
                
$out .= "<".$k.">".$v."</".$k.">\n";
            }
        }

        
// Image
        
if ($this->image['title'] != '' && $this->image['url'] != '' && $this->image['link'] != '') {
            
$out .=
            
"<image>\n" .
            
"<title>>" $this->image['title'] . "</title>\n" .
            
"<url>" $this->image['url'] . "</url>\n" .
            
"<link>" $this->image['link'] . "</link>\n";

            if (
$this->image['description'] != '') {
                
$out .= "<description>" $this->image['description'] . "</description>\n";
            }

            if (
$this->image['w'] and $this->image['h']) {
                
$out .= "<width>" $this->image['w'] . "</width>\n" .
                        
"<height>" $this->image['h'] . "</height>\n";
            }
            
$out .= "</image>\n";
        }

        
// per ogni item stampa tutte le coppie chiave valore
        
for ($i 0$c count($this->Articles); $i $c$i++) {
            
$out .= "<item>\n";

            while(list(
$k$v) = each($this->Articles[$i])) {
                
$out .= "<".$k.">".$v."</".$k.">\n";
            }

            
$out .= "</item>\n";
        }

        
$out .= "</channel>\n</rss>";

        return 
$out;
    }
    
/**
    * @desc funzione per stampare data dell'rss leggibile
    * @access public
    * @param string $date Data rss
    * @return string
    */
    
function get_date($date)
    {
        
date('d/m/Y'strtotime($date));
    }
    
/**
    * @desc Converte entità html dei campi testo
    * @access private
    * @param string $text
    * @patam boolean $c_data Se settato a false non stampa CDATA
    * @return string
    */
    
function _set_text($text)
    {
        if (empty(
$text)) {
            return 
'';
        }
        else {
            if (
is_numeric($text)) {
                return 
$text;
            }
            else {
                
$text html_entity_decode($textENT_QUOTES);
                
$text stripslashes(strip_tags(str_replace('<br />',"\n",$text)));
                return 
'<![CDATA[ '.utf8_encode($text).' ]]>';
            }
        }
    }
    
/**
    * @desc funzione per formattare la data proveniente da un db
    * @access private
    * @param string $date Se non settata inserisce la data odierna
    * @return string
    */
    
function _set_date($date=false)
    {
        if (!
$date) {
            
$date time();
        }
        else {
            
$tmp explode('-'$date);
            
$date gmmktime(000$tmp[1], $tmp[2], $tmp[0]);
            unset(
$tmp);
        }

        return 
gmdate('D, d M Y H:i:s \G\M\T'$date);
    }
}
?>