<?php  
	date_default_timezone_set('America/Mazatlan');
	include "conexion.php";
	//include "insertar_log.php";
	$id_aplicacion = $_GET['id_aplicacion'];
	$texto = $_GET['texto'];
	$fecha = $_GET['fecha'];

	insertarLogAfirme($texto,$id_aplicacion,$fecha);

	echo response(0,"OK",$texto);


	function response($estatus,$estatus_mensaje,$respuesta){
		header("HTTP/1.1".$estatus);

		$response["estatus"]=$estatus;
		$response["estatus_mensaje"]=$estatus_mensaje;
		$response["respuesta"]=$respuesta;
		
		$json_response = json_encode($response, JSON_UNESCAPED_UNICODE);

		echo $json_response;

	}
	
	function insertarLogAfirme($datos,$id_aplicacion,$fecha){
		include "conexion.php";
		//AQUI SE VERIFIFCARIA EL PARAMETRO DE LOG_ANDROID	

		$sentencia = $conexion->prepare("SELECT valor FROM parametros where id_parametro='log_android' ");
		$sentencia->execute();

		$resultado = $sentencia->get_result();
		$valor_log=0;

		while($fila = $resultado->fetch_assoc())
		{
			$valor_log=$fila['valor'];
		}

		$sentencia->close();
		
		if($valor_log==1){
			//$milisegundos = round(microtime(true) * 1000);
			$date = date_create();
			$cadena_fecha_actual = date_format($date, 'Y-m-d H:i:s.u');
			$logFile = fopen("logs/log_afirme_".$id_aplicacion."_".$fecha.".txt", 'a') or die("Error creando archivo");
			fwrite($logFile, "\n".$cadena_fecha_actual." id_app:".$id_aplicacion.$datos) or die("Error escribiendo en el archivo");fclose($logFile);	
		}
	}



?>