379 lines
14 KiB
PHP
379 lines
14 KiB
PHP
<?php
|
|
//SSN earthquakes
|
|
$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);
|
|
}
|
|
|
|
$ctx = stream_context_create(array(
|
|
'http' => array(
|
|
'timeout' => 50
|
|
)
|
|
)
|
|
);
|
|
|
|
$url="http://www.ssn.unam.mx/sismicidad/ultimos/";
|
|
$data = file_get_contents($url,0,$ctx);
|
|
$data = str_replace("\n", '', $data); // remove new lines
|
|
$data = str_replace("\r", '', $data); // remove carriage returns
|
|
|
|
if ($data!=false)
|
|
{
|
|
//extract the maximum date
|
|
$sql=mysql_query("select date from last_notifications where provider='SSN'");
|
|
if (mysql_num_rows($sql)){
|
|
$max_date=mysql_result($sql,0,"date");
|
|
}
|
|
|
|
//empty the table
|
|
$sql=mysql_query("TRUNCATE automatic_ssn_temp");
|
|
|
|
preg_match_all('|<tr class.*?</tr>|',$data,$result);
|
|
$result=$result[0];
|
|
date_default_timezone_set("Europe/Rome");
|
|
$date1=date("Y-m-d H:i:s", time());
|
|
date_default_timezone_set("America/Mexico_City");
|
|
$date2=date("Y-m-d H:i:s", time());
|
|
$hourdiff = round((strtotime($date2) - strtotime($date1))/3600, 1);
|
|
$delta='-'.$hourdiff.' hour';
|
|
|
|
for ($i=1;$i<count($result);$i++){
|
|
preg_match_all('|<td class.*?</td>|',$result[$i],$block);
|
|
$block=$block[0];
|
|
$idx1=strpos($block[0],'>');
|
|
$idx2=strpos($block[0],'</td>');
|
|
$magnitude=substr($block[0],$idx1+1,$idx2-$idx1-1);
|
|
$magnitude_type='ML';
|
|
|
|
$idx1=strpos($block[1],'</span>');
|
|
$date=substr($block[1],$idx1-10,10);
|
|
|
|
$idx1=strpos($block[1],':');
|
|
$time=substr($block[1],$idx1-2,8);
|
|
|
|
$idx1=strpos($block[2],'>');
|
|
$idx2=strpos($block[2],'km');
|
|
$depth=substr($block[2],$idx1+1,$idx2-$idx1-2);
|
|
|
|
preg_match_all('|<td><b><span id.*?</td>|',$result[$i],$block);
|
|
$block=$block[0];
|
|
|
|
$idx1=strpos($block[0],"'>");
|
|
$idx2=strpos($block[0],'</span>');
|
|
|
|
$place=substr($block[0],$idx1+2,$idx2-$idx1-2);
|
|
|
|
$idx1=strpos($block[0],'</b>');
|
|
$block[0]=substr($block[0],$idx1+4,200);
|
|
|
|
$idx1=strpos($block[0],'>');
|
|
$idx2=strpos($block[0],'</');
|
|
$lat=substr($block[0],$idx1+1,$idx2-$idx1-1);
|
|
|
|
$idx1=strpos($block[0],'</span>');
|
|
$block[0]=substr($block[0],$idx1+7,200);
|
|
|
|
$idx1=strpos($block[0],'>');
|
|
$idx2=strpos($block[0],'</');
|
|
$lon=substr($block[0],$idx1+1,$idx2-$idx1-1);
|
|
|
|
$date=$date.' '.$time;
|
|
$timestamp = strtotime($date);
|
|
$stamp2=strtotime($delta,$timestamp);
|
|
$stamp=date('y-m-d H:i:s',$stamp2);
|
|
$tsunami=-1;
|
|
$code='0';
|
|
|
|
if ($magnitude>=2){
|
|
$lat_idx=$lat;
|
|
$lat_idx=(round($lat_idx*10)/10+0.025)*1000;
|
|
if ($lat_idx>90000){
|
|
$lat_idx=90000-25;
|
|
}
|
|
$lon_idx=$lon;
|
|
$lon_idx=(round($lon_idx*10)/10+0.025)*1000;
|
|
if ($lon_idx>180000){
|
|
$lon_idx=$lon_idx-360000;
|
|
}
|
|
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop_recent where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
if (mysql_num_rows($sql)){
|
|
$pop25=mysql_result($sql,0,"pop25");
|
|
$pop100=mysql_result($sql,0,"pop100");
|
|
$pop200=mysql_result($sql,0,"pop200");
|
|
}
|
|
else{
|
|
if ($lon_idx<=-120025){
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop1 where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
}
|
|
else{
|
|
if ($lon_idx<=-60025){
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop2 where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
}
|
|
else{
|
|
if ($lon_idx<=-25){
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop3 where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
}
|
|
else{
|
|
if ($lon_idx<=59975){
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop4 where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
}
|
|
else{
|
|
if ($lon_idx<=119975){
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop5 where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
}
|
|
else{
|
|
$sql=mysql_query("select pop25,pop100,pop200 from worldpop6 where latitude=$lat_idx and longitude=$lon_idx LIMIT 1");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (mysql_num_rows($sql))
|
|
{
|
|
$pop25=mysql_result($sql,0,"pop25");
|
|
$pop100=mysql_result($sql,0,"pop100");
|
|
$pop200=mysql_result($sql,0,"pop200");
|
|
}
|
|
else
|
|
{
|
|
$pop25=0;
|
|
$pop100=0;
|
|
$pop200=0;
|
|
}
|
|
$sql=mysql_query("insert into worldpop_recent (latitude,longitude,pop25,pop100,pop200) values ($lat_idx,$lon_idx,$pop25,$pop100,$pop200)");
|
|
}
|
|
|
|
$ok=0;
|
|
$sql=mysql_query("select * from weather_recent where abs(latitude-$lat)<0.01 and abs(longitude-$lon)<0.01 and abs(TIMESTAMPDIFF(MINUTE,earthquake_date,'$stamp'))<5 LIMIT 1");
|
|
if (mysql_num_rows($sql)){
|
|
$weather_code=mysql_result($sql,0,"weather_code");
|
|
$temperature=mysql_result($sql,0,"temperature");
|
|
$humidity=mysql_result($sql,0,"humidity");
|
|
$pressure=mysql_result($sql,0,"pressure");
|
|
$wind_speed=mysql_result($sql,0,"wind_speed");
|
|
$wind_dir=mysql_result($sql,0,"wind_dir");
|
|
$clouds=mysql_result($sql,0,"clouds");
|
|
$icon=mysql_result($sql,0,"icon");
|
|
$ok=1;
|
|
}
|
|
else{
|
|
$current_time=time();
|
|
if ($current_time-$stamp2<3600){
|
|
$url_weather="http://api.openweathermap.org/data/2.5/weather?lat=".$lat."&lon=".$lon."&appid=2de02dac92c8a20f8571feb5ad5bdcaa";
|
|
$json = file_get_contents($url_weather);
|
|
$data_weather = json_decode($json, TRUE);
|
|
|
|
$weather_code=$data_weather["weather"][0]["id"];
|
|
$temperature=$data_weather["main"]["temp"];
|
|
$humidity=$data_weather["main"]["humidity"];
|
|
$pressure=$data_weather["main"]["pressure"];
|
|
if (array_key_exists('speed', $data_weather["wind"])){
|
|
$wind_speed=$data_weather["wind"]["speed"];
|
|
}
|
|
else{
|
|
$wind_speed=0;
|
|
}
|
|
if (array_key_exists('deg', $data_weather["wind"])){
|
|
$wind_dir=$data_weather["wind"]["deg"];
|
|
}
|
|
else{
|
|
$wind_dir=0;
|
|
}
|
|
$clouds=$data_weather["clouds"]["all"];
|
|
$icon=$data_weather["weather"][0]["icon"];
|
|
|
|
if (!is_null($weather_code)){
|
|
$sql=mysql_query("insert into weather_recent (latitude,longitude,earthquake_date,weather_code,temperature,humidity,pressure,wind_speed,wind_dir,clouds,icon) values ($lat,$lon,'$stamp',$weather_code,$temperature,$humidity,$pressure,$wind_speed,$wind_dir,$clouds,'$icon')");
|
|
$ok=1;
|
|
}
|
|
}
|
|
else{
|
|
$ok=0;
|
|
}
|
|
}
|
|
|
|
if ($magnitude>=5.5){
|
|
if ($magnitude>=5.5 and $magnitude<6.0)
|
|
{
|
|
$max_distance=40;
|
|
}
|
|
else{
|
|
if ($magnitude>=6.0 and $magnitude<6.5){
|
|
$max_distance=100;
|
|
}
|
|
else{
|
|
if ($magnitude>=6.5 and $magnitude<7.0){
|
|
$max_distance=200;
|
|
}
|
|
else{
|
|
if ($magnitude>=7.0 and $magnitude<7.5){
|
|
$max_distance=500;
|
|
}
|
|
else{
|
|
if ($magnitude>=7.5 and $magnitude<8.0){
|
|
$max_distance=800;
|
|
}
|
|
else{
|
|
if ($magnitude>=8.0 and $magnitude<8.5){
|
|
$max_distance=1000;
|
|
}
|
|
else{
|
|
if ($magnitude>=8.5 and $magnitude<9.0){
|
|
$max_distance=2000;
|
|
}
|
|
else{
|
|
if ($magnitude>=9.0){
|
|
$max_distance=3000;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$max_distance_deg=$max_distance/111;
|
|
$sql=mysql_query("select count(*) as count from manual_picture where abs(latitude-$lat)<$max_distance_deg and abs(longitude-$lon)<$max_distance_deg and abs(TIMESTAMPDIFF(MINUTE,date,'$stamp'))<1440");
|
|
if (mysql_num_rows($sql)){
|
|
$picture_count=mysql_result($sql,0,"count");
|
|
}
|
|
else{
|
|
$picture_count=0;
|
|
}
|
|
}
|
|
else{
|
|
$picture_count=0;
|
|
}
|
|
|
|
if ($ok==1){
|
|
$sql=mysql_query("insert into automatic_ssn_temp (latitude,longitude,depth,magnitude,magnitude_type,pop25,pop100,pop200,place,data,code,tsunami,provider,weather_code,temperature,humidity,pressure,wind_speed,wind_dir,clouds,icon,picture_count) values ($lat,$lon,$depth,$magnitude,'$magnitude_type',$pop25,$pop100,$pop200,'$place','$stamp','$code',$tsunami,'SSN',$weather_code,$temperature,$humidity,$pressure,$wind_speed,$wind_dir,$clouds,'$icon',$picture_count)");
|
|
}
|
|
else{
|
|
$sql=mysql_query("insert into automatic_ssn_temp (latitude,longitude,depth,magnitude,magnitude_type,pop25,pop100,pop200,place,data,code,tsunami,provider,picture_count) values ($lat,$lon,$depth,$magnitude,'$magnitude_type',$pop25,$pop100,$pop200,'$place','$stamp','$code',$tsunami,'SSN',$picture_count)");
|
|
}
|
|
}
|
|
}
|
|
|
|
//empty the table
|
|
$sql=mysql_query("TRUNCATE automatic_ssn");
|
|
$sql=mysql_query("INSERT INTO automatic_ssn SELECT * FROM automatic_ssn_temp");
|
|
$sql=mysql_query("DELETE from automatic_all WHERE provider like 'SSN'");
|
|
$sql=mysql_query("INSERT INTO automatic_all (latitude, longitude, depth, magnitude, magnitude_type, felt, cdi, pop25, pop100, pop200, data, code, place, tsunami, url, provider, weather_code, temperature, humidity, pressure, wind_speed, wind_dir, clouds, icon, picture_count) SELECT latitude, longitude, depth, magnitude, magnitude_type, felt, cdi, pop25, pop100, pop200, data, code, place, tsunami, url, provider, weather_code, temperature, humidity, pressure, wind_speed, wind_dir, clouds, icon, picture_count FROM automatic_ssn_temp");
|
|
|
|
//generate the file
|
|
$filename="seismic_free.txt";
|
|
|
|
$sql=mysql_query('SET CHARACTER SET utf8');
|
|
$sql=mysql_query("SELECT latitude,longitude,depth,magnitude,magnitude_type,pop25,pop100,pop200,cdi AS intensity,place,tsunami,url,provider,data,TIMESTAMPDIFF(MINUTE,data,NOW()) AS difference FROM (select * from automatic_all) as a WHERE a.data > date_sub(NOW(), interval 1440 minute) AND a.magnitude>=2 ORDER BY a.data DESC");
|
|
if (mysql_num_rows($sql)){
|
|
while($e=mysql_fetch_assoc($sql))
|
|
$output_file[]=$e;
|
|
$json_file = json_encode($output_file);
|
|
file_put_contents($filename,$json_file);
|
|
}
|
|
|
|
//extract the new earthquakes
|
|
$sql=mysql_query("select latitude,longitude,magnitude,magnitude_type,place,provider,depth,data,pop100,TIMESTAMPDIFF(MINUTE,data,NOW()) AS difference from automatic_ssn_temp where TIMESTAMPDIFF(MINUTE,'$max_date',data)>1 order by TIMESTAMPDIFF(MINUTE,data,NOW()) desc");
|
|
if (mysql_num_rows($sql))
|
|
{
|
|
$num_rows = mysql_num_rows($sql);
|
|
for ($j = 0; $j < $num_rows; $j++)
|
|
{
|
|
$delay=mysql_result($sql,$j,"difference");
|
|
$sql_z=mysql_query("INSERT INTO delay_ssn (delay) values ($delay)");
|
|
|
|
$dataArray = array('latitude'=>mysql_result($sql,$j,"latitude"),
|
|
'longitude'=>mysql_result($sql,$j,"longitude"),
|
|
'magnitude'=>mysql_result($sql,$j,"magnitude"),
|
|
'magnitude_type'=>mysql_result($sql,$j,"magnitude_type"),
|
|
'place'=>mysql_result($sql,$j,"place"),
|
|
'provider'=>mysql_result($sql,$j,"provider"),
|
|
'depth'=>mysql_result($sql,$j,"depth"),
|
|
'data'=>mysql_result($sql,$j,"data"),
|
|
'difference'=>mysql_result($sql,$j,"difference"),
|
|
'pop100'=>mysql_result($sql,$j,"pop100"),
|
|
'type'=>'official');
|
|
|
|
//extract users for notifications
|
|
|
|
$magnitude_notification=mysql_result($sql,$j,"magnitude");
|
|
$current_date=mysql_result($sql,$j,"data");
|
|
|
|
$sql2=mysql_query("select user_id from gcm order by user_id limit 1");
|
|
$user_id_ref = mysql_result($sql2, 0, "user_id");
|
|
$user_id_ref=$user_id_ref-1;
|
|
$row_limit=250000;
|
|
$keep_going=1;
|
|
|
|
while ($keep_going){
|
|
$sql2=mysql_query("SELECT user_id,reg_id FROM gcm where gcm.reg_id<>'' and gcm.notification_official_ssn=1 and ($magnitude_notification>=gcm.notification_official_minmag or gcm.notification_near=1) and gcm.notification_official=1 and user_id>$user_id_ref order by user_id limit $row_limit");
|
|
$num_rows2 = mysql_num_rows($sql2);
|
|
|
|
if ($num_rows2>0){
|
|
while($data = mysql_fetch_assoc($sql2)){
|
|
$regidArray[] = $data['reg_id'];
|
|
$IDArray[] = $data['user_id'];
|
|
}
|
|
$user_id_ref=end($IDArray);
|
|
|
|
$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_official_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($regidArray);
|
|
unset($IDArray);
|
|
}
|
|
else{
|
|
$keep_going=0;
|
|
}
|
|
}
|
|
|
|
$sql_z=mysql_query("select date FROM last_notifications where provider='SSN'");
|
|
$last_date=mysql_result($sql_z,0,"date");
|
|
if ($current_date>$last_date){
|
|
$sql_z=mysql_query("update last_notifications set date='$current_date' where provider='SSN'");
|
|
}
|
|
}
|
|
$sql_z=mysql_query("select max(ID) as max_id FROM delay_ssn");
|
|
$max_id=mysql_result($sql_z,0,"max_id");
|
|
$sql_z=mysql_query("delete from delay_ssn where ID<$max_id-40");
|
|
}
|
|
}
|
|
else{
|
|
print("Timeout");
|
|
}
|
|
mysql_close();
|
|
?>
|