<?php
	header('Content-Type: text/html; charset=utf-8');
	include "logger.php";
	include "settings.php";
	
//ini_set('error_reporting', E_ALL);
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
	error_reporting(0);
	
	$db = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
    $db->query("SET CHARSET SET utf8");
    $db->query("SET NAMES 'utf8'");
    if (mysqli_connect_errno()) {
        echo "Error connecting to DB";
		writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> synchroxml <> "Error connecting to DB"');
        exit;
    }
	
	// new location startTrackingService
	$json = file_get_contents('php://input');
	
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> file_get_contents location <> '.$json );
	
	if($json){
	
	$data = stripslashes($json);
	$arraydata = json_decode($data,true);
	
	//if(gettype($arraydata[0]['login']) === 'string' ){
	
	//if(count($arraydata[0] > 0 )){
	//if(count($arraydata) <= 1 ){	
	if(gettype($arraydata[0]['login']) === 'string' ){
	
			if(!isset($arraydata[0]['login']) || $arraydata[0]['login']=="" ){
				writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location array login empty <> ');
				echo false;
			}else{
				
				/*$query = "SELECT `gmt` FROM `wm_settings` WHERE `client_id` = (SELECT Client_id FROM `tms_device` WHERE `Login` = '".$arraydata[0]['login']."')";
				writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location array <> '.$query);
				if($stmt = $db->prepare($query)){
				if($stmt->execute()){
				$stmt->bind_result($gmt);
				$stmt->fetch();
				$stmt->close(); 
				}else{ $gmt=0; }
				}else{ $gmt=0; }
				$datetime = strtotime($arraydata[0]['datetime']) - abs($gmt)*60*60; 
				$datetime = date('Y-m-d H:i:s',$datetime);
				*/
				
				foreach ($arraydata as $key => $value){
					$xml = '<Data Login="'.$value['login'].'" Password="'.md5($value['pwd']).'">
					<p type="DeviceOnLineData"  >
					<p type="GPSData" Latitude="'.$value['InPointGeoY'].'" Longitude="'.$value['InPointGeoX'].'" Sattelites="'.$value['amountOfSatellites'].'" Gsm="" Charge="" Azimuth="'.$value['azimuth'].'" Speed="'.$value['speed'].'"  Altitude="'.$value['altitude'].'"  IsValid="" Time="'.$value['datetime'].'"/>
					</p>
					</Data>';
	
					$query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$value['login']."','".$xml."','".md5($value['pwd'])."');";
					writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
					if ($stmt = $db->prepare($query)){
						$stmt->execute();
						$stmt->close();
						//echo true;
					}else{ writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location array INSERT <> '.$db->errno . ' ' . $db->error); }
				}
				echo true;
			}
		
	}else{
	
	$datetime = $arraydata["datetime"];
    $InPointGeoX = $arraydata["InPointGeoX"];
    $InPointGeoY = $arraydata["InPointGeoY"];
    $azimuth = $arraydata["azimuth"];
    $speed = $arraydata["speed"];
    $altitude = $arraydata["altitude"];
    $login = $arraydata["login"];
    $password = md5($arraydata["pwd"]);
    $Sattelites = $arraydata["amountOfSatellites"];
    
	
	if(!isset($login) || $login=="" ){
		echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location login empty <> ');
	}else{
	
	/*$query = "SELECT `gmt` FROM `wm_settings` WHERE `client_id` = (SELECT Client_id FROM `tms_device` WHERE `Login` = '".$login."')";
	//writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location <> '.$query);
	if($stmt = $db->prepare($query)){
    if($stmt->execute()){
	$stmt->bind_result($gmt);
    $stmt->fetch();
	$stmt->close(); 
    }else{ $gmt=0; }
	}else{ $gmt=0; }
    $datetime = strtotime($datetime) - abs($gmt)*60*60; 
    $datetime = date('Y-m-d H:i:s',$datetime);
	*/
	//writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location GMT<> '.$gmt.' datetime'.$datetime);
	
    /*$xml = '<Data Login="'.$login.'" Password="'.md5($password).'">
      <p type="DeviceOnLineData"  >
        <p type="GPSData" Latitude="'.$InPointGeoY.'" Longitude="'.$InPointGeoX.'" Sattelites="" Gsm="" Charge="" Azimuth="'.$azimuth.'"
         Speed="'.$speed.'"  Altitude="'.$altitude.'"  IsValid="" Time="'.$datetime.'"/>

        <p type="SensorData"  SensorType=""  SensorValue="" Time=""/>
      </p>
	</Data>';*/
	$xml = '<Data Login="'.$login.'" Password="'.md5($password).'">
      <p type="DeviceOnLineData"  >
        <p type="GPSData" Latitude="'.$InPointGeoY.'" Longitude="'.$InPointGeoX.'" Sattelites="'.$Sattelites.'" Gsm="" Charge="" Azimuth="'.$azimuth.'"
         Speed="'.$speed.'"  Altitude="'.$altitude.'"  IsValid="" Time="'.$datetime.'"/>
      </p>
	</Data>';
	
    $query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".md5($password)."');";
    //writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
	if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->close();
	  echo true;
    }else{ echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> location INSERT <> '.$db->errno . ' ' . $db->error); }
    
	
	}
	
	
	}
	
	}
	
	$db->close();
	exit;
	
	//old location runCoordService
	/*
	//ob_start("ob_gzhandler");
	//ini_set('zlib.output_compression', 'On');
	
	//writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> locationAndroid <> '.$_SERVER['REMOTE_ADDR'] );
	
	$data = stripslashes($_REQUEST['location']);
	$arraydata = (array)json_decode($data,true);
	
	//writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$arraydata[0]['longitude'] );
	//writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$arraydata['longitude'] );
	
	$query = "INSERT INTO ms_location (iduser,latitude,longitude) VALUES (".$arraydata[0]['ClientID'].",'".$arraydata[0]['latitude']."','".$arraydata[0]['longitude']."')";
    //writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> location query <> '.$query );
	if($stmt = $db->prepare($query)){
    $stmt->execute();
    echo 'true';
	}else{ echo 'false'; } 
	
	/*foreach($_REQUEST as $key => $value) {
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$key.' has '.$value );
		
		$res = json_decode($value,true);
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$res[0]['longitude'] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$res['longitude'] );
	}*/
	
	
?>