<?php
	header('Content-Type: text/html; charset=utf-8');
	include "logger.php";
	include "settings.php";
	
	$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;
    }
	
	$db2 = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME2);
    $db2->query("SET CHARSET SET utf8");
    $db2->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 <> '.$json );
	
	$data = stripslashes($json);
	$arraydata = (array)json_decode($data);
	
	$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"]);
    
    
    
	$query = "SELECT `imei` FROM `ms_user` WHERE `id` = ".$login.";";
	if ($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($imei);
    $stmt->fetch();
    $stmt->close();
    
    $xml = '<Data Login="'.$imei.'" Password="'.md5($imei).'">
      <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>';
	
    $query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$imei."','".$xml."','".md5($imei)."');";
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
	if ($stmt = $db2->prepare($query)){
      $stmt->execute();
      $stmt->close();
	  echo true;
    }else{ echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$db->errno . ' ' . $db->error); }
      
	
	
	
	}
	
	$db->close();
	$db2->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'] );
	}*/
	
	
?>