<?php
//Función infalible para conseguir datos exactos de geolocalización mediante ip
function obten_datos_desde_ip($ip,$sessid) // dada una cookie tipo PHPSESSID y una ip, retorna datos de geolocalizacion
{
    
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    
$data['QRY'] = $ip;

    
$ch = curl_init();    // initialize curl handle
    
curl_setopt($ch, CURLOPT_URL, 'http://www.ip-adress.com/'); // set url to post to
    
curl_setopt($ch, CURLOPT_HEADER, 1);
    
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    
$cookies=array(
        
'__utmc=11986028',
        
'__utmz=11986028.1187409683.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)',
        
'__utma=11986028.506816177.1187409683.1187409683.1187409683.1','__utmb=11986028',
        
'PHPSESSID='.$sessid);
    
curl_setopt($ch, CURLOPT_COOKIE, implode(';',$cookies)); //cookie de sesion de ip-address
    
curl_setopt($ch, CURLOPT_POST, 1);
    if(!empty(
$data['QRY'])) // si la ip esta vacia, no voy a gastar oprtunidades...
    
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
curl_setopt($ch, CURLOPT_REFERER, 'http://www.ip-adress.com/');
    
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

    
$document = curl_exec($ch);
    
    
preg_match("#<img src=\"/flags/(.*?)\.png\" alt=\"flag\">(.*?)</td>#is",$document,$country);    
    
$country = trim($country[2]);
    
preg_match("#address state\:</th>(.*?)<td>(.*?)</td>#is",$document,$state);    
    
$state = trim($state[2]);
    
preg_match("#IP address city\:</th>(.*?)<td>(.*?)</td>#is",$document,$city);    
    
$city = trim($city[2]);
    
    
//return $document;
    
return array($country,$state,$city);
}
function
nuevo_correo() // obtiene una cuenta de correo valida para mailinator
{
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL, 'http://www.mailinator.com/');
    
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    
    
$document = curl_exec($ch);
    
    
preg_match("#<a href=/maildir\.jsp\?email=(.*?)>(.*?)@mailinator.com#is",$document,$mail);
    
    return
$mail[1].'@mailinator.com';
}
function
nueva_cuenta($email) //crea una cuenta en ip-adress.com con el email determinado
{
    
## Genero falsas identidades aleatorias
    
$nombres = array('Ellron','Ronn','Ingrid','Elli','Paul','Marta','Evan','Lis','Chriss','Robert','Leela','Forest','Red','Mike','Will','Thomas','Geena','Bertha','Samanta','Carlos','Potz','Henrry','Edward');
    
$apellidos = array('Vandersson','Darwin','Muntson','Muntz','Coll','Ferg','Thompson','Everet','Franklin','Royal','Ectom','Harrie');
    
$rn = rand(0,count($nombres));
    
$nombre = $nombres[$rn];
    
$ra = rand(0,count($apellidos));
    
$apellido = $apellidos[$ra];
    
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL, 'http://www.ip-adress.com/signup/');
    
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    
curl_setopt($ch, CURLOPT_POST, 1);
    
curl_setopt($ch, CURLOPT_POSTFIELDS, array('forename'=>$nombre,'surname'=>$apellido,'email'=>$email));
    
curl_setopt($ch, CURLOPT_COOKIEJAR, '-');
    
    
$document = curl_exec($ch);
    
    return
$document;
}

function
lee_bandeja_entrada($mail)
{
    
sleep(12); //espera unos segundos...
    
    
$mail = explode('@',$mail);
    
$uname = $mail[0];
    
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL, 'http://www.mailinator.com/showmail.jsp?email='.$uname.'&msgnum=0');
    
curl_setopt($ch, CURLOPT_HEADER, 1);
    
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    
curl_setopt($ch, CURLOPT_COOKIEJAR, '-');
    
    
$document = curl_exec($ch);
    
    
preg_match("#http://www\.ip\-adress.com/confirm\.php\?code=(.*?)\s#is",$document,$code); //el codigo
    
preg_match("#<br>password: (.*?)<br>#is",$document,$pass); //el codigo
    
    
return array($code[1],$pass[1]);
}
function
confirma($codigo)
{
    
sleep(5);
    
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL, 'http://www.ip-adress.com/confirm.php?code='.$codigo);
    
curl_setopt($ch, CURLOPT_HEADER, 1);
    
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    
    
$document = curl_exec($ch);
    
    return
$document;
}
function
login($email,$clave)
{
    
sleep(2);
    
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL, 'http://www.ip-adress.com/login/');
    
curl_setopt($ch, CURLOPT_HEADER, 1);
    
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    
curl_setopt($ch, CURLOPT_POST, 1);
    
curl_setopt($ch, CURLOPT_POSTFIELDS, array('login'=>$email,'password'=>$clave));
    
    
$document = curl_exec($ch);
    
    
preg_match("#PHPSESSID=(.*?);#is",$document,$SSD);
    
    return
$SSD[1];
}

//-->>
$time[0] = time(); //pillo el instante en que empieza

$mail = nuevo_correo();
$time[] = time();

nueva_cuenta($mail);
$time[] = time();

$be = lee_bandeja_entrada($mail);
$time[] = time();

confirma($be[0]);
$time[] = time();

$ssid = login($mail,$be[1]);
$time[] = time();

//-->


// Ahora ya tengo lo que queria, el PHPSESSID para acceder con la cookie de sesion

if(!empty($ssid))
{

include_once
'../Gstats.php';

$s = new Gstats($req);
$s->sql_con('localhost','usuario','clave','basedatos'); //conecto con la base de datos
$result = $s->sql_query("SELECT ip FROM gstat WHERE country='' AND state='' AND city='' AND id>10000 LIMIT 50");

if(
mysql_num_rows($result)==0)
$s->error("Hay cero coincidencias en la base de datos");
else
{
    while(
$row=mysql_fetch_row($result))
    {
        
$d = obten_datos_desde_ip($row[0],$ssid);
        if(!empty(
$d[0]))
        {
            if(!
$s->sql_query("UPDATE gstat SET country='".addslashes($d[0])."',state='".addslashes($d[1])."',city='".addslashes($d[2])."' WHERE ip LIKE '$row[0]'"))
            
$s->error("No pudo alterarse la base de datos");
            
sleep(5); // me espero 5 segundos prudentemente, para no llamar la atencion....
        
}
    }
}

$time[] = time();

//Hago que se refresque cada dos minutos
echo '<meta http-equiv="refresh" content="120">';
echo
'<h1>Log de la actual peticion:</h1>';
echo
'<ul>';
foreach(
$s->log as $l)
echo
'<li>'.$l.'</li>';
echo
'</ul>';
echo
'<h2>Tiempos:</h2><ul>';
function
tiempo($c)
{
    if(
$c>60)
    {
        
$min = floor($c/60);
        return
$min.' minutos '.($c-$min*60).' segundos';
    }
    else
        return
$c.' segundos';
}
echo
'<li>Total: '.tiempo($time[6]-$time[0]).'</li>';
echo
'<li>Creacion correo temporal: '.tiempo($time[1]-$time[0]).'</li>';
echo
'<li>Creacion nueva cuenta del servicio: '.tiempo($time[2]-$time[1]).'</li>';
echo
'<li>Lectura bandeja de entrada (email confirmacion): '.tiempo($time[3]-$time[2]).'</li>';
echo
'<li>Ejecucion de la confirmacion: '.tiempo($time[4]-$time[3]).'</li>';
echo
'<li>Accion de Login: '.tiempo($time[5]-$time[4]).'</li>';
echo
'<li>Insercion a la base de datos: '.tiempo($time[6]-$time[5]).'</li>';

echo
'</ul>';
echo
'<small>NOTA: muchos de los tiempos han sido prolongados intencionadamente, para crear un efecto de usuario (lento), y evitar errores.</small>';
echo
'<p><a href="set_from.php">Volver a ejecutar set_from</a></p>';
}
else
echo
"No se pudo obtener los datos necesarios. Refresque la pagina para volver a intentarlo";
?>