<?php
function modeTemplate($tplname,$EXT='htm'){
	global $db_modepages;
	$enter	= M_P."template/$tplname.$EXT";
	$result	= D_P."data/tplcache/$tplname.$EXT";
	$file_str = readover($enter);
	if (array_key_exists($tplname,$db_modepages)) {
		tplParsePW($file_str);
	}
	tplParsePrint($file_str);
	writeover($result,$file_str);
	return $result;
}

function tplParsePW(&$string,$scr=false){
	global $db_mode;
	!$scr && $scr = SCR;
	if (!$scr) {
		if (defined('SCR')) {
			$scr = SCR;
		} else {
			Showmsg("scr_not_defined");
		}
	}
	include(D_P.'data/bbscache/mode_stamp.php');
	if (!$mode_stamp) {
		Showmsg("mode_stamp_block_error");
	}
	@include(D_P.'data/bbscache/mode_'.$scr.'_'.$db_mode.'_config.php');
	@include(D_P.'data/bbscache/mode_push_'.$db_mode.'_config.php');

	if (${$scr.'_config'}) {
		$tem_config 	= ${$scr.'_config'};
		$check_config	= $tem_config;
	} else {
		$tem_config = array();
	}
	if ($PUSH) {
		$tem_push = $PUSH;
		foreach ($tem_push as $key => $value) {
			if ($value['scr'] == $scr) {
				unset($tem_push[$key]);
			}
		}
	} else {
		$tem_push = array();
	}
	preg_match_all('/<pw(.+?)\/>([^\x00]+?<loop>)([^\x00]+?)(<\/loop>)/i',$string,$reg);
	$replace = $condition = array();
	foreach ($reg[1] as $id=>$val) {
		$parameter = '';
		$condition[$id] = array();
		preg_match_all("/[[:blank:]]+?[a-zA-Z\-_]+=(['|\"]?).*?(\\1)/",$val,$match);
		foreach ($match[0] as $pwReg){
			$pos = strpos($pwReg,"=");
			$key = trim(strtolower(substr($pwReg,0,$pos)));
			$value = substr($pwReg,$pos+1);
			if (preg_match("/^('|\")(.*?)(\\1)$/",$value,$newValue)) {
				$value = trim($newValue[2]);
			}
			$condition[$id][$key] = $value;
		}
		if (!$condition[$id]['id']) {
			Showmsg('mode_template_pw_no_id');
		} else {
			if (!preg_match('/[\w]+/is',$condition[$id]['id'])) {
				$id = $condition[$id]['id'];
				global $id;
				Showmsg('mode_temlate_id_error');
			}
		}
		if (!$condition[$id]['stamp']) {
			$condition[$id]['stamp'] = 'subject';
		} else {
			$condition[$id]['stamp'] = strtolower($condition[$id]['stamp']);
		}
		if (!array_key_exists($condition[$id]['stamp'],$mode_stamp)) {
			global $stamp;
			$stamp = $condition[$id]['stamp'];
			Showmsg('mode_stamp_not_exist');
		}
		$condition[$id]['num'] = (int)$condition[$id]['num'] ? (int)$condition[$id]['num']:10;

		$tem_id = $condition[$id]['id'];
		$tem_config[$tem_id]['id']		= $tem_id;
		$tem_config[$tem_id]['name'] 	= $tem_config[$tem_id]['name'] ? $tem_config[$tem_id]['name']:($condition[$id]['name'] ? $condition[$id]['name']:$tem_id);
		$tem_config[$tem_id]['stamp'] 	= $condition[$id]['stamp'];
		$tem_config[$tem_id]['num']		= $condition[$id]['num'];
		$replace[$id] = "\r\n<!--\r\nEOT;\r\n\$pwresult = \$element->elementInterface('".$tem_id."','".$condition[$id]['stamp']."');\r\nprint <<<EOT\r\n-->\r\n";
		$reg[2][$id] = str_replace("<loop>","\r\n<!--\r\nEOT;\r\nforeach(\$pwresult as \$key=>\$val){print <<<EOT\r\n-->\r\n",$reg[2][$id]);
		$replace[$id] .= $reg[2][$id];

		$tem_config[$tem_id]['param'] = array();
		preg_match_all('/\{([\w\,\-:\s]+?)\}/',$reg[3][$id],$mat);
		$replace_3 = array();
		$v_temp = array();
		foreach ($mat[1] as $k=>$v) {
			if (!in_array($v,$tem_config[$tem_id]['param'])) {
				$tem_config[$tem_id]['param'][] = $v;
			} else {
				unset($mat[0][$k]);
				continue;
			}
			$v = strpos($v,',') === false ? trim($v) : trim(substr($v,0,strpos($v,',')));
			if (array_key_exists($v,$v_temp)) {
				$v_temp[$v]++;
			} else {
				$v_temp[$v] = 0;
			}
			if ($v_temp[$v]) {
				$replace_3[$k] = '$val['.$v.'_'.$v_temp[$v].']';
			} else {
				$replace_3[$k] = '$val['.$v.']';
			}
		}
		/**
		if ($tem_id == 'newimage') {
			print_r($mat[0]);
			echo "<br>";
			print_r($replace_3);
			exit;
		}
**/
		$reg[3][$id] = str_replace($mat[0],$replace_3,$reg[3][$id]);
		$replace[$id] .= $reg[3][$id];
		$reg[4][$id] = str_replace("</loop>","\r\n<!--\r\nEOT;\r\n}print <<<EOT\r\n-->\r\n",$reg[4][$id]);
		$replace[$id] .= $reg[4][$id];
		if (strtolower($tem_config[$tem_id]['stamp'])=='push') {
			$tem_push[$tem_id]= array('scr'=>$scr,'id'=>$tem_id,'name'=>$tem_config[$tem_id]['name']);
		}
		unset($check_config[$tem_id]);
	}
	$string = str_replace($reg[0],$replace,$string);
	if ($check_config) {
		foreach ($check_config as $key=>$value) {
			unset($tem_config[$key]);
		}
	}
	tplParseIdName($string,$tem_config);
	writeover(D_P.'data/bbscache/mode_push_'.$db_mode.'_config.php',"<?php\r\n\$PUSH = ".pw_var_export($tem_push)."\r\n?>");
	writeover(D_P.'data/bbscache/mode_'.$scr.'_'.$db_mode.'_config.php',"<?php\r\n\$".$scr."_config = ".pw_var_export($tem_config)."\r\n?>");
}

function tplParsePrint(&$string){
	$s = array(
		"<!--#","#-->",
		);
	$e = array(
		"\r\nEOT;\r\n","print <<<EOT\r\n",
		);
	if (function_exists('str_ireplace')) {
		$string = str_ireplace($s,$e,$string);
	} else {
		$string = str_replace($s,$e,$string);
	}
}

function tplParseIdName(&$string,$config){
	preg_match_all('/\{([\w]+?)\}/i',$string,$reg);
	$search = $replace = array();
	foreach ($reg[1] as $id=>$val){
		if (array_key_exists($val,$config)) {
			$search[] = $reg[0][$id];
			$replace[] = "\r\n<!--\r\nEOT;\r\n\$idname = \$element->getElementName('".$val."');\r\nprint <<<EOT\r\n-->\r\n\$idname";
		}
	}
	$string = str_replace($search,$replace,$string);
}
?>