174 lines
6.6 KiB
PHP
174 lines
6.6 KiB
PHP
<?php
|
|
$ini = parse_ini_file("conf.ini", true);
|
|
|
|
mysql_connect($ini['db']['host'],$ini['db']['user'],$ini['db']['pass']);
|
|
mysql_select_db($ini['db']['db']);
|
|
|
|
if ($ini['db']['log']=="1"){
|
|
$arrival_time=microtime(true);
|
|
$filename=basename(__FILE__);
|
|
$filename="log_".$filename.".txt";
|
|
$chunck=strval($arrival_time)." ".$_SERVER['REQUEST_URI']."\r\n";
|
|
file_put_contents($filename,$chunck,FILE_APPEND);
|
|
}
|
|
|
|
mysql_set_charset("utf8");
|
|
|
|
$user_id=$_REQUEST['u_id'];
|
|
$mag=$_REQUEST['mag'];
|
|
$address=mysql_real_escape_string($_REQUEST['address']);
|
|
|
|
//$sql=mysql_query("select * from banned where user_id=$user_id");
|
|
//if (mysql_num_rows($sql)){
|
|
// $banned=1;
|
|
//}
|
|
//else{
|
|
//$banned=0;
|
|
//}
|
|
|
|
$banned=0;
|
|
if ($user_id<>0 && $banned==0){
|
|
$randcode=mt_rand(1000000,9999999);
|
|
$user_latitude=$_REQUEST['lat'];
|
|
$user_longitude=$_REQUEST['lon'];
|
|
|
|
//cerca il conteggio degli utenti totali nell'area nella tabella di cache
|
|
$sql = mysql_query("SELECT count as users FROM cache_refcount_all WHERE abs(latitude-$user_latitude)<0.1 and abs(longitude-$user_longitude)<0.1");
|
|
|
|
if (mysql_num_rows($sql)) {
|
|
$users=mysql_result($sql,0,"users");
|
|
}
|
|
else{
|
|
//se il conteggio non è trovato viene calcolato e messo nella tabella di cache
|
|
$sql=mysql_query("SELECT count(*) as users FROM notifications WHERE abs(latitude-$user_latitude)<0.3 and abs(longitude-$user_longitude)<0.3");
|
|
$users=mysql_result($sql,0,"users");
|
|
|
|
$sql = mysql_query("INSERT INTO cache_refcount_all (latitude,longitude,count) values ($user_latitude,$user_longitude,$users)");
|
|
}
|
|
|
|
//cerca il conteggio dei sismi già segnalati nella tabella di cache (SOSPESO!)
|
|
$sql = mysql_query("SELECT count as eqn FROM cache_manualcount WHERE abs(latitude-$user_latitude)<0.1 and abs(longitude-$user_longitude)<0.1 and date > date_sub(now(), interval 5 second) order by count desc limit 1");
|
|
|
|
if (mysql_num_rows($sql)) {
|
|
$eqn = mysql_result($sql, 0, "eqn");
|
|
}
|
|
else{
|
|
//se il conteggio non è trovato viene calcolato e messo nella tabella di cache
|
|
|
|
//restituisce il numero di terremoti segnalati entro un raggio di circa 30km e negli ultimi 5 minuti
|
|
$sql=mysql_query("SELECT count(*) as eqn FROM manual WHERE abs(latitude-$user_latitude)<0.2 and abs(longitude-$user_longitude)<0.2 and date > date_sub(now(), interval 2 minute) and user_id!=$user_id");
|
|
$eqn = mysql_result($sql, 0, "eqn");
|
|
|
|
$sql = mysql_query("INSERT INTO cache_manualcount (latitude,longitude,count) values ($user_latitude,$user_longitude,$eqn)");
|
|
}
|
|
|
|
#se è già stato segnalato un certo numero di terremoti (frazione degli utenti attivi) consenti di mostrare la segnalazione del terremoto
|
|
if ($users>2000){
|
|
$limit=floor(sqrt($users)*2);
|
|
}
|
|
else{
|
|
$limit=floor($users/25);
|
|
}
|
|
if ($limit<4){
|
|
$limit=4;
|
|
}
|
|
if ($eqn>$limit){
|
|
$donotshow=0;
|
|
}
|
|
else{
|
|
$donotshow=1;
|
|
}
|
|
|
|
#inserisci la segnalazione nella tabella manual e manual_history
|
|
$sql=mysql_query("insert into manual (user_id,latitude,longitude,magnitude,code,address,donotshow) values ('".$_REQUEST['u_id']."','".$_REQUEST['lat']."','".$_REQUEST['lon']."','".$_REQUEST['mag']."',$randcode,'$address',$donotshow)");
|
|
$sql=mysql_query("insert into manual_history (user_id,latitude,longitude,magnitude,code,address,donotshow) values ('".$_REQUEST['u_id']."','".$_REQUEST['lat']."','".$_REQUEST['lon']."','".$_REQUEST['mag']."',$randcode,'$address',$donotshow)");
|
|
|
|
#Per "rilasciare" immediatamente l'utente senza attendere l'invio delle mail e notifiche gcm
|
|
ob_start();
|
|
$buffer = str_repeat(" ", 4096)."\r\n<span></span>\r\n";
|
|
$output[]="$randcode";
|
|
$out=json_encode($output).$buffer;
|
|
print($out);
|
|
ob_end_flush();
|
|
flush();
|
|
|
|
#se il terremoto può già essere mostrato sulla mappa allora notificalo ma solo se non ne sono già stati notificati altri in 2 minuti
|
|
if ($donotshow==0){
|
|
#restituisce il numero di terremoti segnalati E NOTIFICATI entro un raggio di circa 30km e negli ultimi 2 minuti
|
|
$sql=mysql_query("SELECT count(*) as eqn FROM manual WHERE abs(latitude-$user_latitude)<0.3 and abs(longitude-$user_longitude)<0.3 and date > date_sub(now(), interval 1 minute) and user_id!=$user_id and donotshow=0");
|
|
$eqn=mysql_result($sql,0,"eqn");
|
|
|
|
$sql=mysql_query("SELECT TIMESTAMPDIFF(SECOND,last_notification,NOW()) AS difference from manual_last_update");
|
|
$difference=mysql_result($sql,0,"difference");
|
|
if ($difference>10){
|
|
//abitilita le segnalazioni non mostrate
|
|
$sql2=mysql_query("update manual set donotshow=0 where abs(latitude-$user_latitude)<0.3 and abs(longitude-$user_longitude)<0.3 and date > date_sub(now(), interval 20 minute) and donotshow=1");
|
|
|
|
$sql=mysql_query("update manual_last_update set last_notification=NOW() where ID=0");
|
|
}
|
|
|
|
if ($eqn==0){
|
|
$dataArray = array('latitude'=>$user_latitude,
|
|
'longitude'=>$user_longitude,
|
|
'magnitude'=>$mag,
|
|
'place'=>$address,
|
|
'type'=>'manual');
|
|
|
|
//extract users for notifications
|
|
$row_limit=250000;
|
|
$distance_ref=-1;
|
|
$keep_going=1;
|
|
|
|
while ($keep_going){
|
|
$sql2 = mysql_query("SELECT reg_id,ABS(last_latitude-$user_latitude)+ABS(last_longitude-$user_longitude) as distance FROM gcm where notification_manual=1 and reg_id<>'' and ABS(last_latitude-$user_latitude)+ABS(last_longitude-$user_longitude)>$distance_ref ORDER BY distance ASC limit $row_limit");
|
|
|
|
$num_rows2 = mysql_num_rows($sql2);
|
|
if ($num_rows2>0){
|
|
while ($data = mysql_fetch_assoc($sql2)) {
|
|
$regidArray[] = $data['reg_id'];
|
|
$distanceArray[] = $data['distance'];
|
|
}
|
|
$distance_ref=end($distanceArray);
|
|
|
|
$block_size = 999;
|
|
$iter = ceil($num_rows2 / $block_size);
|
|
|
|
$folder_eqn = uniqid();
|
|
mkdir($folder_eqn);
|
|
$filename_data = $folder_eqn . "/dataarray.json";
|
|
file_put_contents($filename_data, json_encode($dataArray));
|
|
|
|
for ($i=0; $i<$iter; $i++){
|
|
$index_start=$i*$block_size;
|
|
$index_end=($i+1)*$block_size-1;
|
|
if ($index_end>$num_rows2-1){
|
|
$index_end=$num_rows2-1;
|
|
}
|
|
$regidArrayGCM = array_slice($regidArray,$index_start,$index_end-$index_start+1);
|
|
|
|
$filename=$folder_eqn."/gcmid".$i.".json";
|
|
file_put_contents($filename,json_encode($regidArrayGCM));
|
|
|
|
exec("php /var/www/earthquakenetwork.it/mysql/distquake_gcm_report_call.php $filename_data $filename"."> /dev/null 2>/dev/null &");
|
|
sleep(0.1);
|
|
}
|
|
sleep(1);
|
|
$files = glob($folder_eqn.'/*',GLOB_MARK);
|
|
foreach ($files as $file) {
|
|
unlink($file);
|
|
}
|
|
rmdir($folder_eqn.'/');
|
|
|
|
unset($distanceArray);
|
|
unset($regidArray);
|
|
}
|
|
else{
|
|
$keep_going=0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
mysql_close();
|
|
?>
|