<?php
require_once(PHP168_PATH."inc/label_funcation.php");

/**
*獲取模板所有的標簽
**/
unset($haveCache);
if($jobs=='show')
{	
	//目的是為了兼容其它頻道模型
	if(!function_exists('getTpl')){
		function getTpl($a,$b){
			return html($a,$b);
		}
	}
	if(!$_COOKIE[Admin])
	{
		showerr("你無權查看");
	}
	//獲取頭與尾的標簽
	preg_replace('/\$label\[([\'a-zA-Z0-9\_]+)\]/eis',"label_array_hf('\\1')",read_file(getTpl("head",$head_tpl)));
	preg_replace('/\$label\[([\'a-zA-Z0-9\_]+)\]/eis',"label_array_hf('\\1')",read_file(getTpl("foot",$foot_tpl)));
	
	//$label_hf為頭部的檢索數組,檢查頭部有多少個標簽
	
	is_array($label_hf) || $label_hf=array();
	foreach($label_hf AS $key=>$value)
	{
		$rs=$db->get_one("SELECT * FROM {$pre}label WHERE ch='$ch' AND tag='$key' AND module='$ch_module' AND chtype='99'");

		if($rs[tag])
		{
			$divdb=unserialize($rs[divcode]);
			$label[$key]=add_div($label[$key]?$label[$key]:'&nbsp;',$rs[tag],$rs[type],$divdb[div_w],$divdb[div_h],$divdb[div_bgcolor],'99');
		}
		else
		{
			$label[$key] || $label[$key]=add_div("新標簽,無內容",$key,'NewTag','','','','99');
		}
	}

	//獲取內容頁的標簽
	preg_replace('/\$label\[([\'a-zA-Z0-9\_]+)\]/eis',"label_array('\\1')",read_file(getTpl("index",$chdb[main_tpl])));
}
else
{
	$FileName=PHP168_PATH."cache/label_cache/";
	if(!is_dir($FileName)){
		makepath($FileName);
	}
	$FileName.=intval($ch)."_".intval($ch_pagetype)."_".intval($ch_module)."_".intval($ch_fid)."_".intval($city_id).".php";
	//默認緩存3分鐘.
	if(!$webdb[label_cache_time]){
		$webdb[label_cache_time]=3;
	}
	if( (time()-filemtime($FileName))<($webdb[label_cache_time]*60) ){
		@include($FileName);
	}
}

if(!$haveCache){
	unset($label_rubbish);
	//屏蔽了錯誤.考慮到有時切換其他系統的時候SQL語句有錯
	$query=$db->query("SELECT * FROM {$pre}label WHERE ch='$ch' AND pagetype='$ch_pagetype' AND module='$ch_module' AND fid='$ch_fid' AND chtype='0'");
	while( $rs=$db->fetch_array($query) ){
		//頁面沒有的標簽.即多余的舊標簽,做個提示
		if($jobs=='show' && !is_array($label["$rs[tag]"]) ){
			$label_rubbish[$rs[lid]]=$rs[tag];		
			continue;
		}
		//讀數據庫的標簽
		if( $rs[typesystem] )
		{
			$_array=unserialize($rs[code]);
			$value=($rs[type]=='special')?Get_sp($_array):Get_Title($_array);
			if(strstr($value,"(/mv)")){
				$value=get_label_mv($value);
			}
			if($_array[c_rolltype])
			{
				$value="<marquee direction='$_array[c_rolltype]' scrolldelay='1' scrollamount='1' onmouseout='if(document.all!=null){this.start()}' onmouseover='if(document.all!=null){this.stop()}' height='$_array[roll_height]'>$value</marquee>";
			}
		}
		//代碼標簽
		elseif( $rs[type]=='code' )
		{
			$value=stripslashes($rs[code]);
			//糾正一下不完整的javascript代碼,不必做權限判斷,普通用戶也能刪除
			if(eregi("<SCRIPT",$value)&&!eregi("<\/SCRIPT",$value)){
				if($delerror){
					$db->query("UPDATE `{$pre}label` SET code='' WHERE lid='$rs[lid]'");
				}else{
					die("<A HREF='$WEBURL?&delerror=1'>此“{$rs[tag]}”標簽有誤,點擊刪除之!</A><br>$value");
				}			
			}
			//真實地址還原
			$value=En_TruePath($value,0);
		}
		//單張圖片
		elseif( $rs[type]=='pic' )
		{	
			unset($width,$height);
			$picdb=unserialize($rs[code]);
			$picdb[imgurl]=tempdir("$picdb[imgurl]");
			$picdb[width] && $width=" width='$picdb[width]'";
			$picdb[height] && $height=" height='$picdb[height]'";
			if($picdb['imglink'])
			{
				$value="<a href='$picdb[imglink]' target=_blank><img src='$picdb[imgurl]' $width $height border='0' /></a>";
			}
			else
			{
				$value="<img src='$picdb[imgurl]' $width $height  border='0' />";
			}
		}
		//單個FLASH
		elseif( $rs[type]=='swf' )
		{
			$flashdb=unserialize($rs[code]);
			$flashdb[flashurl]=tempdir($flashdb[flashurl]);
			$flashdb[width] && $width=" width='$flashdb[width]'";
			$flashdb[height] && $height=" height='$flashdb[height]'";
			$value="<object type='application/x-shockwave-flash' data='$flashdb[flashurl]' $width $height wmode='transparent'><param name='movie' value='$flashdb[flashurl]' /><param name='wmode' value='transparent' /></object>";
		}
		//普通幻燈片
		elseif( $rs[type]=='rollpic' )
		{
			$value=rollPic_flash(unserialize($rs[code]));
		}
		//其它形式的
		else
		{
			$value=stripslashes($rs[code]);
			//真實地址還原
			$value=En_TruePath($value,0);
		}

		//更新標簽時顯示的頁面
		if($jobs=='show')
		{
			if(!$value)
			{
				$value='&nbsp;';
			}
			$divdb=unserialize($rs[divcode]);
			$value=add_div($value,$rs[tag],$rs[system_type]?$rs[system_type]:$rs[type],$divdb[div_w],$divdb[div_h],$divdb[div_bgcolor]);
		}
		//有些標簽設置了暫時隱藏
		elseif($rs[hide])
		{
			$value='';
		}
		$label[$rs[tag]]=$value;
	}
}


/**
*後台更新標簽
**/
if($jobs=='show')
{
	unlink($FileName);	//把緩存文件刪除掉,前台重新載入新資料

	if($label_rubbish){
		if($delete_label_rubbish){
			//$db->query("DELETE FROM {$pre}label WHERE tag IN ('".implode("','",$label_rubbish)."')");
		}else{
			//echo "<CENTER><br><br>提醒:::<A HREF='$WEBURL&delete_label_rubbish=1'>有 ".count($label_rubbish)." 個冗余的標簽,你是否要刪除它,點擊即可刪除:".implode(",",$label_rubbish)."</A><br><br><br><br></CENTER>";
		}		
	}
	$label || $label=array();
	foreach($label AS $key=>$value)
	{
		//如果是舊標簽的話.$value已經是具體數值了,或者為空了,而不是數組
		if(is_array($value))
		{
			$label[$key]=add_div("新標簽,內容暫無:$key",$key,'NewTag');
		}
	}

	$fromurl=urlencode($WEBURL);
	$label[$key].="<SCRIPT LANGUAGE='JavaScript'>
					<!--
					var admin_url='$webdb[admin_url]';
					var ch='$ch';
					var ch_fid='$ch_fid';
					var ch_pagetype='$ch_pagetype';
					var ch_module='$ch_module';
					var fromurl='$fromurl';
					var mystyle='$STYLE';
					//-->
					</SCRIPT>
					<SCRIPT LANGUAGE='JavaScript' src='$webdb[www_url]/images/default/label.js'></SCRIPT>";
}
else
{
	//寫緩存
	if( (time()-filemtime($FileName))>($webdb[label_cache_time]*60) ){
		$_shows="<?php\r\n\$haveCache=1;\r\n";
		foreach($label AS $key=>$value){
			$value=addslashes($value);
			$_shows.="\$label['$key']=stripslashes('$value');\r\n";
		}
		write_file($FileName,$_shows.'?>');
	}	
}
?>