<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


class IO{
	public static $mail;					
	public static $filecache=array();		
	public static $cachecount=0;			

	public static function sendMail($f,$template,$data=NULL){
		$obj=NULL; $idio="";
		if(!file_exists($template)){
			$obj->result=FALSE; $obj->errCode=404;	
		}
		if(count($data['mails'])==0){
			$obj->result=FALSE; $obj->errCode=900;	
		}
		if(self::$filecache[$template]==NULL){
			$file=file_get_contents($template);
			self::$filecache[$template]=$file;
		}else{
			$file=self::$filecache[$template];
			self::$cachecount++;
		}
		$idio=substr(basename($template),-6,2);		
		foreach($f as $clave0=>$namespace){
			foreach($namespace as $clave1=>$valor){
				$cadena="{{".$clave0.":".$clave1."}}";
				if(stripos((string)$valor,"<!-- lang:{$idio} -->")!==FALSE){
					$valor=Content::extractLang($valor, $idio);
				}
				$file=str_replace($cadena,$valor,$file);
			}
		}
		self::$mail->Body=$file;
		if($data['Subject']==NULL || $data['Subject']==''){
			$temp=preg_match("/<title>(.*?)<\/title>/s",$file,$matches);
			self::$mail->Subject=$matches[1];
		}else{
			self::$mail->Subject=$data['Subject'];
		}
		
		foreach($data['mails'] as $m){
			self::$mail->AddAddress($m);
		}
		if(self::$mail->Send()){
			$obj->result=TRUE; $obj->errCode=200;	
			self::$mail->ClearAllRecipients();
		}else{
			debug(self::$mail, true);
			$obj->result=FALSE; $obj->errCode=500;	
		}
		return $obj;
	}
}
?>