<?php
/*
 * Copyright (c) 2004-2009 Luiz Otavio O Souza <loos.br@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* 
 * $Id: chat.php 102 2009-02-05 18:37:40Z loos-br $
 */

    require('mysql.inc.php');
    require('header.inc.php');

?>

<body bgcolor="#FFFFFF">

<div align=center>
 <a href="javascript: window.close()">Fechar a janela</a><br><br>
 <table border=0 width=500
        style="border-color: gray; border-style: solid; border-width: 1;">
  <tr>
   <td align="center" bgcolor="#EBEBEB" width=120>Data</td>
   <td align="center" bgcolor="#EBEBEB" width=160>De</td>
   <td align="center" bgcolor="#EBEBEB" width=160>Para</td>
   <td align="center" bgcolor="#EBEBEB">Visualizar</td>
  </tr>
<?php
    $sql  = "SELECT DISTINCT sb_id, date, display_name, type ";
    $sql .= "FROM log WHERE ";
    $sql .= "email = '".$_REQUEST['email']."' AND (type = 'start' OR type = 'join')";
    $sql .= "ORDER BY date DESC ";

    $rs = mysql_query($sql, $mysql);

    while($rs && ($row = mysql_fetch_array($rs))) {

	$reset = array("sb_id", "date", "display_name", "type");
	for ($i = 0; $i < count($reset); $i++) {
	    if (!isset($row[$reset[$i]]))
		$row[$reset[$i]] = "";
	}

	$sql  = "SELECT email, display_name FROM log WHERE ";
	$sql .= "sb_id = ".$row['sb_id']." AND type = 'join' ";

	$rsTo = mysql_query($sql, $mysql);
?>
  <tr>
   <td bgcolor="#EBEBEB" align="center"><?php echo $row['date'] ?></td>
   <td bgcolor="#EBEBEB">
<?php
	if ($row['type'] == 'join') {
	    $sql  = "SELECT email, display_name FROM log WHERE ";
	    $sql .= "sb_id =". $row['sb_id']." AND type = 'start'";
	    $rsStart = mysql_query($sql, $mysql);
	    $rowStart = mysql_fetch_array($rsStart);

	    $reset = array("email", "display_name");
	    for ($i = 0; $i < count($reset); $i++) {
		if (!isset($rowStart[$reset[$i]]))
		    $rowStart[$reset[$i]] = "";
	    }
?>
	<a href="chat-to.php?email=<?php echo $_REQUEST['email'] ?>&to=<?php echo $rowStart['email'] ?>&back=on"><?php echo $rowStart['display_name'] ?></a></td>
<?php
	} else {
	    echo $row['display_name']."</td>";
	}
?>
   <td bgcolor="#EBEBEB">
<?php
	while($rsTo && ($rowTo = mysql_fetch_array($rsTo))) {

	    $reset = array("email", "display_name");
	    for ($i = 0; $i < count($reset); $i++) {
		if (!isset($rowTo[$reset[$i]]))
		    $rowTo[$reset[$i]] = "";
	    }

	    if ($rowTo['email'] == $_REQUEST['email']) {
		echo $rowTo['display_name']." <BR>\n";
	    } else {
		echo "<a href=\"chat-to.php?email=".$_REQUEST['email']."&to=".$rowTo['email']."&back=on\">".$rowTo['display_name']."</a><BR>\n";
	    }
	}
?>
   </td>
   <td align="center" bgcolor="#EBEBEB">
	<a href="view.php?sb_id=<?php echo $row['sb_id'] ?>&back=on"><img src="imagens/send_im.gif" border=0></a></td>
  </tr>
<?php
    }
    mysql_close($mysql);
?>
</table><br>
</div>

</body>
</html>

