<?php
header("Expires: Tue, 01 Jul 2001 06:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

date_default_timezone_set('America/Santiago');

define('paginaPrincipal','inicio',true);
define('base', 'http://'.$_SERVER['HTTP_HOST'].str_replace(basename($_SERVER['PHP_SELF']),'',$_SERVER['PHP_SELF']),true);
define('dirImg', base . 'img/',true);
define('dirCss', base . 'css/',true);
define('dirJs', base . 'js/',true);
define('dirLibJs', base . 'js/libs/',true);
define('dirLibPHP', dirname(__FILE__).'/../funciones-xajax/libs/',true);

define('DB_DRIVER','mysql');
define('DB_SERVER','');//$_SERVER['HTTP_HOST']

// define('DB_USER','websizec');
// define('DB_PASS','v0E26n7suZ');
// define('DB_NAME','websizec_siste');

define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','siste');

define('DB_DEBUG',false);

@require_once('funciones-xajax/function.general.php');

class plantilla{

	private $css = array();
	private $js = array();
	private $titulo = '';
	private $keywords = '<meta name="keywords" content="{MAS}" />';
	private $description = '<meta name="description" content="{MAS}" />';
	private $plantilla = 'plantilla.principal.php';

	function __construct(){
	}

	public function setTitle($titulo){
		$this->titulo .= $titulo;
	}

	public function getTitle(){
		if(empty($this->titulo))
			return 'Sin t&iacute;tulo';
		return $this->titulo;
	}

	public function setKeywords($keywords){
		$this->keywords = str_replace("{MAS}", $keywords, $this->keywords);
	}

	public function getKeywords(){
		if(preg_match('#{MAS}#',$this->keywords)==1)
			return null;
		return $this->keywords;
	}

	public function setDescription($description){
		$this->description = str_replace("{MAS}", $description, $this->description);
	}

	public function getDescription(){
		if(preg_match('#{MAS}#',$this->description)==1)
			return null;
		return $this->description;
	}

	public function setPlantilla($archivo){
		if(!empty($archivo))
			$this->plantilla = 'plantilla.'.$archivo.'.php';
		else
			$this->plantilla = '';
	}

	public function getPlantilla(){
		return $this->plantilla;
	}

	public function getFunctionClass(){
		if(empty($_GET['pagina']))
			$contenido=paginaPrincipal;
		else
			$contenido=$_GET['pagina'];
		if(file_exists('funciones-xajax/function.'.$contenido.'.php'))
			include_once('funciones-xajax/function.'.$contenido.'.php');
	}

	private function getXajax(){
		$xajaxJs='';
		if(empty($_GET['pagina']))
			$contenido=paginaPrincipal;
		else
			$contenido=$_GET['pagina'];
		if(file_exists('funciones-xajax/xajax.'.$contenido.'.php') || file_exists('funciones-xajax/xajax.general.php')){
			if(!class_exists('xajax'))
				include_once('xajax/xajax_core/xajax.inc.php');
			$xajax = new xajax();
			$xajax->configure('javascript URI','xajax/');
			$xajax->configure('waitCursor', false);
			// $xajax->configure('responseType', 'JSON');//JSON o XML
			if(isset($_GET['dir']))
				$xajax->configure('requestURI',base.$_GET['dir']);
			else
				$xajax->configure('requestURI',base.$_GET['pagina']);
			if(file_exists('funciones-xajax/xajax.general.php'))
				include_once('funciones-xajax/xajax.general.php');
			if(file_exists('funciones-xajax/xajax.'.$contenido.'.php'))
				include_once('funciones-xajax/xajax.'.$contenido.'.php');
			$xajax->processRequest();
			$xajaxJs = $xajax->getJavascript();
		}
		return $xajaxJs;
	}

	public function content($login,$contenido,$dir=null,$privilegio=null){
		if(empty($_GET['pagina']))
			$contenido=paginaPrincipal;
		if(file_exists('cont/'.$dir.'cont.'.$contenido.'.php')){
			include_once(dirPlantilla.'login.php');
			$this->getFunctionClass();
			$plantilla = $this->replacePlantilla(include_php(dirPlantilla.$this->plantilla));
			$contenido = $this->replaceContent(include_php('cont/'.$dir.'cont.'.$contenido.'.php'));
			if(!empty($plantilla))
				echo str_replace('{CONTENIDO}',$contenido, $plantilla);
			else
				echo $contenido;
		}else{
			header('Location: '.base.'404/');
		}
	}

	private function replacePlantilla($r){
		if(empty($r))
			return '';
		return str_replace(array('{BASE}','{DIRIMG}','{DIRCSS}','{DIRJS}','{DIRLIBJS}','{DIRLIBPHP}','{DIRPLANTILLA}','{TITULO}','{META}','{CSS}','{JS}','{XAJAXJS}'),
			array(base,dirImg,dirCss,dirJs,dirLibJs,dirLibPHP,dirPlantilla,$this->getTitle(),($this->getDescription().$this->getKeywords()),$this->getCss(),$this->getJs(),$this->getXajax()),$r);
	}

	private function replaceContent($r){
		return str_replace(array('{BASE}','{DIRIMG}','{DIRCSS}','{DIRJS}','{DIRLIBJS}','{DIRLIBPHP}','{DIRPLANTILLA}','{TITULO}','{CSS}','{JS}'),
			array(base,dirImg,dirCss,dirJs,dirLibJs,dirLibPHP,dirPlantilla,$this->getTitle(),$this->getCss(),$this->getJs()),$r);
	}

	public function addCss($css){
		$this->css[]=$css;
	}

	private function getCss(){
		$css = '';
		foreach($this->css as $valor)
			$css .= '<link rel="stylesheet" href="'.dirCss.$valor.'.css" media="screen" />';
		return $css;
	}

	public function addJs($js,$wait=false){
		if($wait)
			$this->js[]='.wait().script(\''.dirJs.$js.'.js\')';
		else{
			if(!preg_match('#^(http://)#', $js))
				$this->js[]='.script(\''.dirJs.$js.'.js\')';
			else
				$this->js[]='.script(\''.$js.'.js\')';
		}
	}

	private function getJs(){
		$js = '$LAB.setOptions({CacheBust:true}).wait()';
		foreach($this->js as $valor)
			$js .= $valor;
		$contenido = ';';
		return $js;
	}
}
?>