"; echo ""; echo ''; echo ""; echo ""; echo ""; echo "SeoPanel TrackVisitors"; echo "Hello
"; echo "Now is:
"; echo "UTC Date (d-m-y) = ".gmdate("d-m-Y H:i:s")."
"; require_once "DifTimeProcess.php"; $TimeProcess = new TimeProcess(); if ($_REQUEST["IdWeb"] == "") RegistraError("Service Denied. A visitor has come with empty variable 'IdWeb'", __LINE__); $conexion = Inicia_Sql($BaseDeDatos); //Validate, if Post relation IdWeb-NameWeb are correct, if not, bye bye $Sql = "SELECT * From `websites` WHERE `id` = '".$_REQUEST["IdWeb"]."' and `name` = '".$_REQUEST["NameWeb"]."'"; //echo "Sql=$Sql
"; $Query = mysql_query($Sql, $conexion); if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); $TotalRows = mysql_num_rows($Query); if (!$TotalRows) RegistraError("Service Denied. Incorrect relation de IdWeb-NameWeb", __LINE__); $Fetch = mysql_fetch_array($Query); $Group_id = $Fetch["user_id"]; //Extract user_id for read setup if (!$Group_id or $Group_id == "") RegistraError("Service Denied. This visit will record for a project-web that has no active user.", __LINE__); $Sql_Setup = "SELECT * FROM `TV_Setup` WHERE `Group_id`='".$Group_id."'"; $Query_Setup = mysql_query($Sql_Setup, $conexion); //Lee Setup $Fetch_Setup = mysql_fetch_array($Query_Setup); $GEO_METHOD = $Fetch_Setup["GEO_METHOD"]; $REGISTER_LOCAL_MACHINE_VISIT = $Fetch_Setup["REGISTER_LOCAL_MACHINE_VISIT"]; $REGISTER_NOIP_VISIT = $Fetch_Setup["REGISTER_NOIP_VISIT"]; $DATABASEGEO_COUNTRY = $Fetch_Setup["DATABASEGEO_COUNTRY"]; $DATABASEGEO_CITY = $Fetch_Setup["DATABASEGEO_CITY"]; $IPINFOBD_KEY = $Fetch_Setup["IPINFOBD_KEY"]; $Utc_Fecha = gmdate("Y-m-d H:i:s"); $Label = $_REQUEST["Label"]; require_once "getRealIP.php"; if ($REGISTER_NOIP_VISIT == 0 and $client_ip == "") //Si el navegador del cliente no revela su dirección IP, lo registramos como error RegistraError("Service Denied. A visitor has come with a hidden or masked IP", __LINE__); if ($REGISTER_NOIP_VISIT == 0 and !$client_ip) RegistraError("Service Denied. Empty or invisible Ip Address", __LINE__); if ($REGISTER_LOCAL_MACHINE_VISIT == 0 and $client_ip == "127.0.0.1") //si es ip localhost, bye bye RegistraError("Service Denied. Ip Localhost 127.0.0.1", __LINE__); //Comprueba si la Ip está registrada del día actual, si no lo está la crea en "Conexiones_Entry", devuele el ID de la fila CreaEntry($conexion, $GEO_METHOD, $DATABASEGEO_COUNTRY, $DATABASEGEO_CITY, $IPINFOBD_KEY, $BaseDeDatos, $client_ip, $Utc_Fecha, $client_id, $IsVisitor); if (!$client_id) return; //si la función falla bye bye $Referer = urldecode($_REQUEST["Referer"]); if ( $REGISTER_LOCAL_MACHINE_VISIT == 0 and strtolower(mb_substr($Referer, 0, mb_strlen("http://localhost"))) == "http://localhost") { echo "Service Denied. Omitiendo entrada de dirección local '".urldecode($_REQUEST["Referer"])."'
"; return; } $Sql = "SELECT max(`TV_Visits`.`id_visit`) FROM `TV_Visits`"; //Recoge el ultimo id de la tabla Conexiones_INfo $Query = mysql_query($Sql, $conexion); if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); $Fetch = mysql_fetch_row($Query); $Last_Id = $Fetch[0]; if ($Last_Id=="") $Last_Id=0; $Last_Id++; //Suma +1 al ultimo id de la tabla Conexiones_Info, para disponer a la nueva inserción $Sql = "INSERT INTO `TV_Visits` (`id_visit`, `id_visitor` , `VisitorEntry` , `Utc_Fecha` , `Label` , `Alertado`) VALUES ('$Last_Id', '$client_id', '$IsVisitor', '$Utc_Fecha', '$Label', '$Alertado');"; $Query = mysql_query($Sql, $conexion); //Inserta Entrada Visitante if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); echo "Registrado Ok.
"; echo "Comprobando criterios de Alertas.
"; $Sql = "SELECT * FROM `TV_Alertas` WHERE `website_id` ='".$_REQUEST["IdWeb"]."' AND `Label`='$Label'"; $Query = mysql_query($Sql, $conexion); //Comprueba si hay alertas de este usuario y Label para enviar un email if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); $Total_Rows = mysql_num_rows($Query); if (!$Total_Rows) echo "None active Alert with this label.
"; else { $Fetch = mysql_fetch_array($Query); if ($Fetch["MailAlerta"] > "") { echo "Found active Alert for Label='$Label' for '".$Fetch["MailAlerta"]."'
"; $ArrayMails=explode(",",$Fetch["MailAlerta"]); $Sql = "SELECT * FROM `TV_Visitors` WHERE `id` = '$client_id'"; $Query = mysql_query($Sql, $conexion); //Sacamos información sobre Ip y geolocalización para adjuntarlo al mail if ($Query) { $Fetch = mysql_fetch_array($Query); $Ip = $Fetch["Ip"]; $Pais = $Fetch["PaisNombre"]; $Region = $Fetch["Region"]; $Localidad = $Fetch["Localidad"]; $platform = $Fetch["platform"]; $parent = $Fetch["parent"]; $Referer = $Fetch["Referer"]; } else $Ip = "Error Query
Sql=($Sql) mysql_error=(".mysql_error().") in Line:". __LINE__; $Destinatarios = array(); $DestinatariosCC = array(); $DestinatariosBCC = array(); for ($h=0; $hSend to '".$Fetch["MailAlerta"]."'
"; $Asunto = "Alert Label '$Label'"; $Cuerpo = "A visitor to come, leaving the following tracks:

"; $Cuerpo .= "Label = '$Label'
"; $Cuerpo .= "Referer = '".$Referer."'
"; $Cuerpo .= "IP = '$Ip'
"; $Cuerpo .= "GeoLocation Country = '$Pais'
"; $Cuerpo .= "GeoLocation Region = '$Region'
"; $Cuerpo .= "GeoLocation City = '$Localidad'
"; $Cuerpo .= "Sis.Operative = '$platform'
"; $Cuerpo .= "Navegator = '$parent'
"; $Cuerpo .= "

If this E-Mail not addressed to you and you want to stop receiving, report to: administracion@facturandoweb.com
"; $Cuerpo .= "

Si éste E-Mail no va dirigido a ti y quieres dejar de recibirlo notifícalo a: administracion@facturandoweb.com
"; include "mailer.php"; //Envia emails a los destinatarios de la alerta echo $Dsp; } } if ($exito) { $Sql = "UPDATE `TV_Visits` SET `Alertado`='1' WHERE `id_visit`='$Last_Id'"; $Query = mysql_query($Sql, $conexion); //Actualiza el movimiento con la marca de 'enviada alerta' if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); echo "ALERTED BY MAIL Ok.
"; } if ($IsVisitor) echo "Registered OK Visitor and Visit, Visitor is ID=$Last_Id"; else echo "Registered OK, only Visit, for Visitor ID=$Last_Id"; $TimeProcess->Fin(); function Inicia_Sql(&$BaseDeDatos) { $conexion; require "../../config/sp-config.php"; //Adaptación de SeoPanel $BaseDeDatos = DB_NAME; $Usuario = DB_USER; $Password = DB_PASSWORD; $Servidor = DB_HOST; $Utc_Fecha_Error = gmdate("Y-m-d H:i:s"); if (!$Servidor) RegistraError("Variable 'Servidor' Vacía", __LINE__); if (!$BaseDeDatos) RegistraError("Variable 'BaseDeDatos' Vacía", __LINE__); if (!$Usuario) RegistraError("Variable 'Usuario' Vacía", __LINE__); if (!$Password) RegistraError("Variable 'Password' Vacía", __LINE__); $conexion=mysql_connect($Servidor,$Usuario,$Password); if (!$conexion) { $mysql_error = mysql_error(); if (strpos($mysql_error,"@")) $mysql_error = "user".mb_substr($mysql_error, strpos($mysql_error,"@"), mb_strlen($mysql_error)-strpos($mysql_error,"@")); RegistraError("'conexion' falló (mysql_error=".$mysql_error.")", __LINE__); } $descriptor=mysql_select_db($BaseDeDatos,$conexion); if (!$descriptor) RegistraError("Error seleccionado Base de Datos (mysql_error=".mysql_error().")", __LINE__); @mysql_query("SET NAMES 'utf8'"); $keys_mysql_character = array("NAMES", "character_set_client", "character_set_connection", "character set database", "character set filesystem", "character_set_results", "character_set_server", "character set system"); for ($h=0; $h"; $Query = mysql_query($Sql, $conexion); //Esta consulta comprueba si una IP determinada a entrado otra vez el mismo día, si entra por 2ª vez en lugar de insertar actualizamos sus datos if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); $TotalRows = mysql_num_rows($Query); if ($TotalRows) { //Si existe visitante del día actual, recuperamos el Id de Conexiones_Entry $Fetch = mysql_fetch_row($Query); $ReturnId = $Fetch[0]; $IsVisitor = 0; echo "This Visitor exists, register only as visit without extend information
"; return; } echo "This Visitor no exists, register as visitor and searching extend information
"; //Nuevo registro de Entrada de una nueva IP visitante $New_Id = 0; $Sql = "SELECT `id` FROM `TV_Visitors`"; $Query = mysql_query($Sql, $conexion); if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); if (!$New_Id) { $Sql = "SELECT max(`TV_Visitors`.`id`) FROM `TV_Visitors`"; $Query = mysql_query($Sql, $conexion); if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); $Fetch = mysql_fetch_row($Query); $Last_Id = $Fetch[0]; if (!$Last_Id or $Last_Id == "") $Last_Id = 0; $New_Id = ($Last_Id+1); } $PaisCode = ""; $PaisNombre = ""; $Region = ""; $Localidad = ""; $Latitud = ""; $Longitud = ""; if ($client_ip) { if ($GEO_METHOD == "DataBase") { echo "call function for translate GeoPosition method DataBase
"; GetGeoDataBase($conexion, $DATABASEGEO_COUNTRY, $DATABASEGEO_CITY, $BaseDeDatos, $client_ip, $PaisCode, $PaisNombre, $Region, $Localidad, $Latitud, $Longitud); } elseif ($GEO_METHOD == "Internet") { echo "call function for translate GeoPosition method Internet
"; if ($IPINFOBD_KEY) GetGeoInternet($IPINFOBD_KEY, $client_ip, $PaisCode, $PaisNombre, $Region, $Localidad, $Latitud, $Longitud); else RegistraError("Ohhh!! No have defined IPINFOBD_KEY, omitted, then cannot put the information in the database", __LINE__, $OnlyWarning=1); } else if (!$GEO_METHOD) RegistraError("Ohhh!! Have defined GEO_METHOD incorrectly, value='$GEO_METHOD', omitted, then cannot put the information in the database", __LINE__, $OnlyWarning=1); } echo "Ip=$client_ip
"; echo "PaisCode=$PaisCode
"; echo "PaisNombre=$PaisNombre
"; echo "Region=$Region
"; echo "Localidad=$Localidad
"; echo "Latitud=$Latitud
"; echo "Longitud=$Longitud
"; echo "Referer=".$_REQUEST["Referer"]."
"; $Agente = $_SERVER["HTTP_USER_AGENT"]; if ($Agente) { require_once('php-local-browscap.php'); $navegador=get_browser_local($Agente); foreach ($navegador as $key=>$val) { ${$key} = $val; } } $Sql = "INSERT INTO `TV_Visitors` (`id` , `website_id` , `Ip` , `PaisCode` , `PaisNombre` , `Region` , `Localidad` , `Latitud` , `Longitud`, `REFERER` , `AGENTE` , `parent` , `platform`) VALUES ('$New_Id' , '".$_REQUEST["IdWeb"]."', '$client_ip', '$PaisCode', '$PaisNombre', '$Region', '$Localidad', '$Latitud', '$Longitud', '".addslashes($Referer)."', '".addslashes($Agente)."', '".addslashes($parent)."', '".addslashes($platform)."')"; $Query = mysql_query($Sql, $conexion); //Inserta nuevo registro if (!$Query) RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__); $ReturnId = $New_Id; $IsVisitor = 1; return; } function GetGeoInternet($IPINFOBD_KEY, $client_ip, &$PaisCode, &$PaisNombre, &$Region, &$Localidad, &$Latitud, &$Longitud) { include('ipinfodb.class.php'); //Load the class $ipinfodb = new ipinfodb; $ipinfodb->setKey($IPINFOBD_KEY); //Get errors and locations $locations = $ipinfodb->getGeoLocation($client_ip); $errors = $ipinfodb->getError(); //Getting the result if (!empty($locations) && is_array($locations)) { $PaisCode = $locations["CountryCode"]; $PaisNombre = $locations["CountryName"]; $Region = $locations["RegionName"]; $Localidad = $locations["City"]; $Latitud = $locations["Latitude"]; $Longitud = $locations["Longitude"]; foreach ($locations as $field => $val) { //echo $field . ' : ' . $val . "
\n"; } } $Error = ""; //Dump of all errors if (!empty($errors) && is_array($errors)) { foreach ($errors as $error) { $Error .= var_dump($error)."
"; } } if ($Error) { RegistraError("Error in GeoLocation Service by IpInfoBd.com
$Error", __LINE__, $OnlyWarning=1); } } function GetGeoDataBase($conexion, $DATABASEGEO_COUNTRY, $DATABASEGEO_CITY, $BaseDeDatos, $client_ip, &$PaisCode, &$PaisNombre, &$Region, &$Localidad, &$Latitud, &$Longitud) { if ($DATABASEGEO_COUNTRY) { $descriptor=mysql_select_db($DATABASEGEO_COUNTRY, $conexion); if ($descriptor) { $Sql = "SELECT `ip_group_country`.*, `iso3166_countries`.* FROM `ip_group_country`, `iso3166_countries` where `ip_group_country`.country_code = `iso3166_countries`.code and `ip_start` <= INET_ATON('".$client_ip."') order by ip_start desc limit 1;"; //[ip_start], [ip_cidr], [country_code], [name] $Query = mysql_query($Sql, $conexion); if ($Query) { $Fetch = mysql_fetch_array($Query); //foreach ($Fetch as $key=>$value) echo "[$key], value=$value
"; $PaisNombre = $Fetch["name"]; $PaisCode = $Fetch["code"]; } else RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__, $OnlyWarning=1); } else RegistraError("Error Seleccionando la Base de Datos '".DATABASEGEO_COUNTRY."' (mysql_error=".mysql_error().")", __LINE__, $OnlyWarning=1); } else RegistraError("Ohhh!! No have defined DATABASEGEO_COUNTRY, omitted, then cannot put the information in the database", __LINE__, $OnlyWarning=1); $Region_Code = ""; $Country_Code = ""; if ($DATABASEGEO_CITY) { $descriptor=mysql_select_db($DATABASEGEO_CITY, $conexion); if ($descriptor) { $TablaA = "ip_group_city"; $TablaB = "locations"; $TablaC = "fips_regions"; $Sql = "SELECT * FROM `$TablaA` LEFT OUTER JOIN `$TablaB` ON `$TablaA`.`location` = `$TablaB`.`id`";//, LEFT OUTER JOIN `$TablaC` ON `$TablaC`.`code` = `$TablaB`.`region_code`"; $Sql .= " where `ip_start` <= INET_ATON('".$client_ip."') order by ip_start desc limit 1;"; $Query = mysql_query($Sql, $conexion); //[location], [country_code], [region_code], [city], {zipcode], [latitude], [longitude], [metrocode] if ($Query) { $Fetch = mysql_fetch_array($Query); //foreach ($Fetch as $key=>$value) echo "[$key], value=$value
"; $Region_Code = $Fetch["region_code"]; $Country_Code = $Fetch["country_code"]; $Localidad = $Fetch["city"]; $Latitud = $Fetch["latitude"]; $Longitud = $Fetch["longitude"]; } else RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__, $OnlyWarning=1); } else RegistraError("Error Seleccionando la Base de Datos '".DATABASEGEO_CITY."' (mysql_error=".mysql_error().")", __LINE__, $OnlyWarning=1); } else RegistraError("Ohhh!! No have defined DATABASEGEO_CITY, omitted, then cannot put the information in the database", __LINE__, $OnlyWarning=1); if ($DATABASEGEO_CITY and $Region_Code and $Country_Code) { $TablaA = "ip_group_city"; $TablaB = "locations"; $TablaC = "fips_regions"; $Sql = "SELECT * FROM `fips_regions` WHERE `country_code` = '$Country_Code' and `code` = '$Region_Code'";//, LEFT OUTER JOIN `$TablaC` ON `$TablaC`.`code` = `$TablaB`.`region_code`"; $Sql .= " limit 1;"; //echo "Sql=$Sql
"; $Query = mysql_query($Sql, $conexion); if ($Query) { $Fetch = mysql_fetch_array($Query); //foreach ($Fetch as $key=>$value) echo "[$key], value=$value
"; $Region = $Fetch["name"]; } else RegistraError("Error Query
Sql=($Sql) mysql_error=(".mysql_error().")", __LINE__, $OnlyWarning=1); } $descriptor=mysql_select_db($BaseDeDatos, $conexion); if (!$descriptor) RegistraError("Error Seleccionando la Base de Datos '".$BaseDeDatos."'", __LINE__, $OnlyWarning=0); } function RegistraError($Mensaje, $Linea, $OnlyWarning=0) { $Utc_Fecha_Error = gmdate("Y-m-d H:i:s"); //Error Out to Screen echo "ERROR:
Utc DateTime: $Utc_Fecha_Error
Line: $Linea
Description: $Mensaje
"; //Error Out to File $myFile = "Scanner_Errors/Log_for_".$_REQUEST["IdWeb"].".log"; $fh = fopen($myFile, 'a'); if ($fh) { $msg = "\n"; fwrite($fh, $msg); $msg = "UTCDATE: $Utc_Fecha_Error\nLine: $Linea\nDescription: $Mensaje\n"; fwrite($fh, $msg); fclose($fh); echo "Error Reported in File $myFile"; } else { echo "Failed to Report to file ".$myFile.""; } if ($OnlyWarning) return; die(); } ?>