<?php
session_start();
///////////////////////////////////////////////////////////////////////////////////
//	Wak's Ask&Answer/FAQs Script
//
//	Script by Katie http://luved.org - Teljes magyar felület by Adliz 
//
//	This script is linkware and can be used or modified as long as all credit is
//	left attached to this file and a link is placed on your website to
//	http://www.luved.org
//	You are forbidden to sell or distribute this script without permission by
//	it's creator and if a bug is found, report to scripts@luved.org
///////////////////////////////////////////////////////////////////////////////////

include_once('functions.php');
if ($_GET['action'] == 'login') {
	$pass = $_POST['p'];
	$strLogged = logUser($pass);
	if ($strLogged != '') echo $TOP_LOGIN_FORM . $strLogged . $BOTTOM_LOGIN_FORM;
} else if ($_GET['go'] == 'home') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = HOME
//		---------
//		Main area when first logged in
//////////////////////////////////////////////////////////////////////////
	isLoggedIn();
	echo $HEADER;
	$unanswered = getTotalUnanswered();
	$pagenum = $_GET['page'];
	$unansweredRows = perPage($unanswered, $defaultEntryPerPage, $pagenum);
	$numPages = $unansweredRows['numpages']; 
	list($id, $key, $url, $email, $notify, $entryPerPage, $dtFormat, $display) = getFileRow(CONFIGFILE);
	echo '<tr><td colspan="6"><h2> <b>'.count($unanswered).'</b> megválaszolatlan kérdés</h2></td></tr>';
	?>
	<form action="<?php echo $phpSelf?>?go=delete" method="post" name="delanswers">
	<?php
		if (count($unanswered) > 0) {
			$unanswered = desc($unanswered);
			$x = 0;
			?>
			<tr>
				<td colspan="6"><input class="noborder" type="checkbox" name="selectEntry1" onClick="javascript:checkall('delanswers', 'delete')" /"> Összes kijelölése
				<input type="submit" onClick="return confirmDelete()" name="delete" value="Kijelöltek törlése" />
				</td>
			</tr>
			<tr><td colspan="6" align="right"><?php printJumpToPage("$phpSelf?go=home&page=", $numPages, $pagenum)?></td></tr>
			<?php
			$ansRows = $unansweredRows['entries'];
			if (is_array($ansRows)) {
				if ($display == 2)
					$ansRows = array_reverse($ansRows);
			foreach ($ansRows as $r) {
				list($id, $q, $a, $ip, $timestamp) = get($r);
					$x++;
				if (strlen($q) > $qLen)
					$q = substr($q, 0, $qLen)."..."; ?>
				<tr>
					<td class="td"><input id="delete<?php echo $x?>" class="noborder" type="checkbox" name="todel[]" value="<?php echo $id?>" /></td>
					<td class="td"><a href="<?php echo $phpSelf?>?go=answer&id=<?php echo $id?>">Válaszolok</a></td>
					<td class="td"><a onClick="return confirmDelete()" href="<?php echo $phpSelf?>?go=delete&id=<?php echo $id?>">Törlöm</a></td>
					<td class="td"><?php echo $q?></td>
					<td class="td"><b>IP:</b> <?php echo $ip?></td>
					<td class="td"><?php echo date('d-m-y h:i:s',$timestamp)?> </td>
				</tr>
			<?php }
			}
		} ?>
		</form>
		<?php
	echo $FOOTER;
} else if ($_GET['go'] == 'answer') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = ANSWER
//		---------
//		Answer questions
//////////////////////////////////////////////////////////////////////////
	isLoggedIn();
	echo $HEADER;
	$id = $_GET['id'];
	$row = getFileRowById(DBFILE, $id);
	list($id, $q, $a, $ip, $timestamp) = $row;
	if ($_POST['insert'] != '') {
		$id = $_POST['id'];
		$answerId = $_POST['savedanswer'];
		$answer = fixStr($_POST['answer']);
		$q = $_POST['q'];
		list($answerId, $answerName, $answerBody) = getFileRowById(ANSWERS, $answerId);
		$answerBody = $answer.$answerBody;
	}
	?>
		<tr>
		<?php
		if ($_POST['submitanswer'] != '') {
			$id = $_POST['id'];
			$row = getFileRowById(DBFILE, $id);
			list($id, $q, $a, $ip, $timestamp) = $row;
			$a = fixStr($_POST['answer']);
			$currentTime = time();
			$wdata = "$id\¦$q\¦$a\¦$ip\¦$timestamp\¦$currentTime";
			replaceRow(DBFILE, $id, $wdata);
			echo "Sikeresen válaszoltál.";
		} else {
		?>
			<td>Kérdés:</td>
			<td><b><?php echo $q?></b></td>
		</tr>
		<tr><form action="<?php echo $phpSelf?>?go=answer" method="post">
			<input type="hidden" name="id" value="<?php echo $id?>" />
			<input type="hidden" name="q" value="<?php echo $q?>" />
			<td>Válasz:</td>
			<td><textarea name="answer" tabindex="1" cols="30" rows="4"><?php echo fixBrk($answerBody)?></textarea></td>
		</tr>
		<tr>
			<td><b>VAGY</b> </td>
			<td>
				<?php
				printTemplateAnswers();	
				?>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submitanswer" value="Válaszolok" /></td>
			</form>
			<?php } ?>
		</tr>
	<?php
	echo $FOOTER;
} else if ($_GET['go'] == 'manage') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = MANAGE
//		---------
//		Old questions
//////////////////////////////////////////////////////////////////////////
	isLoggedIn();
	echo $HEADER;
	$answered = count(getTotalAnswered());
	$total = total(DBFILE);
	$pagenum = $_GET['page'];
	
	echo '<tr><td colspan="5"><h2>Sikeresen válaszoltál a <b>'.$answered.'</b> kérdésre a '.$total.' -ból/ből</h2></td></tr>';
	?>
	<form action="<?php echo $phpSelf?>?go=delete" method="post" name="delanswers">
	<?php	
	if ($total > 0) {
			$answeredRows = getTotalAnswered();
			list($id, $key, $url, $email, $notify, $entryPerPage, $dtFormat, $display) = getFileRow(CONFIGFILE);
			if (is_array($answeredRows)) {
			
			$answeredRows = perPage($answeredRows, $defaultEntryPerPage, $pagenum);
			$numPages = $answeredRows['numpages']; 
			?>
			<tr>
				<td colspan="5"><input class="noborder" type="checkbox" name="selectEntry1" onClick="javascript:checkall('delanswers', 'delete')" /"> Összes kijelölése
				<input type="submit" onClick="return confirmDelete()" name="delete" value="Kijelöltek törlése" />
				</td>
			</tr>
				<tr><td colspan="5" align="right"><?php printJumpToPage("$phpSelf?go=manage&page=", $numPages, $pagenum)?></td></tr>
			<?php
			$x = 0;
			$ansRows = $answeredRows['entries'];
			if (is_array($ansRows)) {
				if ($display == 2)
					$ansRows = array_reverse($ansRows);
			foreach ($ansRows as $r) {
				list($id, $q, $a, $ip, $timestamp) = get($r);
				$x++;
				if (strlen($q) > $qLen)
					$q = substr($q, 0, $qLen)."...";
				?>
				<tr>
					<td class="td"><input id="delete<?php echo $x?>" class="noborder" type="checkbox" name="todel[]" value="<?php echo $id?>" /></td>
					<td class="td"><a href="<?php echo $phpSelf?>?go=edit&id=<?php echo $id?>">Szerkesztem</a></td>
					<td class="td"><a onClick="return confirmDelete()" href="<?php echo $phpSelf?>?go=delete&id=<?php echo $id?>">Törlöm</a></td>
					<td class="td"><?php echo $q?></td>
					<td class="td"><b>IP:</b> <?php echo $ip?></td>
					<td class="td"><?php echo dateFormatFull($timestamp)?> </td>
				</tr>
			<?php
			}
			}
			}
		}
	?>
	</form>
	<?php
	echo $FOOTER;
} else if ($_GET['go'] == 'edit') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = EDIT
//		---------
//		Edit Old questions
//////////////////////////////////////////////////////////////////////////
	isLoggedIn();
	echo $HEADER;
	$id = $_GET['id'];
	$row = getFileRowById(DBFILE, $id);
	list($id, $q, $a) = $row;
	if ($_POST['insert'] != '') {
		$id = $_POST['id'];
		$row = getFileRowById(DBFILE, $id);
		list($id, $q) = $row;
		$answerId = $_POST['savedanswer'];
		$answer = fixStr($_POST['answer']);
		list($answerId, $answerName, $answerBody) = getFileRowById(ANSWERS, $answerId);
		$answerBody = $answer.$answerBody;
	}
	echo $printUpdated;
	if ($printUpdated == '') {
	?>
		<tr>
		<?php
		if ($_POST['submitanswer'] != '') {
			$id = $_POST['id'];
			$row = getFileRowById(DBFILE, $id);
			list($id, $q, $a, $ip, $timestamp) = $row;
			$q = fixStr($_POST['question']);
			$a = fixStr($_POST['answer']);
			$currentTime = time();
			$wdata = "$id\¦$q\¦$a\¦$ip\¦$timestamp\¦$currentTime";
			replaceRow(DBFILE, $id, $wdata);
			echo "Kész.";
		} else {
		?><form action="<?php echo $phpSelf?>?go=edit" method="post">
			<input type="hidden" name="id" value="<?php echo $id?>" />
			<td>Kérdés:</td>
			<td><input type="text" name="question" size="30" value="<?php echo $q?>" /></td>
		</tr>
		<tr>
			<td>Válasz:</td>
			<td><textarea name="answer" tabindex="1" cols="30" rows="4"><?php echo fixBrk($a) . fixBrk($answerBody)?></textarea></td>
		</tr>
		<tr>
			<td><b>VAGY</b> </td>
			<td>
				<?php
				printTemplateAnswers();
				?>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submitanswer" value="Válasz" /></td>
			</form>
			<?php } ?>
		</tr>
	<?php 
	}
	echo $FOOTER;
} else if ($_GET['go'] == 'delete') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = DELETE
//		---------
//		Delete questions
//////////////////////////////////////////////////////////////////////////
	isLoggedIn();
	echo $HEADER;
	if (isSet($_POST['todel']) || is_numeric($_GET['id'])) {
		if (is_numeric($_GET['id']))
			delete(DBFILE, $_GET['id']);
		$deleteId = $_POST['todel'];
		if (is_array($deleteId)) {
			foreach ($deleteId as $id) {
				delete(DBFILE, $id);
			}
			
		}
		?>
				<tr>
					<td><b>Törölve!</b></td></tr>
		<?php
	}
	echo $FOOTER;
} else if ($_GET['go'] == 'templates') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = TEMPLATES
//		---------
//		Saved answered
//////////////////////////////////////////////////////////////////////////
	isLoggedIn();
	echo $HEADER;
	?>
		<tr>
			<td><h2>Sablon válaszok</h2>
			<?php if ($_POST['add'] != '') {
		$answerId = getFileId(ANSWERS);
		$answerName = fixStr($_POST['name']);
		$answerBody = fixStr($_POST['answertosave']);
		if ($answerName == '' || $answerBody == '') {
			echo "You must supply both an answer name and its content.";
		} else {
			$answerFull = "$answerId\¦$answerName\¦$answerBody";
			addAnswer($answerFull);
		}
	} else if ($_POST['open'] != '') {
		$answerId = $_POST['answerid'];
		list($answerId, $answerName, $answerBody) = getFileRowById(ANSWERS, $answerId);
	} else if ($_POST['update'] != '') {
		$answerId = $_POST['answerid'];
		$answerName = fixStr($_POST['name']);
		$answerBody = fixStr($_POST['answertosave']);
		$wdata = "$answerId\¦$answerName\¦$answerBody";
		replaceRow(ANSWERS, $answerId, $wdata);
	} else if ($_POST['delete'] != '') {
		$answerId = $_POST['answerid'];
		delete(ANSWERS, $answerId);
	} else if ($_POST['clear'] != '') {
		$answerId = "";
		$answerName = "";
		$answerBody = "";
	} ?>
			</td>
		</tr>
		<tr>
			<td><b>Megj.: </b> Itt létrehozhatsz olyan sablon szövegeket, amelyeket válaszoláskor egyszerűen berakhatsz. Ez igen praktikus, ha többször is felteszik ugyanazt a kérdést, vagy hasonlót. Az adott név fog megjelenni a kérdéseknél lenyíló menüben, tehát ezt csak azért kell megadnod, hogy tudd, melyik válasz az. Figyelj oda arra, hogyha meg van nyitva egy régi sablonod, amit átírsz, akkor a Sablon frissítésére kattints, ne az új hozzáadására, mert akkor kettő lesz belőle. Ha megnyitottál egy kész sablont, de nem szeretnéd szerkeszteni, vagy teljesen új sablont akarsz készíteni, akkor kattints a mégse gombra. </td>
		</tr>
		<tr><form method="post" action="<?php echo $phpSelf?>?go=templates">
			<td>Kész sablonok:
			<?php
			$templates = file(ANSWERS);
			$numOfAnswers = count($templates);
			if ($numOfAnswers > 0) {
			echo '<select name="answerid">';
				foreach ($templates as $row) {
					list($id, $title) = get($row);
					if ($answerId == $id)
						$checked = ' selected="selected"';
					else
						$checked = "";
					echo '<option value="'.$id.'"'.$checked.'>'.$title.'</option>';
				}
			echo '</select>';
			} else {
				echo "Nincs";
			}
			?>
			<?php if ($numOfAnswers > 0) { ?>
				<input type="submit" name="open" value="Megnyit" />
				<input type="submit" name="delete" value="Töröl" />
			<?php } ?>
			</td>
		</tr>
		<tr>
			<td>Név: <input type="text" name="name" size="30" value="<?php echo $answerName?>" /></td>
		</tr>
		<tr>
			<td>Válasz: </td></tr>
		<tr>
			<td><textarea name="answertosave" cols="40" rows="5"><?php echo fixBrk($answerBody)?></textarea></td>
		</tr>
		<tr>
			<td>
				<input type="submit" name="clear" value="Mégse" />
				<input type="submit" name="add" value="Új hozzáadása" />
			<?php if ($numOfAnswers > 0) { ?>
				<input type="submit" onClick="return confirmUpdate()" name="update" value="Sablon frissítése" />
			<?php } ?>
			</form>	
				</td>
		</tr>
	<?php
	echo $FOOTER;
} else if ($_GET['go'] == 'config') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = Config
//		---------
//		Saved answered
//////////////////////////////////////////////////////////////////////////
	echo $HEADER;
	?>
		<tr>
			<td><h2>Beállítások</h2>
			<?php if ($_POST['configupdate'] != '') {
		$id = $_POST['id'];
		$newpass = $_POST['newpass'];
		if ($newpass != '') {
			$pass = strtolower($newpass);
			$pass = crypt($pass);
		} else {
			$pass = $_SESSION['password'];
		}
		$email = $_POST['emailaddy'];
		if ($email == '')
			$email = "@";
		$domainUrl = $_POST['domainurl'];
		if ($domainUrl == '')
			$domainUrl = 'http://'.$_SERVER['HTTP_HOST'].$phpSelf;
		$emailnotify = $_POST['emailnotify'];
		if ($emailnotify == '')
			$emailnotify = "no";
		$entryPerPage = $_POST['perpage'];
		if (!is_numeric($entryPerPage)) {
			$entryPerPage = $defaultEntryPerPage;
		}
		$dtFormat = $_POST['dtformat'];
		if ($dtFormat == '')
			$dtFormat = 'd-m-y';
		$display = $_POST['display'];
		$wdata = "$id\¦$pass\¦$domainUrl\¦$email\¦$emailnotify\¦$entryPerPage\¦$dtFormat\¦$display";
		replaceRow(CONFIGFILE, $id, $wdata);
		$_SESSION['password'] = $pass; 
		echo "<b>Sikeres változtatás.</b>";
		
	}
	list($id, $key, $domainUrl, $email, $notify, $entryPerPage, $dtFormat, $display) = getFileRow(CONFIGFILE);
	if ($domainUrl == '') {
		$domainUrl = "http://".$_SERVER['HTTP_HOST'].$phpSelf;
	}
	?>
			</td>
		</tr>
		<tr><form method="post" action="<?php echo $phpSelf?>?go=config">
			<input type="hidden" name="id" value="<?php echo $id?>" />
			<td width="40%">Jelszó megváltoztatása:</td>
			<td><input type="password" name="newpass" size="35" value="" /></td>
		</tr>
		<tr>
			<td>URL elérhetősége a lapnak [alap beállítás jó]:</td>
			<?php if (substr($domainUrl, -10) != substr($phpSelf, -10)) $domainUrl = 'http://'.$_SERVER['HTTP_HOST'].$phpSelf?>
			<td><input type="text" name="domainurl" size="35" value="<?php echo $domainUrl?>" /></td>
		</tr>
		<tr>
			<td>Email cím (nem kötelező):</td>
			<td><input type="text" name="emailaddy" size="35" value="<?php echo $email?>" /></td>
		</tr>
		<tr>
			<td>Küldjön nekem e-mailt, ha új kérdést kapok:</td><br>
			<?php if ($notify == 'yes') $checked = " checked=\"checked\""; ?>
			<td><input type="checkbox" name="emailnotify" value="yes"<?php echo $checked?> /> Igen, értesítsen</td>
		</tr>
		<tr>
			<td>Bejegyzések száma egy lapon:</td>
			<td><input type="text" name="perpage" value="<?php echo $entryPerPage?>" /></td>
		</tr>
		<tr>
			<td>Dátum formátum (Használd a PHP dátum formátumot): (<a href="http://au3.php.net/manual/en/function.date.php" target="_blank">?</a>)</td>
			<td><input type="text" name="dtformat" value="<?php echo $dtFormat?>" /></td>
		</tr>
		<tr>
			<td>Kérdések/válaszok megjelenítése:</td>
			<?php
			if ($display == 1) {
				$selected[1] = ' selected="selected"';
			} else if ($display == 2) {
				$selected[2] = ' selected="selected"';
			}
			?>
			<td><select name="display">
					<option value="1"<?php echo $selected[1]?>>Legújabb alul</option>
					<option value="2"<?php echo $selected[2]?>>Legújabb felül</option>
				</select></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="configupdate" value="Frissít" /></td>
		</tr>
		</form>
	<?php
	echo $FOOTER;
} else if ($_GET['go'] == 'logout') {
/////////////////////////////////////////////////////////////////////////
//
//		GO = LOG OUT
//		---------
//		Saved answered
//////////////////////////////////////////////////////////////////////////
	$_SESSION['password'] = "";
	header("Location: $phpSelf");
} else {
/////////////////////////////////////////////////////////////////////////
//
//		INDEX.PHP page --> LOGIN page
//		-----------------------------
//
/////////////////////////////////////////////////////////////////////////
$config = getFileRow(CONFIGFILE);
list($id, $key) = $config;
if ($_SESSION['password'] == $key) {
	header("Location: $phpSelf?go=home");
}
echo $TOP_LOGIN_FORM . $BOTTOM_LOGIN_FORM;
}
?>

