<?php
  header('Content-Type: text/html; charset=utf-8');
  include "logger.php";
  include "settings.php";
  //ob_start("ob_gzhandler");
  //ini_set('zlib.output_compression', 'On');
		
    $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";
    exit;
    }
		
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> '.$_SERVER['REMOTE_ADDR'] );
	
	if(isset($_FILES)){
	foreach($_FILES as $key => $value) {
		//writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _FILES parameter: '.$key.' has '.$value );
		
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _FILES parameter name : '.$_FILES[$key]['name'] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _FILES parameter tmp_name : '.$_FILES[$key]['tmp_name'] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _FILES parameter size : '.$_FILES[$key]['size'] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _FILES parameter type : '.$_FILES[$key]['type'] );
		
		//$dir = $_SERVER['DOCUMENT_ROOT']."/Logist/files/";
		$dir = $IMAGE_PATH;

		//$filename = time().'.'.pathinfo($_FILES[$key]['name'], PATHINFO_EXTENSION);
		$filename = $_FILES[$key]['name'];
		
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid dir '.$dir.$filename );
		
		if(!move_uploaded_file($_FILES[$key]['tmp_name'], $dir.$filename) ){      
				writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid error move_uploaded_file' );
				echo false; exit;
		}else{
				writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid success move_uploaded_file' );
				echo true; exit;
		}
	}
	}
	
	if(isset($_REQUEST)){
		
		$data = stripslashes( $_REQUEST['data'] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$data );
		$arraydata = (array)json_decode($data,true);
		if 	(count($arraydata)==0)
		{
			$arr_data=explode('?',$data,2);
		}
		
		$sendFotoData = $arr_data[1];        
        $sendFotoDataArray = explode(';;;;;', $sendFotoData);
        
		$pathArray = explode("/",$sendFotoDataArray[0]);
        $namePhoto = $pathArray[4];
		
		$description = $sendFotoDataArray[1];
		
		if($sendFotoDataArray[2]==0){ $status = 'Черновик'; }else{ $status = 'Чистовик'; }
		if($sendFotoDataArray[3]==0){ $type = 'Общий вид'; }elseif($sendFotoDataArray[3]==1){$type='Полка';}else{ $type = 'Стелаж'; }
		
		$date = $sendFotoDataArray[4];
		$latitude = $sendFotoDataArray[5];
		$longitude = $sendFotoDataArray[6];
		$id_cat = $sendFotoDataArray[7];
		$id_brand = $sendFotoDataArray[8];
		$id_visit = $sendFotoDataArray[9];
		
		$query = "INSERT INTO `ms_photo` (`photo`,`description`,`status`,`type`,`date`,`latitude`,`longitude`,`id_cat`,`id_brand`,`id_visit`) VALUES('".$IMAGE_PATH2.$namePhoto."', '".$description."', '".$status."', '".$type."', '".$date."', '".$latitude."', '".$longitude."', ".$id_cat.", ".$id_brand.", ".$id_visit.");";
		writeToLogFile( date('Y-m-d H:i:s').' | query | '.$query );
		$stmt = $db->prepare($query);
		$stmt->execute();
        $stmt->close();
		
		$query = "UPDATE ms_visit SET photo = 1 WHERE `id` = ".$id_visit.";";
		writeToLogFile( date('Y-m-d H:i:s').' | query | '.$query );
		$stmt = $db->prepare($query);
		$stmt->execute();
        $stmt->close();
		
		echo true; exit;
		$db->close();
		/*
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[0] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[1] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[2] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[3] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[4] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[5] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[6] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[7] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[8] );
		writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$sendFotoDataArray[9] );
		*/
		
		
		
	}
	
	//foreach($_REQUEST as $key => $value) {
	//	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroAndroid <> _REQUEST parameter: '.$key.' has '.$value );
	//}
?>