<?php
	session_start();
	include ('../Amfphp/Services/conexao.php');
	$con=new conexao();
	include ('funcoes.php');
	if (isset($_SESSION['acesso_idusuario'])){
		$acesso_idusuario=$_SESSION['acesso_idusuario'];
		$acesso_idempresa=$_SESSION['acesso_idempresa'];
		$acesso_admin=$_SESSION['acesso_admin'];
		$acesso_revenda=$_SESSION['acesso_revenda'];
		$diaatual=date('d/m/Y');
		$horaatual=date('h:i');
		$csp_hora=date('hi');
		$sql="select * from profile where idempresa='$acesso_idempresa' and situacao='Ativo' and idprofile='$sel_profile'";
		$resultado = $con->banco->Execute($sql);
		$registro = $resultado->FetchNextObject();
		$csphost=$registro->HOST;
		$cspporta=$registro->PORTACSP;
		$cspusuario=decripta($registro->USUARIO);
		$cspsenha=decripta($registro->SENHA);
		$n=1;
		$online=0;
	} else {
		echo "<script>window.location='index.php';</script>";
	}
	if ($csphost == "" || $cspusuario == "" || $cspsenha == ""){
		echo "<script>window.alert('ERRO. Dados de configuracoes do CSP nao foram encontrados.');</script>";
		echo "<script>window.location='menu_mobile.php';</script>";
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSPADM Mobile - Usuarios Online </title>
<link href="cspadm.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
if ($comando=="proxy-users"){
	$xml = simplexml_load_file("http://$cspusuario:$cspsenha@$csphost:$cspporta/xmlHandler?command=proxy-users");
	?>
<table width="410" border="1">
  <tr class="txtBOX">
    <td colspan="4"  bgcolor="#FFFFCC" class="txtAlertaBOX"><a href="menu_mobile.php">(&lt;-- Voltar) CSPADM Mobile - Usuarios Online</a></td>
  </tr>
  <tr class="txtBOX">
    <td width="37"  bgcolor="#CCCCCC">Acao</td>
    <td width="92"  bgcolor="#CCCCCC">Login</td>
    <td width="92"  bgcolor="#CCCCCC">Tempo</td>
    <td width="161"  bgcolor="#CCCCCC">Canal</td>
  </tr>
<?php
	foreach ($xml->children() as $teste1) {
		foreach ($teste1->children() as $teste2) {
			$online_login=$teste2['name'];
			$online_session=$teste2['sessions'];
			$fundo = ($n % 2) ? 'relatorio1a' : 'relatorio3';
			$n++;
			$online++;
		?>
  <tr class="<?php echo $fundo;?>" onmouseover="javascript:this.style.backgroundColor='#C0B085'" onmouseout="javascript:this.style.backgroundColor=''">
    <td><a href="csp_xml.php?oqfazer=desconectar&csplogin=<?php echo $online_login;?>&revededor=0&sel_profile=<?php echo $sel_profile;?>" class="edtBOX">Desc.</a></td>
    <td><?php echo "$online_login ($online_session)";  ?></td>
    <td colspan="3">
  <?php
			foreach ($teste2->children() as $teste3) {
				$online_host=$teste3['host'];
				$online_connected=$teste3['connected'];
				$online_connected=substr($online_connected,0,25);
				$online_duracao=$teste3['duration'];
				$online_last=$teste3['last-transaction'];
				foreach ($teste3->children() as $teste4) {
					$online_canal=$teste4['name'];
					$online_cdata=$teste4['cdata'];
					$img="imagens/canais/$online_cdata.gif";
					if (file_exists($img)){
						$canal="<img src=$img> - $online_canal";	
					} else {
						$canal="$online_canal - $online_cdata";	
					}
					
				}
				?>
              <table width="251" border="0">
              <tr>
              <td width="89"><?php echo $online_last;?></td>
              <td width="152"><?php echo $canal; ?></td>
              </tr></table>
  <?php
			}
			?>            </tr>
            <?php
		}
	}
	?>
  
</table>
<?php
}



if ($comando=="login-failures"){
?>
<table width="788" border="1">
  <tr class="txtBOX" >
    <td colspan="4" bgcolor="#CCCCCC">Logs de Erros em Acessos</td>
    <td bgcolor="#CCCCCC"><div align="right"><a href="csp_xml.php?oqfazer=limparlog&sel_profile=<?php echo $sel_profile;?>">Limpar CSP LOG Failures</a></div></td>
  </tr>
  <tr class="txtBOX">
    <td width="97" bgcolor="#CCCCCC">Login</td>
    <td width="42" bgcolor="#CCCCCC">Qtde</td>
    <td width="96" bgcolor="#CCCCCC">Endereco IP</td>
    <td width="176" bgcolor="#CCCCCC">Ultima Tentaviva</td>
    <td width="343" bgcolor="#CCCCCC">Motivo</td>
  </tr>
<?php
	$xml = simplexml_load_file("http://$cspusuario:$cspsenha@$csphost/xmlHandler?command=login-failures");
	foreach ($xml->children() as $teste1) {
		foreach ($teste1->children() as $teste2) {
			$erro_login=$teste2['name'];
			$erro_qtde=$teste2['failure-count'];
			$erro_host=$teste2['host'];
			$erro_ultima=$teste2['last-failure'];
			$erro_ultima=substr($erro_ultima,0,25);
			$erro_motivo=$teste2['reason'];
			$fundo = ($n % 2) ? 'relatorio1' : 'relatorio3';
			$n++;
		?>
  		<tr class="<?php echo $fundo;?>">
    		<td><?php echo $erro_login;  ?></td>
            <td><?php echo $erro_qtde;  ?></td>
            <td><?php echo $erro_host;  ?></td>
            <td><?php echo $erro_ultima;  ?></td>
            <td><?php echo $erro_motivo;  ?></td>
        </tr>

  <?php
		}
	}
	?>
</table>
    <?php
}
if ($oqfazer=="desconectar"){
	simplexml_load_file("http://$cspusuario:$cspsenha@$csphost:$cspporta/xmlHandler?command=kick-user&name=$csplogin");
	echo "<script>window.alert('Usuario $csplogin desconectado com sucesso');</script>";
	if ($acesso_admin==0){
		echo "<script>window.location='sel_profile_online.php?oqfazer=revendedor';</script>";
	} else {
		echo "<script>window.location='sel_profile_online.php?comando=proxy-users';</script>";
	}
}

if ($oqfazer=="limparlog"){
	simplexml_load_file("http://$cspusuario:$cspsenha@$csphost:$cspporta/xmlHandler?command=remove-failed");
	echo "<script>window.alert('Logs de falhas de Logins limpo com sucesso');</script>";
	echo "<script>window.location='csp_xml.php?comando=login-failures&sel_profile=$sel_profile';</script>";
}

if ($oqfazer == "revendedor"){
	$sql="select csp_user.* from csp_user, provedor where provedor.idempresa='$acesso_idempresa' and provedor.idprovedor = csp_user.id";
	if ($acesso_revenda == 1){
		$sql .= " and provedor.idrevendedor='$acesso_idusuario'";
	}
	$sql .= "order by csp_user.name ASC";
	$exe=mysql_query($sql);
	?>
	<table width="410" border="1">
	  <tr class="txtBOX">
	    <td colspan="4" bgcolor="#FFFFCC" class="txtAlertaBOX"><a href="menu_mobile.php">(&lt;-- Voltar) CSPADM Mobile - Usuarios Online</a></td>
      </tr>
	  <tr class="txtBOX">
	    <td width="49" bgcolor="#CCCCCC">Acao</td>
	    <td width="102" bgcolor="#CCCCCC">Login</td>
	    <td width="91" bgcolor="#CCCCCC">Time</td>
	    <td width="140" bgcolor="#CCCCCC">Canal</td>
	  </tr>
       <?php
	while($tmp=mysql_fetch_array($exe)){
		$val_name=$tmp[name];
		$xml = simplexml_load_file("http://$cspusuario:$cspsenha@$csphost:$cspporta/xmlHandler?command=proxy-users&name=$val_name");
		foreach ($xml->children() as $teste1) {
			foreach ($teste1->children() as $teste2) {
			$online_login=$teste2['name'];
				$online_session=$teste2['sessions'];
				$fundo = ($n % 2) ? 'relatorio1a' : 'relatorio3';
				$n++;
				$online++;
				?>
				<tr class="<?php echo $fundo;?>" onmouseover="javascript:this.style.backgroundColor='#C0B085'" onmouseout="javascript:this.style.backgroundColor=''">
				<td><a href="csp_xml.php?oqfazer=desconectar&csplogin=<?php echo $online_login;?>&revendedor=1&sel_profile=<?php echo $sel_profile;?>" class="edtBOX">Desc.</a></td>
				<td><?php echo "$online_login ($online_session)";  ?></td>
				<td colspan="4">
				<?php
				foreach ($teste2->children() as $teste3) {
					$online_host=$teste3['host'];
					$online_connected=$teste3['connected'];
					$online_connected=substr($online_connected,0,25);
					$online_duracao=$teste3['duration'];
					$online_last=$teste3['last-transaction'];
					foreach ($teste3->children() as $teste4) {
						$online_canal=$teste4['name'];
						$online_cdata=$teste4['cdata'];
					}
					?>
	                <table width="237" border="0">
         			    <tr>
		              <td width="88"><?php echo "$online_last"; ?></td>
           			  <td width="139"><?php echo "$online_canal ($online_cdata)";?></td>
		              </tr></table>
					  <?php
				}
			?></tr><?php
		}
	}
}
?>
</table>

    <?php
}


if ($comando=="atualizar"){
	simplexml_load_file("http://$cspusuario:$cspsenha@$csphost/xmlHandler?command=update-users");
	echo "Atualizado!";
}

if ($comando=="reset"){
	simplexml_load_file("http://$cspusuario:$cspsenha@$csphost/xmlHandler?command=reset-connector&name=ALL");
	echo "Todos os Cards foram resetados.";
}

?>

</body>
</html>
