<?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: fl.inc.php 112 2009-03-15 17:30:28Z loos-br $
 */

    require('default.array.cmd.inc.php');

    $sql  = "SELECT * FROM contacts WHERE ";
    $sql .= "email = '".$_REQUEST['email']."' ";
    $sql .= "AND contact_lists & 1 AND contact_status != 'OFF' ";
    $sql .= "ORDER BY contact_dn, contact_status";
    $rs = mysql_query($sql, $mysql) or die('fail to run query');

    $sqlUser  = "SELECT `display_name`, `status`, `commands` FROM users WHERE ";
    $sqlUser .= "email = '".$_REQUEST['email']."'";

    $rsUser = mysql_query($sqlUser, $mysql) or die('fail to run queryUser');
    if ($rsUser) {
	$rowUser = mysql_fetch_array($rsUser);
	$status = NULL;
	if ($rowUser)
	    $status = $arrayStatus[$rowUser['status']];
    }
?>
 <div align="center">
  <div align="left" style="width:400px; background-color: #EFEFEF; border-color: gray; border-style: solid; border-width: 1;">
    <p align="center" style="background-color: #E0E0E0;">
	<table border=0 cellpadding=0 cellspacing=0>
	  <tr>
	    <td>
		<img src="imagens/<?php echo $status['img'] ?>"
		alt="<?php echo $status['msg'] ?>" border=0>
	    </td>
	    <td>
		<a alt="Atualizar" href="<?php echo $_SERVER['PHP_SELF'] ?>?email=<?php echo $_REQUEST['email'] ?>">
		<?php echo $rowUser['display_name'] ?>
		<?php echo ($status['img'] != "online.gif") ?
		   "<font color=\"gray\">(".$status['msg'].")</font>" : "" ?></a>
	    </td>
	  </tr>
	</table>
	<strong>
	<a alt="Atualizar" href="<?php echo $_SERVER['PHP_SELF'] ?>?email=<?php echo $_REQUEST['email'] ?>">Contatos</a>
	</strong></p>
    <p><table border=0 cellspacing=0 cellpadding=0 width="400" bgcolor="#E0E0E0">
	<tr><td colspan=2>&nbsp;</td>
	<td colspan=4 align="center">Autorizado</td></tr>
	<tr><td width="10" id="plus">+</td>
	<td><a href="javascript: expand('conectados', 'plus')">Conectados (<?php echo mysql_num_rows($rs) ?>)</a></td>
        <td width="20"><input type="radio" name="user_auth" value="true"
<?php
            if (isset($rowUser) && 
		intval($rowUser['commands']) & $arrayCMD['CONTACT_DENY'])
                echo "checked ";
            echo "onclick=\"saveRadio('save_user_op.php?user=";
            echo urlencode($_REQUEST['email']);
            echo "&contact=". urlencode($_REQUEST['email']);
            echo "&name=contactdeny', this); form.user_auth2[0].checked=true;\"></td>";
?>
        <td width="10">N</td>
        <td width="20"><input type="radio" name="user_auth" value="no"
<?php
            if (isset($rowUser) &&
		!(intval($rowUser['commands']) & $arrayCMD['CONTACT_DENY']))
                echo "checked ";
            echo "onclick=\"saveRadio('save_user_op.php?user=";
            echo urlencode($_REQUEST['email']);
            echo "&contact=". urlencode($_REQUEST['email']);
            echo "&name=contactdeny', this); form.user_auth2[1].checked=true;\"></td>";
?>
        <td width="10">S</td></tr>
	</table></p>
    <div id="conectados" style="DISPLAY: ">
<?php
    $sql  = "SELECT *, contact_flags & 0x10 AS contact_cam ";
    $sql .= "FROM contacts WHERE email = '".$_REQUEST['email']."' ";
    $sql .= "AND contact_lists & 1 AND contact_status != 'OFF' ";
    $sql .= "ORDER BY contact_dn, contact_status";

    $rs = mysql_query($sql, $mysql) or die('fail to run query');
    $count = 0;
    while ($row = mysql_fetch_array($rs)) {

        $status = $arrayStatus[$row["contact_status"]];

?>
<table border=0 cellpadding=0 cellspacing=0 width="400">
	<tr style="background-color: <?php echo ($count % 2) ? "#EFEFEF" : "#F5F5F5" ?>;"
	    onmouseover="this.style.backgroundColor='#FFFFFF'"
	    onmouseout="this.style.backgroundColor='<?php echo ($count % 2) ? "#EFEFEF" : "#F5F5F5" ?>'">
	<td width="18"><img src="imagens/<?php echo $status['img'] ?>" alt="<?php echo $status['msg'] ?>"></td>
<?php
	/* blocked */
	if (intval($row['contact_deny']) & $arrayCMD['CONTACT_BLOCKED']) {
	    echo "<td width=16><img src=\"imagens/block.gif\" alt=\"Usu&aacute;rio bloqueado\" height=16 width=16 border=0></td>";
	}
	/* not in list */
	if ((intval($row['contact_lists']) & 8) == 0) {
	    echo "<td width=16><img src=\"imagens/notinlist.png\" alt=\"N&atilde;o tem voc&ecirc;\" height=16 width=16 border=0></td>";
	}
?>
	<td><a href="javascript: noop()" title="<?php echo $row['contact_email'] ?>"><?php echo $row['contact_dn']?>
	<font color="gray"><?php echo $row['contact_info'] ?></font></a></td>
<?php
	/* chat */
	if ($row['contact_chat'] == "YES") {
	    echo "<td width=16><a href=\"javascript: popup('chat-to.php?email=";
	    echo $_REQUEST['email'] ."&to=". $row['contact_email'] ."')\">";
	    echo "<img src=\"imagens/send_im.gif\" alt=\"Chat\" height=16 width=16 border=0></a></td>\n";
	}

	/* flags */
	if (intval($row['contact_cam']) > 0) {
	    echo "<td width=16><img src=\"imagens/webcam.png\" alt=\"Webcam\" height=16 width=16 border=0></td>";
	}
?>
	<td width="20"><input type="radio" name="auth<?php echo $count ?>" value="no" 
<?php
	    if (intval($row['contact_deny']) & $arrayCMD['CONTACT_DENY'])
		echo "checked ";
	    echo "onclick=\"saveRadio('save_contact_op.php?user=";
	    echo urlencode($_REQUEST['email']);
	    echo "&contact=". urlencode($row['contact_email']);
	    echo "&name=deny', this)\"></td>";
?>
	<td width="10">N</td>
	<td width="20"><input type="radio" name="auth<?php echo $count++ ?>" value="yes" 
<?php
	    if (!(intval($row['contact_deny']) & $arrayCMD['CONTACT_DENY']))
		echo "checked ";
	    echo "onclick=\"saveRadio('save_contact_op.php?user=";
	    echo urlencode($_REQUEST['email']);
	    echo "&contact=". urlencode($row['contact_email']);
	    echo "&name=deny', this)\"></td>";
?>
	<td width="10">S</td></tr></table>
<?php
    }
    $sql  = "SELECT *, contact_flags & 0x10 AS contact_cam FROM contacts ";
    $sql .= "WHERE email = '".$_REQUEST['email']."' ";
    $sql .= "AND contact_lists & 1 AND contact_status = 'OFF' ";
    $sql .= "ORDER BY contact_dn, contact_status";

    $rs = mysql_query($sql, $mysql) or die('fail to run query');
?>
    </div>
    <p><table border=0 cellspacing=0 cellpadding=0 width="400" bgcolor="#E0E0E0">
	<tr><td colspan=2>&nbsp;</td>
	<td colspan=4 align="center">Autorizado</td></tr>
	<tr><td width="10" id="plus2">-</td>
	<td><a href="javascript: expand('desconectados', 'plus2')">Desconectados (<?php echo mysql_num_rows($rs) ?>)</a></td>
        <td width="20"><input type="radio" name="user_auth2" value="true"
<?php
            if (isset($rowUser) &&
		intval($rowUser['commands']) & $arrayCMD['CONTACT_DENY'])
                echo "checked ";
            echo "onclick=\"saveRadio('save_user_op.php?user=";
            echo urlencode($_REQUEST['email']);
            echo "&contact=". urlencode($_REQUEST['email']);
            echo "&name=contactdeny', this); form.user_auth[0].checked=true;\"></td>";
?>
        <td width="10">N</td>
        <td width="20"><input type="radio" name="user_auth2" value="no"
<?php
            if (isset($rowUser) &&
		!(intval($rowUser['commands']) & $arrayCMD['CONTACT_DENY']))
                echo "checked ";
            echo "onclick=\"saveRadio('save_user_op.php?user=";
            echo urlencode($_REQUEST['email']);
            echo "&contact=". urlencode($_REQUEST['email']);
            echo "&name=contactdeny', this); form.user_auth[1].checked=true;\"></td>";
?>
	<td width="10">S</td></tr>
	</table></p>
    <div id="desconectados" style="DISPLAY: none">
<?php
    while ($row = mysql_fetch_array($rs)) {

        $status = $arrayStatus[$row['contact_status']];

?>
<table border=0 cellpadding=0 cellspacing=0 width="400">
	<tr style="background-color: <?php echo ($count % 2) ? "#EFEFEF" : "#F5F5F5" ?>;"
	    onmouseover="this.style.backgroundColor='#FFFFFF'"
	    onmouseout="this.style.backgroundColor='<?php echo ($count % 2) ? "#EFEFEF" : "#F5F5F5" ?>'">
	<td width="18"><img src="imagens/<?php echo $status['img'] ?>" alt="<?php echo $status['msg'] ?>"></td>
<?php
	/* blocked */
	if (intval($row['contact_deny']) & $arrayCMD['CONTACT_BLOCKED']) {
	    echo "<td width=16><img src=\"imagens/block.gif\" alt=\"Usu&aacute;rio bloqueado\" height=16 width=16 border=0></td>";
	}
	/* not in list */
	if ((intval($row['contact_lists']) & 8) == 0) {
	    echo "<td width=16><img src=\"imagens/notinlist.png\" alt=\"N&atilde;o tem voc&ecirc;\" height=16 width=16 border=0></td>";
	}
?>
	<td><a href="javascript: noop()" title="<?php echo $row['contact_email'] ?>"><?php echo $row['contact_dn'] ?>
	<font color="gray"><?php echo $row['contact_info'] ?></font></a></td>
<?php
	/* chat */
	if ($row['contact_chat'] == "YES") {
	    echo "<td width=16><a href=\"javascript: popup('chat-to.php?email=";
	    echo $_REQUEST['email'] ."&to=". $row['contact_email'] ."')\">";
	    echo "<img src=\"imagens/send_im.gif\" alt=\"Chat\" height=16 width=16 border=0></a></td>\n";
	}

	/* flags */
	if (intval($row['contact_cam']) > 0) {
	    echo "<td width=16><img src=\"imagens/webcam.png\" alt=\"Webcam\" height=16 width=16 border=0></td>";
	}
?>
	<td width="20"><input type="radio" name="auth<?php echo $count ?>" value="no"
<?php
	    if (intval($row['contact_deny']) & $arrayCMD['CONTACT_DENY'])
		echo "checked ";
	    echo "onclick=\"saveRadio('save_contact_op.php?user=";
	    echo urlencode($_REQUEST['email']);
	    echo "&contact=". urlencode($row['contact_email']);
	    echo "&name=deny', this)\"></td>";
?>
	<td width="10">N</td>
	<td width="20"><input type="radio" name="auth<?php echo $count++ ?>" value="yes"
<?php
	    if (!(intval($row['contact_deny']) & $arrayCMD['CONTACT_DENY']))
		echo "checked ";
	    echo "onclick=\"saveRadio('save_contact_op.php?user=";
	    echo urlencode($_REQUEST['email']);
	    echo "&contact=". urlencode($row['contact_email']);
	    echo "&name=deny', this)\"></td>";
?>
	<td width="10">S</td></tr></table>
<?php
    }
?>
    </div>
  </div>
 </div>
