<?php
	require_once "include/standard.inc.php";
	include_once "themes/default/theme.php";
	include_once "include/lang.inc.php";

	if ($lang == "fr")
	{
		$post_a_new_message = "Poster un nouveau message";
		$anonymous = "Anonyme";
		$cancel = "Annuler";
		$help = "Aide";
		$name = "Nom/Pseudo (facultatif)";
		$email_adress = "e-mail (facultatif)";
		$subject = "Sujet";
		$view = "Voir";
		$clear = "Effaçer";
		$write = "Ecrire";
		$hide_help = "Cacher Aide";
		$back = "Retour";
		$confirm_post_message = "Poster votre message ?";
		$please_fill_fields = "Vous devez remplir au minimum les champs Sujet et Message.";
		$previewed_message = "prévisualisé le ".strftime ("%d/%m/%Y", time())." à ".date("H:i")." par ";
		$message_sent = "Réponse postée !";
		$reply = "Répondre";
	}
	else
	{
		$post_a_new_message = "New message";
		$anonymous = "Anonymous";
		$cancel = "Cancel";
		$help = "Help";
		$name = "Name (facultative)";
		$email_adress = "e-mail adress (facultative)";
		$subject = "Subject";
		$view = "Preview";
		$clear = "Clear";
		$write = "Write";
		$hide_help = "Hide Help";
		$back = "Back";
		$confirm_post_message = "Send your message?";
		$please_fill_fields = "Subject and Message fields can't be empty.";
		$previewed_message = "previewed on ".strftime ("%m/%d/%Y", time())." at ".date("h:i A")." by ";
		$message_sent = "Message sent!";
		$reply = "Reply";
	}
	
	if ($_GET["forumfile"] != "")
		$forumfile = $_GET["forumfile"];
	
	if ($_GET["idnews"] != "")
		$idnews = $_GET["idnews"];
	
	$index = $oldstrength = 1;
	if (!isset($File) || $File=="")
	{
		if (file_exists("modules/news/news.$lang.csv"))
			$File="modules/news/news.$lang";
		else
			$File="modules/news/news";
	}

	
	if (!isset($forumfile) || $forumfile=="")
		$forumfile = $File.".".$idnews.".forum.csv";

	//echo $forumfile;
	echo "<!-- preview:$preview-->";
function write2csv($csvfile, $data)
{
	$fp = fopen ($csvfile,"a");
	$csvstring = "\"".$data["idmessage"].
		     "\",\"".$data["date"].
		     "\",\"".$data["heure"].
		     "\",\"".$data["authorname"].
		     "\",\"".$data["authoremail"].
		     "\",\"".$data["titlemessage"].
		     "\",\"".$data["textmessage"]."\"\n";
	fwrite ($fp, $csvstring);
	fclose ($fp);
}

function form($rep, $to,$numrep, $idnews, $File)
{
	global $lang;
	global $name;
	global $email_adress;
	global $subject;
	global $help;
	global $closedpost;
	global $view, $clear, $write, $hide_help, $confirm_post_message, $previewed_message, $posted_message, $reply;
	
	if ($_POST["preview"] == "Voir" || $_POST["preview"] == "Preview")
	{
		$preview = $_POST["preview"];
		$VName = $_POST["VName"];
                $VEmail = $_POST["VEmail"];
        	$VSubject = $_POST["VSubject"];
                $VText = $_POST["VText"];
	}
	else $VSubject = $rep;

	$VSubject = str_replace("'","&#39;",$VSubject);
	$VName = str_replace("'","&#39;",$VName);
	
	// affichage du formulaire
	echo "<hr /><table class='addcomment'><tr><td>";
	echo "<form action='$to' method='post' name='addnews'>
	<input name='modulepage' value='commentnews' type='hidden'></input>
        <div class='title_field'>$name</div><input name='VName' type='text' size='30' value='".stripcslashes($VName)."'></input><br />
        <div class='title_field'>$email_adress</div><input name='VEmail' type='text' size='30' value='".$VEmail."'></input><br />
	<div class='title_field'>$subject</div><input name='VSubject' type='text' size='46' value='".stripcslashes($VSubject)."'></input><br /><br />
        <div class='title_field'>Message</div><textarea name='VText' cols='50' rows='12'>".stripcslashes(htmlentities($VText))."</textarea><br />

	<!-- <input name='piejacon' value='piejacon' /> -->
        <input name='NumRep' value='".$numrep."' type='hidden' />
	<input name='idnews' value='$idnews' type='hidden' />
	<div class='buttons_field'>
	<input name='preview' value='$view' type='submit' class='boutton' />
	<input name='reset' value='$clear' type='button' onclick=\"document.addnews.VText.value=''\" class='boutton' />
	
	<input name='Ecrire' value='$write' type='submit' onclick=\"return confirm('$confirm_post_message')\" class='boutton' />
	<input name='hidehelp' id='hidehelp' value='$hide_help' type='button' onclick=\"togglehelp('$lang');\" class='boutton' />
	</div>
	</form>";

	echo "</td><td><div class='title_field' id='wikihelp_title'>$help</div>
	                ".wiki_help($lang)."</td></tr></table>";
	
	// affichage d'un message à prévisualister
	if (isset($preview))
	{
		if ($VName == "")
			$VName = "Anonyme";
			
		if ($VEmail != "")
			$VEmail = "<a href='mailto:$VEmail'>$VName</a>";
		else
			$VEmail = $VName;
			
		//echo "<div class='forum_message_header'>&nbsp;&nbsp;[".stripcslashes($VSubject)."]</a> prévisualisé le ".strftime ("%d/%m/%Y", time())." à ".date("H:i")."&nbsp;par ".stripcslashes($VEmail)."</div><br />";
		echo "<div class='forum_message_header'>&nbsp;&nbsp;[".stripcslashes($VSubject)."]</a> ".$previewed_message.stripcslashes($VEmail)."</div><br />";
		echo "<table class='comment'><tr><td>".stripcslashes(parse_string($VText,"yes"))."</td></tr></table><br />";
       		echo "</td></tr>\n";
	}
	
       echo "<hr /></td></tr>\n";
}
	
	// récupération du numéro du dernier message	
	if (file_exists("$forumfile"))
	{
  		$fp = fopen ($forumfile,"r");
			while ($data[$index++] = fgetcsv ($fp, 1000, ","));
		fclose ($fp);
	}
	else
	{
		$index = 0;
	}

	// Écriture effective du message
	if (isset($Ecrire) && !isset($piejacon) && ($Ecrire == "Ecrire" || $Ecrire == "Write"))
	{
		if ($VSubject == "" || $VText == "" || $NumRep == "")
			echo $please_fill_fields."<br />";
		else
		{
			$t["idmessage"] = $NumRep;
			$t["titlemessage"] = $VSubject;
			$t["textmessage"] = htmlentities(parse_string($VText,"yes"));
			$VSubject = str_replace("'","&#39;",$VSubject);
			$VName = str_replace("'","&#39;",$VName);
			$t["authorname"] = $VName;
			$t["authoremail"] = $VEmail;
			if ($lang == "fr")
			{
				$t["date"] = strftime("%d/%m/%Y", time());
                		$t["heure"] = date("H:i");
			}
			else
			{
				$t["date"] = strftime("%m/%d/%Y", time());
                		$t["heure"] = strftime("%I:%M %p", time());
			}

			write2csv($forumfile,$t);
			
			//mail("n@tourmentine.com","[Tourmentine] post forum ".$forumfile, "titre:".$VSubject."\nauteur: ".$VName." <".$VEmail.">");
			mailalert("n@tourmentine.com",("post forum ".$forumfile), $VName, $VEmail, $VSubject, $VText);
			sendviajabber("n@tourmentine.com","post forum $forumfile $VName <$VEmail> $VSubject: $VText");
			// met à jour le cache
			touch("modules/news");
			
			echo "$message_sent<br />";
		}
		echo "<a href ='billet.$idnews.html'>$back</a>";
	}
	else
	{
	// affichage des messages
	for ($i=0; $i < $index; $i++)
	{
		$thread[$i]["idmessage"] = $data[$i][0];
		$thread[$i]["date"] = $data[$i][1];
		$thread[$i]["heure"] = $data[$i][2];
		$thread[$i]["auteur"] = $data[$i][3];
		$thread[$i]["mail"] = $data[$i][4];
		$thread[$i]["titlemessage"] = $data[$i][5];
		$thread[$i]["textmessage"] = $data[$i][6];
	}
	
	if ($thread[1]["idmessage"] != "")
		sort($thread);

	$nbthreads = substr($thread[$i-1]["idmessage"],2,2);
	
	if (isset($indexmsg) && $indexmsg == 0)
	{
		if ($nbthreads < 11)
			$NumRep = "id0".($nbthreads+1);
		else
			$NumRep = "id".($nbthreads+1);
							
		//form("", "?modulepage=commentnews&amp;idnews=$idnews&amp;indexmsg=$indexmsg", $NumRep, $idnews, $File);
		form("", "comment.$idnews.$indexmsg.html#$indexmsg", $NumRep, $idnews, $File, "yes");
		
		echo "<tr><td>\n";
		//echo "<a href ='?modulepage=commentnews&amp;idnews=$idnews'>Annuler</a><hr/>\n";
		echo "<a href ='billet.$idnews.html'>$cancel</a><hr />\n";
	}
	else
		//echo "<a href='?modulepage=commentnews&amp;idnews=$idnews&amp;indexmsg=0'>[Poster un nouveau message]</a><br /><br />\n";
		if ($closedpost == false)
		echo "\n<a href='comment.$idnews.0000.html'>[$post_a_new_message]</a><br /><br />\n";

	// entête d'un message (auteur, @email, titre, date, etc.)
	for ($i=1; $i < $index; $i++)
	{
		if ($thread[$i]["idmessage"] != "")
		{
			if (strlen($thread[$i]["idmessage"]) >= $oldstrength)
				echo str_repeat("&nbsp;",strlen($thread[$i]["idmessage"])-2);
			
			if ($thread[$i]["auteur"] == "")
				if ($lang == "fr")
					$thread[$i]["auteur"] = "Anonyme";
				else
					$thread[$i]["auteur"] = "Anonymous";
				
			if ($thread[$i]["mail"] != "")
				$thread[$i]["auteur"] = "<a href='mailto:".$thread[$i]["mail"]."'>".$thread[$i]["auteur"]."</a>";
				
			// formatage du numéro de commentaire (nombre à 4 chiffres)
			$indexmsglength = strlen($i);
			
			if ($indexmsglength == 1 )
				$izero = "000".$i;
			
			if ($indexmsglength == 2 )
                                $izero = "00".$i;
			
			if ($indexmsglengh == 3 )
                                $izero = "0".$i;
			
			//echo "\n<a href='?modulepage=commentnews&amp;File=$File&amp;idnews=$idnews&amp;indexmsg=$i' title='Répondre'>[".stripcslashes($thread[$i]["titlemessage"])."]</a> posté le ".$thread[$i]["date"]." à ".$thread[$i]["heure"]." par ".stripcslashes($thread[$i]["auteur"])."<br />\n";
			if ($lang == "fr")
				echo "\n\n<div class='forum_message_header'>".str_repeat("&nbsp;",strlen($thread[$i]["idmessage"])-2)."<a name='$izero' href='comment.$idnews.$izero.html#$izero' title='Répondre'>[".stripcslashes($thread[$i]["titlemessage"])."]</a> posté le ".$thread[$i]["date"]." à ".$thread[$i]["heure"]." par ".stripcslashes($thread[$i]["auteur"])."</div><br />\n";
			else
				echo "\n\n<div class='forum_message_header'>".str_repeat("&nbsp;",strlen($thread[$i]["idmessage"])-2)."<a name='$izero' href='comment.$idnews.$izero.html#$izero' title='Répondre'>[".stripcslashes($thread[$i]["titlemessage"])."]</a> posted on ".$thread[$i]["date"]." at ".$thread[$i]["heure"]." by ".stripcslashes($thread[$i]["auteur"])."</div><br />\n";
				
			echo str_repeat("&nbsp;",strlen($thread[$i]["idmessage"]));
			echo "<table class='comment'><tr><td>".stripcslashes($thread[$i]["textmessage"])."</td></tr></table><br />\n";
			//echo "</tr></table>";
			$oldlength = strlen($thread[$i]["idmessage"]);
		}

		// le numéro de message corresponds à celui demandé: on affiche le formulaire
		
		if (isset($indexmsg) && $indexmsg == $i)
		{
			$long =  strlen($thread[$i]["idmessage"]);
			//$cpt = $i+1;

			while (strlen($thread[$cpt]["idmessage"]) == $long+2)
				if (substr($thread[$cpt]["idmessage"], 0, $long) == $thread[$i]["idmessage"])
					$cpt++;

			$NbReponses = ($cpt);
			if ($NbReponses < 10)
				$NbReponses = "0".$NbReponses;

			if ($NbReponses == 0)
				$NumRep = $thread[$i]["idmessage"]."01";
			else
				$NumRep = $thread[$i]["idmessage"].$NbReponses;
			$reponse = "RE:".$thread[$i]["titlemessage"];
			
			form($reponse, "comment.$idnews.$indexmsg.html#$indexmsg", $NumRep, $idnews, $File);
			echo "<tr><td>";
		}
	}

}
 ?>