<?php
// /////////////////////////////////////////////
// ask_rave: QUESTION AND ANSWER SCRIPT
// Copyright 2004-2007: Some Rights Reserved
// url: http://raveraverave.co.uk
// /////////////////////////////////////////////
session_start(); // start session
if (file_exists("install.php")) { 
echo '<html><head><title>ask_rave - install.php still exists</title></head>
<body><font size="7" face="Bitstream Vera Sans, Tahoma, Verdana, Times New Roman, Arial">Install File Exists</font><br />
<font size="5" face="Bitstream Vera Sans, Tahoma, Verdana, Times New Roman, Arial">install.php still exists</font><br /><br />
<font size="3" face="Bitstream Vera Sans, Tahoma, Verdana, Times New Roman, Arial">Please either...
<ul><li>Run the install file, install.php, and then delete it</li><li>If you have already installed ask_rave, please 
delete install.php: it is a security risk</li></ul></font></body</html>'; exit; };

// Start
include_once ("start.php");

// data file checking
if (!file_exists("data.txt")) { die("Data file (data.txt) does not exist. Please reupload this file."); };
if (!file_exists("dataedit.txt")) { die("Data file (dataedit.txt) does not exist. Please reupload this file."); };

$pagewanted = $_SERVER['QUERY_STRING'];
if (!is_numeric($num_of_entries_per_page) && isset($num_of_entries_per_page)) { echo "'Number of Pages Per Page' value is not a number: please change this in the options or reinstall ask_rave. Default setting of twenty has been set."; $num_of_entries_per_page = "20"; };

if ($pagewanted == NULL) { $_SESSION['requestedpage'] = "1"; }
elseif (!is_numeric($pagewanted)) { echo "Query String Error: Page which was selected was not an integer, and therefore cannot be selected. Please go back and try again."; }
else { $_SESSION['requestedpage'] = $pagewanted; };

// ensure a display mode is set to 1 or 2
if ($displaymode != "1" && $displaymode != "2") { echo "Error: No display mode set. Assuming you mean two-column mode!"; $displaymode = "2"; };

// reversedata is automatically on!
$reversedata = "y";

// Find number of lines in data.txt
$lines14 = file("data.txt");
$lines = count($lines14);

// if there are less entries than required for a page, disable pages!
if ($lines <= $num_of_entries_per_page) { $usepages = "n"; };

// show script
if ($showscript == "y") {

// qinfo: questions asked, answered etc.
echo "<div class=\"qinfo\">\n";

$datafilesize = filesize("data.txt");
if ($datafilesize == "1") {$lines = "no";};
echo "Kopā ir $lines jautājumi.";
//if ($lines != "1") {echo "s"; };
//echo ", and has ";
//$editsize = filesize("dataedit.txt");
//$edlines2 = file("dataedit.txt");
//$edlines = count($edlines2);
//if ($editsize == "1") {echo "no questions to answer.";} else {echo "$edlines question"; if ($edlines != "1") {echo "s";}; echo " to answer.";};

if ($usepages == "y") {

		// set start entry and ending entry
		$currententry = $num_of_entries_per_page * $_SESSION['requestedpage'] - $num_of_entries_per_page;
		
		// increment cur entry to get starting entry
		$startqnum = $currententry + "1";
		
		// align current entry: again a piss attempt at a fix
		if ($currententry != "1") { $currententry++; }; // but it works ;)
		
		$maxentry = $num_of_entries_per_page * $_SESSION['requestedpage'];
		
		// is the last entry over the number of entries?
		if ($maxentry > $lines) { $maxentry = $lines; $endqnum = $maxentry; $maxentry++; } // correct the max amount!
		else { $endqnum = $maxentry; };
		
	// tell user what questions are being displayed
	echo "<br />";
	echo "You're currently viewing ";
	if ($_SESSION['requestedpage'] == "1") { echo "the latest $num_of_entries_per_page questions."; }
	else { echo " questions <b>$startqnum</b> through to <b>$endqnum</b>."; };
	
	echo "<br />Pages: ";
	
	$pages = $lines / $num_of_entries_per_page;
	if (!is_int($pages)) { 	// what a crazy fix... beautiful!
							$exploded = explode(".",$pages);
							$pages = $exploded[0] + 1;
						 };
	
	$loopnum = "1";
	while ($pages >= $loopnum)
	{
		if ($loopnum == $_SESSION['requestedpage']) {
			echo "<b>[$loopnum]</b> ";
		}
		else
		{
			echo "<a href=\"index.php?";
			echo $loopnum;
			echo "\">[$loopnum]</a> ";
		};
		$loopnum++;
	};
		
	echo "<br />";

};

echo "</div>";
echo "\n \n";

echo "<center><u><b><a href=\"ask.php\"><font color=\"#FF0000\">Ja vēlaties uzdot jautājumu, spiediet šeit.</font></a></b></u></center><br />";
echo "\n \n";

// open data.txt
$filename = "data.txt";
$handle = fopen($filename, "r");
$dfile = fread($handle, filesize($filename));
fclose($handle);

// are we using two-column or one-column mode?

if ($displaymode == "1") { // ONE-COLUMN!
	$dfile = str_replace("|~1|", "<table border=\"0\" class=\"bd\" style=\"align: center; width: 95%; margin-left: 2%; margin-right: 2%;\"><tr><td><b>J. ", $dfile);
	// --> Question Here
	$dfile = str_replace("|~2|", "</b></td></tr><tr><td>A. ", $dfile);
	// --> Answer Here
	$dfile = str_replace("|~3|", "</td></tr><tr><td><b>", $dfile);
	// --> Name Here
	$dfile = str_replace("|~4|", "</b> // ", $dfile);
	// --> Date Here
	$dfile = str_replace("|~5|", "</td></tr></table><br />", $dfile);
	// URL + EMAIL
	$dfile = str_replace("|~e1|", "<a href=\"mailto:", $dfile);
	$dfile = str_replace("|~e2|", "\">", $dfile);
	$dfile = str_replace("|~e3|", "</a>", $dfile);
	$dfile = str_replace("|~u1|", " // <a href=\"", $dfile);
	$dfile = str_replace("|~u2|", "\" target=\"blank\"><img src=\"uri.png\" alt=\"Universal Resource Indicator\" border=\"0\"/></a>", $dfile);
	// END URL + EMAIL
};

if ($displaymode == "2") { // TWO-COLUMN!
	echo "<table align=\"center\" width=\"90%\"><tr><td width=\"75%\" class=\"bd\">\n";
	echo "<b>Jautājumi</b> un atbildes.</td><td width=\"25%\" class=\"bd\"><b>Jautājumu pievienoja ...</b> ";
	if ($reversedata == "y") {echo "<sup>(asc.)</sup>";} else {echo "<sup>(desc.)</sup>";};
	echo "</td></tr>\n";
	
	// replace |~?| placeholders with tags, then echo it...
	$dfile = str_replace("|~1|", "<tr><td class=\"bd\"><b>J. ", $dfile);
	$dfile = str_replace("|~2|", "</b><br />A. ", $dfile);
	$dfile = str_replace("|~3|", "</td><td class=\"bd\">", $dfile);
	$dfile = str_replace("|~4|", "<br />Jautāja: ", $dfile);
	$dfile = str_replace("|~5|", "</td></tr>", $dfile);
	// URL + EMAIL
	$dfile = str_replace("|~e1|", "<a href=\"mailto:", $dfile);
	$dfile = str_replace("|~e2|", "\">", $dfile);
	$dfile = str_replace("|~e3|", "</a>", $dfile);
	$dfile = str_replace("|~u1|", "&nbsp;&nbsp;<a href=\"", $dfile);
	$dfile = str_replace("|~u2|", "\" target=\"blank\"><img src=\"uri.png\" border=\"0\" alt=\"Universal Resource Indicator\"></a>", $dfile);
	// END URL + EMAIL
}; // END TWO-COLUMN

$dfile = str_replace("a-n-s-w-e-r", "[This question has not been answered yet]", $dfile);
$dfile = stripslashes($dfile);

// Explode the data, reverse the arrays, flipping all the data... RECENT FIRST DUDES!
if ($reversedata == "y") {
$dfile = explode("\n", $dfile);
$dfile = array_reverse($dfile);
$dfile = implode("\n", $dfile);
};

// emoticons!
if ($emoenable == "y") {
// count emoticon arrays
$emocountC = count($emoticoncode); $emocountF = count($emoticonfile);
// quick check to ensure that both codes have filenames
if ($emocountF != $emocountC) { die("Fatal Error! Emoticons are turned on, however $yourname has not correctly filled in the emoticon data.<br />$yourname: You must ensure that there are an equal amount of emoticon codes to filenames!"); };
$emocount = "0";
while ($emocount < $emocountC) {
$dfile = str_replace($emoticoncode[$emocount], "<img src=\"$emoticonfile[$emocount]\" alt=\"$emoticoncode[$emocount]\" />", $dfile);
$emocount++;
};
};
// end emoticons

// using pages?
if ($usepages == "y") {
	$dfile = explode("\n", $dfile);
			
		while ($maxentry >= $currententry) 
			{
				echo $dfile[$currententry];
				$currententry++; // increment!
			};
		
} else {
	echo "$dfile"; // just echo, baby!
};


// if display mode is set to 2
if ($displaymode == "2") {
echo "\n</table>";
};

echo "\n\n";

// end of if script is on
} else {
	if ($nsnotice == NULL) {
		echo "The script has been disabled by $yourname.";
	} else {
		echo $nsnotice;
	};
};
	
// End
//include_once ("end.php");
?>