264 lines
9.8 KiB
PHP
264 lines
9.8 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']);
|
|
|
|
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;
|
|
}
|
|
|
|
if ($user_id<>0 && $banned==0){
|
|
$randcode=mt_rand(1000000,9999999);
|
|
$user_latitude=$_REQUEST['lat'];
|
|
$user_longitude=$_REQUEST['lon'];
|
|
$sql=mysql_query("SELECT count(*) as users FROM notifications_copy WHERE abs(latitude-$user_latitude)<0.3 and abs(longitude-$user_longitude)<0.3");
|
|
$users=mysql_result($sql,0,"users");
|
|
|
|
if ($users<0){
|
|
#se ci sono meno di 4 utenti attivi consenti di mostrare la segnalazione del terremoto. Attualmente disabilitato per evitare falsi allarmi!!
|
|
$donotshow=0;
|
|
}
|
|
else{
|
|
#restituisce il numero di terremoti segnalati entro un raggio di circa 30km e negli ultimi 10 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 10 minute) and user_id!=$user_id");
|
|
$eqn=mysql_result($sql,0,"eqn");
|
|
#se è già stato segnalato un certo numero di terremoti (frazione degli utenti attivi) consenti di mostrare la segnalazione del terremoto
|
|
$limit=floor($users/100);
|
|
if ($eqn>$limit || $eqn>4){
|
|
$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 5 minuti
|
|
if ($donotshow==0){
|
|
#restituisce il numero di terremoti segnalati E NOTIFICATI 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.3 and abs(longitude-$user_longitude)<0.3 and date > date_sub(now(), interval 2 minute) and user_id!=$user_id and donotshow=0");
|
|
$eqn=mysql_result($sql,0,"eqn");
|
|
|
|
if ($eqn==0){
|
|
$dataArray = array('latitude'=>$user_latitude,
|
|
'longitude'=>$user_longitude,
|
|
'magnitude'=>$mag,
|
|
'place'=>$address,
|
|
'type'=>'manual');
|
|
|
|
//extract users for notifications
|
|
$sql2=mysql_query("SELECT reg_id FROM gcm LEFT JOIN subscriptions ON gcm.user_id = subscriptions.user_id ORDER BY top_100 DESC, top_1000 DESC, top_10000 DESC, subscriptions.date");
|
|
$counter=0;
|
|
$num_rows = mysql_num_rows($sql2);
|
|
for ($i = 0; $i < $num_rows; $i++)
|
|
{
|
|
$regidArray[$counter]=mysql_result($sql2,$i,"reg_id");
|
|
$counter=$counter+1;
|
|
if ($counter==500 or $i==($num_rows-1))
|
|
{
|
|
$headers = array(
|
|
'Authorization: key=AIzaSyAYaXDEapyd-mF9V9LNW1dmuoS-ea9GwWI',
|
|
'Content-Type: application/json');
|
|
$fields = array(
|
|
'registration_ids' => $regidArray,
|
|
'data' => $dataArray);
|
|
|
|
$context = stream_context_create(array(
|
|
'http' => array(
|
|
'method' => 'POST',
|
|
'header' => $headers,
|
|
'content' => json_encode($fields))));
|
|
$response = file_get_contents('https://android.googleapis.com/gcm/send', FALSE, $context);
|
|
var_dump($response);
|
|
$counter=0;
|
|
$regidArray=array();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($donotshow==0){
|
|
$sql2=mysql_query("SELECT * FROM manual WHERE abs(latitude-$user_latitude)<0.3 and abs(longitude-$user_longitude)<0.3 and date > date_sub(now(), interval 20 minute)");
|
|
$num_rows2 = mysql_num_rows($sql2);
|
|
for ($j=0;$j<$num_rows2;$j++){
|
|
$donotshow_current=mysql_result($sql2,$j,"donotshow");
|
|
#rendi visibili le segnalazioni di terremoto che possono essere rese visibili
|
|
if ($donotshow_current==1){
|
|
$ID_current=mysql_result($sql2,$j,"ID");
|
|
$sql3=mysql_query("update manual set donotshow=0 where ID=$ID_current");
|
|
}
|
|
}
|
|
}
|
|
|
|
#Notifica mail del terremoto segnalato con la richiesta php attuale
|
|
if ($donotshow==0){
|
|
$sql=mysql_query("select ID,user_id,address,latitude,longitude,language,radius,verified from mailservice where verified=1 and latitude<>0 and longitude<>0 and user_id<>$user_id and last_mail < date_sub(now(), interval 30 minute)");
|
|
if (mysql_num_rows($sql)){
|
|
$lat1=$_REQUEST['lat'];
|
|
$lon1=$_REQUEST['lon'];
|
|
$mag=$_REQUEST['mag'];
|
|
$num_rows = mysql_num_rows($sql);
|
|
for ($i=0;$i<$num_rows;$i++){
|
|
$lat2=mysql_result($sql,$i,"latitude");
|
|
$lon2=mysql_result($sql,$i,"longitude");
|
|
$radius=mysql_result($sql,$i,"radius");
|
|
$language=mysql_result($sql,$i,"language");
|
|
$mail_address=mysql_result($sql,$i,"address");
|
|
$ID=mysql_result($sql,$i,"ID");
|
|
if ($radius==0){
|
|
$min_distance=50;
|
|
}
|
|
if ($radius==1){
|
|
$min_distance=100;
|
|
}
|
|
if ($radius==2){
|
|
$min_distance=200;
|
|
}
|
|
if ($radius==3){
|
|
$min_distance=400;
|
|
}
|
|
if ($radius==4){
|
|
$min_distance=1000;
|
|
}
|
|
|
|
$dlat=($lat1-$lat2)/180*pi();
|
|
$dlon=($lon1-$lon2)/180*pi();
|
|
$lat1=$lat1/180*pi();
|
|
$lat2=$lat2/180*pi();
|
|
$a=pow(sin($dlat/2),2)+cos($lat1)*cos($lat2)*pow(sin($dlon/2),2);
|
|
$c=2*atan2(sqrt($a),sqrt(1-$a));
|
|
$distance=6371*$c;
|
|
if ($distance<$min_distance){
|
|
if($language==0){
|
|
if ($mag==1){
|
|
$magnitude="LEGGERO";
|
|
}
|
|
if ($mag==2){
|
|
$magnitude="FORTE";
|
|
}
|
|
if ($mag==3){
|
|
$magnitude="MOLTO FORTE";
|
|
}
|
|
$message = "Un utente dell'applicazione Earthquake Network ha segnalato un terremoto ".$magnitude;
|
|
if ($address != ''){
|
|
$message.=" a ".$address;
|
|
}
|
|
$message.=" a ".round($distance)." km da te.";
|
|
$ban=0;
|
|
if ($mag==1 and $distance<50){
|
|
$ban=1;
|
|
}
|
|
if ($mag==2 and $distance<150){
|
|
$ban=1;
|
|
}
|
|
if ($mag==3 and $distance<350){
|
|
$ban=1;
|
|
}
|
|
if ($ban){
|
|
$message.="\r\n\r\nSe ritieni che questo terremoto non si e' verificato puoi segnalarlo come falso allarme attraverso l'applicazione Earthquake Network entro 3 ore da questo momento.";
|
|
}
|
|
$message.="\r\n\r\nSe non vuoi più ricevere queste mail di notifica disiscriviti usando l'applicazione Earthquake Network installata sul tuo smartphone o invia una mail a finazzif@gmail.com comunicando l'intenzione di volersi disiscrivere.";
|
|
$subject = "Terremoto segnalato da utente";
|
|
}
|
|
if($language==1){
|
|
if ($mag==1){
|
|
$magnitude="MILD";
|
|
}
|
|
if ($mag==2){
|
|
$magnitude="STRONG";
|
|
}
|
|
if ($mag==3){
|
|
$magnitude="VERY STRONG";
|
|
}
|
|
$message = "A user of the Earthquake Network application reported a ".$magnitude." earthquake";
|
|
if ($address != ''){
|
|
$message.=" in ".$address;
|
|
}
|
|
$message.=" at ".round($distance)." km from your position.";
|
|
$ban=0;
|
|
if ($mag==1 and $distance<50){
|
|
$ban=1;
|
|
}
|
|
if ($mag==2 and $distance<150){
|
|
$ban=1;
|
|
}
|
|
if ($mag==3 and $distance<350){
|
|
$ban=1;
|
|
}
|
|
if ($ban){
|
|
$message.="\r\n\r\nIf you think this earthquake did not happen you can report it as a false allarm within 3 hours from now using the Earthquake Network application.";
|
|
}
|
|
$message.="\r\n\r\nIf you don't want to receive this e-mail please unsubscribe using the Earthquake Network application or send an e-mail to finazzif@gmail.com ";
|
|
$subject = "Earthquake reported by user";
|
|
}
|
|
if($language==2){
|
|
if ($mag==1){
|
|
$magnitude="TEMPLADO";
|
|
}
|
|
if ($mag==2){
|
|
$magnitude="FUERTE";
|
|
}
|
|
if ($mag==3){
|
|
$magnitude="MUY FUERTE";
|
|
}
|
|
$message = "Una aplicación de usuario de la Red Sísmica registró un sismo ".$magnitude;
|
|
if ($address != ''){
|
|
$message.=" a ".$address;
|
|
}
|
|
$message.=" a ".round($distance)." km de usted.";
|
|
$ban=0;
|
|
if ($mag==1 and $distance<50){
|
|
$ban=1;
|
|
}
|
|
if ($mag==2 and $distance<150){
|
|
$ban=1;
|
|
}
|
|
if ($mag==3 and $distance<350){
|
|
$ban=1;
|
|
}
|
|
if ($ban){
|
|
$message.="\r\n\r\nSi usted cree que este terremoto no ocurrió puedes reportarlo como una falsa alarma mediante la aplicación de la Red Sísmica de 3 horas a partir de ahora.";
|
|
}
|
|
$message.="\r\n\r\nSi usted no desea recibir estas alertas de correo electrónico con el terremoto de darse de baja aplicación de red instalado en su teléfono o enviar un correo electrónico a finazzif@gmail.com diciéndoles de querer darse de baja.";
|
|
$subject = "Terremoto reportado por un usuario";
|
|
}
|
|
|
|
$message = wordwrap($message, 120, "\r\n");
|
|
$headers = 'From: Earthquake Network <alert@earthquakenetwork.it>'."\r\n".
|
|
'Reply-To: alert@earthquakenetwork.it'."\r\n".
|
|
'X-Mailer: PHP/' . phpversion();
|
|
mail($mail_address, $subject, $message,$headers);
|
|
|
|
$sql5=mysql_query("update mailservice set last_mail=now() where ID=$ID");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
mysql_close();
|
|
?>
|