<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//echo "Caçar Reshare";
/*
  id	int(11)	Não
  ip_serv	varchar(50)	Não
  porta_serv	varchar(50)	Não
  user_serv	varchar(50)	Não
  senha_serv	varchar(50)	Não
  protocol_serv	varchar(50)	Não
  desc_serv	varchar(255)	Não
 *  */
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$SQL = "SELECT id,ip_serv, desc_serv FROM cmum_csp_config";
$rs = $conn->query($SQL);
$csps = array();
while ($reg = $rs->fetch_assoc()) {
    $csps[] = $reg;
}
?>
<hr/>
<table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 0px solid #DDDDDD; border-right: 0px solid #DDDDDD; text-align: left;">
    <tr>
        <td>Pegar Reshares</td><td></td>
    </tr>
    <tr>
        <td>
            <p><form method="post">
                <label class="col-sm-2" style="font-size: 8pt; text-align: center">Selecione o CSP</label>
                <div class="col-sm-6">
                    <select name="servidor" class="form-control">
                        <option value="0" <?php if (isset($_POST['servidor']) && $_POST['servidor'] == "0"): ?>selected<?php endif; ?>>Todos</option>
                        <?php
                        if (!empty($csps)) {
                            foreach ($csps as $csp)
                                if ($csp['ip_serv'] != "") {
                                    ?>
                                    <option value="<?php echo $csp['id'] ?>" <?php
                                    if (isset($_POST['servidor']) && $_POST['servidor'] == $csp['id']) {
                                        echo 'selected="true"';
                                    }
                                    ?>><?php echo $csp['desc_serv'] ?></option> 
                                            <?php
                                        }
                                } else {
                                    ?>
                            <option>Não Existem CSPS Cadastrados</option> 
                        <?php } ?>

                    </select>
                </div>
                <button class="btn btn-default">Iniciar Procura</button>
                </p>
        </td>
        <td>
            <label class="col-sm-2" style="font-size: 8pt; text-align: center">Buscar por IP:</label>
            <div class="col-sm-6">
                <input name="busca_ip" id="busca_ip" class="form-control" value="<?php echo isset($_POST['busca_ip']) ? $_POST['busca_ip'] : ""; ?>"/>
            </div>
            <button class="btn btn-default">Buscar</button>
            </form>

        </td>
    </tr>
</table>
<?php if (isset($reshares) && $reshares != "") { ?>
    <table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" class="table-bordered table-condensed" style="font-size: 10pt">
        <tr>
            <th>Login</th>
            <th>Servidor</th>
            <th>Revenda</th>
            <th>IP</th>
            <th>Inicio da Conexão</th>
            <th>Tempo Conectado</th>
            <th>FLAGS</th>
            <th>&nbsp;</th>
        </tr>
        <?php
        foreach ($reshares as $r) {
            if (isset($r->attributes()->name)) {
                $SQL = "SELECT u.id,u.name,a.username FROM cmum_udb AS u LEFT JOIN cmum_admins AS a ON u.addedby = a.id WHERE u.name = '{$r->attributes()->name}'";
                $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
                $rs = $conn->query($SQL);
                $login = array();
                if ($rs->num_rows > 0) {
                    $login = $rs->fetch_assoc();
                    $json["id"] = "{$login['id']}";
                    $p = base64_encode(json_encode($json));
                }
                $id_csp = $r->id_servidor;
                $_login = isset($login["name"]) ? $login["name"] : $r->attributes()->name;

                $logoff = '<a href="kickuser_desativar.php?login=' . $_login . '&id=' . $id_csp . '&opc_kick=1" title="Desconectar" class="kick-user">Desconectar</a>';
                if(isset($login["name"])){
                    $logoff .= '&nbsp;|&nbsp;<a href="kickuser_desativar.php?login=' . $_login . '&id=' . $id_csp . '&opc_kick=2" title="Desativar" class="kick-user">Desativar</a>';
                }
                
            }
            ?>     
            <tr>
                <td><?php echo isset($login['name']) ? '<a href="edituser.php?p=' . $p . '" title="Editar" target="_blank">' . $login['name'] . '</a>' : $r->attributes()->name; ?></td>
                <td><?php echo $r->servidor ?></td>
                <td><?php echo isset($login['username']) ? $login['username'] : " - "; ?></td>
                <td><a href="#" onclick="abrirJanela('infoip.php?ip=<?php echo $r->session->attributes()->host ?>')"><?php echo $r->session->attributes()->host ?></a></td>
                <td><?php echo date("d/m/Y H:i:s", strtotime($r->session->attributes()->connected)) ?></td>
                <td><?php echo $r->session->attributes()->duration ?></td>
                <td><?php echo $r->session->attributes()->flags ?></td>
                <td><?php echo $logoff; ?></td>
            </tr>
            <?php
        }
    } elseif (isset($reshares) && $reshares == "") {
        ?>
        <div style="text-align: center">Não foram encontrados resultados, refaça sua busca..</div>        
    <?php } ?>
    <script>
        $('.kick-user').on('click', this, function(e) {
            e.preventDefault();
            var url = $(this).attr('href');
            var w = 400;
            var h = 200;
            var left = (screen.width / 2) - (w / 2);
            var top = (screen.height / 2) - (h / 2);
            var janela = window.open(url, "", "width=" + w + ", height=" + h + ", left=" + left + ", top=" + top);
            /*var id_int = setInterval(function() {
                if (janela.closed == true) {
                    window.self.location.reload();
                    clearInterval(id_int);
                }
            }, 1000);*/
        });
    </script>


