<?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: index.php 102 2009-02-05 18:37:40Z loos-br $
 */

    require('default.array.option.inc.php');
    require('default.array.status.inc.php');
    require('default.array.cmd.inc.php');
    require('header.inc.php');
    require('mysql.inc.php');

    $sql  = "SELECT connect, save_msg, save_contacts, commands ";
    $sql .= "FROM defaults LIMIT 1";
    $rs = mysql_query($sql, $mysql) or die('falha no sql');
    $DENYMASK = 0;
    if (mysql_num_rows($rs) > 0) {
	$row = mysql_fetch_array($rs) or die("falha no result do sql");
	$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'];
    }

    $sqloffl  = "SELECT email, display_name, status, ";
    $sqloffl .= "last_seen, last_addr, connect, ";
    $sqloffl .= "save_msg, save_contacts, commands ";
    $sqloffl .= "FROM users WHERE status = 'OFF' ";
    $sqloffl .= "ORDER BY display_name, email";

    $sql  = "SELECT email, display_name, status, ";
    $sql .= "last_seen, last_addr, connect, ";
    $sql .= "save_msg, save_contacts, commands ";
    $sql .= "FROM users WHERE status != 'OFF' ";
    $sql .= "ORDER BY status, display_name, email";

    $rs = mysql_query($sql, $mysql) or die('falha no sql');
    $rsoffl = mysql_query($sqloffl, $mysql) or die('falha no sqloffl');

    $count = mysql_num_rows($rs);
    $countoffl = mysql_num_rows($rsoffl);

    require("user.inc.php");
    require("js.inc.php");
?>

<script language="JavaScript">
<!--

    function popup(url) {
	window.open(url, '', 'height=450,width=550,scrollbars=yes');
    }

    function popup2(sb_id) {
	url = 'view.php?sb_id=' + sb_id;
	window.open(url, '', 'height=450,width=550,scrollbars=yes');
    }

    function showOptions(id) {
	var itm = null;

	if (document.getElementById) {
	    itm = document.getElementById(id);
	} else if (document.all){
	    itm = document.all[id];
	} else if (document.layers){
	    itm = document.layers[id];
	}
        
	if (!itm) {
	    // do nothing
	}
	else if (itm.style) {
	    if (itm.style.display == 'none') { itm.style.display = ''; }
	    else { itm.style.display = 'none'; }
	}
	else { itm.visibility = 'show'; }
    }

//-->
</script>

<body bgcolor="#FFFFFF">

<form action="index.php" method="POST" name="form">
<input type="hidden" name="force">
<div align="center">
  <div align=center style="width: 892px; border-color: gray; border-style: solid; border-width: 1;">
	<p><strong>MSN Proxy - Usu&aacute;rios</strong></p>
	<p><a href="conf.php">Configuraçőes</a></p>
	<p>
<?php
    if ($count == 0) {
	echo "Nenhum usu&aacute;rio conectado<br>\n";
    } else {
	echo $count ." Usuário";
	echo ($count > 1) ? "s" : "";
	echo " conectado";
	echo ($count > 1) ? "s": "";
	echo "<br>\n";
    }
    if ($countoffl == 0) {
	echo "Nenhum usu&aacute;rio desconectado<br>\n";
    } else {
	echo $countoffl ." Usuário";
	echo ($countoffl > 1) ? "s" : "";
	echo " desconectado";
	echo ($countoffl > 1) ? "s" : "";
	echo "<br>\n";
    }
?>
	</p>
	<p><input type="checkbox" name="showoffline" id="off"
	onclick="form.force.value='1';form.submit();"
<?php
    $showoffline = "off";
    if ((isset($_REQUEST['showoffline']) && $_REQUEST['showoffline'] === "on" &&
	isset($_REQUEST['force']) && $_REQUEST['force'] === "1") ||
	(!isset($_REQUEST['force']) && isset($_SESSION['showoffline']))) {
	echo " checked ";
	$showoffline = "on";
	if (!isset($_SESSION['showoffline']))
	    $_SESSION["showoffline"] = "on";
    }
    if ($showoffline === "off") {
	if (isset($_SESSION['showoffline']))
	    unset($_SESSION['showoffline']);
    }
?>>
	<label for="off">Mostrar usuários offline</label></p>
</div><br>

<?php
    $i = 1;
    $stop = 0;
    while($stop == 0 && ($row = mysql_fetch_array($rs))) {
?>
<table border=0 width="600">
 <tr>
  <td valign="top" align="center">
<?php
	print_user($row, $i, $DENYMASK);
	$row2 = mysql_fetch_array($rs);
	if ($row2 == NULL) {
?>
  </td>
  <td valign="top" align="center">
	&nbsp;
<?php
	    $stop = 1;
	} else {
?>
  </td>
  <td valign="top" align="center">
<?php
	    $i += 3;
	    print_user($row2, $i, $DENYMASK);
	}
?>
  </td>
 </tr>
</table>
<br>
<?php
	$i += 3;
    }

    if ($showoffline == "on") {
	$stop = 0;
	while($stop == 0 && ($row = mysql_fetch_array($rsoffl))) {

?>
<table border=0 width="600">
 <tr>
  <td valign="top" align="center">
<?php
	    print_user($row, $i, $DENYMASK);
	    $row2 = mysql_fetch_array($rsoffl);
	    if ($row2 == NULL) {
?>
  </td>
  <td valign="top" align="center">
        &nbsp;
<?php
		$stop = 1;
	    } else {
?>
  </td>
  <td valign="top" align="center">
<?php
		$i += 3;
		print_user($row2, $i, $DENYMASK);
	    }
?>
  </td>
 </tr>
</table>
<br>
<?php
	    $i += 3;
	}
    }
?>

</div>
</form>

</body>
</html>
<?php
    mysql_close($mysql);
?>
