<?
session_name('portal_absa');
session_start();
if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){
	?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?
	exit;
}
require("../MandaCorreos/FuncionSendMail.php");
require "../conexion/conectar.php";
require "../libs/funciones.php";

$conexion = new CONDB();
$conexion->conectar();

$query="Select numeroevento,evento,fechaevento,status,comentario from detallerecibedoce 
where rfcEmisor='".urldecode($_POST['rfcemisor'])."' and tipoDocto='".$_POST['tipodocto']."' and serie='".$_POST['serie']."' and folio='".$_POST['folio']."' and uuid='".$_POST['uuid']."' and
fecharecibido='".str_replace("A",":",str_replace("--"," ",$_POST['fecharecibido']))."'";
$detalleResult = mysqli_query($conexion->CONEXION,$query);

$query2="Select r.fechadocto,r.rfc_receptor,t.nombre,r.nombreemi from recibedoce r,receptor t
where t.rfc=r.rfc_receptor and r.rfcEmisor='".urldecode($_POST['rfcemisor'])."' and r.tipoDocto='".$_POST['tipodocto']."' 
and r.serie='".$_POST['serie']."' and r.folio='".$_POST['folio']."' and r.uuid='".$_POST['uuid']."' limit 1";
$encResult = mysqli_query($conexion->CONEXION,$query2);

if(mysqli_num_rows($encResult)>0){
	$tmpEnc = mysqli_fetch_array($encResult,MYSQLI_ASSOC);
}

if(file_exists("../config.ini")){
		$vlineas = file("../config.ini");
		foreach ($vlineas as $sLinea) {
			if(substr($sLinea,0,20) == "CUERPO_VALIDACIONES="){
				$body=trim(str_replace("CUERPO_VALIDACIONES=","",$sLinea));
			}else if(substr($sLinea,0,20) == "ASUNTO_VALIDACIONES="){
				$SubjectMessage=trim(str_replace("ASUNTO_VALIDACIONES=","",$sLinea));
			}
		}
		$encabezado = "";
		$detalle = "";
		$pie = "";
		$ban=true;
		$ban2=true;
		
		$lin = file($body);
		foreach ($lin as $sLinea) {
			if(trim($sLinea) != "<SPLIT>"){
				if($ban){
					$encabezado.=$sLinea;
				}elseif(trim($sLinea) != "</SPLIT>"){
					if($ban2)
						$detalle.=$sLinea;
					else
						$pie.=$sLinea;
				}else{$ban2=false;}
					
			}else{
				$ban=false;
			}
		}
		$multiDetalle="";
		
		$arrLbl=array("<<NO_EVENTO>>","<<EVENTO>>","<<COMENTARIO>>");
		$arrVal=array("numeroevento","evento","comentario");
		$TIPO_CORREO = getINI("FUNCION_ENVIA_CORREO=","../config.ini");
		$FROM = getINI("FROM_CORREO=","../config.ini");
	if(mysqli_num_rows($detalleResult) > 0){
		while($tmpDetalle = mysqli_fetch_array($detalleResult,MYSQLI_ASSOC)){
			$icono="";
			$auxDetalle=$detalle;
			for($i=0; $i < count($arrLbl); $i++){
				$auxDetalle=str_replace($arrLbl[$i],stripAccents($tmpDetalle[$arrVal[$i]]),$auxDetalle);
			}
			if($tmpDetalle["status"]=="A")
				$icono="acceptHand.png";
			elseif($tmpDetalle["status"]=="R")
				$icono="rejectHand.png";
			elseif($tmpDetalle["status"]=="W")
				$icono="Attention.png";
			elseif($tmpDetalle["status"]=="V")
				$icono="verificadoSAT.png";
			elseif($tmpDetalle["status"]=="?")
				$icono="about.png";
			elseif($tmpDetalle["status"]=="C")
				$icono="Cancel.png";
			else
				$icono="watch.png";
			
			
			$auxDetalle=str_replace("<<SAT>>","",str_replace("<<STATUS>>",$icono,$auxDetalle));
			$multiDetalle.=$auxDetalle;
		}
	
		
		$arrEncLbl=array("<<RFC_REC>>","<<RECEPTOR>>","<<SERIE>>","<<FOLIO>>","<<UUID>>","<<FECHA_D>>",
		"<<FECHA_R>>","<<RFC_EMISOR>>","<<EMISOR>>");
		$arrEncVal=array($tmpEnc["rfc_receptor"],$tmpEnc["nombre"],$_POST["serie"],$_POST["folio"],$_POST["uuid"],
		$tmpEnc["fechadocto"],str_replace("A",":",str_replace("--"," ",$_POST['fecharecibido'])),
		urldecode($_POST['rfcemisor']),$tmpEnc["nombreemi"]);
			
		$body=$encabezado.$multiDetalle.$pie;
		
		for($i=0; $i < count($arrEncLbl); $i++){
				$body=str_replace($arrEncLbl[$i],stripAccents($arrEncVal[$i]),$body);
				$SubjectMessage=str_replace($arrEncLbl[$i],stripAccents($arrEncVal[$i]),$SubjectMessage);
			}
			
		
		
		
		//$SubjectMessage=str_replace("<<SERIEFOLIO>>",$nombreEmpresa,str_replace("<<USERNAME>>",$usr,str_replace("<<NOMBRE>>",$nombre,str_replace("<<EMAIL>>",$email,$SubjectMessage))));
		//$BodyHTMLFileMessage=str_replace("<<RAZONSOCIALURL>>",utf8_decode($nombreEmpresa),str_replace("<<RAZONSOCIAL>>",$nombreEmpresa,str_replace("<<ID>>",md5($_REQUEST['pass']),str_replace("<<EMAIL>>",$email,str_replace("<<NOMBRE>>",$nombre,str_replace("<<USERNAME>>",$usr,file_get_contents($body)))))));
		//echo $BodyHTMLFileMessage;
		$AdjuntoA="";
		$AdjuntoB="";
		$AdjuntoC="";
		if(strtoupper(trim($TIPO_CORREO)) == "MAIL"){
			if(!mail($ParaEmail,$SubjectMessage,$BodyHTMLFileMessage,'From: '.$FROM))
				echo "No se pudo enviar el correo";
			else
				echo "Correo enviado correctamente";
		}else{
			if(!SendMail($_REQUEST["correos"],$_REQUEST["correos"],$SubjectMessage,$body,"","",""))
				echo "No se pudo enviar el correo";
			else
				echo "Correo enviado correctamente";
		}
	}
	
}else{echo "La configuración no se puede leer";}

function stripAccents($string){

	$acentuadas=array("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ñ","Ñ");
	$sinacentuar=array("&aacute;","&eacute;","&iacute;","&oacute;","&uacute;","&Aacute;","&Eacute;","&Iacute;","&Oacute;","&Uacute;","&ntilde;","&Ntilde;");
	
	for($i=0; $i < count($acentuadas);$i++){
		$string = str_replace($acentuadas[$i],$sinacentuar[$i],$string);
	}
	return $string;
	/*return strtr($string,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
	'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');*/
}
?>
<html>
<body>

</body>
</html>