<?php
	/**************************************************************************\
	* phpGroupWare - Chat                                                      *
	* http://www.phpgroupware.org                                              *
	* This application written by Joseph Engo <jengo@phpgroupware.org>         *
	* --------------------------------------------                             *
	*  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.                                              *
	\**************************************************************************/

	/* $Id: messages.php,v 1.6.2.3 2003/07/02 20:45:25 skwashd Exp $ */

	$GLOBALS['phpgw_info']['flags'] = array(
		'currentapp' => 'chat',
		'noheader'   => True,
		'nonavbar'   => True
	);
	include('../header.inc.php');

	$loginid = $GLOBALS['phpgw_info']['user']['userid'];
	$refresh = 3;

	function get_var($name,$from)	// a simple version
	{
		if (!is_array($from)) $from = explode(',',$from);
		while ((list(,$f) = each($from)) && !isset($GLOBALS['HTTP_'.$f.'_VARS'][$name])) ;
		$ret = $GLOBALS['HTTP_'.$f.'_VARS'][$name];
		//echo "HTTP_$f"."_VARS[$name]=$ret<br>";
		return $ret;
	}
	$cannel   = get_var('channel',array('POST','GET'));
	$action   = get_var('action',array('POST','GET'));
	$location = get_var('location',array('POST','GET'));
	$user2    = get_var('user2',array('POST','GET'));
	
	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_chat_messages WHERE channel='$channel'");
	$GLOBALS['phpgw']->db->next_record();
	$status = $GLOBALS['phpgw']->db->f('status');
	$title = $GLOBALS['phpgw']->db->f('title');
	$user2 = $channel;

	// below line is abusively bad HTML form, but works in NS so far.
	echo "<html>\n<head>\n\t".
	     '<META HTTP-EQUIV="Refresh" Content="10; URL='.$GLOBALS['phpgw']->link('/chat/messages.php',array(
		'channel'  => $channel,
		'action'   => $action,
		'location' => $location,
		'unique'    => time()
	)).'#bottom">'."\n</head>\n<body>\n";

	echo "<center><strong>$loginid in $channel</strong></center>\n";

	if ($location == 'private')
	{
		$sql = "SELECT * FROM phpgw_chat_privatechat WHERE (user1='$loginid' AND user2='$user2') OR (user1='$user2' AND user2='$loginid')";
	}
	else
	{
		$sql = "SELECT * FROM phpgw_chat_messages WHERE channel='$channel' AND timesent>'" . $GLOBALS['phpgw_info']['user']['logintime']. "'";
	}
	$GLOBALS['phpgw']->db->query($sql.' ORDER BY timesent',__FILE__,__LINE__);

	while($GLOBALS['phpgw']->db->next_record())
	{
		echo '<font color="blue">' . 
		     $GLOBALS['phpgw']->db->f($location == 'privat' ? 'sentby' : 'loginid') . ' (' . 
		     $GLOBALS['phpgw']->common->show_date($GLOBALS['phpgw']->db->f('timesent'),'H:i.s') . 
		     '):  </font><font color="red">' . htmlentities(stripslashes($GLOBALS['phpgw']->db->f('message'))) . 
		     "</font><br>\n";
	}

	echo '<a name="bottom"></a>';
	echo "\n</body>\n</html>\n";
?>
