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

    function print_user($row = NULL, $i = 0, $denymask = 0) {

	global $mysql, $arrayCMD, $arrayOption, $arrayStatus;
	if ($row == NULL) return;

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

        $sql = "SELECT DISTINCT id, since FROM sb WHERE email = '".$row['email']."'";
        $rs2 = mysql_query($sql, $mysql);
        $sb = mysql_num_rows($rs2);

        $sql  = "SELECT DISTINCT contact_chat FROM contacts WHERE ";
        $sql .= "email = '".$row['email']."' AND contact_chat = 'YES' ";
        $rsChat = mysql_query($sql, $mysql) or die ("SELECT FAIL: ($sql)");
        $rowChat = mysql_fetch_array($rsChat);

	/* check from default denymask */
	$DenyMask = intval($row['commands']);
	if (strcasecmp($row['connect'], "no") == 0)
            $DenyMask |= $arrayCMD[CONNECT];
	if (strcasecmp($row['save_msg'], "no") == 0)
            $DenyMask |= $arrayCMD[SAVE_MSG];
	if (strcasecmp($row['save_contacts'], "no") == 0)
            $DenyMask |= $arrayCMD[SAVE_CONTACTS];

	$DenyBold = ($DenyMask == $denymask) ? 0 : 1;
?>
<table border=0 cellpadding=0 cellspacing=0
	style="border-color: gray; border-style: solid; border-width: 1;">
  <tr><td>
   <table border=0 width=440>
    <tr align="center">
     <td bgcolor="#EBEBEB" width=440 colspan=3>MSN</td>
    </tr>
    <tr>
     <td bgcolor="#EBEBEB" colspan=3>
        <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 width="100%">
                <a href="user.php?email=<?php echo $row[0] ?>" title="<?php echo $row[0] ?>"><?php echo $row['display_name'] ?>
                <?php echo ($status['img'] != "online.gif") ?
                        "<font color=\"gray\">(".$status['msg'].")</font>" : "" ?></a>
            </td>
            <td>
<?php
        /* user has any chat ? */
        if ($rowChat[0] == "YES") {
?>
                <a href="javascript: popup('chat.php?email=<?php echo $row[0] ?>')"
		title="<?php echo $row[0] ?>"><img src="imagens/send_im.gif" border=0></a>
<?php
        }
?>
          </td>
          </tr>
        </table></td>
    </tr>
    <tr>
     <td bgcolor="#EBEBEB" align=center>Última conexão</td>
     <td bgcolor="#EBEBEB" align=center>Último IP</td>
     <td bgcolor="#EBEBEB" align=center>Excluir / Novo</td>
    </tr>
    <tr>
     <td bgcolor="#EBEBEB" align=center><?php echo $row['last_seen'] ?></td>
     <td bgcolor="#EBEBEB" align=center><?php echo $row['last_addr'] ?></td>
     <td bgcolor="#EBEBEB" align=center><a
        href="javascript: showOptions('user_op_<?php echo $i ?>');
        showOptions('user_op_<?php echo $i + 1 ?>');
        showOptions('user_op_<?php echo $i + 2 ?>');"
	><?php echo $DenyBold ? "<b>" : "" ?>Outras opções<?php echo $DenyBold ? "</b>" : "" ?></a></td>
    </tr>
        <tr id="user_op_<?php echo $i ?>" style="DISPLAY: none;">
          <td bgcolor="#EBEBEB" align=center>Conecta</td>
          <td bgcolor="#EBEBEB" align=center>Salva mensagens</td>
          <td bgcolor="#EBEBEB" align=center>Salva contatos</td>
        </tr>
        <tr id="user_op_<?php echo $i + 1 ?>" style="DISPLAY: none;">
          <td bgcolor="#EBEBEB" align=center>
	    <select name="connect" onchange="javascript: saveSelect('save_user_op.php?user=<?php echo $row['email'] ?>&name=connect', this)">
              <option value="yes" <?php echo ($row['connect'] == "YES") ? "selected" : "" ?>>Sim
              <option value="no" <?php echo ($row['connect'] == "NO") ? "selected" : "" ?>>Não
            </select>
          </td>
          <td bgcolor="#EBEBEB" align="center">
	    <select name="save_msg" onchange="javascript: saveSelect('save_user_op.php?user=<?php echo $row['email'] ?>&name=save_msg', this)">
              <option value="yes" <?php echo ($row['save_msg'] == "YES") ? "selected" : "" ?>>Sim
              <option value="no" <?php echo ($row['save_msg'] == "NO") ? "selected" : "" ?>>Não
            </select>
          </td>
          <td bgcolor="#EBEBEB" align="center">
	    <select name="save_contacts" onchange="javascript: saveSelect('save_user_op.php?user=<?php echo $row['email'] ?>&name=save_contacts', this)">
              <option value="yes" <?php echo ($row['save_contacts'] == "YES") ? "selected" : "" ?>>Sim
              <option value="no" <?php echo ($row['save_contacts'] == "NO") ? "selected" : "" ?>>Não
            </select>
          </td>
        </tr>
        <tr id="user_op_<?php echo $i + 2 ?>" style="DISPLAY: none;">
          <td bgcolor="#EBEBEB" colspan=3 align=center>Outras Opções<br>
                <table border=0 width="100%">
<?php
	$o = 0;
	for(;;) {

	    if (!isset($arrayOption[$o]))
		break;

	    print_option($i, $o, $row['commands'], $row['email']);
	    $o += 2;
	}
?>
                </table>
          </td>
        </tr>
<?php
        if ($sb > 0) {
?>
    <tr>
     <td colspan=5>
      <table border=0 width="100%">
       <tr>
        <td bgcolor="#EBEBEB" align="center" width=440>Chats</td>
       </tr>
<?php
            while (($row2 = mysql_fetch_array($rs2))) {

                $sql  = "SELECT DISTINCT(email), display_name FROM log ";
                $sql .= "WHERE (type = 'join' OR type = 'start') AND ";
                $sql .= "sb_id =". $row2['id'] ." AND ";
                $sql .= "email <> '".$row['email']."' ";
                $rsSB = mysql_query($sql, $mysql);
?>
       <tr>
        <td bgcolor="#EBEBEB">
<?php
                while (($rowSB = mysql_fetch_array($rsSB))) {

                    $sql  = "SELECT * FROM contacts WHERE ";
                    $sql .= "email = '".$row['email']."' AND ";
                    $sql .= "contact_email = '".$rowSB['email']."' ";
                    $rsContact = mysql_query($sql, $mysql);
                    $rowContact = mysql_fetch_array($rsContact);
                    $statusContact = $arrayStatus[$rowContact["contact_status"]];
?>

        <table border=0 cellpadding=0 cellspacing=0>
          <tr>
            <td>
                <img src="imagens/<?php echo $statusContact['img'] ?>" alt="<?php echo $statusContact['msg'] ?>" border=0>
            </td>
            <td>
                <a href="javascript: popup2('<?php echo $row2['id'] ?>')" title="<?php echo $rowSB['email'] ?>">
                <?php echo $rowSB['display_name'] ?>
                <?php echo ($statusContact['img'] != "online.gif") ?
                        "<font color=\"gray\">(".$statusContact['msg'].")</font>" : "" ?></a><br>
            </td>
          </tr>
        </table>
<?php
                }
                mysql_free_result($rsSB);
?>
        </td>
       </tr>
<?php
            }
?>
      </table>
     </td>
    </tr>
<?php
        }
?>
   </table>
   </td>
  </tr>
 </table>
<?php
    }

    function print_option( $i, $o, $commands, $email) {

	global $arrayCMD, $arrayOption;

	$c    = $o;
	$desc = $arrayOption[$c]['desc'];
	$name = $arrayOption[$c]['name'];
	$cmd  = $arrayOption[$c]['cmd'];
	$id   = $name ."_". ($i + 2);
?>
		  <tr>
		    <td><input type=checkbox value="<?php echo $name ?>" id="<?php echo $id ?>"
			<?php echo ($commands & $arrayCMD[$cmd]) ? "checked" : "" ?>
<?php
	$link  = "javascript: saveCheckBox('save_user_op.php?user=" . $email . "&name=";
	$link .= $name ."', this)";;
?>
			onclick="<?php echo $link ?>">
		    </td><td width="45%"><label for="<?php echo $id ?>"><?php echo $desc ?></label></td>
<?php
	$c    = $o + 1;
	if ($arrayOption[$c]) {
	    $desc = $arrayOption[$c]['desc'];
	    $name = $arrayOption[$c]['name'];
	    $cmd  = $arrayOption[$c]['cmd'];
	    $id   = $name ."_". ($i + 2);
?>
		    <td><input type=checkbox value="<?php echo $name ?>" id="<?php echo $id ?>"
			<?php echo ($commands & $arrayCMD[$cmd]) ? "checked" : "" ?>
<?php
	$link  = "javascript: saveCheckBox('save_user_op.php?user=" . $email . "&name=";
	$link .= $name ."', this)";
?>
			onclick="<?php echo $link ?>">
		    </td><td width="45%"><label for="<?php echo $id ?>"><?php echo $desc ?></label></td>
		  </tr>
<?php
	} else {
?>
		    <td>&nbsp;</td>
		    <td>&nbsp;</td>
		  </tr>
<?php
	}
    }
?>
