<?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)
#############################################################################
# download_log.php: last changed 23/02/2005  , version: 1.0
#              by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: download_log.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"); // including database configuration file and connecting
mysql_connect("$db_server", "$db_username", "$db_password");
mysql_select_db("$db_name");

require ("../../core/session.php"); //build a session

if (IsSet ($_GET['logchat']))
{
	if (!get_magic_quotes_gpc())
	{
		$logchat = intval(addslashes($_GET['logchat']));
	}
	else
	{
		$logchat = intval($_GET['logchat']);
	}
}

if (IsSet ($_SESSION['userid']) AND ($_SESSION['usertype'] == "teacher" OR $_SESSION['user_type'] > 19))
{
	$chat_messagetable = $dbprefix . "m_chat_log";
	$chats_table = $dbprefix . "m_chat";

	$query = "SELECT name FROM $chats_table WHERE id='" . $logchat . "'"; // get chat name
	$result = mysql_query($query) or die_script($query, mysql_error());
	$chat_name = mysql_result($result, 0, 0);

	$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 ";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$data = "Chatlog for $chat_name\n";

	while ($message_list = mysql_fetch_array($result))
	{
		$msg = str_replace("<i>", "", $message_list[2]);
		$msg = str_replace("</i>", "", $msg);
		$msg = str_replace("&lt;", "", $msg);
		$msg = str_replace("&gt;", "", $msg);
		$data .= $message_list[1] . ":  " . $message_list[3] . " " . $msg . " " . $message_list[4] . "\n";
	}
	$filename = "chatlog_" . $chat_name . ".txt";
	header("Content-type: text/plain");
	header("Content-Disposition: attachment; filename=$filename");
	header("Content-Description: PHP Generated Data");
	echo $data;
}
?>  
