<?php

#############################################################################
#                              Site@School Chat module
# Site@School is a website management system designed for schools.
# This is a chat module which can be used in Site@School.
#
# coded by Fred Stuurman (fc.stuurman@hccnet.nl)
#############################################################################
#		based on 	CHATNESS v2		 see http://www.chatness.us/
#		BY: Stephen Craton
#   Altered for Site@School with consent of the author
#############################################################################
# admin.php: last changed 31/05/2005  version 1.0.3
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: admin.php,v 1.1 2006/10/03 13:58:57 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002, 2003, 2004 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}
if (IsSet ($_GET['addchat']))
{
	$addchat = $_GET['addchat'];
}
if (IsSet ($_GET['editchat']))
{
	$editchat = $_GET['editchat'];
}
if (IsSet ($_GET['savechat']))
{
	$savechat = $_GET['savechat'];
}
if (IsSet ($_GET['deletechat']))
{
	$deletechat = $_GET['deletechat'];
}
if (IsSet ($_GET['logchat']))
{
	$logchat = $_GET['logchat'];
}
if (IsSet ($_GET['modoption']))
{
	$modoption = $_GET['modoption'];
}

if (IsSet ($_POST['name']))
{
	$name = $_POST['name'];
}
if (IsSet ($_POST['chat_width']))
{
	$chat_width = $_POST['chat_width'];
}
if (IsSet ($_POST['chat_height']))
{
	$chat_height = $_POST['chat_height'];
}
if (IsSet ($_POST['is_public']))
{
	$is_public = $_POST['is_public'];
}
if (IsSet ($_POST['password']))
{
	$password = $_POST['password'];
}
if (IsSet ($_POST['refresh']))
{
	$refresh = $_POST['refresh'];
}
if (IsSet ($_POST['message_list']))
{
	$message_list = $_POST['message_list'];
}
if (IsSet ($_POST['show_time']))
{
	$show_time = $_POST['show_time'];
}
if (IsSet ($_GET[b]))
{
	$b = $_GET[b];
}
if (IsSet ($_GET[l]))
{
	$l = $_GET[l];
}
if (IsSet ($_GET[number]))
{
	$number = $_GET[number];
}

if (!get_magic_quotes_gpc())
{
	$welcome = addslashes($_POST['welcome']);
	$title = addslashes($_POST['title']);
}
else
{
	$welcome = $_POST['welcome'];
	$title = $_POST['title'];
}

load_language('', 'sn_chat'); //load language file

$query = "SELECT name FROM $table_modules where id=$module"; //get the module name                
$result = mysql_query($query);
$modulename = mysql_result($result, 0);
unset ($query);
unset ($result);

$chat_userstable = $dbprefix . "m_chat_users";
$chat_table = $dbprefix . "m_chat";
$chat_messagetable = $dbprefix . "m_chat_log";

include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");
print "<span class=\"heading\">$modulename<p></span>";

if ($modoption == "deletemessages")
{
	foreach ($message_list as $value)
	{
		mysql_query("DELETE FROM $chat_messagetable WHERE id='$value'");
	}
}

if (IsSet ($addchat))
{
	print "<p><span class=\"text\"><b>" . $sas_lang['chat_module_add'] . "</b><br></span>";
	print "<FORM METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&savechat=new\"><br>";

	print "<span class=\"text\">" . $sas_lang['chat_module_title'] . "<br>
	       <input type=text name=\"name\" size=40 class=\"textfield\">&nbsp;<img src=graphics/help.gif 
	       ONMOUSEOVER=\"popup('" . $sas_lang['title_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
	print $sas_lang['chat_module_welcome_login'] . "<br><textarea name=\"title\" ROWS=2 COLS=60></textarea><br><br>";
	print $sas_lang['chat_module_welcome_page'] . "<br><textarea name=\"welcome\" ROWS=2 COLS=60></textarea><br><br>";
	print "<span class=\"text\">" . $sas_lang['chat_module_width'] . " <input type=text name=\"chat_width\" size=5 class=\"textfield\"><br><br>";

	print "<span class=\"text\">" . $sas_lang['chat_module_height'] . " <input type=text name=\"chat_height\" size=5 class=\"textfield\"><br><br>";
	print "<span class=\"text\">" . $sas_lang['chat_module_public'] . "  &nbsp;" . $sas_lang['yes'] . "";

	print "<input type=\"radio\" value=\"1\" name=\"is_public\">";
	print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
	print "<input type=\"radio\" name=\"is_public\" value=\"0\" checked>";

	print "<p><span class=\"text\">" . $sas_lang['chat_module_password'] . " <input type=text name=\"password\" size=15 class=\"textfield\"><br>";
	print "<p><span class=\"text\">" . $sas_lang['chat_module_refresh'] . " <input type=text name=\"refresh\" size=5 class=\"textfield\"><br>";
	print "<p><span class=\"text\">" . $sas_lang['chat_module_show_time'] . "  &nbsp;" . $sas_lang['yes'] . "";
	print "<input type=\"radio\" value=\"1\" name=\"show_time\" checked>";
	print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
	print "<input type=\"radio\" name=\"show_time\" value=\"0\">";

	print "<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\"
	       onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"cancelbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</span></form>";

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($editchat))
{
	$query = "SELECT id, name,title, welcome, width, height, public, password, refresh, options FROM $chat_table WHERE id='$editchat'";
	$result = mysql_query($query);
	$chat_list = mysql_fetch_array($result);
	print "<span class=\"text\"><b>" . $sas_lang['chat_module_edit'] . "</b><br></span><br>";

	print "<FORM METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&savechat=$editchat\"><br>";

	print "<span class=\"text\">" . $sas_lang['chat_module_title'] . "<br>
	<input type=text name=\"name\" size=40 value=\"$chat_list[1]\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['title_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
	print $sas_lang['chat_module_welcome_login'] . "<br><textarea name=\"title\" ROWS=2 COLS=60>$chat_list[2]</textarea><br><br>";
	print $sas_lang['chat_module_welcome_page'] . "<br><textarea name=\"welcome\" ROWS=2 COLS=60>$chat_list[3]</textarea><br><br>";
	print "<span class=\"text\">" . $sas_lang['chat_module_width'] . " <input type=text name=\"chat_width\" value=\"$chat_list[4]\" size=5 class=\"textfield\"><br><br>";

	print "<span class=\"text\">" . $sas_lang['chat_module_height'] . " <input type=text name=\"chat_height\" value=\"$chat_list[5]\" size=5 class=\"textfield\"><br><br>";
	print "<span class=\"text\">" . $sas_lang['chat_module_public'] . "  &nbsp;" . $sas_lang['yes'] . "";
	if ($chat_list[6] == "1")
	{
		print "<input type=\"radio\" value=\"1\" name=\"is_public\" checked>";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"is_public\" value=\"0\">";
	}
	else
	{
		print "<input type=\"radio\" value=\"1\" name=\"is_public\">";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"is_public\" value=\"0\" checked>";
	}
	print "<p><span class=\"text\">" . $sas_lang['chat_module_password'] . " <input type=text name=\"password\" value=\"$chat_list[7]\" size=15 class=\"textfield\"><br>";
	print "<p><span class=\"text\">" . $sas_lang['chat_module_refresh'] . " <input type=text name=\"refresh\" value=\"$chat_list[8]\" size=5 class=\"textfield\"><br>";
	print "<p><span class=\"text\">" . $sas_lang['chat_module_show_time'] . "  &nbsp;" . $sas_lang['yes'] . "";
	if ($chat_list[9] == "1")
	{
		print "<input type=\"radio\" value=\"1\" name=\"show_time\" checked>";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"show_time\" value=\"0\">";
	}
	else
	{
		print "<input type=\"radio\" value=\"1\" name=\"show_time\">";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"show_time\" value=\"0\" checked>";
	}

	print "<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\"
	       onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"cancelbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</span></form>";

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($savechat))
{
	if ($savechat == "new")
	{
		mysql_query("INSERT INTO $chat_table (id, name, title, welcome, width, height, public, password, refresh, options) 
		     VALUES (NULL, '$name', '$welcome', '$title', '$chat_width', '$chat_height', '$is_public', '$password', '$refresh', '$show_time')");
	}
	else
	{
		mysql_query("UPDATE $chat_table SET name='$name' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET welcome='$welcome' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET title='$title' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET width='$chat_width' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET height='$chat_height' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET public='$is_public' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET password='$password' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET refresh='$refresh' WHERE id='$savechat'");
		mysql_query("UPDATE $chat_table SET options='$show_time' WHERE id='$savechat'");
	}
}

if (IsSet ($deletechat))
{
	mysql_query("DELETE from $chat_messagetable WHERE room = '$deletechat'");
	mysql_query("DELETE from $chat_userstable WHERE room = '$deletechat'");
	mysql_query("DELETE from $chat_table WHERE id = '$deletechat'");
}

if (IsSet ($logchat))
{
?>
<script>
    function checkUncheckAll(theElement) 
    {
	 //documentation for this script at http://www.shawnolson.net/a/693/
     var theForm = theElement.form, z = 0;
     while (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
      theForm[z].checked = theElement.checked;
      z++;
     }
    }
</script>
<?php

	if ($b == "")
	{
		$b = 1;
	}

	if (IsSet ($number))
	{
		$number_of_items = $number;
	}
	elseif (IsSet ($_POST['number_of_items'])) //user gave a new value
	{
		$number_of_items = $_POST['number_of_items'];
	}
	else
	{
		$number_of_items = 20;
	}

	if (!IsSet ($l))
	{
		$l = $number_of_items;
	}
	$result = mysql_query("SELECT id FROM $chat_messagetable WHERE room = '$logchat'");
	$aantal = mysql_num_rows($result); //number of chat entries

	$limit_begin = $b -1;

	$query = "SELECT name FROM $chat_table WHERE id='$logchat'";
	$result = mysql_query($query);
	$chatname = mysql_result($result, 0);

	print "<span class=\"text\">" . $sas_lang['chat_module_log'] . " " . $sas_lang['chat_module_for_chat'] . "<b> $chatname</b></span><p>";
	//$query = "SELECT MessageID,DATE_format(Posted,'%d-%m-%Y %H:%i:%s') AS display_date,Message,username,UserID FROM $chat_messagetable WHERE room = '$logchat' ORDER BY Posted";
	$query = "SELECT id,DATE_format(posted,'%d-%m-%Y %H:%i:%s') AS display_date, Text, user, ip FROM $chat_messagetable 
	  WHERE room = '$logchat' ORDER BY posted DESC LIMIT $limit_begin,$number_of_items";
	$result = mysql_query($query);

	$i = 0;
	$pagtot = ceil($aantal / $number_of_items); //number of pages
	if ($b == 1)
	{
		$pag = 1;
	} //page 1
	else
	{
		$pag = $l / $number_of_items;
	} //different as page 1

	if ($aantal < $l)
	{
		$l = $aantal;
	} //less records than the number per page

	$low = $b - $number_of_items;

	if ($b > $number_of_items) //we have more than one page
	{
		$high = $b -1;
		$previous_page = "<a STYLE=\"color: blue ; font-size: 12\" href=\"index.php?option=modulemanager&module=$module&logchat=$logchat&b=$low&l=$high&number=$number_of_items\">" . $sas_lang['chat_module_previous_page'] . "</a>";
	}
	else
	{
		$previous_page = ''; //no previous page
	}
	if ($aantal > $l) //number of records biggter than $l
	{
		$low = $b + $number_of_items; //$b + number per page
		$high = $l + $number_of_items; //$l + number per page
		$next_page = "<a STYLE=\"color: blue ; font-size: 12\" href=\"index.php?option=modulemanager&module=$module&logchat=$logchat&b=$low&l=$high&number=$number_of_items\">" . $sas_lang['chat_module_next_page'] . "</a>";
	}
	else
	{
		$next_page = ''; //no next page
	}
	print "<form METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&logchat=$logchat\">
	         <span class=\"text\"><b>" . $sas_lang['chat_module_number_of_items'] .
	" </span><input type=\"text\" value=\"$number_of_items\" name=\"number_of_items\" size=\"5\"></form>";
	print "<FORM METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&logchat=$logchat&modoption=deletemessages&b=$low&l=$high&number=$number_of_items\"><br>";

	print "<table width=\"100%\">
	         <tr><td>&nbsp;</td><td width=\"23%\"><span class=\"text\"><b>" . $sas_lang['date'] . ":</b></span></td>
	         <td width=\"17%\"><span class=\"text\"><b>" . $sas_lang['username'] . ":</b></span></td>
	         <td width=\"45%\"><span class=\"text\"><b>" . $sas_lang['chat_module_messages'] . ":</b></span></td>
	         <td width=\"15%\"><span class=\"text\"><b>" . $sas_lang['chat_module_ip'] . ":</b></span></td></tr>";
	$i = 0;
	while ($message_list = mysql_fetch_array($result))
	{
		if ($message_list[3] > 9000)
		{
			$username = $message_list[3] . " (" . $sas_lang['chat_module_guest'] . ")";
		}
		else
		{
			$username = $message_list[3];
		}
		print "<tr><td valign=top width=\"30\"><INPUT type=checkbox name=message_list[$i] value=\"$message_list[0]\"></td>
					 <td valign=top><span class=\"text\">$message_list[1]</span></td>
					 <td valign=top><span class=\"text\">$username</span></td>
					 <td valign=top><span class=\"text\">$message_list[2]</span></td>
					 <td valign=top><span class=\"text\">$message_list[4]</span></td></tr>";
		$i++;
	}
	print "<tr><td colspan=\"5\"><table width=\"100%\"><tr>
	          <td align=\"left\">$previous_page&nbsp;</td>
	          <td width='28%' align=\"right\">&nbsp$next_page</td>
	          </tr></table></td></tr>";
	print "<tr><td colspan=\"5\"><input type=\"button\" value=\"" . $sas_lang['chat_module_download'] . "\" name=\"download\" 
	          onClick=\"javascript:parent.location='modules/sn_chat/download_log.php?logchat=$logchat';\">&nbsp;</td></tr>
	          <tr><td colspan=\"2\">&nbsp;</td>
	          <td colspan=\"3\"><input type=\"checkbox\" name=\"checkall\" 
	          onclick=\"checkUncheckAll(this);\"/><span class=\"text\">" . $sas_lang['chat_module_selectall'] . "&nbsp;</span>
	          <input type=\"submit\" value=\"" . $sas_lang['delete'] . "\" name=\"savebutton\"></td></tr>
	          </form></table>";
	print "\n<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
	       onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"backbutton\">";
}
else
{
	print "<span class=\"text\">" . $sas_lang['chat_module_intro'] . "</span><p>";

	$query = "SELECT id, name FROM $chat_table";
	$result = mysql_query($query);
	if (mysql_num_rows($result) > 0)
	{
		while ($chat_list = mysql_fetch_array($result))
		{
			print "<br><table border=\"0\">
						 <tr>
						 <td><img border=\"0\" src=\"graphics/delete.gif\" alt=\"" . $sas_lang['delete'] . "\" title=\"" . $sas_lang['delete'] . "\" 
						 onClick=\"if(confirm('" . $sas_lang['chat_module_delete_confirm'] . "')) parent.location='index.php?option=modulemanager&module=$module&deletechat=$chat_list[0]';\">
						 <img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\" 
						 onClick=\"parent.location='index.php?option=modulemanager&module=$module&editchat=$chat_list[0]'\">
						 <img border=\"0\" src=\"graphics/logfile.gif\" alt=\"" . $sas_lang['chat_module_log'] . "\" title=\"" . $sas_lang['chat_module_log'] . "\" 
						 onClick=\"parent.location='index.php?option=modulemanager&module=$module&logchat=$chat_list[0]'\">
						 </td>
						 <td><span class=link><a href=\"index.php?option=modulemanager&module=$module&editchat=$chat_list[0]\">$chat_list[1]</a></span></td>
						 </tr></table>";
			print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\">";

			print "</table>";
		}
	}
	else
	{
		print "<span class=text>" . $sas_lang['chat_module_add_category_warning'] . "</span><br>";
	}
	print "<p><img border=\"0\" src=\"graphics/document.gif\">
	       <span class=\"link\"><a href=\"index.php?option=modulemanager&module=$module&addchat=1\">" . $sas_lang['chat_module_add'] . "</a></span>";
	print "\n<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
	       onClick=\"javascript:parent.location='index.php?option=modulemanager';\" class=\"backbutton\">";

}
include ("interface/pageend.inc.php");
include ("interface/footer.inc.php");
?>
