<?php
 #header('Content-Type: text/plain; charset=utf-8');
 header('Content-Type: application/json;');

/*Global variable */
$tele_path='/var/www/html/xconf/upload/';
$debug=0;

function getValfromJSON($jsonStr1,$pattern)
{
	$value='';
	foreach($jsonStr1 as $arrIdx =>$jsonStr) {
		if(key($jsonStr)==$pattern) {
			if($debug) print("Matched: ".$jsonStr[$pattern]);
			if($pattern=="mac")
			  $value = str_replace(array(':',' '), '', $jsonStr[$pattern]);
			else
			  $value = $jsonStr[$pattern];
		}
	}
	return $value;
}

try {
	$rtlStr = file_get_contents('php://input');
	$data = json_decode($rtlStr,true);

	$mac_addr=getValfromJSON($data['searchResult'], "mac");

	$time_raw = getValfromJSON($data['searchResult'], "Time");
	$time_str = new DateTime($time_raw);
	$time_conv = $time_str->format('m-d-Y-h-iA');

	$json_file=$tele_path.$mac_addr.'_TELE_'.$time_conv.'.json';
	if($debug) print($json_file);
if (1) {
	$fp = fopen($json_file, 'w');
	fwrite($fp, $rtlStr."\n");
	fclose($fp);
}
} catch (RuntimeException $e) {
	echo $e->getMessage();
}
?>
