Files
EQN/appServer/distquake_count_widget_gcm.php
2017-09-22 15:16:26 +02:00

127 lines
4.3 KiB
PHP

<?php
// Parse with sections
$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);
}
$sql1=mysql_query("select count(*) as count_earthquakes from earthquakes where donotshow=0");
$sql2=mysql_query("select count(*) as count_green from notifications where latitude<>0 AND longitude<>0 AND enabled=1");
$count_red=0;
$sql4=mysql_query("select least(count(*),999) as count_manual_green from manual where magnitude=1 and ban<3 and donotshow=0 and date > date_sub(now(), interval 1440 minute)");
$sql5=mysql_query("select least(count(*),999) as count_manual_yellow from manual where magnitude=2 and ban<3 and donotshow=0 and date > date_sub(now(), interval 1440 minute)");
$sql6=mysql_query("select least(count(*),999) as count_manual_red from manual where magnitude=3 and ban<3 and donotshow=0 and date > date_sub(now(), interval 1440 minute)");
$dataArray = array('count_quakes'=>mysql_result($sql1,0,"count_earthquakes"),
'count_green'=>mysql_result($sql2,0,"count_green"),
'count_red'=>$count_red,
'count_manual_green'=>mysql_result($sql4,0,"count_manual_green"),
'count_manual_yellow'=>mysql_result($sql5,0,"count_manual_yellow"),
'count_manual_red'=>mysql_result($sql6,0,"count_manual_red"),
'type'=>'counts');
$sql=mysql_query("select user_id from gcm order by user_id limit 1");
$user_id_ref = mysql_result($sql, 0, "user_id");
$user_id_ref=$user_id_ref-1;
$row_limit=250000;
$keep_going=1;
$counter_block=1;
while ($keep_going){
print($counter_block."\r\n");
$sql2=mysql_query("select user_id,reg_id from gcm where reg_id<>'' 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);
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);
$IDArrayGCM = array_slice($IDArray,$index_start,$index_end-$index_start+1);
$headers = array(
'Authorization: key=AIzaSyAYaXDEapyd-mF9V9LNW1dmuoS-ea9GwWI',
'Content-Type: application/json');
$fields = array(
'registration_ids' => $regidArrayGCM,
'time_to_live' => 1500,
'collapse_key' => 'counts_widget',
'priority' => 'normal',
'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);
$response=json_decode($response);
$counter=0;
foreach ($response->results as $item)
{
if (array_key_exists('error', $item))
{
$error=$item->error;
if (strcmp($error,'NotRegistered')==0){
$ID=$IDArrayGCM[$counter];
$sql3=mysql_query("update gcm set to_be_deleted=1 where user_id=$ID");
print("Delete user_id ".$ID." at position ".$counter."\r\n");
}
}
if (array_key_exists('registration_id', $item))
{
$new_reg_id=$item->registration_id;
$ID=$IDArrayGCM[$counter];
$sql3=mysql_query("update gcm set reg_id='$new_reg_id' where user_id=$ID");
print("Replaced the reg_id of user with user_id".$ID." at array position ".$counter." with the new id ".$new_reg_id."\r\n");
}
$counter=$counter+1;
}
}
unset($regidArray);
unset($IDArray);
}
else{
$keep_going=0;
}
$counter_block=$counter_block+1;
}
$sql3=mysql_query("delete from gcm where to_be_deleted>=1");
$output[]="ok";
print(json_encode($output));
mysql_close();
?>