<?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
#############################################################################
# message.php: last changed 05/10/2006  , version: 2.4.10
#              by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: message.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
#
# 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.
##############################################################################
define('IN_SAS', true);

include ("../../configuration/database.inc.php");
mysql_connect("$db_server", "$db_username", "$db_password");
mysql_select_db("$db_name");
require ("../../core/session.php"); //build a session

if (!IsSet ($_SESSION['Chat_Username']))
{
	exit;
}

$chats_usertable = $dbprefix . "m_chat_users";
$language = $_SESSION['chat_language'];

if (file_exists("../../languages/" . $language . "/sn_chat/" . $language . ".php"))
{
	include ("../../languages/" . $language . "/sn_chat/" . $language . ".php");
}
else
{
	include ("../../languages/EN/sn_chat/EN.php");
}
?>
<html>
<head>
<?php


if (isset ($_GET['post']))
{
	echo '<script language="JavaScript1.1"><!--
				function goto_bottom() {
				    window.parent.chatlogs.scroll(0,1000000000);
				}
				//--></script>
				<script language="JavaScript1.2"><!--
				function goto_bottom() {
				    window.parent.chatlogs.scrollTo(0,1000000000);
				}
				//-->
		      </script>';

	echo '<script language="javascript">';
	if (@ ($_SESSION['to'] == '0') || @ empty ($_SESSION['to']))
	{
		if ($_GET['show_time'] == 1)
		{
			$message = '<font size=2>' . date("H:i:s") . ':</font><b> [' . $_SESSION['Chat_Username'] . ']</b>&nbsp;';
		}
		else
		{
			$message = '<b> [' . $_SESSION['Chat_Username'] . ']</b>&nbsp;';
		}
		$message2 = htmlspecialchars(strip_tags(stripslashes($_GET['message'])));
		$message2 = $message . '<font color=\\"' . @ ($_SESSION['color']) . '\\">' . $message2;
		$message2 .= '</font>';
		echo 'window.parent.chatlogs.thelement.innerHTML += "' . $message2 . '<br>";' . "\n";
		echo 'goto_bottom();' . "\n";

		if ($_GET['post'] == 2)
		{
			echo 'window.parent.users.location.reload();';
		}
	}
	else
	{
		if ($_GET['show_time'] == 1)
		{
			$message = '<font size=2>' . date("H:i:s") . ':</font><font color=\\"red\\"><b> [' . $_SESSION['Chat_Username'] . ']</b>&nbsp;<i>(to ' . @ ($_SESSION['to']) . ')</i></font>:&nbsp;';
		}
		else
		{
			$message = '<font color=\\"red\\"><b> [' . $_SESSION['Chat_Username'] . ']</b>&nbsp;<i>(to ' . @ ($_SESSION['to']) . ')</i></font>:&nbsp;';
		}
		$message2 = htmlspecialchars(strip_tags(stripslashes($_GET['message'])));
		$message2 = $message . '<font color=\\"' . @ ($_SESSION['color']) . '\\">' . $message2;
		$message2 .= '</font>';
		echo 'window.parent.chatlogs.thelement.innerHTML += "' . $message2 . '<br>";' . "\n";
		echo 'goto_bottom();' . "\n";
		if ($_GET['post'] == 2)
		{
			echo 'window.parent.users.location.reload();';
		}
	}
	echo '</script>';
}
?>
</head>
<body leftmargin="0" topmargin="0" rightmargin="0" onload="document.send.message.focus()">
<form action="post.php" method="POST" name="send">
<?php

echo "<input type='hidden' name='user' value='" . $_SESSION['Chat_Username'] . "'>";
echo "<input type='hidden' name='show_time' value='" . $_GET['show_time'] . "'>";

print "<center>";

$query = "SELECT status FROM $chats_usertable WHERE user='" . $_SESSION['Chat_Username'] . "'";
$result = mysql_query($query) or die_script($query, mysql_error());
$status = mysql_result($result, 0);
?>
<table cellpadding="2" cellspacing="2"><tr><td height="50" valign="left">
<b><?php echo $_SESSION['Chat_Username']; ?></b>: <input type="text" name="message" size="55" maxlength="155">
&nbsp;
<select name="to">
<option value="0" <?php if(@($_SESSION['to'] == 'everyone')) { echo 'selected'; } ?>><?php echo $sas_lang['chat_module_everyone']; ?></option>
<?php

$query = "SELECT user FROM $chats_usertable";
$result = mysql_query($query) or die_script($query, mysql_error());

if (mysql_num_rows($result))
{
	while ($users_list = mysql_fetch_array($result))
	{
		$dbuser = $users_list[0];
		if (strlen($dbuser) > 10)
		{
			$dbuser = substr($dbuser, 0, 8);
			$dbuser .= '..';
		}
		echo '<option value="' . $users_list[0] . '"';
		if (@ ($_SESSION['to'] == $users_list[0]))
		{
			echo ' selected';
		}
		echo '>' . $dbuser . '</option>' . "\n";
	}
}

print "</select>

</center>
</td></tr></table>
</form>
</body>
</html>";
?>
