<?php
/********************************************************************
 *                             Dircms模板解析函数
 *                            --------------------
 *   begin                : 2009/11/24
 *   copyright            : (C) 2009 DirCMS
 *   email                : uuturn@126.com
 *
 *   $Id: template.func.php,Sp4 2011-1-1  M Exp $
 ********************************************************************/
function template_complie($tlpname)
{
	$tlpfile=TPL_ROOT.TPL_NAME.'/'.$tlpname.'.html';
	!file_exists($tlpfile) && fatal_error('模板文件'.$tlpname.'不存在');
	$content=get_file_content($tlpfile);
	$compliedfile=TPL_CACHEPATH.$tlpname.'.tlp.php';
	$content=template_parse($content);
	$strlen=@file_put_contents($compliedfile,$content);
	@chmod($compliedfile,0777);
	return $strlen;
}

function spacetlp_complie($path,$tlpname)
{
	$cachepath=TPL_CACHEPATH.'comtmp/'.$path.'/';
	if(!file_exists($cachepath))@mkdir($cachepath);
	$tlpfile=DIRCMS_ROOT.'template/comtmp/'.$path.'/'.$tlpname.'.html';
	!file_exists($tlpfile) && fatal_error('模板文件'.$tlpname.'不存在');
	$content=get_file_content($tlpfile);
	$compliedfile=$cachepath.$tlpname.'.tlp.php';
	$content=template_parse($content);
	$strlen=@file_put_contents($compliedfile,$content);
	@chmod($compliedfile,0777);
	return $strlen;
}

function template_parse($str) //解析模板 
{
	$str=preg_replace('/\{\{tlp\s+(.+)\}\}/i','<?php include template(\'\\1\');?>',$str); 
	$str=preg_replace('/\{\{include\s+(.+)\}\}/i','<?php include(\\1);?>',$str);
	$str=preg_replace('/\{\{mydc:([0-9a-z_]+)\}\}/i','<?php echo $r[\'\\1\'];?>',$str);
	$str=preg_replace('/mydc:([0-9a-z_]+)/i','$r[\'\\1\']',$str);
	$str=preg_replace('/\{\{code\s+(.+)\}\}/','<?php \\1?>',$str);

	$str=preg_replace('/\{\{(\$[a-z0-9_\'\"\[\]\x7f-\xff\$]+)\}\}/i','<?php echo \\1;?>',$str);
	$str=preg_replace('/\{\{if\s+([^\}\}]+)\}\}/i','<?php if(\\1) { ?>',$str);
	$str=preg_replace('/\{\{else\s*if\s+([^\}\}]+)\}\}/i','<?php }elseif(\\1){ ?>',$str);
	$str=preg_replace('/\{\{else\}\}/i','<?php }else{ ?>',$str);
	$str=preg_replace('/\{\{end\s*if\}\}/i','<?php }?>',$str);
	$str=preg_replace('/\{\{\/if\}\}/i','<?php }?>',$str);
	$str=preg_replace('/\{\{loop\s+(\S+)\s+(\S+)\}\}/i','<?php $no=1;if(is_array(\\1))foreach(\\1 as \\2){?>',$str);
	$str=preg_replace('/\{\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}\}/i','<?php $no=1;if(is_array(\\1))foreach(\\1 as \\2=>\\3){?>',$str);
	$str=preg_replace('/\{\{end\s*loop\}\}/i','<?php $no++;}?>',$str);
	$str=preg_replace('/\{\{\/loop\}\}/i','<?php $no++;}?>',$str);
	$str=preg_replace('/\{\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}\}/','<?php echo \\1;?>',$str);

	$str=preg_replace('/\{\{dir:area\s+parentid=\"([0-9a-z_\$:\[\]\']+)\" row=\"([0-9]+)\"\}\}/ie',"get_area_tag('\\1','\\2')",$str);
	$str=preg_replace('/\{\{\/dir:area\}\}/i',"<?php }unset(\$_DATA);\$areaid=\$dir_areaid;?>",$str); 

	$str=preg_replace('/\{\{dir:membertype\s+parentid=\"([0-9a-z_\$:\[\]\']+)\" row=\"([0-9]+)\"\}\}/ie',"get_membertype_tag('\\1','\\2')",$str);
	$str=preg_replace('/\{\{\/dir:membertype\}\}/i',"<?php }unset(\$_DATA);\$membertypeid=\$dir_membertypeid;?>",$str);

	$str=preg_replace('/\{\{dir:category\s+parentid=\"([0-9a-z\[\]\'\"\$_]*)\"\s+row=\"([0-9]*)\"\s+type=\"([a-z]*)\"\s+istop=\"([0-1]*)\"\}\}/i',"<?php \$_DATA=get_category_tag(\\1,'\\2','\\3','\\4');\$dir_catid=\$catid;foreach(\$_DATA as \$no => \$r){ \$catid=\$r['id'];?>",$str);
	$str=preg_replace('/\{\{\/dir:category\}\}/i',"<?php }unset(\$_DATA); \$catid=\$dir_catid;?>",$str); 

	$str=preg_replace('/\{\{dir:content\s+catid=\"([0-9a-z_\[\]\'\"\$]*)\"\s+posid=\"([0-9]*)\"\s+row=\"([0-9]*)\"\s+limit=\"([0-9]*)\"\s+page=\"([\$a-z_0-1]*)\"\s+withthumb=\"([0-1]*)\"\s+orderby=\"([a-zA-Z\s ]*)\"\s+keyword=\"([^\"]*)\"\s+ismore=\"([0-1]*)\"\}\}/ie',"get_tag('\\1','\\2','\\3','\\4','\\5','\\6','\\7','\\8','\\9')",$str); 

	$str=preg_replace('/\{\{dir:content\s+catid=\"([0-9a-z_\[\]\'\"\$]*)\"\s+posid=\"([0-9]*)\"\s+row=\"([0-9]*)\"\s+limit=\"([0-9]*)\"\s+page=\"([\$a-z_0-1]*)\"\s+withthumb=\"([0-1]*)\"\s+orderby=\"([a-zA-Z\s ]*)\"\s+keyword=\"([^\"]*)\"\}\}/ie',"get_tag('\\1','\\2','\\3','\\4','\\5','\\6','\\7','\\8')",$str);

	$str=preg_replace('/\{\{\/dir:content\}\}/i',"<?php } \$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);
	
	$str=preg_replace('/\{\{dir:flink\s+row=\"([0-9]+)\"\s+withlogo=\"([0-9]+)\"\s+isindex=\"([0-9]+)\"\}\}/ie',"get_flink_tag('\\1','\\2','\\3')",$str); 
	$str=preg_replace('/\{\{\/dir:flink\}\}/i',"<?php }unset(\$_DATA); ?>",$str); 

	$str=preg_replace('/\{\{dir:notice\s+row=\"([0-9]+)\"\s+orderby=\"([0-9a-z_\s ]+)\"\}\}/ie',"get_notice_tag('\\1','\\2')",$str); 
	$str=preg_replace('/\{\{\/dir:notice\}\}/i',"<?php }unset(\$_DATA); ?>",$str);

	$str=preg_replace('/\{\{dir:member\s+modelid=\"([a-z0-9_]+)\"\s+row=\"([0-9]+)\"\s+orderby=\"([0-9a-z_\s ]+)\"\s+ispage=\"([0-9a-z_\$]+)\"\}\}/ie',"get_member_tag('\\1','\\2','\\3','\\4')",$str); 
	$str=preg_replace('/\{\{\/dir:member\}\}/i',"<?php } \$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);

	$str=preg_replace('/\{\{dir:member\s+modelid=\"([a-z0-9_]+)\"\s+row=\"([0-9]+)\"\s+orderby=\"([0-9a-z_\s ]+)\"\s+ispage=\"([0-9a-z_\$]+)\"\s+grade=\"([0-9]+)\"\}\}/ie',"get_member_tag('\\1','\\2','\\3','\\4','\\5')",$str); 
	$str=preg_replace('/\{\{\/dir:member\}\}/i',"<?php } \$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);
	
	$str=preg_replace('/\{\{dir:comment\s+contentid=\"([0-9a-z_\$]+)\"\s+row=\"([0-9]+)\"\s+page=\"([\$a-z_0-1]*)\"\}\}/ie',"get_comment_tag('\\1','\\2','\\3')",$str); 
	$str=preg_replace('/\{\{\/dir:comment\}\}/i',"<?php }\$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);
	
	$str=preg_replace('/\{\{dir:co_guestbook\s+memberid=\"([0-9a-z_\$]+)\"\s+row=\"([0-9]+)\"\s+page=\"([\$a-z_0-1]*)\"\}\}/ie',"get_co_guestbook_tag('\\1','\\2','\\3')",$str); 
	$str=preg_replace('/\{\{\/dir:co_guestbook\}\}/i',"<?php }\$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str); 

	$str=preg_replace('/\{\{dir:co_coupon\s+memberid=\"([0-9a-z_\$]+)\"\s+row=\"([0-9]+)\"\s+page=\"([\$a-z_0-1]*)\"\}\}/ie',"get_co_coupon_tag('\\1','\\2','\\3')",$str); 
	$str=preg_replace('/\{\{\/dir:co_coupon\}\}/i',"<?php }\$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);

	$str=preg_replace('/\{\{dir:co_news\s+memberid=\"([0-9a-z_\$]+)\"\s+row=\"([0-9]+)\"\s+page=\"([\$a-z_0-1]*)\"\}\}/ie',"get_co_news_tag('\\1','\\2','\\3')",$str); 
	$str=preg_replace('/\{\{\/dir:co_news\}\}/i',"<?php }\$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);

	$str=preg_replace('/\{\{dir:co_product\s+memberid=\"([0-9a-z_\$]+)\"\s+row=\"([0-9]+)\"\s+page=\"([\$a-z_0-1]*)\"\}\}/ie',"get_co_product_tag('\\1','\\2','\\3')",$str); 
	$str=preg_replace('/\{\{\/dir:co_product\}\}/i',"<?php }\$dir_page=\$_DATA['pagestring'];unset(\$_DATA); ?>",$str);


	$str=preg_replace('/\{\{dir:vote\s+action=\"([a-z]+)\" id=([\$0-9a-z_]+)\}\}/i','<?php if(\\1=="post"){postvote(\\2);}else{viewvote(\\2);}?>',$str); // 2010-11-17
	return $str;
}

function getpre($id,$len=20)
{
	global $db;
	$id=intval($id);
	$r=$db->fetch_one("SELECT `".DB_PRE."content`.`url`,`".DB_PRE."content`.`title` FROM `".DB_PRE."content` WHERE `".DB_PRE."content`.`id`<$id ORDER BY id DESC LIMIT 0,1");
	return $r?'<a href="'.$r['url'].'" title="'.$r['title'].'">'.sub_string($r['title'],$len).'</a>':'没有了';
}

function getnext($id,$len=20)
{
	global $db;
	$id=intval($id);
	$r=$db->fetch_one("SELECT `".DB_PRE."content`.`url`,`".DB_PRE."content`.`title` FROM `".DB_PRE."content` WHERE `".DB_PRE."content`.`id`>$id ORDER BY id ASC LIMIT 0,1");
	return $r?'<a href="'.$r['url'].'" title="'.$r['title'].'">'.sub_string($r['title'],$len).'</a>':'没有了';
}

function get_area_tag($parentid=0,$row=0)
{	
	return '<?php $dir_areaid=$areaid;$_DATA=get_sql_area_data('.$parentid.','.$row.');foreach($_DATA as $no => $r)if(is_array($r)){$areaid=$r["id"];?>';
}

function get_member_tag($modelid='nomalmember',$row=0,$orderby='id DESC',$ispage=0,$grade=10) // 2011-1-27
{	
	global $db;
	$r=$db->fetch_one("SELECT `".DB_PRE."membermodel`.`id` FROM `".DB_PRE."membermodel` WHERE `".DB_PRE."membermodel`.`name`='{$modelid}'");
	$r=cache_read('cache_membermodel'.$r['id'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');

	if($r['iscompany'])
	{
		$sql="SELECT a.*,b.* FROM ".DB_PRE."member a,".DB_PRE.$modelid." b WHERE a.id=b.userid AND a.islocked=0 AND a.gradeid=".intval($grade)." AND a.ispassed=1 AND a.iscompmayed=1 ORDER BY a.".$orderby;
	}
	else
	{
		$sql="SELECT a.*,b.* FROM ".DB_PRE."member a,".DB_PRE.$modelid." b WHERE a.id=b.userid AND a.islocked=0 AND a.gradeid=".intval($grade)." AND a.ispassed=1 ORDER BY a.".$orderby;
	}
	return '<?php $dir_membertypeid=$areaid;$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$ispage.',1);foreach($_DATA as $no => $r)if(is_array($r)){$membertypeid=$r["id"];?>';
}

function get_category_tag($parentid=0,$row="10",$type="top",$istop=0)
{
	$row=intval($row);
	$t=$_DATA=array();
	switch($type)
	{
		case 'top':
			$_DATA=cache_read('parent.cache.php',DIRCMS_ROOT.'data/cache_category/');
			break;
		case 'son':
			$_DATA=cache_read('parent'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			break;
		case 'self':
			$r=cache_read('category'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			$_DATA=cache_read('parent'.$r['parentid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			break;
	}

	if($istop)
	{
		foreach($_DATA as $_r)
		{
			if($_r['istop'] && !$_r['ispage'])$t[]=$_r;
		}
	}
	else
	{
		foreach($_DATA as $_r)
		{
			if(!$_r['ispage'])$t[]=$_r;
		}
	}
	return array_slice($t,0,$row);
}

function get_sql_area_data($parentid=0,$row=0)
{
	$r=cache_read('all_area.cache.php',DIRCMS_ROOT.'data/cache/');
	$_DATA=array();
	$i=0;
	if(!$parentid) // 返回根列表
	{
		if($r)foreach($r as $_r)
		{
			$i++;
			if($i>$row && $row)break;
			if($_r['parentid']==0)$_DATA[]=$_r;
		}
	}
	else // 子分类
	{
		if($r)foreach($r as $_r)
		{
			$i++;
			if($i>$row && $row)break;
			if($_r['parentid']==$parentid)$_DATA[]=$_r;
		}
	}

	return $_DATA;
}

function get_membertype_tag($parentid=0,$row=0)
{	
	return '<?php $_DATA=get_sql_membertype_data('.$parentid.','.$row.');foreach($_DATA as $no => $r)if(is_array($r)){$typeid=$r["id"];?>';
}

function get_sql_membertype_data($parentid=0,$row=0)
{
	$r=cache_read('all_member_type.cache.php',DIRCMS_ROOT.'data/cache/');
	$_DATA=array();
	$i=0;
	if(!$parentid) // 返回根列表
	{
		if($r)foreach($r as $_r)
		{
			$i++;
			if($i>$row && $row)break;
			if($_r['parentid']==0)$_DATA[]=$_r;
		}
	}
	else // 子分类
	{
		if($r)foreach($r as $_r)
		{
			$i++;
			if($i>$row && $row)break;
			if($_r['parentid']==$parentid)$_DATA[]=$_r;
		}
	}

	return $_DATA;
}

function get_notice_tag($row="10", $orderby="id desc")
{
	$row=$row?$row:10;
	$orderby=$orderby?$orderby:"id desc";
	$sql="SELECT * FROM `\".DB_PRE.\"notice` ORDER BY ".$orderby;
	return '<?php $_DATA=get_sql_tag_data("'.$sql.'",'.$row.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_co_coupon_tag($memberid="0", $row="10",$page="0")
{
	$row=$row?$row:10;
	if($memberid)
	{
		if($memberid=='$id')
		{
			$sql='SELECT * FROM `".DB_PRE."co_coupon` WHERE `".DB_PRE."co_coupon`.`status`=1 AND `".DB_PRE."co_coupon`.`userid`=".intval($id)." ORDER BY `".DB_PRE."co_coupon`.`id` DESC';	
		}
		else
		{
			$sql='SELECT * FROM `".DB_PRE."co_coupon` WHERE `".DB_PRE."co_coupon`.`status`=1 AND `".DB_PRE."co_coupon`.`userid`='.intval($memberid).' ORDER BY `".DB_PRE."co_coupon`.`id` DESC';	
		}
	}
	else
	{
		$sql="SELECT * FROM `\".DB_PRE.\"co_coupon` WHERE `\".DB_PRE.\"co_coupon`.`status`=1 ORDER BY `\".DB_PRE.\"co_coupon`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}


function get_co_product_tag($memberid="0", $row="10",$page="0")
{
	global $colname;
	$colname=intval($colname);
	$row=$row?$row:10;
	if($colname)
	{
		if($memberid)
		{
			if($memberid=='$id')
			{
				$sql='SELECT * FROM `".DB_PRE."co_product` WHERE `".DB_PRE."co_product`.`status`=1 AND `".DB_PRE."co_product`.`typeid`='.$colname.' AND `".DB_PRE."co_product`.`userid`=".intval($id)." ORDER BY `".DB_PRE."co_product`.`id` DESC';		
			}
			else
			{
				$sql='SELECT * FROM `".DB_PRE."co_product` WHERE `".DB_PRE."co_product`.`status`=1 AND `".DB_PRE."co_product`.`typeid`='.$colname.' AND `".DB_PRE."co_product`.`userid`='.intval($memberid).' ORDER BY `".DB_PRE."co_product`.`id` DESC';		
			}
		}
		else
		{
			$sql="SELECT * FROM `\".DB_PRE.\"co_product` WHERE `\".DB_PRE.\"co_product`.`status`=1 AND `".DB_PRE."co_product`.`typeid`=".$colname." ORDER BY `\".DB_PRE.\"co_product`.`id` DESC";
		}
	}
	else
	{
		if($memberid)
		{
			if($memberid=='$id')
			{
				$sql='SELECT * FROM `".DB_PRE."co_product` WHERE `".DB_PRE."co_product`.`status`=1 AND `".DB_PRE."co_product`.`userid`=".intval($id)." ORDER BY `".DB_PRE."co_product`.`id` DESC';		
			}
			else
			{
				$sql='SELECT * FROM `".DB_PRE."co_product` WHERE `".DB_PRE."co_product`.`status`=1 AND `".DB_PRE."co_product`.`userid`='.intval($memberid).' ORDER BY `".DB_PRE."co_product`.`id` DESC';		
			}
		}
		else
		{
			$sql="SELECT * FROM `\".DB_PRE.\"co_product` WHERE `\".DB_PRE.\"co_product`.`status`=1 ORDER BY `\".DB_PRE.\"co_product`.`id` DESC";
		}
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}


function get_co_news_tag($memberid="0", $row="10",$page="0")
{
	$row=$row?$row:10;
	if($memberid)
	{
		if($memberid=='$id')
		{
			$sql='SELECT * FROM `".DB_PRE."co_news` WHERE `".DB_PRE."co_news`.`status`=1 AND `".DB_PRE."co_news`.`userid`=".intval($id)." ORDER BY `".DB_PRE."co_news`.`id` DESC';	
		}
		else
		{
			$sql='SELECT * FROM `".DB_PRE."co_news` WHERE `".DB_PRE."co_news`.`status`=1 AND `".DB_PRE."co_news`.`userid`='.intval($memberid).' ORDER BY `".DB_PRE."co_news`.`id` DESC';		
		}
	}
	else
	{
		$sql="SELECT * FROM `\".DB_PRE.\"co_news` WHERE `\".DB_PRE.\"co_news`.`status`=1 ORDER BY `\".DB_PRE.\"co_news`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_co_guestbook_tag($memberid="0", $row="10",$page="0")
{
	$row=$row?$row:10;
	if($memberid)
	{
		if($memberid=='$id')
		{
			$sql='SELECT * FROM `".DB_PRE."member_comment` WHERE `".DB_PRE."member_comment`.`status`=1 AND `".DB_PRE."member_comment`.`memberid`=".intval($id)." ORDER BY `".DB_PRE."member_comment`.`id` DESC';	
		}
		else
		{
			$sql='SELECT * FROM `".DB_PRE."member_comment` WHERE `".DB_PRE."member_comment`.`status`=1 AND `".DB_PRE."member_comment`.`memberid`='.intval($memberid).' ORDER BY `".DB_PRE."member_comment`.`id` DESC';		
		}
	}
	else
	{
		$sql="SELECT * FROM `\".DB_PRE.\"member_comment` WHERE `\".DB_PRE.\"member_comment`.`status`=1 ORDER BY `\".DB_PRE.\"member_comment`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_comment_tag($contentid="0", $row="10",$page="0")
{
	$row=$row?$row:10;
	if($contentid)
	{
		if($contentid=='$id')
		{
			$sql='SELECT * FROM `".DB_PRE."comment` WHERE `".DB_PRE."comment`.`status`=1 AND `".DB_PRE."comment`.`contentid`=".intval($id)." ORDER BY `".DB_PRE."comment`.`support` DESC,`".DB_PRE."comment`.`id` DESC';	
		}
		else
		{
			$sql='SELECT * FROM `".DB_PRE."comment` WHERE `".DB_PRE."comment`.`status`=1 AND `".DB_PRE."comment`.`contentid`='.intval($contentid).' ORDER BY `".DB_PRE."comment`.`support` DESC,`".DB_PRE."comment`.`id` DESC';	
		}
	}
	else
	{
		$sql="SELECT * FROM `\".DB_PRE.\"comment` WHERE `\".DB_PRE.\"comment`.`status`=1 ORDER BY `\".DB_PRE.\"comment`.`support` DESC,`\".DB_PRE.\"comment`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.',0);foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_flink_tag($row="10", $withlogo="0",$isindex="0")
{
	$row=$row?$row:10;
	$isindex=$isindex?$isindex:0;
	$withlogo=$withlogo?$withlogo:0;
	if($withlogo==0)  //文字链接
	{
		$sql="SELECT `\".DB_PRE.\"link`.`name`,`\".DB_PRE.\"link`.`url`,`\".DB_PRE.\"link`.`introduce` FROM `\".DB_PRE.\"link` WHERE `\".DB_PRE.\"link`.`isindex`=".intval($isindex)." AND `\".DB_PRE.\"link`.`ispassed`=1 AND `\".DB_PRE.\"link`.`logo`='' ORDER BY `\".DB_PRE.\"link`.`orderby` ASC";
	}
	elseif($withlogo==1) //图片链接
	{
		$sql="SELECT `\".DB_PRE.\"link`.`name`,`\".DB_PRE.\"link`.`url`,`\".DB_PRE.\"link`.`logo`,`\".DB_PRE.\"link`.`introduce` FROM `\".DB_PRE.\"link` WHERE `\".DB_PRE.\"link`.`isindex`=".intval($isindex)." AND `\".DB_PRE.\"link`.`ispassed`=1 AND `\".DB_PRE.\"link`.`logo`!='' ORDER BY `\".DB_PRE.\"link`.`orderby` ASC";
	}
	return '<?php $_DATA=get_sql_tag_data("'.$sql.'",'.$row.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_sql_tag_data($sql='',$row=10,$page=0,$cache=false)
{
	global $db,$dir_page;
	$offset=0;
	$row=intval($row)?intval($row):1;
	if($page)$page=max(intval($page),1);
	
	if($page) 
	{
		$row=PAGESIZE;
		$count_sql=preg_replace("/^SELECT([^(]+)\s*FROM(.+)(ORDER BY.+)$/i", "SELECT COUNT(*) AS `count` FROM\\2", $sql);
		$count=get_cache_counts($count_sql); 
		$pagecount=max(ceil($count/$row),1);
		$page=$page>$pagecount?$pagecount:$page;
		$offset=($page-1)*$row;
		$dir_page=getpages($count,$page,$row,0,'center',0);
	}
	$sql.=' LIMIT '.$offset.','.$row;
	if($page || !$cache)
	{
		$data=$db->fetch_all($sql);
	}
	else
	{		
		$data=cache_read(md5($sql).'.cache.php',DIRCMS_ROOT.'data/cache_tag/');
		if(!$data)
		{
			$data=$db->fetch_all($sql);
			
			if($data)
			{
				cache_write(md5($sql).'.cache.php',$data,DIRCMS_ROOT.'data/cache_tag/');
			}
		}
	}

	$data['pagestring']=$dir_page;
	return $data;
}


function get_tag($catid, $posid="0",$row="10",$limit="0",$page=0,$withthumb="0",$orderby="id DESC",$keyword="",$ismore="0")
{
	$keyword=$keyword?$keyword:0;
	return '<?php $dir_page="";$_DATA=get_tag_data('.$catid.',\''. $posid.'\',\''.$row.'\','.$limit.','.$page.',\''.$withthumb.'\',\''.$orderby.'\','.$keyword.',\''.$ismore.'\');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_tag_data($catid, $posid="0",$row="10",$limit=0,$page=0,$withthumb="0",$orderby="id DESC",$keyword="",$ismore="0")
{
	global $db,$dir_page;
	$limit=intval($limit);
	$offset=$limit?$limit:0;
	$row=intval($row)?intval($row):1;
	if($page)$page=max(intval($page),1);
	$orderby=' ORDER BY '.$orderby;
	$where='a.status=1';
	if($catid!='all')
	{
		$catid=str_replace(',','',$catid);
		$catid=intval($catid)?intval($catid):0;
		$where.=' AND a.'.getCatid($catid);
	}
	$where.=intval($withthumb)?' AND a.thumb!=\'\'':'';
	$where.=intval($posid)?' AND b.posid='.$posid:'';
	$where.=$keyword?' AND MATCH(a.tags) AGAINST (\''.strtoZoneCode($keyword).'\' IN BOOLEAN MODE)':'';
	if($posid)
	{
		if($catid && $catid!='all' &&$ismore)
		{
			$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			if($r)
			{
				$r=cache_read('cache_model'.$r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				if($r)
				{
					$sql="SELECT * FROM ".DB_PRE."content a ,".DB_PRE."content_posid b,".DB_PRE.$r['name']." c WHERE a.id=b.contentid AND a.id=c.contentid AND ".$where.$orderby;
				}
				else
				{
					$sql="SELECT * FROM ".DB_PRE."content a ,".DB_PRE."content_posid b WHERE a.id=b.contentid AND ".$where.$orderby;
				}
			}
			else
			{
				$sql="SELECT * FROM ".DB_PRE."content a ,".DB_PRE."content_posid b WHERE a.id=b.contentid AND ".$where.$orderby;
			}
		}
		else
		{
			$sql="SELECT * FROM ".DB_PRE."content a ,".DB_PRE."content_posid b WHERE a.id=b.contentid AND ".$where.$orderby;
		}
	}
	else
	{
		if($catid && $catid!='all' &&$ismore)
		{
			$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			if($r)
			{
				$r=cache_read('cache_model'.$r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				if($r)
				{
					$sql="SELECT * FROM ".DB_PRE."content a ,".DB_PRE.$r['name']." c WHERE a.id=c.contentid AND ".$where.$orderby;
				}
				else
				{
					$sql="SELECT * FROM ".DB_PRE."content a WHERE ".$where.$orderby;
				}
			}
			else
			{
				$sql="SELECT * FROM ".DB_PRE."content a WHERE ".$where.$orderby;
			}	
		}
		else
		{
			$sql="SELECT * FROM ".DB_PRE."content a WHERE ".$where.$orderby;
		}
	}
	if($page) 
	{
		$row=PAGESIZE;
		$count_sql=preg_replace("/^SELECT([^(]+)\s*FROM(.+)(ORDER BY.+)$/i", "SELECT COUNT(*) AS `count` FROM\\2", $sql);
		$count=get_cache_counts($count_sql); 
		$pagecount=max(ceil($count/$row),1);
		$page=$page>$pagecount?$pagecount:$page;
		$offset=($page-1)*$row;
		$offset=!$offset&&$limit?$limit:$offset;
		$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
		$ishtml=$r['ishtml'] && $catid?1:0;
		$dir_page=getpages($count,$page,$row,$ishtml,'center',$catid);
	}
	$sql.=' LIMIT '.$offset.','.$row;

	if($page)
	{
		$data=$db->fetch_all($sql);
	}
	else
	{		
		$data=cache_read(md5($sql).'.cache.php',DIRCMS_ROOT.'data/cache_tag/');
		if(!$data)
		{
			$data=$db->fetch_all($sql);
			
			if($data)
			{
				cache_write(md5($sql).'.cache.php',$data,DIRCMS_ROOT.'data/cache_tag/');
			}
		}
	}
	$data['pagestring']=$dir_page;
	return $data;
}

function postvote($id)
{
	global $db;
	$r=$db->fetch_one("SELECT * FROM `".DB_PRE."vote` WHERE `".DB_PRE."vote`.`id`={$id}");
	
	if(!$r || !$id)
	{
		echo('暂无投票!');
	}
	else
	{
		$item=explode("\n",$r['votenote']);
		if($item)foreach($item as $_item)
		{
			if($r['ismore'])
			{
				$itemstr.=preg_replace('/<vote id="([0-9]+)" count="([0-9]+)">(.+?)<\/vote>/i',"<tr><td height=22><input type='checkbox' name='voteitem[]' value='\\1' style='border:0px' />\\3</td></tr>",$_item);
			}
			else
			{
				$itemstr.=preg_replace('/<vote id="([0-9]+)" count="([0-9]+)">(.+?)<\/vote>/i',"<tr><td height=22><input type='radio' name='voteitem' value='\\1' />\\3</td></tr>",$_item);
			}
		}
		echo "<table width='100%' border='0' cellspacing='1' cellpadding='1'>
	<form name='voteform' method='post' action='".DIRCMS_PATH."plus/vote.php' target='_blank'>
	<input type='hidden' name='action' value='post' />
	<input type='hidden' name='id' value='".$r['id']."' />
	<tr><td height=22><strong>".$r['votename']."</strong></td></tr>".$itemstr."
	<tr><td height='22'>
	<input type='submit' style='width:60px;border: 1px outset;line-height:18px;padding:0px' name='postbt' value='投票' />
	<input type='button' style='width:80px;border: 1px outset;line-height:18px;padding:0px' name='viewbt' value='查看结果' onClick=\"window.open('".DIRCMS_PATH."plus/vote.php?action=view&id=".$r['id']."');\" /></td></tr>
	</form>
	</table>";
	}
}



function viewvote($id)
{
	global $db;
	$id=intval($id);
	$itemstr='';
	$r=$db->fetch_one("SELECT * FROM `".DB_PRE."vote` WHERE `".DB_PRE."vote`.`id`={$id}");
	
	if(!$r)showmsg('无此投票!');
	$item=explode("\n",$r['votenote']);
	$totalitem=$r['totalcount']?$r['totalcount']:sizeof($item);
	if($item)foreach($item as $_item)
	{
		$len=preg_match('/<vote id="([0-9]+)" count="([0-9]+)">(.+?)<\/vote>/i',$_item,$lenreg);
		$len=$lenreg[2];
		$len=min(100,round($len/$totalitem,2)*100);
		$itemstr.=preg_replace('/<vote id="([0-9]+)" count="([0-9]+)">(.+?)<\/vote>/i',"<tr height='30'><td style='border-bottom:1px solid'>\\1、\\3</td><td style='border-bottom:1px solid'>
			
			<table border='0' cellspacing='0' cellpadding='2' title='$len%' width='$len%'><tr><td height='16' background='images/votebg.gif' style='border:1px solid #666666;font-size:9pt;line-height:110%'>\\2</td></tr></table>
			</td></tr>",$_item);
	}
	echo "<table width='98%' border='0' cellspacing='1' cellpadding='1'>
<tr><td height=22><strong>".$r['votename']."</strong> </td><td align='right'>投票开始时间：".date('Y-m-d',$r['starttime'])."　 投票结束时间：".date('Y-m-d',$r['endtime'])."   &nbsp;投票总计：".$r['totalcount']."</td></tr>
<tr height='8'><td width='30%'></td><td></td></tr>".$itemstr."
<tr><td></td><td></td></tr>
</table>";
}

function hh($b)
{
	return base64_decode($b);
}
?>