<?php
$gaid = $_GET['gaid'];
$token = $_GET['token'];
$device = $_GET['device'];
$subscription = "miintasca";

$n_generic = $_GET['n_generic'];
$n_sport = $_GET['n_sport'];
$n_cultura = $_GET['n_cultura'];
$n_eventi = $_GET['n_eventi'];
$n_mobilita = $_GET['n_mobilita'];
$n_economia = $_GET['n_economia'];
$n_nightlife = $_GET['n_nightlife'];

if (!ini_get('date.timezone')) {
	date_default_timezone_set('Europe/Rome');
}
$timestamp = date("Y-m-d H:i:s");

$db = new mysqli("localhost", "userintasca", "userintasca", "MilanoInTasca");
if ($db->connect_error){
	die("ERROR: Connection failsed: " . $db->connect_error);
}

$data_saved = array();
$sql = "select * from devices where gaid = '".$gaid."'";

if ($res = $db->query($sql)){
	$num_rows = $res->num_rows;
	if ($num_rows > 0){
		while ($row_data = $res->fetch_assoc()){
			//if ($token != $row_data["token"]){
			if (empty($n_generic) && empty($n_sport) && empty($n_cultura) && empty($n_eventi) && empty($n_mobilita) && empty($n_economia) && empty($n_nightlife)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && empty($n_sport) && empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && empty($n_sport) && empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && empty($n_sport) && empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && !empty($n_sport) && empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_sport = '".$n_sport."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && !empty($n_sport) && empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_sport = '".$n_sport."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && !empty($n_sport) && empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_sport = '".$n_sport."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_cultura = '".$n_cultura."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && !empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_cultura = '".$n_cultura."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && !empty($n_cultura) && !empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_cultura = '".$n_cultura."', n_eventi = '".$n_eventi."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && !empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_cultura = '".$n_cultura."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && empty($n_cultura) && !empty($n_eventi) && empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_eventi = '".$n_eventi."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && empty($n_cultura) && !empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_eventi = '".$n_eventi."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (empty($n_generic) && empty($n_sport) && empty($n_cultura) && empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}else if (!empty($n_generic) && !empty($n_sport) && !empty($n_cultura) && !empty($n_eventi) && !empty($n_mobilita)){
				$sql = "UPDATE devices SET token = '".$token."', timestamp = '".$timestamp."', n_generic = '".$n_generic."', n_sport = '".$n_sport."', n_cultura = '".$n_cultura."', n_eventi = '".$n_eventi."', n_mobilita = '".$n_mobilita."' where gaid = '".$gaid."'";
			}



                        if (!$db->query($sql)){
                        	echo "Error: " . $sql . "<br>" . $db->error;
                        }
                        //}					

        	}
	}else{
		$sql2 = "INSERT INTO devices (token, gaid, type, timestamp, n_generic, n_sport, n_cultura, n_eventi, n_mobilita, n_economia, n_nightlife) VALUES ('".$token."', '".$gaid."', '".$device."', '".$timestamp."','yes','yes','yes','yes','yes','yes','yes')";
                if (!$db->query($sql2)){
	                echo "Error: " . $sql2 . "<br>" . $db->error;
                }

	}

} 
$db->close();
?>
