<?php
if(!defined("IN_GATEWEI")) {	exit("Access Denied");}

//write to setting
function w2setting()
{
	global $db;
	$w  = "<?php\n\nif(!defined('IN_GATEWEI')) {	exit('Access Denied');}\n";
    $query = $db->query("SELECT * FROM g_settings");
     while($c= $db->fetch_array($query)) {
	 $settings[$c['var']] = addslashes($c['val']);}

	$w .="\$_GCACHE['settings'] = array(\n";
     foreach ($settings as $key => $val)
       {
		 if(substr($val,0,2)=='{{'&&substr($val,-2,2)=='}}'){
			 $val=str_replace(array('{{','}}'), array('',''), $val);
			 $w .= "'".$key."' => array(\n";
			 $va=explode(';',$val);
			 foreach($va as $k => $val){
				 $va2=explode(',',$val);
				 if(count($va2)==1){
					 $w .= $k." => '".$va2[0]."',\n"; 
					}elseif(count($va2)==2){
					$va2[1]=str_replace('[]',',',$va2[1]);
					$w .= $va2[0]." => '".$va2[1]."',\n"; 
					}elseif(count($va2)==3){
					$w .= $va2[0]." => array('".$va2[1]."','".$va2[2]."'),\n"; 
					}elseif(count($va2)==4){
					$w .= $va2[0]." => array('".$va2[1]."','".$va2[2]."','".$va2[3]."'),\n"; 
				 }		
			 }
			 $w .= "),\n";
			 }else{
			$w .= "'".$key."' => '".$val."',\n"; 
			}
		}
	   $w .= ');?>'; 
       $wfile = './clientdata/cache/cache_settings.php';
	if ( ! $fp = @fopen($wfile, 'wb')){
		 gadminmessage("写入不成功");
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);
}



//write to friendlylink

function w2friendlylink()
{
	global $db;
	$w  = "<?php\n\nif(!defined('IN_GATEWEI')) {	exit('Access Denied');}\n";
	$w .="\$_GCACHE['friendlylink'] = array(\n";
    $q = $db->query("SELECT * FROM g_friendlylink ORDER BY top DESC LIMIT 0,30");
	$i=0;
     while($links = $db->fetch_array($q)) {
		 $w .= $i." => array(\n"; 
		 $w .= "'sitename' => '".$links['sitename']."',\n"; 
		 $w .= "'siteurl' => '".$links['siteurl']."',\n"; 
		 $w .= "'sitelogo' => '".$links['sitelogo']."',\n  ),\n"; 
	  $i++;
	}

	   $w .= ");\n\n?>";
       $wfile = './clientdata/cache/cache_friendlylink.php';
	if ( ! $fp = @fopen($wfile, 'wb'))
	{
		gadminmessage("写入不成功");
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);
}

//write to articlecategory
function w2articlecategory()
{
	global $db;
$q = $db->query("select * from g_articlecategory order by ord");	
$w  = "<?php\n\nif(!defined('IN_GATEWEI')) {	exit('Access Denied');}\n";
$w .="\$_GCACHE['articlecategory'] = array(\n";

	while($articlecategory = $db->fetch_array($q)) 
	    {
		$articlecategory=gaddslashes($articlecategory,1);
		$w .= $articlecategory['acid']." => array(\n'pid' => ".$articlecategory['pid'].", \n'categorytitle' => '".$articlecategory['categorytitle']."', \n'ord'=> ".$articlecategory['ord'].", \n'displayway' => ".$articlecategory['displayway'].", \n'hlightway'=> ".$articlecategory['hlightway'].", \n'pagerecords' => ".$articlecategory['pagerecords'].", \n'dateformat' => '".$articlecategory['dateformat']."'),\n"; 
	   }
	$w .= ");\n?>";
	$wfile = './clientdata/cache/cache_articlecategory.php';
	if ( ! $fp = @fopen($wfile, 'wb')){
		 gadminmessage("写入不成功");
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);
}


//write to productcategory
function w2productcategory()
{
	global $db;
$q = $db->query("select * from g_productcategory order by ord");	
$w  = "<?php\n\nif(!defined('IN_GATEWEI')) {	exit('Access Denied');}\n";
$w .="\$_GCACHE['productcategory'] = array(\n";

	while($productcategory = $db->fetch_array($q)) 
	    {
		$productcategory=gaddslashes($productcategory,1);
		$w .= $productcategory['pcid']." => array(\n'pid' => ".$productcategory['pid'].", \n'categorytitle' => '".$productcategory['categorytitle']."', \n'ord'=> ".$productcategory['ord'].", \n'displayway' => ".$productcategory['displayway'].", \n'hlightway'=> ".$productcategory['hlightway'].", \n'pagerecords' => ".$productcategory['pagerecords'].", \n'dateformat' => '".$productcategory['dateformat']."'),\n"; 
	   }
	$w .= ");\n?>";
	$wfile = './clientdata/cache/cache_productcategory.php';
	if ( ! $fp = @fopen($wfile, 'wb')){
		 gadminmessage("写入不成功");
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);
}


//write to forumcategory
function w2forumcategory()
{
	global $db;

$q = $db->query("select * from g_forumcategory order by ord");	
$w  = "<?php\n\nif(!defined('IN_GATEWEI')) {	exit('Access Denied');}\n";
$w .="\$_GCACHE['forumcategory'] = array(\n";

	while($forumcategory = $db->fetch_array($q)) 
	    {
		$forumcategory=gaddslashes($forumcategory,1);
		$w .= $forumcategory['forid']." => array('pid' => ".$forumcategory['pid'].", \n'forumtitle' => '".$forumcategory['forumtitle']."', \n'ord'=> ".$forumcategory['ord'].", \n'pagerecords' => ".$forumcategory['pagerecords'].", \n'access' => ".$forumcategory['access'].", \n'accesspwd' => '".$forumcategory['accesspwd']."',  \n'forumurl' => '".$forumcategory['forumurl']."', \n'dateformat' => '".$forumcategory['dateformat']."', \n'hlightway' => ".$forumcategory['hlightway']."),\n"; 
	   }
	$w .= ");\n?>";
	$wfile = './clientdata/cache/cache_forumcategory.php';
	if ( ! $fp = @fopen($wfile, 'wb')){
		 gadminmessage("写入不成功");
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);
}


//write to navigation
function w2navigation($nav,$act="save"){

	$w = "<?php";
	$w .= "\n";

	$w .= "\$nav = array(\n"; 
	foreach($nav as $key => $navigations)
       {
		if($act=="save"){
		list($title,$url,$type,$id)=explode(";",$navigations);	
		}elseif($act=="update"){
		$title=$navigations['title'];
		$url=$navigations['url'];
		$type=$navigations['type'];
		$id=$navigations['id'];
		}
		$w .= $key."=>array('title'=>'".$title."','url'=>'".$url."','type'=>'".$type."','id'=>".$id."),\n";
        }
		$w .= ");\n?>";
       $cfile = './clientdata/cache/cache_navigation.php';
	   if ( ! $fp = @fopen($cfile, 'wb'))
	{
		 $errors[] = "写入缓存文件创建失败";
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);

}




?>