<?php

// use smileys > true or false
$useSmileys = true;

// reverse answer list > true or false
$reverseAnswers = true;

// number of answers/topics per page
$itemsPerPage = 20;

//----------------------------------------------------------------------------------------------
// retrieving all variables
//----------------------------------------------------------------------------------------------

if($_POST) { extract($_POST, EXTR_PREFIX_SAME, "post_"); }
if($_GET) { extract($_GET, EXTR_PREFIX_SAME, "get_"); }
if($_COOKIE) { extract($_COOKIE, EXTR_PREFIX_SAME, "cookie_"); }

if(!isset($_ForumNick)) { $_ForumNick = ""; }

//----------------------------------------------------------------------------------------------
// functions for inserting and showing the smilies
//----------------------------------------------------------------------------------------------

function insertSmileys() { ?>

	<script type="text/javascript">	
	function insSml(face) {
		var newText;
		var currentText = document.form.txt.value;
		newText = currentText+face;
		document.form.txt.value=newText;
		document.form.txt.focus();
	}
	</script>

	<a href="#"><img src="smileys/smile.gif" hspace="2" border="0" onClick="insSml(' :) ');"></a>
	<a href="#"><img src="smileys/wink.gif" hspace="2" border="0" onClick="insSml(' ;)' );"></a>
	<a href="#"><img src="smileys/laugh.gif" hspace="2" border="0" onClick="insSml(' :d ');"></a>
	<a href="#"><img src="smileys/doubt.gif" hspace="2" border="0" onClick="insSml(' :s ');"></a>
	<a href="#"><img src="smileys/hot.gif" hspace="2" border="0" onClick="insSml(' (h) ');"></a>
	<a href="#"><img src="smileys/tears.gif" hspace="2" border="0" onClick="insSml(' :\'( ');"></a>
	<a href="#"><img src="smileys/angry.gif" hspace="2" border="0" onClick="insSml(' :@ ');"></a>
	<a href="#"><img src="smileys/angel.gif" hspace="2" border="0" onClick="insSml(' (a) ');"></a>
	<a href="#"><img src="smileys/bloss.gif" hspace="2" border="0" onClick="insSml(' :$ ');"></a>
	<a href="#"><img src="smileys/tongue.gif" hspace="2" border="0" onClick="insSml(' :p ');"></a>
	<a href="#"><img src="smileys/beer.gif" hspace="8" border="0" onClick="insSml(' (b) ');"></a>

<? }

function showSmileys($face) {
	$temp = str_replace(":)","<img src=\"smileys/smile.gif\" alt=\"smiley\" />", $face);
	$temp = str_replace(";)","<img src=\"smileys/wink.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace(":d","<img src=\"smileys/laugh.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace(":s","<img src=\"smileys/doubt.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace("(h)","<img src=\"smileys/hot.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace(":'(","<img src=\"smileys/tears.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace(":@","<img src=\"smileys/angry.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace("(a)","<img src=\"smileys/angel.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace(":$","<img src=\"smileys/bloss.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace(":p","<img src=\"smileys/tongue.gif\" alt=\"smiley\" />", $temp);
	$temp = str_replace("(b)","<img src=\"smileys/beer.gif\" alt=\"smiley\" />", $temp);
	return $temp;
}

//----------------------------------------------------------------------------------------------
//  list the available topics and print add topic form
//----------------------------------------------------------------------------------------------

if (!isset($act) || $act == "list") {
	
	// reading topic list into array
	$n = 0;
    $list = array();
	$fp = fopen("data/topics.txt", "r");
	while (!feof($fp)) {
		$lastpost = fgets($fp, 1024);
		$nickname = fgets($fp, 1024);
		$description = fgets($fp, 1024);
		$file = fgets($fp, 1024);
		$ans = fgets($fp, 1024);
		if ($lastpost && $nickname && $description && $file && $ans) {
			$list[$n][0] = str_replace("\n","",$lastpost);
			$list[$n][1] = str_replace("\n","",$nickname);
			$list[$n][2] = str_replace("\n","",$description);
			$list[$n][3] = str_replace("\n","",$file);
			$list[$n][4] = str_replace("\n","",$ans);
			$n++;
		}
	}
    fclose($fp);
	array_multisort($list);
	$list = array_reverse($list);
	
	// checking the number of last topic in case a topic will be added
	$max = 0;
	for($i=0; $i<sizeof($list); $i++) {
		if ( intval($list[$i][3]) > $max) { $max = intval($list[$i][3]); } }

	// start printing the page
	include("header.php");
	?>
<h3>
	Seznam témat
</h3>
<p>Diskuzní fórum bylo dočasně pozastaveno.</p>
<?
	//closing the page
include("footer.php");
	
//----------------------------------------------------------------------------------------------
//  show the specified topic list and add answer form
//----------------------------------------------------------------------------------------------

} else if ($act == "show") {
	
	// reading the answers into an array
	$n = 0;
    $topic = array();
	$fp = fopen("data/topic".$nr.".txt", "r");
	$description = str_replace("\n","",fgets($fp, 1024));
	while (!feof($fp)) {
		$date = fgets($fp, 1024);
		$nickname = fgets($fp, 1024);
		$text = fgets($fp, 9000);
		if ($date && $nickname && $text) {
			$topic[$n][0] = str_replace("\n","",$date);
			$topic[$n][1] = str_replace("\n","",$nickname);
			$topic[$n][2] = str_replace("\n","",$text);
			$n++;
		}
	}
    fclose($fp);
	
	if ($reverseAnswers == true) { $topic = array_reverse($topic); }
	
	// start building the page
	include("header.php");
    ?>
<h3>
	<?=$description?>
</h3>
<table width="700">
	<?
	// build pagination
	if (!isset($s)) { $s=0; $e=$itemsPerPage; } else { $e=$s+$itemsPerPage; }
	if($e>sizeof($topic)) { $e=sizeof($topic); }
	
	// show pagination
		echo "<tr>\n		<td></td><td class=\"grey\">odpovědi ".($s+1)."-$e of ".sizeof($topic)."";
		echo " | <a href=\"?act=list\" class=\"grey\">zpátky k tématům</a></td>\n";
		if ($s>0) { echo " | <a href=\"?act=show&nr=$nr&s=".($s-$itemsPerPage)."\" class=\"grey\">previous page</a>\n"; }
		echo "	</tr>\n";
	
	// print answers list
	for ($i=$s; $i<$e; $i++) {	
		if ($i/2 != round($i/2)) { echo "	<tr>\n"; } 
		else { echo "	<tr class=\"light\">\n"; }
		echo "		<td><b>".$topic[$i][1]."</b><br /><i>&nbsp;".date("d.m.Y H:i", $topic[$i][0])."</i></td>\n";
		if ($useSmileys == true) { 
			echo "		<td>".showSmileys($topic[$i][2])."</td>\n	</tr>\n";
		} else {
			echo "	<td>".$topic[$i][2]."\n\n	</td></tr>\n\n";
		}	
	}
		
	// show pagination
		echo "	<tr>\n		<td></td><td class=\"grey\">odpovědi ".($s+1)."-$e of ".sizeof($topic)."";
		echo " | <a href=\"?act=list\" class=\"grey\">zpátky k tématům</a></td>\n";
		if ($s>0) { echo " | <a href=\"?act=show&nr=$nr&s=".($s-$itemsPerPage)."\" class=\"grey\">previous page</a>\n"; }
		echo "	</tr>\n</table>";
		
	// print reaction form
    ?> 
	<script type="text/javascript">
	function checkform (form) {
  	if (form["nick"].value == "") {
    	alert("Prosím zadejte Vaše jméno");
    	form["nick"].focus();
    	return false ; }
	if (form["txt"].value == "") {
		alert("Vyplňte text zprávy");
    	form["txt"].focus();
    	return false ; }
  	return true; }
	</script>
	
<form id="form" action="?act=adda" method="post" onsubmit="return checkform(this);">
	<p><input type="hidden" name="nr" value="<?=$nr?>" /></p>
<hr />
<h3>
	Přidání odpovědi
</h3>
<table>
	<tr>
		<th>
			Autor
		</th>
	</tr>
	<tr>
		<td>
			<input type="text" name="nick" value="<?=$_ForumNick?>" maxlength="20" />
		</td>
	</tr>
	<tr>
		<th>
			Zpráva
		</th>
	</tr>
	<tr>	
		<td>
			<textarea rows="6" cols="20" name="txt"></textarea>
		</td>
	</tr>
	<tr>
		<td>
			<input type="submit" value="přidat odpověď" class="button" />
		</td>
	</tr>
</table>
</form>
<?
include("footer.php");

//----------------------------------------------------------------------------------------------
//   add a topic from the form to the topic list
//----------------------------------------------------------------------------------------------

} else if ($act == "addt") {
	
	// save nickname as cookie
	setcookie("_ForumNick", $nick, time()+3600*24*90);
	
	// strip tags
	$desc = substr(strip_tags($desc),0,40);
	$nick = substr(strip_tags($nick),0,20);
	$txt = strip_tags($txt);
	
	// add topic to list
    $fp = fopen("data/topics.txt", "a");
    fputs($fp, time()."\n");
    fputs($fp, stripslashes($nick)."\n");
    fputs($fp, stripslashes($desc)."\n");
    fputs($fp, stripslashes($nr)."\n");
    fputs($fp, "0\n");
    fclose($fp);

	// add first answer to topic
    $fp = fopen("data/topic".$nr.".txt", "w");
    fputs($fp, stripslashes($desc)."\n");
    fputs($fp, time()."\n");
    fputs($fp, stripslashes($nick)."\n");
    fputs($fp, stripslashes(str_replace("\r", "", str_replace("\n", "", nl2br($txt))))."\n");
    fclose($fp);

	// jump to topic
	header("Location: index.php?act=show&nr=$nr");

//----------------------------------------------------------------------------------------------
//  add an answer to the current topic
//----------------------------------------------------------------------------------------------

} else if ($act == "adda") {

	// save nickname as cookie
	setcookie("_ForumNick", $nick, time()+3600*24*90);

	// strip tags
	$nick = substr(strip_tags($nick),0,20);
	$txt = strip_tags($txt);

	// add answer to the corresponding topic
	$fp = fopen("data/topic".$nr.".txt", "a");
	fputs($fp, time()."\n");
	fputs($fp, stripslashes($nick)."\n");
	fputs($fp, stripslashes(str_replace("\r", "", str_replace("\n", "", nl2br($txt))))."\n");
	fclose($fp);

	// read the topic list
	$n = 0;
	$list = array();
	$fp = fopen("data/topics.txt", "r");
	while (!feof($fp)) {
		$lastpost = fgets($fp, 1024);
		$nickname = fgets($fp, 1024);
		$description = fgets($fp, 1024);
		$file = fgets($fp, 1024);
		$ans = fgets($fp, 1024);
		// update time and munber of andwers to the right topic
		if ($lastpost && $nickname && $description && $file && $ans) {
			if (trim($file) == $nr) { $list[$n][0] = time()."\n"; } 
			else { $list[$n][0] = $lastpost; }
			$list[$n][1] = $nickname;
			$list[$n][2] = $description;
			$list[$n][3] = $file;
			if (trim($file) == $nr) { $list[$n][4] = ($ans+1)."\n"; } 
			else { $list[$n][4] = $ans; }
		}
		$n++;
	}
	fclose($fp);

	// write the updated topic list
	$fp = fopen("data/topics.txt", "w");
	for ($i=0; $i<sizeof($list); $i++) {
    	fputs($fp, stripslashes($list[$i][0]));
    	fputs($fp, stripslashes($list[$i][1]));
    	fputs($fp, stripslashes($list[$i][2]));
    	fputs($fp, stripslashes($list[$i][3]));
    	fputs($fp, stripslashes($list[$i][4]));
	}
	fclose($fp); 
	
	// jump to topic
	header("Location: index.php?act=show&nr=$nr"); 

}
	
?>
