<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright (c) 2005 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------*/
require_once "../maincore.php";
require_once BASEDIR."subheader.php";
require_once ADMIN."navigation.php";
include LOCALE.LOCALESET."admin/shoutbox.php";

if (!checkrights("S")) fallback("../index.php");
if (isset($shout_id) && !isNum($shout_id)) fallback(FUSION_SELF);
if (!isset($action)) $action = "";

if ($action == "deleteshouts") {
	$deletetime = time() - ($_POST['num_days'] * 86400);
	$result = dbquery("SELECT * FROM ".$db_prefix."shoutbox WHERE shout_datestamp < '$deletetime'");
	$numrows = dbrows($result);
	$result = dbquery("DELETE FROM ".$db_prefix."shoutbox WHERE shout_datestamp < '$deletetime'");
	opentable($locale['400']);
	echo "<center><br>
$numrows ".$locale['401']."<br><br>
<a href='shoutbox.php'>".$locale['402']."</a><br><br>
<a href='index.php'>".$locale['403']."</a><br><br>
</center>\n";
	closetable();
} else if ($action == "delete") {
	opentable($locale['404']);
	$result = dbquery("DELETE FROM ".$db_prefix."shoutbox WHERE shout_id='$shout_id'");
	echo "<center><br>
".$locale['405']."<br><br>
<a href='shoutbox.php'>".$locale['402']."</a><br><br>
<a href='index.php'>".$locale['403']."</a><br><br>
</center>\n";
	closetable();
} else {
	if (isset($_POST['saveshout'])) {
		if ($action == "edit") {
			$shout_message = str_replace("\n", " ", $_POST['shout_message']);
			$shout_message = preg_replace("/^(.{255}).*$/", "$1", $shout_message);
			$shout_message = preg_replace("/([^\s]{25})/", "$1\n", $shout_message);
			$shout_message = stripinput($shout_message);
			$shout_message = str_replace("\n", "<br>", $shout_message);
			$result = dbquery("UPDATE ".$db_prefix."shoutbox SET shout_message='$shout_message' WHERE shout_id='$shout_id'");
			redirect("shoutbox.php");
		}
	}
	if ($action == "edit") {
		$result = dbquery("SELECT * FROM ".$db_prefix."shoutbox WHERE shout_id='$shout_id'");
		$data = dbarray($result);
		opentable($locale['420']);
		echo "<form name='editform' method='post' action='".FUSION_SELF."?action=edit&shout_id=".$data['shout_id']."'>
<table align='center' cellspacing='0' cellpadding='0'>
<tr>
<td class='tbl'>".$locale['421']."</td>
</tr>
<tr>
<td class='tbl'><textarea name='shout_message' rows='3' class='textbox' style='width:250px;'>".str_replace("<br>", "", $data['shout_message'])."</textarea></td>
</tr>
<tr>
<td align='center' class='tbl'><input type='submit' name='saveshout' value='".$locale['422']."' class='button'></td>
</tr>
</form>
</table>\n";
		closetable();
		tablebreak();
	}
	opentable($locale['440']);
	$result = dbquery("SELECT * FROM ".$db_prefix."shoutbox");
	$rows = dbrows($result);
	if (!isset($rowstart) || !isNum($rowstart)) $rowstart = 0;
	echo "<form name='deleteform' method='post' action='".FUSION_SELF."?action=deleteshouts'>\n<div align='center'>\n";
	if ($rows != 0) {
		if ($rowstart == 0) {
			$opts = "<option value='90'>90</option>
<option value='60'>60</option>
<option value='30'>30</option>
<option value='20'>20</option>
<option value='10'>10</option>\n";
			echo $locale['430']." <select name='num_days' class='textbox' style='width:50px'>$opts</select>".$locale['431']."<br><br>
<input type='submit' name='deleteshouts' value='".$locale['432']."' class='button'><br><hr>
</div>
</form>\n";
		}
		$i = 0;
		$result = dbquery(
			"SELECT * FROM ".$db_prefix."shoutbox LEFT JOIN ".$db_prefix."users
			ON ".$db_prefix."shoutbox.shout_name=".$db_prefix."users.user_id
			ORDER BY shout_datestamp DESC LIMIT $rowstart,20"
		);
		echo "<table width='100%' align='center' cellpadding='0' cellspacing='1' class='tbl-border'>\n";
		while ($data = dbarray($result)) {
			echo "<tr>\n<td class='".($i% 2==0?"tbl1":"tbl2")."'><span class='comment-name'>";
			if ($data['user_name']) {
				echo "<a href='".BASEDIR."profile.php?lookup=".$data['shout_name']."' class='slink'>".$data['user_name']."</a>";
			} else {
				echo $data['shout_name'];
			}
			echo "</span>
<span class='small'>".$locale['041'].showdate("longdate", $data['shout_datestamp'])."</span><br>
".str_replace("<br>", "", parsesmileys($data['shout_message']))."<br>
<span class='small'><a href='".FUSION_SELF."?action=edit&shout_id=".$data['shout_id']."'>".$locale['441']."</a> -
<a href='".FUSION_SELF."?action=delete&shout_id=".$data['shout_id']."'>".$locale['442']."</a> -
<b>".$locale['443'].$data['shout_ip']."</b></span>
</td>\n</tr>\n";
			$i++;
		}
		echo "</table>\n";
	} else {
		echo "<center><br>\n".$locale['444']."<br><br>\n</center>\n";
	}
	closetable();
	echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,20,$rows,3)."\n</div>\n";
}

echo "</td>\n";
require_once BASEDIR."footer.php";
?>