<?php 
include_once 'db_functions.php'; 
$db = new DB_Functions(); 
$json = $_POST["tadaJSON"];
///// remove special character
function cleanData($str) {
	$str = preg_replace("/\t/", "", $str); 
	$str = preg_replace("/\r?\n/", "", $str);
	$str = preg_replace("/\n/", "", $str);
	$str = preg_replace("/%0A/", "", $str);
	$str = preg_replace('/\s+/','',$str);
	if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
	return $str;
} 
//////
function filesize_dir($file) {
	exec('dir ' . $file, $inf);
	$size_raw = $inf[6];
	$size_exp = explode(" ",$size_raw);
	$size_ext = $size_exp[19];
	$size_int = (float) str_replace(chr(255), '', $size_ext);
	return $size_int;
}
//////	
if (get_magic_quotes_gpc()){ 
	$json = stripslashes($json); 
}
//Decode JSON into an Array 
$data = json_decode($json); 
//Util arrays to create response JSON 
$a=array();
///// extract JSON
for($i = 0; $i < count($data) ; $i++) {
	$uid = $data[$i]->userid;
	$latitude = $data[$i]->latitude;
	$longitude = $data[$i]->longitude;
	$address = $data[$i]->address;
	$distance = $data[$i]->distance;
	$imei = $data[$i]->imei;
	$modelid = $data[$i]->model_id;
	
	$food_exp = $data[$i]->foodExp;
	$food_base = $data[$i]->foodEncodedStr;
	$food_filename = $data[$i]->foodFileName;
	
	$courier_exp = $data[$i]->courierExp;
	$courier_base = $data[$i]->courierEncodedStr;
	$courier_filename = $data[$i]->courierFileName;
	
	$localconv_exp = $data[$i]->localConvExp;
	$localconv_base = $data[$i]->localConvEncodedStr;
	$localconv_filename = $data[$i]->localConvFileName;
	
	$mobile_exp = $data[$i]->mobileExp;
	$mobile_base = $data[$i]->mobileEncodedStr;
	$mobile_filename = $data[$i]->mobileFileName;
	
	$other_exp = $data[$i]->otherExp;
	$other_base = $data[$i]->otherEncodedStr;
	$other_filename = $data[$i]->otherFileName;
	////// decode image
	if($food_base!=""){
		$food_binary = base64_decode($food_base);
		$food_result = file_put_contents("tada/food/".$food_filename,$food_binary);
	}else{
		$food_binary = NULL;
	}
	if($courier_base!=""){
		$courier_binary = base64_decode($courier_base);
		$courier_result = file_put_contents("tada/courier/".$courier_filename,$courier_binary);
	}else{
		$courier_binary = NULL;
	}
	if($localconv_base!=""){
		$localconv_binary = base64_decode($localconv_base);
		$localconv_result = file_put_contents("tada/local_conv/".$localconv_filename,$localconv_binary);
	}else{
		$localconv_binary = NULL;
	}
	if($mobile_base!=""){
		$mobile_binary = base64_decode($mobile_base);
		$mobile_result = file_put_contents("tada/mobile/".$mobile_filename,$mobile_binary);
	}else{
		$mobile_binary = NULL;
	}
	if($other_base!=""){
		$other_binary = base64_decode($other_base);
		$other_result = file_put_contents("tada/other/".$other_filename,$other_binary);
	}else{
		$other_binary = NULL;
	}
	
	$remk = cleanData($data[$i]->remark);
	//Store TA DA into MySQL DB 
	$res = $db->addTaDa($uid , $food_exp , $food_filename , $courier_exp , $courier_filename , $localconv_exp , $localconv_filename , $mobile_exp , $mobile_filename , $other_exp , $other_filename , $remk,$latitude,$longitude,$address,$distance,$imei,$modelid);
	//Based on inserttion, create JSON response 
	array_push($a,$res);    
}
echo json_encode($a); 
?>