<?php
/*
	[SueamCMS] (C)2009-2010 ZMRNet Studio
	sueam_tpl.php by 2009-12-25 zmrnet
*/
class sueam_tpl{
	var $htm = '';
	var $ext = '.html';
	var $dat = array();
	var $dir = '../skin/';
	
	function load($tn){
		$fn = $this->dir . $tn . $this->ext;
		if(!is_file($fn)){
			$ta = explode('_', $tn);
			if(count($ta)>1){
				$tn = '_'.$ta[1];
			}else{
				$tn = '_'.$ta[0];
			}
			$fn = $this->dir . $tn . $this->ext;
		}
		$fs = new sueam_vfs();
		$fd = $fs->fread($fn);
		unset($fs);
		$ts = array ('"{@include\((.*?)\)/}"ise');
		$vs = array ('$this->inc("\\1")');
		$fd = preg_replace($ts, $vs, $fd);
		$this->htm = $fd;
	}
	function save($fn){
		$this->show(true);
		$this->show(false);
		$fs = new sueam_vfs();
		$fw = $fs->fwrite($fn, $this->htm);
		unset($fs);
		return $fw;
	}

	function show($site){
		if($site){
			$tags = '\'{\$(site_[^}]*?)}\'ie';
			$vals = '$this->dat[\'\\1\']';
		}else{
			$tags = array(
				'\'{@loop_list\(([^,]*?),([^)]*?)\)}(.+?){/loop_list}\'ise',
				'\'{@loop_this}(.+?){/loop_this}\'ise',
				'\'{\$([^}]+?)}\'ie',
				'\'{@if\(([^}]*?)\)}(.+?){/if}\'ise',
			);
			$vals = array(
				'$this->loop_list(\'\\1\', \'\\3\', \\2)',
				'$this->loop_this(\'\\1\')',
				'$this->dat[\'\\1\']',
				'$this->if_then(\'\\1\', \'\\2\')',
			);
		}
		$this->htm = preg_replace($tags, $vals, $this->htm);
		return $this->htm;
	}
	function loop_list($type, $loop, $num){
		$res = '';
		$loop = stripslashes($loop);
		if($num > 0){
			$vdb = new sueam_vdb();
			$_VDB = $vdb->connect();
			$this_dir = $type;
			$this_type = $_VDB['art'][$this_dir];
			if(!isset($this_type)){
				$this_type = $_VDB['bbs'][$this_dir];
			}
			$_LST = $vdb->opendir($this_dir.'/list');
			$cnt = count($_LST);
			if($num > $cnt){$num = $cnt;}
			for($i=0; $i<$num; $i++){
				$_LST[$i]['dir'] = $this_dir;
				$_LST[$i]['type'] = $this_type;
				$tags = '\'{\$this_([^}]*?)}\'ie';
				$vals = '$_LST[$i][\\1]';
				$res .= preg_replace($tags, $vals, $loop);
			}
			unset($_LST);
			unset($vdb);
		}
		return $res;
	}
	function loop_this($loop){
		$res = '';
		$loop = stripslashes($loop);
		$this_list = $this->dat['this_list'];
		$this_dir = $this->dat['this_dir'];
		$this_type = $_VDB['art'][$this_dir];
		if(!isset($this_type)){
			$this_type = $_VDB['bbs'][$this_dir];
		}
		for($i=0; $i<count($this_list); $i++){
			$this_list[$i]['dir'] = $this_dir;
			$this_list[$i]['type'] = $this_type;
			$tags = array('\'{\$this_([^}]*?)}\'ie');
			$vals = array('$this_list[$i][\\1]');
			$res .= preg_replace($tags, $vals, $loop);
		}
		return $res;
	}
	function if_then($test, $then){
		$res = '';
		$test = trim($test);
		$then = stripslashes($then);
		if($test==''){$test = '0';}
		@eval('$_test=('.$test.');');
		if($_test){$res = $then;}
		return $res;
	}
	function inc($tn){
		$tpl = new sueam_tpl();
		$tpl->load($tn);
		$res = $tpl->htm;
		unset($tpl);
		return $res;
	}
	
	function sueam_tpl(){
		$vdb = new sueam_vdb();
		$this->dat = $vdb->open('index');
		$site_tpl = $this->dat['site_tpl'].'/';
		if($site_tpl=='/'){$site_tpl='sueam/';}
		$this->dir .= $site_tpl;
		$this->dat['site_skin'] = '{$site_dir}skin/{$site_tpl}';
		unset($vdb);
	}
	function __construct(){
		$this->sueam_tpl();
	}
	function __destruct(){
		
	}
}
?>