<?php
require_once(SYS_ROOT. '/cls_ctl/ctl.templates.php');
function temp_load($msgfile)
{
	$obj_ctl_templates=new cls_ctl_templates;
	$str_path=$obj_ctl_templates->on_load($msgfile);
	$str_real_path=get_real_path("",$str_path);
	if(!is_file($str_real_path)) $str_path="";
	return $str_path;
}
function temp_load_lib($msg_lib1,$msg_lib2=""){
	if(is_file($msg_lib1)) return $msg_lib1;
	return $msg_lib2;
}
function get_sql($sql,$var)
{
	global $sql_return,$obj_db;
	if(isset($sql_return[$var])) unset($sql_return[$var]);
	$sql_return[$var]=array();
	$str_sql=$sql;
	$obj_result = $obj_db->query($str_sql);
	while($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$sql_return[$var][]=$obj_rs;
	}
}
function get_sql_one($sql,$var)
{
	global $sql_return_one,$obj_db;
	if(isset($sql_return_one[$var])) unset($sql_return_one[$var]);
	$str_sql=$sql;
	$obj_rs = $obj_db->get_one($str_sql);
	$sql_return_one[$var]=$obj_rs;
}

function get_sql_page($val,$str_sql,$pagesize=20)
{
	global $sql_return,$obj_db,$get_page;
	if(intval($get_page)<1)$page=1;
	if(intval($pagesize)<1)$pagesize=20;

	if(strtolower(DB_CHARSET)=="gbk"){
		$str_utf8_u="";
		$str_chinacode=chr(0xa1)."-".chr(0xff);
	}else{
		$str_utf8_u="u";
		$str_chinacode="\x{4e00}-\x{9fa5}";
	}

	$str_sql_count=preg_replace("/^select\s([_.,*\/a-z0-9".$str_chinacode."]{1,})\sfrom\s([_.\/a-z0-9]{1,})/".$str_utf8_u."is", "select count(1) as num from \\2", $str_sql);
	$rs  = $obj_db->get_one($str_sql_count);
	$total=$rs["num"];
	$page   = max(intval($page), 1);
	$_strPageList=get_pages_html($total, $page, $pagesize,format_url_query(array('page'=>'')));
	$offset = $pagesize*($page-1);

	$limit = " LIMIT $offset, $pagesize";
	$result = $obj_db->query($str_sql." ".$limit);
	$sql_return["$val"]=array();
	while($rs = $obj_db->fetch_array($result))
	{
		$sql_return["$val"][] = $rs;
	}
	$sql_return["pagelist"][$val]=$_strPageList;
	return $sql_return;
}
function cache_to_html($msg_file,$msg_go=0,$msg_url="",$msg_cache_time=0){
	//msg_go:0=转向新网页 99=js刷新缓存模式
	//当前模式是生成模式还是正常模式
	global $get_ishtml;
	$str_content=ob_get_contents();
	$str_file=get_real_path("",$msg_file);
	//缓存时间
	if($msg_cache_time>0){
		$str_x="<script src=\"".WEB_DOMAIN."/plus/js/refresh.php?act=auto&file=".$msg_file."&url=".WEB_SCRIPT_URL."&cache_time=".$msg_cache_time."\"></script>";
		$str_content=str_replace("</head>",$str_x."</head>",$str_content);
	}
	file_create($str_file,$str_content);
	if($get_ishtml==1){
			ob_end_clean();
	}else{
		if($msg_url==""){
			$str_url=WEB_URL.format_path("/".$msg_file);
		}else{
			$str_url=$msg_url;
		}
		if($msg_go==0){
			ob_end_clean();
			go_url($str_url);
		}else if($msg_go==99){
			global $get_is_refresh;
			if($get_is_refresh!=""){
				ob_end_clean();
				echo "window.location.reload();";
			}
		}
	}
}
?>