<?php
/*************************************************************/
/* Author: Robert Sebille, robert(at)banlieues(dot)be        */
/* XML Parser V1.01, under GNU/GPL LICENCE                   */
/*                                                           */
/* Requirements: php >= 4.1.0, registers_globals on or off   */
/* safe_mode on or off, safe_mode_gid on if safe_mode on     */
/*************************************************************/


/******************/
/* Initialization */
/******************/

// To increase your security, set "registers_globals off" in your php.ini file !
// url of file to parse
if (isset($url) && $url != "") {$rss_file_url $url;}
    elseif (isset(
$_POST["url"]) && $_POST["url"] != "" && $post == "oui") {$rss_file_url $_POST["url"];}
     elseif (isset(
$_GET["url"]) && $_GET["url"] != "" && $get == "oui") {$rss_file_url $_GET["url"];}
      else {die(
"<b>- Vous n'avez pas d&eacute;fini l'url du fichier xml &agrave; analyser<br />ou<br />- Vous n'&ecirc;tes pas autoris&eacute; &agrave passer des param&egrave;tres &agrave ce script</b>");}

// Must I display the title of the site ?
if (isset($site) && $site == "oui") {$titresite TRUE;}
    elseif (isset(
$_POST["site"]) && $_POST["site"] == "oui" && $post == "oui") {$titresite TRUE;}
     elseif (isset(
$_GET["site"]) && $_GET["site"] == "oui" && $get == "oui") {$titresite TRUE;}
      else {
$titresite FALSE;}

// Maybe prefers display the logo of the site inside of title ?
if (isset($site) && $site == "logo") {$logosite TRUE;}
    elseif (isset(
$_POST["site"]) && $_POST["site"] == "logo" && $post == "oui") {$logosite TRUE;}
     elseif (isset(
$_GET["site"]) && $_GET["site"] == "logo" && $get == "oui") {$logosite TRUE;}
      else {
$logosite FALSE;}

// Must I display the puce picture next the title of the articles ?
if (isset($puce) && $puce == "oui") {$pucetitre TRUE;}
    elseif (isset(
$_POST["puce"]) && $_POST["puce"] == "oui" && $post == "oui") {$pucetitre TRUE;}
     elseif (isset(
$_GET["puce"]) && $_GET["puce"] == "oui" && $get == "oui") {$pucetitre TRUE;}
      else {
$pucetitre FALSE;}



// Globals
// Data to display
$titre "";
$description "";
$lien "";
$image "";

// maybe for upgrade
$depth = array();



/******************************************/
/* Parser functions and html data display */
/******************************************/

// Save opening tags
function startElement($parser$name$attrs) {
    global 
$opentag$depth;

        
$opentag $name;
  
$depth[$parser]++;

}

// Closing tags  and data display
function endElement($parser$name) {
    global 
$depth$titre$description$lien$auteur$image$logosite$titresite$pucetitre;

    switch (
$name) {

        case 
"CHANNEL":
            
// Print data before end tag
            
if ($titresite) {
                echo 
"\n<tr><td class=\"psr-titre-site\">\n";
                echo 
"<a href=".trim($lien).">".htmlentities(trim($titre))."</a>\n</td></tr>\n";            
                if (
trim($description) != "") {
                    echo 
"\n<tr><td class=\"psr-descri-site\">\n";
                    echo 
htmlentities(trim$description))."\n</td></tr>\n";
                    }
            }
            
$titre "";
            
$description "";
            
$lien "";
            
$image "";
            
$auteur "";
            break;

        case 
"IMAGE":
            
// Print data before end tag
            
if ($logosite) {
                if (
trim($image) != "") {
                    echo 
"\n<tr><td class=\"psr-titre-site\">\n";
                    echo 
"<a href=".trim($lien)."><img src=\"".trim($image)."\"";
                    echo 
" alt=\"".htmlspecialchars(trim($titre))."\"";
                    echo 
" title=\"".htmlspecialchars(trim($titre))."\"";
                    echo 
" ></img></a>\n</td></tr>\n";            
                    echo 
"\n</td></tr>\n";            
                    if (
trim($description) != "") {
                        echo 
"\n<tr><td class=\"psr-descri-site\">\n";
                        echo 
htmlentities(trim$description))."\n</td></tr>\n";
                        }
                    }
                else {
                    echo 
"\n<tr><td class=\"psr-titre-site\">";
                    echo 
"L'analyseur n'a pas trouvĂ© de logo pour ce site";
                    echo 
"</td></tr>\n";            
                    }
            }
            
$titre "";
            
$description "";
            
$lien "";
            
$image "";
            
$auteur "";

            break;

        case (
"ITEM"):
            
// Print data before end tag
            
echo "\n<tr><td class=\"psr-titre\">\n";
            if (
$pucetitre) {echo "<img src=\"puce.gif\" alt=\"-\"></img>";}
            echo 
"<a href='".trim($lien)."'";
            if (
trim($auteur) != "") {echo " title=\"par ".htmlentities(trim($auteur))."\" ";}
            echo 
">".htmlentities(trim($titre))."</a>\n</td></tr>\n";            
            if (
trim($description) != "") {
                echo 
"\n<tr><td class=\"psr-descri\">\n";
                echo 
htmlentities(trim$description))."\n</td></tr>\n";
                }

            
$titre "";
            
$description "";
            
$lien "";
            
$image "";
            
$auteur "";

            break;
            }

$depth[$parser]--;

}

// Data retieve processing
function characterData($parser$data) {
    global 
$depth$opentag$titre$description$lien$auteur$image;

    switch (
$opentag) {
        case 
"TITLE":
            
$titre .= $data;
            break;
        case 
"DESCRIPTION":
            
$description .= $data;
            break;
        case 
"LINK":
            
$lien .= $data;
            break;
        case 
"AUTHOR":
            
$auteur .= $data;
            break;
        case 
"URL":
            
$image .= $data;
            break;
        }
}


// Prepare to parse
$xml_parser xml_parser_create();
//xml_set_default_handler ($xml_parser, "<!DOCTYPE rss PUBLIC '-//Netscape Communications//DTD RSS 0.91//EN' 'http://my.netscape.com/publish/formats/rss-0.91.dtd'>");
xml_parser_set_option ($xml_parserXML_OPTION_TARGET_ENCODING"ISO-1859-1");
xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");


// Reading xml file and begining to parse
$fp fopen($rss_file_url,"r")
    or die(
"<b>Erreur de lecture des donn&eacute;es RSS.<br>- L'url du fichier xml &agrave; analyser est incorrecte</b>");
while (
$data fread($fp4096))
    
xml_parse($xml_parser$datafeof($fp))
        or die(
sprintf("<b>Erreur XML: %s &agrave; la ligne %d<br>- Le fichier xml &agrave; analyser est incorrect<br /></b>"
            
xml_error_string(xml_get_error_code($xml_parser)), 
            
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);


?>