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


class Content{
	public static $prefs=NULL;						
	public static $res=NULL;						
	public static $data=array();					
	public static $dateformat="d/m/Y";				
	public static $datetimeformat="d/m/Y H:i:s";	
	public static $timeformat="H:i:s";				
	
	public static function parse($text, $f=NULL){
		$matches=array();
		preg_match_all('/{{(.*?)}}/', $text, $matches);
		for($i=0; $i<count($matches[0]); $i++){
			$completa=$matches[0][$i]; $marca=$matches[1][$i]; $partes=explode(":", $marca); $namespace=$partes[0]; $variable=$partes[1];
			switch($namespace){
				case "session":			
					$text=str_replace($completa,$_SESSION[$variable],$text);	
					break;
				case "post":			
					$text=str_replace($completa,$_POST[$variable],$text);
					break;
				case "get":				
					$text=str_replace($completa,$_GET[$variable],$text);
					break;
				case "request":			
					$text=str_replace($completa,$_REQUEST[$variable],$text);
					break;
				case "cookie":			
					$text=str_replace($completa,$_COOKIE[$variable],$text);
					break;
				case "server":			
					$text=str_replace($completa,$_SERVER[$variable],$text);
					break;
				case "prefs":			
					$text=str_replace($completa,self::$prefs[$variable],$text);
					break;
				case "date":			
					if(stripos($variable,"noformat.")!==false){
						$formato="Y-m-d";
						$variable=str_replace("noformat.","",$variable);
					}else{
						$formato=self::$dateformat;
					}
					$text=str_replace($completa,date($formato, strtotime($variable)),$text);
					break;
				case "datetime":		
					if(stripos($variable,"noformat.")!==false){
						$formato="Y-m-d H:i:s";
						$variable=str_replace("noformat.","",$variable);
					}else{
						$formato=self::$datetimeformat;
					}
					$text=str_replace($completa,date($formato, strtotime($variable)),$text);
					break;
				case "time":		
					if(stripos($variable,"noformat.")!==false){
						$formato="H:i:s";
						$variable=str_replace("noformat.","",$variable);
					}else{
						$formato=self::$timeformat;
					}
					$text=str_replace($completa,date($formato, strtotime($variable)),$text);
					break;
				case "const":			
					$text=str_replace($completa,constant($variable),$text);
					break;
				case "user":			
					$text=str_replace($completa,$_SESSION[session_id()][$variable],$text);
					break;
				case "res":				
					$temp=array('id','resId','parentId','returnPath');
					$text=str_replace($completa,$temp[$_REQUEST[$variable]],$text);
					break;
				case "app":				
					break;
				case "data":			
					$temp=explode(".",$variable); $dataset=$temp[0]; $campo=$temp[1];
					$text=str_replace($completa,self::$data[$dataset][$campo],$text);
					break;				
			}
		}
		return $text;
	}
	
	public static function escape($value){
		if (get_magic_quotes_gpc()) {
			$value = stripslashes($value);
		}
		$value = mysql_real_escape_string($value);
		return $value;
	}
	
	public static function extractLang($texto, $idioma){
		$pattern="@\<!-- lang:$idioma --\>(.*)\<!-- \/lang:$idioma --\>@s";
		preg_match($pattern, $texto, $matches, PREG_OFFSET_CAPTURE);
		return ($matches[1][0]);
	}
	
	public static function sluggify($str, $replace=array(), $delimiter='-') {
		$old_locale = setlocale(LC_ALL, NULL);										
		setlocale(LC_ALL, 'en_US.UTF8');											
		
		if( !empty($replace) ) $str = str_replace((array)$replace, ' ', $str);
		$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
		$clean = preg_replace("/[^\.a-zA-Z0-9\/_|+ -]/", '', $clean);
		$clean = strtolower(trim($clean, '-'));
		$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
		
		setlocale(LC_ALL, $old_locale);												
		return $clean;
	}

	public static function cutf($param){
		return iconv(iconv_get_encoding("input_encoding"),"UTF-8",$param);
	}
	
	public static function procPass($cadena, $metodo="none"){
		$metodo=strtolower($metodo);
		if($metodo=="none"){
			return $cadena;
		}else if($metodo=="md5"){
			return md5($cadena);
		}else if($metodo=="sha1"){
			return sha1($cadena);
		}
		return $cadena;
	}
}
?>