<?php
/*
Name    : backend.php
Author  : NicoSoft
Création: 18/09/01
MAJ     : 08/08/05
*/

require_once "include/standard.inc.php";
require_once "include/news.inc.php";
//require_once "include/lang.inc.php";

extract(parse_ini_file("/www/website/config/pip.ini"));

clearstatcache();

// paramètres

$NbMax = 10;

if (isset($_GET["language"]))
	$language = $_GET["language"];

if (!isset($language))
        $language = "fr";


$FileNews="news/news.$language";

$totalnews = gettotal("$FileNews","file");
echo "<br />nb total de news:$totalnews<hr />";


if (isset($_GET["typebackend"]))
	$typebackend = $_GET["typebackend"];

if (!isset($typebackend))
        $typebackend = "all";

switch($typebackend)
{
        case "txt":
		gen_txt($language);
                break;
		
	case "atom":
		gen_atom($language);
		break;
		
	case "opml":
		gen_opml($language);
		break;
	case "rss":
		gen_rss($language);
		break;
	case "all":
        default:
		gen_rss("en");
		gen_rss("fr");

		gen_atom("en");
		gen_atom("fr");

		//gen_opml("en");
		//gen_opml("fr");

		gen_txt("en");
		gen_txt("fr");
}

function gen_txt($language)
{
		global $NbMax;
		extract(parse_ini_file("/web/website/config/pip.ini"));

		$FileNews="news/news.$language";
		
		$totalnews = gettotal("$FileNews","file");

                $outputfile = "../backends/txt/tourmentine.$language.txt";

		if ($NbMax > $totalnews)
                        $NbMax = $totalnews;

                if ($totalnews >= 1)
                {
                        $backend = fopen("$outputfile","w");
                        for($n = 0 ; $n < $NbMax ; $n++)
                        {
                                $newsnb = $totalnews-$n;
                                $MyNews = getnews($FileNews,$newsnb,"file");
                                fwrite ($backend," %%\n");
                                fwrite ($backend,$MyNews["date"]."\n");
                                fwrite ($backend,$MyNews["titre"]."\n");
                                #fwrite ($backend,"$urlsite/index.php?modulepage=affnews&File=$FileNews&start=".$newsnb."\n");
                                fwrite ($backend,"$urlsite/billet.".$newsnb.".".$language.".html\n");
                        }
                        fclose($backend);
                        echo "<br />$outputfile généré.<br />";
                }
                else echo "fichier non créé : nombre de news insuffisant ($totalnews)<br />";
}

function gen_atom($language)
{
		
	global $NbMax;
	extract(parse_ini_file("/web/website/config/pip.ini"));
	$FileNews="news/news.$language";

	$totalnews = gettotal("$FileNews","file");
			
		// entete atom
		$entete = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<?xml-stylesheet href=\"http://tourmentine.com/css/atom.css\" type=\"text/css\"?>
<?xml-stylesheet type=\"text/xsl\" href=\"/css/atom.xsl\"?>
<feed version=\"0.3\"
	xmlns=\"http://purl.org/atom/ns#\"
	xml:lang=\"$language\">
	<title>$titlesite</title>
	<tagline>$descriptionsite</tagline>
      <link rel=\"alternate\" type=\"text/html\" href=\"$urlsite\"/>
      <generator url=\"http://tourmentine.com/\" version=\"beta\">WebLoom</generator>
  <author>
      <name>n</name>
      <url>http://tourmentine.com/</url>
  </author>
  <modified>".date("c")."</modified>\n\n";
  
  	$outputfile = "../backends/atom/tourmentine.$language.xml";
        
	if ($NbMax > $totalnews)
	        $NbMax = $totalnews;

        if ($totalnews >= 1)
        {
                $backend = fopen("$outputfile","w");

                fwrite($backend,$entete);
                for($n = 0 ; $n < $NbMax ; $n++)
                {
                        $newsnb = $totalnews-$n;
                        $MyNews = getnews("$FileNews",$newsnb,"file");
                        
			fwrite ($backend,"  <entry>\n");
                        fwrite ($backend,"    <title>".$MyNews["titre"]."</title>\n");
			
		        if (strlen(strip_tags($MyNews["message"])) > 100)
	                       $summary = substr(strip_tags($MyNews["message"]), 0, 100)."...";
			else 
				$summary = strip_tags($MyNews["message"]);
				
                        fwrite ($backend,"    <summary>".$summary."</summary>\n");
                        fwrite ($backend,"    <content type=\"text/html\" mode=\"escaped\">\n<![CDATA[\n".$MyNews["message"]."\n]]>\n</content>\n");
                        fwrite ($backend,"    <link rel=\"alternate\" type=\"text/html\" href=\"$urlsite/billet.".$newsnb.".".$language.".html\" />\n");
                        fwrite ($backend,"    <issued>".date("c",$MyNews["timestamp"])."</issued>\n");
                        fwrite ($backend,"    <modified>".date("c",$MyNews["timestamp"])."</modified>\n");
			fwrite ($backend,"    <id>$urlsite/billet.".$newsnb.".html</id>\n");
                        fwrite ($backend,"  </entry>\n\n");
                }
                fwrite ($backend,"</feed>");
                fclose($backend);
                echo "<br />$outputfile généré.<br />";
        }
        else echo "fichier non créé : nombre de news insuffisant ($totalnews)<br />";
}

function gen_opml($language)
{
	
	global $NbMax;
	extract(parse_ini_file("/web/website/config/pip.ini"));
       $FileNews="news/news.$language";

       $totalnews = gettotal("$FileNews","file");

		$entete = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!-- generator=\"WebLoom Beta\" -->
<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
	<head>
		<title>$titlesite</title>
	</head>
	<body>\n\n";

	$outputfile = "../backends/opml/tourmentine.$language.opml";

	if ($NbMax > $totalnews)
		$NbMax = $totalnews;

	if ($totalnews >= 1)
	{
		$backend = fopen("$outputfile","w");

		fwrite($backend,$entete);
		for($n = 0 ; $n < $NbMax ; $n++)
		{
			$newsnb = $totalnews-$n;
			$MyNews = getnews("$FileNews",$newsnb,"file");

			fwrite ($backend,"		<outline type=\"rss\" title=\"".$MyNews["titre"]."\" text=\"".$MyNews["titre"]."\" url=\"$urlsite/billet.".$newsnb.".".$language.".html\" />\n");
		}
		fwrite ($backend,"	</body>\n</opml>");
		fclose($backend);
		echo "<br />$outputfile généré.<br />";
	}
	else echo "fichier non créé : nombre de news insuffisant ($totalnews)<br />";
}

function gen_rss($language)
{
	global $NbMax;	
	extract(parse_ini_file("/www/website/config/pip.ini"));
       $FileNews="news/news.$language";

       $totalnews = gettotal("$FileNews","file");
                // entete rss
                $entete = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<?xml-stylesheet href=\"http://tourmentine.com/css/rss.css\" type=\"text/css\"?>
<?xml-stylesheet title=\"gris\" type=\"text/xsl\" href=\"/css/rss.xsl\"?>
<rss version=\"2.0\">

   <channel>

   <title>$titlesite</title>
   <link>$linksite</link>
   <description>$descriptionsite</description>
   <language>$language</language>
   <pubDate>".date("r")."</pubDate>
   <lastBuildDate>".date("r")."</lastBuildDate>
   <managingEditor>$MailAdr (Nico)</managingEditor>
   <webMaster>Nico $MailAdr (Nico)</webMaster>
   <generator>WebLoom beta</generator>\n\n";

        $outputfile = "../backends/xml/tourmentine.$language.rss";

        if ($NbMax > $totalnews)
	        $NbMax = $totalnews;

        if ($totalnews >= 1)
        {
                $backend = fopen("$outputfile","w");

                fwrite($backend,$entete);
                for($n = 0 ; $n < $NbMax ; $n++)
                {
                        $newsnb = $totalnews-$n;
                        $MyNews = getnews("$FileNews",$newsnb,"file");
                        
			fwrite ($backend,"  <item>\n");
                        fwrite ($backend,"    <title>".$MyNews["titre"]."</title>\n");
                        //fwrite ($backend,"    <dc:creator>".$MyNews["nom"]."</dc:creator>\n");
                        fwrite ($backend,"    <description><![CDATA[\n".$MyNews["message"]."\n]]></description>\n");
                        fwrite ($backend,"    <link>$urlsite/billet.".$newsnb.".".$language.".html</link>\n");
                        fwrite ($backend,"    <pubDate>".date("r",$MyNews["timestamp"])."</pubDate>\n");
                        fwrite ($backend,"  </item>\n\n");
                }
                fwrite ($backend,"  </channel>\n\n</rss>");
                fclose($backend);
                echo "<br />$outputfile généré.<br />";
        }
        else echo "fichier non créé : nombre de news insuffisant ($totalnews)<br />";
}
?>