<?php
class MakeRSS{
  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);

  function MakeRSS($title, $link, $description, $optional = ''){
    $this->title = $title;
    $this->link = $link;
    $this->description = $description;

    if( is_array($optional) and count($optional) ){
      $this->optional = $optional;
    }
  }
  
  function AddOptional($key, $value){
    $this->optional[$key] = $value;
  }

  function AddImage($title, $url, $link, $description = ''){
    $this->image['title'] = $title;
    $this->image['url'] = $url;
    $this->image['link'] = $link;
    $this->image['description'] = $description;

    if( $tmp = @getimagesize($url) ){
      $this->image['w'] = ($tmp[0] > 144) ? 144 : $tmp[0];
      $this->image['h'] = ($tmp[1] > 400) ? 400 : $tmp[1];
    }
  }
  
  function AddArticle($title, $link, $description, $author, $optional = ''){
    $i = array_push($this->Articles, array('title' => $title, 'link' => '' . $link, 'description' => $description, 'author' => $author));

    if( is_array($optional) and count($optional) ){
      --$i;
      while( list($k, $v) = each($optional) ){
         $this->Articles[$i][$k] = $v;
      }
    }
  }

  function Output($save = false, $path = ''){
    $out = '<?xml version="1.0" encoding="ISO-8859-1" ?>' . "\n" .
       '<rss version="2.0">' . "\n" .
       '<channel>' . "\n";
      
    $out .= "<title>$this->title</title>\n" .
        "<link>$this->link</link>\n" .
        "<description>$this->description</description>\n";
    
    //Channel optionals
    if( is_array($this->optional) and count($this->optional) ){
      while( list($k, $v) = each($this->optional) ){
        $out .= "<$k>$v</$k>\n";
      }
    }
    
//Image
   $out .= "<image>\n" .
     "<title>" . $this->image['title'] . "</title>\n" .
     "<url>" . $this->image['url'] . "</url>\n" .
     "<link>" . $this->image['link'] . "</link>\n";

    $out .= "<width>75</width>\n" .
      "<height>55</height>\n";
   $out .= "</image>\n";

    //Articles
    for( $i = 0, $c = count($this->Articles); $i < $c; $i++ ){
       $out .= "<item>\n" .
           "<title>" . $this->Articles[$i]['title'] . "</title>\n" .
           "<link>" . $this->Articles[$i]['link'] . "</link>\n" .
           "<description>" . $this->Articles[$i]['description'] . "</description>\n" .
           "<author>" . $this->Articles[$i]['author'] . "</author>\n";

       if( count($this->Articles[$i]) > 4 ){
         while( list($k, $v) = each($optional) ){
           if( !in_array($k, array('title', 'link', 'description', 'author')) ){
             $out .= "<$k>$v</$k>\n";
           }
         }
       }


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

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


    // True output
    if( !$save or !$path ){
      header("Content-type: application/xml");
      echo $out;
      return true;
    }
    else{
      $fh = fopen($path, 'w');
      if( $fh ){
        fwrite($fh, $out);
        fclose($fh);        
        
        return true;
      }
      return false;
    }
  }
}
require_once ("config.php");
//require_once ("includes/overture.php");
/*
$frasi = overture_load_frasi();
srand((float)microtime() * 1000000);
shuffle($frasi );
*/

$sql = OpenDb();
//Inizializziamo la classe passando i parametri fondamentali
$titolo_del_canale = "Unione Consulenti";
$link_al_sito_generatore = "http://www.unioneconsulenti.it";
$descrizione_del_canale = "Unione Consulenti è la risorsa d'informazioni utili e servizi professionali per aziende e privati."; 
$r = new MakeRSS($titolo_del_canale, $link_al_sito_generatore, $descrizione_del_canale);
$limit = 11;
$query = "SELECT sid, title, hometext, time FROM nuke_stories ORDER BY time DESC LIMIT 10";
		 
$result = mysql_query($query);
$titolo = "Unione Consulenti";
$url = "http://www.unioneconsulenti.it/themes/Unione/images/bilancia2.gif"; 
$link= "http://www.unioneconsulenti.it"; 
$r->AddImage($titolo, $url, $link, " ");

$i = 0;

while ($art = mysql_fetch_assoc($result)){
		$title=htmlspecialchars($art['title']);
		$author = "Unione Consulenti";
		$link = "http://www.unioneconsulenti.it/article.php?sid=" . $art['sid'];
		$description = $art['hometext'];
		$r->AddArticle ($title, $link, $description, $author);
/*
		if ($i == 4) {
			 $author = "Overture";
			 $link = "overture.php?keywords=". $frasi[$i]['frase'];
			 $title="Ads: " . htmlspecialchars($frasi[$i]['keyword']);
			 $description = "Ads by Overture";
			 $r->AddArticle ($title, $link, $description, $author);
			 $i++;
			 $author = "Overture";
			 $link = "overture.php?keywords=". $frasi[$i]['frase'];
			 $title="Ads: " . htmlspecialchars($frasi[$i]['keyword']);
			 $description = "Ads by Overture";
			 $r->AddArticle ($title, $link, $description, $author);
		}
		$i++;		
*/
}
$r->Output();
closeDb($sql);
//<link rel="alternate" type="application/rss+xml" title="RSS NEWS" href="/rss_news.xml">
//<link rel="alternate" type="application/rss+xml" title="RSS ARTICOLI" href="/rss_articoli.xml">

function openDb() {
         global $sql;
         global $dbhost;
         global $dbuname;
         global $dbpass;
         global $dbname;
         $sql= mysql_connect ($dbhost, $dbuname, $dbpass);
        if (! $sql ) die (_DBERROR);
         mysql_select_db ($dbname) or die (_CANNOTOPENTABLE." $dbname");
         return $sql;
}

function closeDb ($sql){


         global $sql;
         mysql_close ($sql);

}		
?>