<?php
//include_once  I_PATH."/efb84f34.php";
if (! defined ( 'APP_PATH' ) || ! defined ( 'I_PATH' )) {
	exit ( 'Access Denied' );
}

function appRun() {
	
	GLOBAL $__controller, $__action;
	
	syClass ( 'sysession' );
	spLaunch ( "router_prefilter" );
	$handle_controller = syClass ( $__controller, null, $GLOBALS ['LD_I'] ["controller_path"] . '/' . $__controller . ".php" );
	if (! is_object ( $handle_controller ) || ! method_exists ( $handle_controller, $__action )) {
		syError ( 'route Error' );
		exit ();
	}
	$handle_controller->$__action ();
	if (FALSE != $GLOBALS ['LD_I'] ['view'] ['auto_display']) {
		$__tplname = $__controller . $GLOBALS ['LD_I'] ['view'] ['auto_display_sep'] . $__action . $GLOBALS ['LD_I'] ['view'] ['auto_display_suffix'];
		$handle_controller->auto_display ( $__tplname );
	}
	spLaunch ( "router_postfilter" );
}
/**
 * 
 * @param int $page 焦点页码
 * @param int $nums 数据总数
 * @param int $pagesize 每页显示的数据条目。
 */
function page_c($nums,$page=1,$pagesize =20){
	$pagearray['current_page'] = $page;
    		$pagearray['total_page'] = ceil($nums/$pagesize);
    		$pagearray['last_page'] = ceil($nums/$pagesize);
    		$pagearray['prev_page'] = ($page)> 1?$page-1:1;
    		$pagearray['next_page'] = ($page)<ceil($nums/$pagesize)?$page+1:ceil($nums/$pagesize);
    return $pagearray;
}



function getlistData($table,$condition,$sql=NULL,$p=array('curr'=>1,'size'=>20)){
	$nums = syDB($table)->findCount($condition);
	if($sql){
		$data  = syDB($table)->findsql($sql);
	}else{
		$data = syDB($table)->syPager($p[curr],$p[size])->findall($condition);
		$page = pagetxt(syDB($table)->syPager()->getPager());
	}
	return array('nums'=>$nums,'list'=>$data,'page'=>$page);
	
}
function dump($vars, $output = TRUE, $show_trace = FALSE) {
	if (TRUE != SP_DEBUG && TRUE != $GLOBALS ['LD_I'] ['allow_trace_onrelease'])
		return;
	if (TRUE == $show_trace) {
		$content = syError ( htmlspecialchars ( print_r ( $vars, true ) ), TRUE, FALSE );
	} else {
		$content = "<div align=left><pre>\n" . htmlspecialchars ( print_r ( $vars, true ) ) . "\n</pre></div>\n";
	}
	if (TRUE != $output) {
		return $content;
	}
	echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>{$content}</body></html>";
	return;
}

function import($sfilename, $auto_search = TRUE, $auto_error = FALSE) {
	if (isset ( $GLOBALS ['LD_I'] ["import_file"] [md5 ( $sfilename )] ))
		return TRUE;
	if (TRUE == @is_readable ( $sfilename )) {
		require ($sfilename);
		$GLOBALS ['LD_I'] ['import_file'] [md5 ( $sfilename )] = TRUE;
		return TRUE;
	} else {
		if (TRUE == $auto_search) {
			foreach ( array_merge ( $GLOBALS ['LD_I'] ['include_path'], array ($GLOBALS ['LD_I'] ['model_path'] ), $GLOBALS ['LD_I'] ['sp_include_path'] ) as $sp_include_path ) {
				if (isset ( $GLOBALS ['LD_I'] ["import_file"] [md5 ( $sp_include_path . '/' . $sfilename )] ))
					return TRUE;
				if (is_readable ( $sp_include_path . '/' . $sfilename )) {
					require ($sp_include_path . '/' . $sfilename);
					$GLOBALS ['LD_I'] ['import_file'] [md5 ( $sp_include_path . '/' . $sfilename )] = TRUE;
					return TRUE;
				}
			}
		}
	}
	if (TRUE == $auto_error)
		syError ( "未能找到名为：{$sfilename}的文件" );
	return FALSE;
}

function syAccess($method, $name, $value = NULL, $life_time = -1) {
	if ($launch = spLaunch ( "function_access", array ('method' => $method, 'name' => $name, 'value' => $value, 'life_time' => $life_time ), TRUE ))
		return $launch;
	if (! is_dir ( $GLOBALS ['LD_I'] ['sp_cache'] ))
		__mkdirs ( $GLOBALS ['LD_I'] ['sp_cache'] );
	$sfile = $GLOBALS ['LD_I'] ['sp_cache'] . '/' . $GLOBALS ['LD_I'] ['sp_app_id'] . md5 ( $name ) . ".php";
	if ('w' == $method) {
		$life_time = (- 1 == $life_time) ? '300000000' : $life_time;
		$value = '<?php die();?>' . (time () + $life_time) . serialize ( $value );
		return file_put_contents ( $sfile, $value );
	} elseif ('c' == $method) {
		return @unlink ( $sfile );
	} else {
		if (! is_readable ( $sfile ))
			return FALSE;
		$arg_data = file_get_contents ( $sfile );
		if (substr ( $arg_data, 14, 10 ) < time ()) {
			@unlink ( $sfile );
			return FALSE;
		}
		return unserialize ( substr ( $arg_data, 24 ) );
	}
}

/**
 * 判断是否登录    判断用户是否已经登录  登录 返回会员编号  否则返回 false
 * Enter description here ...
 */
function checklogined() {
	//syClass('syssession');
	if (! empty ( $_SESSION ['member'] )) {
		return $_SESSION ['member'] ['id'];
	
	}
	return false;
}

function syClass($class_name, $args = null, $sdir = null, $force_inst = FALSE) {
	if (preg_match ( "/^[a-zA-Z0-9_\-]*$/", $class_name ) == 0)
		syError ( "类定义不存在，请检查。" );
	if (TRUE != $force_inst)
		if (isset ( $GLOBALS ['LD_I'] ["inst_class"] [$class_name] ))
			return $GLOBALS ['LD_I'] ["inst_class"] [$class_name];
	if (null != $sdir && ! import ( $sdir ) && ! import ( $sdir . '/' . $class_name . '.php' ))
		return FALSE;
	$has_define = FALSE;
	if (class_exists ( $class_name, false ) || interface_exists ( $class_name, false )) {
		$has_define = TRUE;
	} else {
		if (TRUE == import ( $class_name . '.php' )) {
			$has_define = TRUE;
		}
	}
	if (FALSE != $has_define) {
		$argString = '';
		$comma = '';
		if (null != $args)
			for($i = 0; $i < count ( $args ); $i ++) {
				$argString .= $comma . "\$args[$i]";
				$comma = ', ';
			}
		eval ( "\$GLOBALS['LD_I']['inst_class'][\$class_name]= new \$class_name($argString);" );
		return $GLOBALS ['LD_I'] ["inst_class"] [$class_name];
	}
	syError ( $class_name . "类定义不存在，请检查。" );
}

function syError($msg, $output = TRUE, $stop = TRUE) {
	if ($GLOBALS ['LD_I'] ['sp_error_throw_exception'])
		throw new Exception ( $msg );
	if (TRUE != SP_DEBUG) {
		error_log ( $msg );
		echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
		echo "<body>程序出错！<br><br>出现此情况可能为：1、模板标签书写错误。2、修改过程序代码，代码修改有误。<br><br>请在后台系统设置中开启PHP错误调试，或者修改include\inc.php第一行'mode' => 'debug'后查看信息错误记录<br><br>您可以购买官方商业服务，为您提供全程服务http://www.5545.cn</body></html>";
		if (TRUE == $stop)
			exit ();
	}
	$traces = debug_backtrace ();
	$bufferabove = ob_get_clean ();
	require_once ($GLOBALS ['LD_I'] ['notice_php']);
	if (TRUE == $stop)
		exit ();
}

function spLaunch($configname, $launchargs = null, $returns = FALSE) {
	if (isset ( $GLOBALS ['LD_I'] ['launch'] [$configname] ) && is_array ( $GLOBALS ['LD_I'] ['launch'] [$configname] )) {
		foreach ( $GLOBALS ['LD_I'] ['launch'] [$configname] as $launch ) {
			if (is_array ( $launch )) {
				$reval = syClass ( $launch [0] )->{$launch [1]} ( $launchargs );
			} else {
				$reval = call_user_func_array ( $launch, $launchargs );
			}
			if (TRUE == $returns)
				return $reval;
		}
	}
	return false;
}

function spUrl($geturl = null, $controller = null, $action = null, $args = null, $anchor = null, $no_sphtml = FALSE) {
	if (TRUE == $GLOBALS ['LD_I'] ['html'] ["enabled"] && TRUE != $no_sphtml) {
		$realhtml = syhtml::getUrl ( $geturl, $controller, $action, $args, $anchor );
		if (isset ( $realhtml [0] ))
			return $realhtml [0];
	}
	$geturl = (null != $geturl) ? $geturl : basename ( __FILE__ );
	$controller = (null != $controller) ? $controller : $GLOBALS ['LD_I'] ["default_controller"];
	$action = (null != $action) ? $action : $GLOBALS ['LD_I'] ["default_action"];
	if ($launch = spLaunch ( "function_url", array ('controller' => $controller, 'action' => $action, 'args' => $args, 'anchor' => $anchor, 'no_sphtml' => $no_sphtml ), TRUE ))
		return $launch;
	if (TRUE == $GLOBALS ['LD_I'] ['url'] ["url_path_info"]) {
		$url = "{$controller}/{$controller}/{$action}";
		if (null != $args)
			foreach ( $args as $key => $arg )
				$url .= "/{$key}/{$arg}";
	} else {
		$url = $geturl . "?" . $GLOBALS ['LD_I'] ["url_controller"] . "={$controller}&";
		$url .= $GLOBALS ['LD_I'] ["url_action"] . "={$action}";
		if (null != $args)
			foreach ( $args as $key => $arg )
				$url .= "&{$key}={$arg}";
	}
	if (null != $anchor)
		$url .= "#" . $anchor;
	return $url;
}

function __mkdirs($dir, $mode = 0755) {
	if (! is_dir ( $dir )) {
		__mkdirs ( dirname ( $dir ), $mode );
		return @mkdir ( $dir, $mode );
	}
	return true;
}

/**
 * 获取版权  0 免费版 1 企业版 2 VIP版
 * Enter description here ...
 * @throws Exception
 */
function ed0b7b79()
{
	return 0;
	global $cert,$key;
	return  eqxtv( $cert['f607c01d'],$key);
}


function setLink($links)
{
	$matchs=array('www.5545.cn','cms.5545.cn');
	foreach ($links as $k)
	{
		foreach ($matchs as $m)
		{
			if(strpos($m,$k)!==false)
			{
				return $links;
			}
		}
	}
	$links[]=array('gourl'=>'http://www.5545.cn','name'=>'东莞蓝鼎网络科技','image'=>'http://www.5545.cn/upload/images/201206/134070443554.jpg');	
	return $links;
}


function syExt($ext_node_name) {
	if(ed0b7b79()=='f607c01d')
	{
		$sign=array('site_powerby','site_copyright');
		if(in_array($ext_node_name,$sign ))
		{
			return "<a href='http://www.5545.cn'>".$GLOBALS ['LD_I'] ['ext'] [$ext_node_name]."</a>";
		}
	}
	return (empty ( $GLOBALS ['LD_I'] ['ext'] [$ext_node_name] )) ? FALSE : $GLOBALS ['LD_I'] ['ext'] [$ext_node_name];
}
function syCus($ext_node_name) {
	return (empty ( $GLOBALS ['LD_I'] ['cus'] [$ext_node_name] )) ? FALSE : $GLOBALS ['LD_I'] ['cus'] [$ext_node_name];
}
function spAddViewFunction($alias, $callback_function) {
	return $GLOBALS ['LD_I'] ["view_registered_functions"] [$alias] = $callback_function;
}

function syDB($tbl_name, $pk = null) {
	$modelObj = syClass ( "modelBase" );
	$modelObj->tbl_name = (TRUE == $GLOBALS ['LD_I'] ["db_spdb_full_tblname"]) ? $tbl_name : $GLOBALS ['LD_I'] ['db'] ['prefix'] . $tbl_name;
	if (! $pk) {
		@list ( $pk ) = $modelObj->_db->getTable ( $modelObj->tbl_name );
		$pk = $pk ['Field'];
	}
	$modelObj->pk = $pk;
	return $modelObj;
}

function spConfigReady($preconfig, $useconfig = null) {
	$nowconfig = $preconfig;
	if (is_array ( $useconfig )) {
		foreach ( $useconfig as $key => $val ) {
			if (is_array ( $useconfig [$key] )) {
				@$nowconfig [$key] = is_array ( $nowconfig [$key] ) ? spConfigReady ( $nowconfig [$key], $useconfig [$key] ) : $useconfig [$key];
			} else {
				@$nowconfig [$key] = $val;
			}
		}
	}
	return $nowconfig;
}
//提示信息
function message($info, $gurl) {
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	if ($gurl == '') {
		echo "<script>alert('" . $info . "');javascript:history.go(-1);</script>";
	} else {
		echo "<script>alert('" . $info . "');window.location.href='" . $gurl . "';</script>";
	}
	exit ();
}
function message_err($newerrors) {
	foreach ( $newerrors as $errortxt ) {
		$error_txt1 = $errortxt;
		foreach ( $error_txt1 as $msg ) {
			$error_txt = $msg;
		}
	}
	message ( $error_txt );
}
//获取IP
function GetIP() {
	if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" ))
		$ip = getenv ( "HTTP_CLIENT_IP" );
	else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" ))
		$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
	else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" ))
		$ip = getenv ( "REMOTE_ADDR" );
	else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" ))
		$ip = $_SERVER ['REMOTE_ADDR'];
	else
		$ip = "unknown";
	return ($ip);
}
//字符截断,中文算2个字符
function newstr($string, $length, $dot = "...") {
	if (strlen ( $string ) <= $length) {
		return $string;
	}
	$string = str_replace ( array ('&amp;', '&quot;', '&lt;', '&gt;' ), array ('&', '"', '<', '>' ), $string );
	$strcut = '';
	$n = $tn = $noc = $noct = $nc = $tnc = 0;
	while ( $n < strlen ( $string ) ) {
		$t = ord ( $string [$n] );
		if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
			$tn = 1;
			$n ++;
			$noct ++;
		} elseif (194 <= $t && $t <= 223) {
			$tn = 2;
			$n += 2;
			$noct += 2;
		} elseif (224 <= $t && $t <= 239) {
			$tn = 3;
			$n += 3;
			$noct += 2;
		} elseif (240 <= $t && $t <= 247) {
			$tn = 4;
			$n += 4;
			$noct += 2;
		} elseif (248 <= $t && $t <= 251) {
			$tn = 5;
			$n += 5;
			$noct += 2;
		} elseif ($t == 252 || $t == 253) {
			$tn = 6;
			$n += 6;
			$noct += 2;
		} else {
			$n ++;
		}
		if ($noct >= $length) {
			if ($noct == 0)
				$noc = $noct;
			if ($nc == 0)
				$nc = $n;
			if ($tnc == 0)
				$tnc = $tn;
		}
	}
	if ($noct <= $length) {
		return str_replace ( array ('&', '"', '<', '>' ), array ('&amp;', '&quot;', '&lt;', '&gt;' ), $string );
	}
	if ($noc > $length) {
		$nc -= $tnc;
	}
	$strcut = substr ( $string, 0, $nc );
	$strcut = str_replace ( array ('&', '"', '<', '>' ), array ('&amp;', '&quot;', '&lt;', '&gt;' ), $strcut );
	return $strcut . $dot;
}

//数据操作过滤
function is_escape($value) {
	if (is_null ( $value ))
		return 'NULL';
	if (is_bool ( $value ))
		return $value ? 1 : 0;
	if (is_int ( $value ))
		return ( int ) $value;
	if (is_float ( $value ))
		return ( float ) $value;
	$value = htmlspecialchars ( trim ( $value ) );
	if (! get_magic_quotes_gpc ())
		$value = addslashes ( $value );
	return $value;
}
//替换url参数
function url_set_value($url, $key, $value) {
	parse_str ( $url, $arr );
	$arr [$key] = $value;
	return '?' . http_build_query ( $arr );
}
//内容推荐名称
function traitinfo($traitid) {
	$traitid = trim ( $traitid, ',' );
	$traitinfo = syDB ( 'traits' )->findAll ( ' id in (' . $traitid . ') ', null, 'id,name' );
	foreach ( $traitinfo as $v ) {
		$trait .= ' ' . $v ['name'];
	}
	return $trait;
}
//栏目名称
function typename($tid) {
	$t = syDB ( 'classtype' )->find ( array ('tid' => $tid ), null, 'classname' );
	return $t ['classname'];
}
//栏目信息
function typeinfo($tid, $q) {
	$t = syDB ( 'classtype' )->find ( array ('tid' => $tid ), null, $q );
	return $t [$q];
}
//自定义标签
function labelcus($id, $q) {
	$l = syClass ( 'ldi_labelcus' )->syCache ( 3600 * 240 )->find ( array ('id' => $id ) );
	return $l [$q];
}
//自定义页面
function custom_html($file) {
	$c = syDB ( 'custom' )->find ( array ('file' => $file ) );
	return $c;
}
//频道信息获取
function moldsinfo($molds, $q) {
	$m = syDB ( 'molds' )->find ( array ('molds' => $molds ), null, $q );
	return $m [$q];
}
//内容信息获取
function contentinfo($molds, $id, $q) {
	$c = syDB ( $molds )->find ( array ('id' => $id ), null, $q );
	return $c [$q];
}
//会员信息获取
function memberinfo($id, $q) {
	$m = syDB ( 'member' )->find ( array ('id' => $id ), null, $q );
	return $m [$q];
}
//会员组信息获取
function membergroup($weight, $q) {
	$m = syDB ( 'member_group' )->find ( array ('weight ' => $weight ), null, $q );
	return $m [$q];
}
//插件信息获取
function funsinfo($funs, $q) {
	$f = syDB ( 'funs' )->find ( array ('funs' => $funs ), null, $q );
	return $f [$q];
}
//自定义字段，单选多选项名获取
function fieldsinfo($fields, $key, $molds = 'article') {
	$f = syDB ( 'fields' )->find ( array ('fields' => $fields, 'molds' => $molds ), null, 'selects' );
	$fields = explode ( ',', $f ['selects'] );
	$k = array_search ( '=' . $key, $fields );
	$fields = explode ( '=', $fields [$k] );
	return $fields [0];
}
//返回多附件字段数组
function fileall($fileall) {
	if ($fileall != '') {
		$fileall = explode ( '|-|', $fileall );
		$f = array ();
		foreach ( $fileall as $v ) {
			$v = explode ( '|,|', $v );
			$f = array_merge ( $f, array (array ($v [0], $v [1] ) ) );
		}
		return $f;
	}
}
//时间比较
function newest($t, $h) {
	$t = (time () - $t) / 3600;
	if ($t < $h) {
		return true;
	} else {
		return false;
	}
}

//替换内容静态规则
//添加时间//id//生成的文件名
function html_rules($d, $id = '', $f = '') {
	if ($f == '')
		$f = $id;
	$u = '/' . str_replace ( array ('[y]', '[m]', '[d]', '[id]', '[file]' ), array (date ( 'Y', $d ), date ( 'm', $d ), date ( 'd', $d ), $id, $f ), syExt ( 'site_html_rules' ) );
	return str_replace ( array ('///', '//' ), '/', $u );
}
function  html_dir_rules($tid,$id='',$f='')
{
	if ($f == '')
		$f = $id;
		$paths=getAllDir();
	$u =  '/'.$paths[$tid].'/'.$id. syExt( "site_html_suffix" );
	return str_replace ( array ('///', '//' ), '/', $u );
	
}


//#路径#id#文件名#类型
function i_html_rules($dir, $id = '', $f = '', $pagetype = '', $filetype = '.html') {
	if ($f == '') {
		$f = $id;
	}
	if ($filetype == '') {
		$type = '.html';
	}
	if ($pagetype != '') {
		$pagetype = $pagetype . '/';
	}
	$str = '/' . $dir . '/' . $pagetype . $f . $filetype;
	return $str;
}
//调用小图片
function smallimage($str) {
	return $str != '' ? (str_replace ( basename ( $str ), $GLOBALS ['LD_I'] ['ext'] ['site_product_thumb_prev'] . basename ( $str ), $str )) : '';
}
//页面URL判断
function html_url($type, $c, $pages = 0, $ispage) {

	if ($c ['gourl'] != '')
		return $c ['gourl'];
	$sh = syExt ( "site_html" );
	$sr = $GLOBALS ['LD_I'] ['url'] ["url_path_base"];
	$sg = $GLOBALS ["WWW"] . ".";
	$re = $GLOBALS ['LD_I'] ['rewrite'] ["rewrite_open"];
	switch ($type) {
		case 'article' :
			if ($re == 1) {
				$re_url = $sg . $GLOBALS ['LD_I'] ['rewrite'] ["rewrite_article"];
				if ($c ['htmlfile'] == '')
					$c ['htmlfile'] = $c ['id'];
				$go_url = str_replace ( array ('{id}', '{file}' ), array ($c ['tid'], $c ['htmlurl'] ), $re_url );
				$go_url = str_replace ( array ('{article}' ), 'article', $go_url );
				if ($pages !== 0) {
					$go_url = str_replace ( '{page}', '[p]', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				} else {
					$go_url = str_replace ( '{page}', 1, $go_url );
				}
			} else if ($sh == 1 && $c ['mrank'] == 0 && $c ['mgold'] == 0 && $c ['htmlurl'] != '') {
				$go_url = $sg . $c ['htmlurl'];
				if ($pages !== 0) {
					$go_url = str_replace ( '.', '[p].', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				}
			} else {
				$go_url = $sr . '?c=article&id=' . $c ['id'];
				if ($pages !== 0)
					$go_url = pagetxt ( $pages );
			}
			break;
		case 'product' :
			if ($re == 1) {
				$re_url = $sg . $GLOBALS ['LD_I'] ['rewrite'] ["rewrite_product"];
				
				if ($c ['htmlfile'] == '')
					$c ['htmlfile'] = $c ['id'];
				$go_url = str_replace ( array ('{id}', '{file}' ), array ($c ['id'], $c ['htmlfile'] ), $re_url );
				$go_url = str_replace ( array ('{product}' ), 'product', $go_url );
				if ($pages !== 0) {
					$go_url = str_replace ( '{page}', '[p]', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				} else {
					$go_url = str_replace ( '{page}', 1, $go_url );
				}
			} else if ($sh == 1 && $c ['mrank'] == 0 && $c ['mgold'] == 0 && $c ['htmlurl'] != '') {
				$go_url = $sg . $c ['htmlurl'];
				if ($pages !== 0) {
					$go_url = str_replace ( '.', '[p].', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				}
			} else {
				$go_url = $sr . '?c=product&id=' . $c ['id'];
				if ($pages !== 0)
					$go_url = pagetxt ( $pages );
			}
			break;
		case 'classtype' :

			if ($re == 1) {
				$re_url = $sg . $GLOBALS ['LD_I'] ['rewrite'] ['rewrite_' . $c ['molds'] . '_type'];
				
				$go_url = str_replace ( array ('{tid}', '{file}' ), array ($c ['tid'], $c ['htmlfile'] ), $re_url );
				$go_url = str_replace ( array ('{' . $c ['molds'] . '}', '{type}' ), array ($c ['molds'], type ), $go_url );
				if ($pages !== 0) {
					$go_url = str_replace ( '{page}', '[p]', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				} else {
					$go_url = str_replace ( '{page}', 1, $go_url );
				}
			} else if ($sh == 1 && $c ['mrank'] == 0) {
				if ($c ["htmlfile"] != '') {
					$html_file = $c ["htmlfile"] . syExt ( "site_html_suffix" );
				} else {
					$html_file = 'index' . syExt ( "site_html_suffix" );
				}
				if ($c ["htmldir"] == '') { 			
					$tag_url = get_type_fullDir ( $c ['tid'] );
					
					$sg1 = $GLOBALS ["WWW"];								
					$go_url = $sg1 . $tag_url.'/' ;// SEO 优化掉 . "/" . $html_file; //fkebbypeu
				} else {
					$go_url = $sg . $c ["htmldir"] . "/" . $html_file;
					$go_url = str_replace ( array ("///", "//" ), "/", $go_url );
				}
				if ($pages !== 0) {
					$go_url = str_replace ( '.', '[p].', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				}
			} else {
				$go_url = $sr . "?c=" . $c ["molds"] . "&a=type&tid=" . $c ["tid"];
				if ($pages !== 0)
					$go_url = pagetxt ( $pages );
			}
			break;
		case 'special' :
			if ($re == 1) {
				$re_url = $sg . $GLOBALS ['LD_I'] ['rewrite'] ["rewrite_special"];
				$go_url = str_replace ( array ('{sid}', '{file}' ), array ($c ['sid'], $c ['htmlfile'] ), $re_url );
				$go_url = str_replace ( array ('{special}' ), 'special', $go_url );
				if ($pages !== 0) {
					$go_url = str_replace ( '{page}', '[p]', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				} else {
					$go_url = str_replace ( '{page}', 1, $go_url );
				}
			} else if ($sh == 1) {
				if ($c ["htmlfile"] != '') {
					$html_file = $c ["htmlfile"] . syExt ( "site_html_suffix" );
				} else {
					$html_file = 'index' . syExt ( "site_html_suffix" );
				}
				if ($c ["htmldir"] == '') { //TODO'site_html_dir' => 'myfiles', lsdjf
					$go_url = $sg . "s/" . $c ["sid"] . "/" . $html_file;
				} else {
					$go_url = $sg . $c ["htmldir"] . "/" . $html_file;
					$go_url = str_replace ( array ("///", "//" ), "/", $go_url );
				}
				if ($pages !== 0) {
					$go_url = str_replace ( '.', '[p].', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				}
			} else {
				$go_url = $sr . "?c=special&sid=" . $c ["sid"];
				if ($pages !== 0)
					$go_url = pagetxt ( $pages );
			}
			break;
		case 'labelcus_custom' :
			if ($re == 1) {
				$re_url = $sg . $GLOBALS ['LD_I'] ['rewrite'] ["rewrite_labelcus_custom"];
				$go_url = str_replace ( array ('{file}' ), array ($c ['file'] ), $re_url );
				if ($pages !== 0) {
					$go_url = str_replace ( '{page}', '[p]', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				} else {
					$go_url = str_replace ( '{page}', 1, $go_url );
				}
			} else if ($sh == 1) {
				$html_file = $c ["file"] . syExt ( "site_html_suffix" );
				if ($c ["dir"] == '') { //TODO  'site_html_dir' => 'myfiles', xjlxjlk 
					$go_url = $sg . "/" . $html_file;
				} else {
					$go_url = $sg . $c ["dir"] . "/" . $html_file;
					$go_url = str_replace ( array ("///", "//" ), "/", $go_url );
				}
				if ($pages !== 0) {
					$go_url = str_replace ( '.', '[p].', $go_url );
					$go_url = pagetxt_html ( $go_url, $pages ['total_page'], $ispage );
				}
			} else {
				$go_url = "index.php?file=" . $c ["file"];
				if ($pages !== 0)
					$go_url = pagetxt ( $pages );
			}
			break;
	}
	return $go_url;
}

//分页代码
function pagetxt($pagearray, $pageno = 3) {
	if (is_array ( $pagearray )) {
		$pagetxt = '';
		//print_r($pagearray);die;
		$pageurl = $_SERVER ["QUERY_STRING"];
		if ($pagearray ['current_page'] > 1) {
			$pagetxt .= '<li><a href="' . url_set_value ( $pageurl, 'page', 1 ) . '">首页</a></li><li><a href="' . url_set_value ( $pageurl, 'page', $pagearray ['prev_page'] ) . '">上一页</a></li>';
		} else {
			
			$pagetxt .= '<li><a href="javascript:;">首页</a></li>';
		}
		$pageno1 = $pagearray ['current_page'] - $pageno;
		if ($pageno1 < 1)
			$pageno1 = 1;
		$pageno2 = $pagearray ['current_page'] + $pageno;
		if ($pageno2 > $pagearray ['total_page']) {
			$pageno2 = $pagearray ['total_page'];
		}
		while ( $pageno1 <= $pageno2 ) {
			if ($pagearray ['current_page'] == $pageno1) {
				$pagetxt .= '<li class="c"><a href="javascript:;" class="current_page">' . $pageno1 . '</a></li>';
			} else {
				$pagetxt .= '<li><a href="' . url_set_value ( $pageurl, 'page', $pageno1 ) . '">' . $pageno1 . '</a></li>';
			}
			$pageno1 ++;
		}
		
		if ($pagearray ['current_page'] < $pagearray ['last_page']) {
			$pagetxt .= '<li><a href="' . url_set_value ( $pageurl, 'page', $pagearray ['next_page'] ) . '">下一页</a></li><li><a href="' . url_set_value ( $pageurl, 'page', $pagearray ['last_page'] ) . '">尾页</a></li>';
		}
	}
	return $pagetxt;
}
//静态html分页代码
function pagetxt_html($url, $total_page, $current_page, $pageno = 3) {
	if ($GLOBALS ['LD_I'] ['rewrite'] ["rewrite_open"] == 1) {
		$is_p = 1;
	} else {
		$is_p = '';
	}
	$pagetxt = '';
	$n = $current_page + 1;
	$p = $current_page - 1;
	if ($current_page > 1) {
		$pagetxt .= '<li><a href="' . str_replace ( '[p]', $is_p, $url ) . '">首页</a></li>';
		if ($current_page == 2) {
			$pagetxt .= '<li><a href="' . str_replace ( '[p]', $is_p, $url ) . '">上一页</a></li>';
		} else {
			$pagetxt .= '<li><a href="' . str_replace ( '[p]', $p, $url ) . '">上一页</a></li>';
		}
	}
	$pageno1 = $current_page - $pageno;
	if ($pageno1 < 1)
		$pageno1 = 1;
	$pageno2 = $current_page + $pageno;
	if ($pageno2 > $total_page)
		$pageno2 = $total_page;
	while ( $pageno1 <= $pageno2 ) {
		if ($current_page == $pageno1) {
			$pagetxt .= '<li class="c">' . $pageno1 . '</li>';
		} else {
			if ($pageno1 == 1) {
				$pagetxt .= '<li><a href="' . str_replace ( '[p]', $is_p, $url ) . '">' . $pageno1 . '</a></li>';
			} else {
				$pagetxt .= '<li><a href="' . str_replace ( '[p]', $pageno1, $url ) . '">' . $pageno1 . '</a></li>';
			}
		}
		$pageno1 ++;
	}
	if ($current_page < $total_page) {
		$pagetxt .= '<li><a href="' . str_replace ( '[p]', $n, $url ) . '">下一页</a></li><li><a href="' . str_replace ( '[p]', $total_page, $url ) . '">尾页</a></li>';
	}
	return $pagetxt;
}
//评论分页
function pagetxt_comment($pagearray, $url, $pageno = 3) {
	$pagetxt = '';
	if ($pagearray ['current_page'] > 1) {
		$pagetxt .= '<li><a href="' . $url . '&comment_page=1">首页</a></li><li><a href="' . $url . '&comment_page=' . $pagearray ['prev_page'] . '">上一页</a></li>';
	}
	$pageno1 = $pagearray ['current_page'] - $pageno;
	if ($pageno1 < 1) {
		$pageno1 = 1;
	}
	$pageno2 = $pagearray ['current_page'] + $pageno;
	if ($pageno2 > $pagearray ['total_page']) {
		$pageno2 = $pagearray ['total_page'];
	}
	while ( $pageno1 <= $pageno2 ) {
		if ($pagearray ['current_page'] == $pageno1) {
			$pagetxt .= '<li class="c">' . $pageno1 . '</li>';
		} else {
			$pagetxt .= '<li><a href="' . $url . '&comment_page=' . $pageno1 . '">' . $pageno1 . '</a></li>';
		}
		$pageno1 ++;
	}
	if ($pagearray ['current_page'] < $pagearray ['last_page']) {
		$pagetxt .= '<li><a href="' . $url . '&comment_page=' . $pagearray ['next_page'] . '">下一页</a></li><li><a href="' . $url . '&comment_page=' . $pagearray ['last_page'] . '">尾页</a></li>';
	}
	return $pagetxt;
}

function log_write($message, $type = 'php') {
	global $ld_IP, $ld_TIME, $_username;
	
	$ld_IP or $ld_IP = get_env ( 'ip' );
	$ld_TIME or $ld_TIME = time ();
	$user = $_username ? $_username : 'guest';
	$log = "<$type>\n";
	$log .= "\t<time>" . date ( 'Y-m-d H:i:s', $ld_TIME ) . "</time>\n";
	$log .= "\t<ip>" . $ld_IP . "</ip>\n";
	$log .= "\t<user>" . $user . "</user>\n";
	$log .= "\t<file>" . $_SERVER ['SCRIPT_NAME'] . "</file>\n";
	$log .= "\t<querystring>" . str_replace ( '&', '&amp;', $_SERVER ['QUERY_STRING'] ) . "</querystring>\n";
	$log .= "\t<message>" . $message . "\t</message>\n";
	$log .= "</$type>";
	file_put ( APP_PHAT . '/file/log/' . date ( 'Ym', $ld_TIME ) . '/' . $type . '-' . date ( 'Y.m.d H.i.s', $ld_TIME ) . '-' . strtolower ( random ( 10 ) ) . '.xml', $log );
}

function get_env($type) {
	switch ($type) {
		case 'ip' :
			$ld_IP = '';
			if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
				$ld_IP = getenv ( 'HTTP_CLIENT_IP' );
			} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
				$ld_IP = getenv ( 'HTTP_X_FORWARDED_FOR' );
			} elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
				$ld_IP = getenv ( 'REMOTE_ADDR' );
			} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
				$ld_IP = $_SERVER ['REMOTE_ADDR'];
			}
			preg_match ( "/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ld_IP ) or $ld_IP = 'unknown';
			return $ld_IP;
			break;
		case 'self' :
			return isset ( $_SERVER ['PHP_SELF'] ) ? $_SERVER ['PHP_SELF'] : (isset ( $_SERVER ['SCRIPT_NAME'] ) ? $_SERVER ['SCRIPT_NAME'] : $_SERVER ['ORIG_PATH_INFO']);
			break;
		case 'referer' :
			return isset ( $_SERVER ['HTTP_REFERER'] ) ? $_SERVER ['HTTP_REFERER'] : '';
			break;
		case 'domain' :
			return $_SERVER ['SERVER_NAME'];
			break;
		case 'scheme' :
			return $_SERVER ['SERVER_PORT'] == '443' ? 'https://' : 'http://';
			break;
		case 'port' :
			return $_SERVER ['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER ['SERVER_PORT'];
			break;
		case 'url' :
			if (isset ( $_SERVER ['REQUEST_URI'] )) {
				$uri = $_SERVER ['REQUEST_URI'];
			} else {
				$uri = isset ( $_SERVER ['argv'] ) ? $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['argv'] [0] : $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['QUERY_STRING'];
			}
			$uri = dhtmlspecialchars ( $uri );
			
			$urls = get_env ( 'scheme' ) . $_SERVER ['HTTP_HOST'] . (strpos ( $_SERVER ['HTTP_HOST'], ':' ) === false ? get_env ( 'port' ) : '') . $uri;
			return $urls;
			break;
	}
}

function func_get_iglobal($key) {
	$val = $GLOBALS ['LD_I'];
	foreach ( $key as $v ) {
		$val = $val [$v];
	}
	return $val;
}

function func_set_iglobal($arr, $val) {
	$str = "\$GLOBALS['LD_I']";
	foreach ( $arr as $key => $v ) {
		$GLOBALS ['LD_I'] [$key] = $v;
		$str .= "[" . $v . "]";
	}
	$str .= "= \"$val\";";
	eval ( $str );
}

/**
 * 获取请求ip
 *
 * @return ip地址
 */
function ip() {
	if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
		$ip = getenv ( 'HTTP_CLIENT_IP' );
	} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
		$ip = getenv ( 'HTTP_X_FORWARDED_FOR' );
	} elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
		$ip = getenv ( 'REMOTE_ADDR' );
	} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
		$ip = $_SERVER ['REMOTE_ADDR'];
	}
	return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}

/**
 * HTML转String
 */
function SpHtml2Text($str) {
	$str = preg_replace ( "/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU", "", $str );
	$alltext = "";
	$start = 1;
	for($i = 0; $i < strlen ( $str ); $i ++) {
		if ($start == 0 && $str [$i] == ">") {
			$start = 1;
		} else if ($start == 1) {
			if ($str [$i] == "<") {
				$start = 0;
				$alltext .= " ";
			} else if (ord ( $str [$i] ) > 31) {
				$alltext .= $str [$i];
			}
		}
	}
	$alltext = str_replace ( "　", " ", $alltext );
	$alltext = preg_replace ( "/&([^;&]*)(;|&)/", "", $alltext );
	$alltext = preg_replace ( "/[ ]+/s", " ", $alltext );
	return $alltext;
}

function get_type_fullDir($tid)
{
	$type_dirs=getAllDir();
	return $type_dirs[$tid];
}

function getAllDir() {
	global $type_dirs;
	if (! isset ( $type_dirs )) {
		$types = syDB ( 'classtype' )->findAll ();
		$type_dirs = array ();
		foreach ( $types as $t ) {
			
			$path = $t ['childrendir'];
			$pid = $t ['pid'];
			$path = getAllPath ( $types, $pid, $path );
			$type_dirs [$t ['tid']] = $path;
		}
	}
	return $type_dirs;
}

function getAllPath($types, $pid, $path) {
	global $type_dirs;
	foreach ( $types as $parent ) {
		if ($parent ['tid'] == $pid) {
			$path = $parent ['childrendir'] . "/" . $path;
			$pid = $parent ['pid'];
			$path = getAllPath ( $types, $pid, $path );
		}
	}
	return $path;
}

	/**
	 * 调用第三方支付接口
	 */
	function call_other_pay($record)
	{
		global  $iConfig;	
		$id=$record["id"];
		if($record['payment_type']==1){
			echo "<script type='text/javascript'>window.location='".$iConfig['ext']['http_path']."/payment/alipay/alipayto.php?id=".$id."'</script>";
		}
		if($record['payment_type']==2){
			echo "<script type='text/javascript'>window.location='".$iConfig['ext']['http_path']."/payment/tenpay/tenpay.php?id=".$id."'</script>";
		}
	}
	
	/**
 * 字符截取 支持UTF8/GBK
 * @param $string
 * @param $length
 * @param $dot
 */
function str_cut($string, $length, $dot = '...') {
	$strlen = strlen($string);
	if($strlen <= $length) return $string;
	$string = str_replace(array(' ','&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
	$strcut = '';
	if(strtolower(CHARSET) == 'utf-8') {
		$length = intval($length-strlen($dot)-$length/3);
		$n = $tn = $noc = 0;
		while($n < strlen($string)) {
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif(224 <= $t && $t <= 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}
			if($noc >= $length) {
				break;
			}
		}
		if($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr($string, 0, $n);
		$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
	} else {
		$dotlen = strlen($dot);
		$maxi = $length - $dotlen - 1;
		$current_str = '';
		$search_arr = array('&',' ', '"', "'", '“', '”', '—', '<', '>', '·', '…','∵');
		$replace_arr = array('&amp;','&nbsp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;',' ');
		$search_flip = array_flip($search_arr);
		for ($i = 0; $i < $maxi; $i++) {
			$current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
			if (in_array($current_str, $search_arr)) {
				$key = $search_flip[$current_str];
				$current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
			}
			$strcut .= $current_str;
		}
	}
	return $strcut.$dot;
}


/**
 * 获取远程内容
 * Enter description here ...
 * @param unknown_type $remote_url
 * @throws Exception
 */

function get_remote_content($remote_url) {
	if (extension_loaded ( "curl" )) {
		$ch = curl_init ( $remote_url );
		$str = curl_exec ( $ch );
		curl_close ( $ch );
		return $str;
	} else {
		$file = fopen ( $remote_url, "r" );
		$t = fread ( $file, '1024' );
		$str .= $t;
		while ( strlen ( $t ) > 0 ) {
			$t = fread ( $file, '1024' );
			$str .= $t;
		}
		fclose ( $remote_url );
		return $str;
	}
}
 
/**
 * 进行版本比对  如果  需要更新的返回  最新的版本号  下载并且更新,否则 返回 false 
 * Enter description here ...
 * @param string $vision 版本号  
 */

function checkVision($version) {	
	$url = "http://union.5545.cn/version.php?a=last&v=$version&e=" . Encoding . "&arc=" . ARC;
	try {
		$str = get_remote_content($url);
		if ($str === 0) //传入参数异常 需要设置系统违法
		{
		
		} elseif ($str !== 1) //获取的是版本列表  	//1  表示已经是系统最新版本 无需操作
		{
			$list = json_decode ( $str );
			if (count ( $list ) > 0) {
				//根据远程路径获取包 进行更新	
				foreach ( $list as $v ) {
					$script = $v ['script']; //每个要更新的动作都是通过\r 分开的一条记录
					$scripts = explode ( '\r', $script );
					foreach ( $scripts as $s ) {
						$a = substr ( $s, 0, 1 );
						$path = substr ( $s, 2 );
						$lpath = APP_PATH . $path;
						if ($a === 'a' || $a === 'u') //需要下载
						{
							$rurl = "http://union.5545.cn/version/" . $v ['v'] . $path . ".js";
							$content=get_remote_content($rurl);
							if ($a === 'u') {
								if (file_exists ( $lpath )) {
									unlink ( $lpath );
								}
							}
							$fp=fopen($lpath, 'r+');
							fwrite($fp, $content);
							fclose($fp);
						} else if ($a === 'd') {
							if (file_exists ( $lpath )) {
								unlink ( $lpath );
							}
						}
					}
					updateconfig(array('ext','version'), $v['version']);
				}
			}
			
			
		}
	} catch ( Exception $e ) {
	}
}

/**
 * 更新配置系统信息
 * Enter description here ...
 * @param array $key  可以的数组 比如 更新 $iConfig['ext']['version'] 时   key 为 array('ext','version');
 * @param string $value 要更新的值
 * @param int $isstr 是否为字符串
 */
function updateconfig($key=array(),$value,$isstr=0)
{		
	
	
}

/**
 * 异步提交安装信息
 * Enter description here ...
 * @param unknown_type $domain  域名
 * @param unknown_type $email	邮件
 * @param unknown_type $industry 主营行业
 * @param unknown_type $version  
 * @param unknown_type $installip 安装IP
 */
function submitdata($domain, $email, $industry, $version, $installip, $site_name = null, $link_user = null, $qq = null) {
	try {
		$url = "http://union.5545.cn/installsite.php";
		$url .= "?domain=$domain&email=$email&industry=$industry&version=$version&installip=$installip&u=$link_user&sn=$site_name&qq=$qq&e=" . Encoding . "&arc=" . ARC;
		if (extension_loaded ( "curl" )) {
			$ch = curl_init ( $url );
			curl_exec ( $ch );
			curl_close ( $ch );
		} else {
			$file = fopen ( $url, "r" );
			$t = fread ( $file, '1024' );
		}
	} catch ( Exception $e ) {
	}
}
