<?php 
  //include "../../settings.php";
  include "settings.php";
  include "logger.php";
  
  $db = new mysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME2);
  $db->query("SET CHARSET SET utf8");
  $db->query("SET NAMES 'utf8'");
  if (mysqli_connect_errno()) {
    echo "Error connecting to DB";
    exit;
  }
  
	$db2 = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
    $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 FOR startTrackingService
  
	$json = file_get_contents('php://input');
	//$json = '[{"pwd":"123","providerName":"network","speed":0,"altitude":0,"login":"SGR1","InPointGeoX":36.2828654,"amountOfSatellites":0,"azimuth":"0","InPointGeoY":50.0459029,"datetime":"2016-05-26 12:47:00"}]';
	//$json = '{"pwd":"123","providerName":"network","speed":0,"altitude":0,"login":"SGR1","InPointGeoX":36.2828654,"amountOfSatellites":0,"azimuth":"0","InPointGeoY":50.0459029,"datetime":"2016-05-26 12:47:00"}';	
	
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> file_get_contents json <> '.$json );
	if($json){
	
		$data = stripslashes($json);
		$arraydata = (array)json_decode($data,true);
		
	if(count($arraydata) <= 1 ){
			if(!isset($arraydata[0]['login']) || $arraydata[0]['login']=="" ){
				writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents array login empty <> ');
				echo false;
			}else{
				
				$query = "SELECT `imei` FROM `toir_users` WHERE `name` = '".$arraydata[0]['login']."';";
				writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> file_get_contents array query <> '.$query );
				if ($stmt = $db2->prepare($query)){
				$stmt->execute();
				$stmt->bind_result($imei);
				$stmt->fetch();
				$stmt->close();
				
				if(!$imei || $imei==""){
					echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents array imei empty <> '.$imei); exit;
				}else{
				
				foreach ($arraydata as $key => $value){
					$xml = '<Data Login="'.$imei.'" Password="'.md5($imei).'">
					<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')."','".$imei."','".$xml."','".md5($imei)."');";
					writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> file_get_contents array query <> '.$query );
					if ($stmt = $db->prepare($query)){
						$stmt->execute();
						$stmt->close();
						//echo true;
					}else{ writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents array INSERT <> '.$db->errno . ' ' . $db->error); }
				}
				echo true;
				
				}
				}else{ echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents <> '.$db->errno . ' ' . $db->error); }
			}
	}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').' <> file_get_contents location login empty <> ');
		}else{
			
				$query = "SELECT `imei` FROM `toir_users` WHERE `name` = '".$login."';";
				writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> file_get_contents array query <> '.$query );
				if ($stmt = $db2->prepare($query)){
				$stmt->execute();
				$stmt->bind_result($imei);
				$stmt->fetch();
				$stmt->close();
				
				if(!$imei || $imei==""){
					echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents array imei empty <> '.$imei); exit;
				}else{
			
		$xml = '<Data Login="'.$imei.'" Password="'.md5($imei).'">
		<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')."','".$imei."','".$xml."','".md5($imei)."');";
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> file_get_contents query <> '.$query );
		if ($stmt = $db->prepare($query)){
			$stmt->execute();
			$stmt->close();
			echo true;
		}else{ echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents INSERT <> '.$db->errno . ' ' . $db->error); }
		
				}
				}else{ echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> file_get_contents <> '.$db->errno . ' ' . $db->error); }
				
				
		}
	}
	
	}
	$db->close();
	$db2->close();
	exit;
	

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//OLD FOR sendLocation
/*	$data = $_POST['data'];
	$data = stripslashes($data);
	$arraydata = (array)json_decode($data);
	
  if ($arraydata['func']=='DeviceOnLineData')
  {
    $datetime = $arraydata["datetime"];
    $InPointGeoX = $arraydata["InPointGeoX"];
    $InPointGeoY = $arraydata["InPointGeoY"];
    $azimuth = $arraydata["azimuth"];
    $speed = $arraydata["speed"];
    $altitude = $arraydata["altitude"];
    //$id_trip = $arraydata["id_trip"];
    //$id_point = $arraydata["id_point"];
    //$login = $arraydata["id_user"];
    $login = $arraydata["login"];
    $password = md5($arraydata["pwd"]);
    
	if(!isset($login) || $login=="" ){
		echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> login empty <> '.$login); exit;
	}else{
	
	
	$query = "SELECT `imei` FROM `toir_users` WHERE `name` = ".$login.";";
	if ($stmt = $db2->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($imei);
    $stmt->fetch();
    $stmt->close();
	
	if(!$imei || $imei==""){
		echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> synchroxml imei empty <> '.$imei); exit;
	}else{
	
    /*$xml = '<Data Login="'.$login.'" Password="'.$password.'">
      <p type="DeviceOnLineData"  >
        <p type="GPSData" Latitude="'.$InPointGeoX.'" Longitude="'.$InPointGeoY.'" 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="'.$password.'">
      <p type="DeviceOnLineData"  >
        <p type="GPSData" Latitude="'.$InPointGeoX.'" Longitude="'.$InPointGeoY.'" 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."','".$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').' <> synchroxml <> '.$db->errno . ' ' . $db->error); }
    
	}
	
	}else{ echo false; writeToLogFile(' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$db->errno . ' ' . $db->error); }
	
	$db->close();
	$db2->close();
	exit;
	
	}
	
	
  }
	
*/	
	
	
?>