<?php
/*************************************************************\
+-------------------------------------------------------------+
| 通灵佛教网在线烧香程序 System                                 |
| Copyright (c) 2003-2010 Alfred China                        |
| All rights reserved.                                        |
+-------------------------------------------------------------+
\*************************************************************/
if ( !defined('NetGou_START') ) { header("Location: ../"); die("Access denied"); }

function writeover($filename,$data,$method='rb+',$iflock=1,$check=1,$chmod=1){
	$check && strpos($filename,'..')!==false && exit('Forbidden');
	touch($filename);
	$handle = fopen($filename,$method);
	$iflock && flock($handle,LOCK_EX);
	fwrite($handle,$data);
	$method=='rb+' && ftruncate($handle,strlen($data));
	fclose($handle);
	$chmod && @chmod($filename,0777);
}

function ng_cachearray($input,$t = null){
	$output = '';
	if (is_array($input)) {
		$output .= "array(\r\n";
		foreach ($input as $key => $value) {
			$output .= $t."\t".ng_cachearray($key,$t."\t").' => '.ng_cachearray($value,$t."\t");
			$output .= ",\r\n";
		}
		$output .= $t.')';
	} elseif (is_string($input)) {
		$output .= "'".str_replace(array("\\","'"),array("\\\\","\'"),$input)."'";
	} elseif (is_int($input) || is_double($input)) {
		$output .= "'".(string)$input."'";
	} elseif (is_bool($input)) {
		$output .= $input ? 'true' : 'false';
	} else {
		$output .= 'NULL';
	}
	return $output;
}

function updatecache_netgou(){
	global $cache_dir,$sql_tbl;
	$ng_tmp = "";
	$c_result = db_query("SELECT name, value, category FROM $sql_tbl[config]");
	while ($row = mysql_fetch_row($c_result)) {	
	
		$row[1]=str_replace("'","\"",$row[1]);	
		$ng_tmp .= "\$netgou[\"$row[0]\"]='$row[1]';\r\n";

	};

	mysql_free_result($c_result);
	writeover($cache_dir.'/ngcache.php',"<?php\r\n".$ng_tmp."?>");
};


function updatecache_nglink(){
	global $netgou, $cache_dir, $sql_tbl;
	
	$links = ng_query("select * from $sql_tbl[links] order by orderby");
	if(!empty($links)){
		$txtlink = "<div class=links-title>友情链接</div>";
		$txtlink .= "<A href=http://www.tlfjw.com target=_blank>NetGou</A>";
		foreach($links as $ls){
			if($ls[status] == "Y"){
				$newwin = ($ls["newwin"] == "N")?"target=_blank":"";
				$ls["title"]=str_replace("'","\"",$ls["title"]);
				$ls["intro"]=str_replace("'","\"",$ls["intro"]);
				$txtlinks .= " | <A href=$ls[intro] $newwin>$ls[title]</A>";	
			}	
		}
	};
	$nglinks = "\$ng_txtlinks='$txtlinks';\r\n";
	
	$imglinks = ng_query("select * from $sql_tbl[imglinks] order by orderby");
	if(!empty($imglinks)){
		$txtimglinks = "";
		foreach($imglinks as $ls){
			if($ls[status] == "Y"){
				$newwin = ($ls["newwin"] == "N")?"target=_blank":"";
				$ls["title"]=str_replace("'","\"",$ls["title"]);
				$ls["intro"]=str_replace("'","\"",$ls["intro"]);
				$txtimglinks .= "<A href=$ls[intro] $newwin><img alt=$netgou[web_name] src=$ls[title] border=0></A>";
			}	
		}
	};
	$nglinks .= "\$ng_imglinks='$txtimglinks';\r\n";
	
	$tops = ng_query("select * from $sql_tbl[top] order by orderby");
	if(!empty($tops)){
		$txttop = "";
		foreach($tops as $ls){
			if($ls[status] == "Y"){
				$newwin = ($ls["newwin"] == "N")?"target=_blank":"";
				$ls["title"]=str_replace("'","\"",$ls["title"]);
				$ls["intro"]=str_replace("'","\"",$ls["intro"]);
				$txttop .= " | <A href=$ls[intro] $newwin>$ls[title]</A>";	
			}	
		}
	};
	$nglinks .= "\$ng_top='$txttop';\r\n";
	
	$bottoms = ng_query("select * from $sql_tbl[bottom] order by orderby");
	if(!empty($bottoms)){
		$txtbottom = "";
		foreach($bottoms as $ls){
			if($ls[status] == "Y"){
				$newwin = ($ls["newwin"] == "N")?"target=_blank":"";
				$ls["title"]=str_replace("'","\"",$ls["title"]);
				$ls["intro"]=str_replace("'","\"",$ls["intro"]);
				
				if($txtbottom == ""){
					$txtbottom .= "<A href=$ls[intro] $newwin>$ls[title]</A>";
				}else{
					$txtbottom .= " | <A href=$ls[intro] $newwin>$ls[title]</A>";
				}
			}	
		}
	};
	$nglinks .= "\$ng_bottom='$txtbottom';\r\n";
	
	$ng_ad = ng_query("select * from $sql_tbl[gao] order by orderby");
	$adtxt = "";
	$rotateimg = "";
	$rotateurl = "";
	$rotatetxt = '';
	if(!empty($ng_ad)){
		$imgnum = 0;
		foreach($ng_ad as $ad){
			if($ad[status] == "Y"){
			
				$ad["intro"]=str_replace("'","\"",$ad["intro"]);
				$ad["title"]=str_replace("'","\"",$ad["title"]);
				
				if($ad[weizhi] == 31 or $ad[weizhi] == 32 or $ad[weizhi] == 33 or $ad[weizhi] == 34 or $ad[weizhi] == 35){
					if($rotateimg ==""){
						$rotateimg .= $ad[title];
						$rotateurl .= $ad[intro];
						$rotatetxt .= "|";
					}else{
						$rotateimg .= "|".$ad[title];
						$rotateurl .= "|".$ad[intro];
						$rotatetxt .= "|";
					}
					$imgnum++;
				}else{
					$adtxt = "\$ngad[".$ad[weizhi]."]='<a href=".$ad[intro]." target=_blank><img src=".$ad[title]." border=0></a>';\r\n";
					$nglinks .= $adtxt;
				}
			}	
		}
	};
	$nglinks .= "\$rotateimg='$rotateimg';\r\n\$rotateurl='$rotateurl';\r\n\$imgnum='$imgnum';\r\n";
	
	writeover($cache_dir.'/nglinks.php',"<?php\r\n".$nglinks."\r\n?>");
	unset($speed_bar);
	unset($txtlink);
	unset($imglink);
	unset($nglink);
};


function updatecache_ngall(){
	updatecache_netgou();
	updatecache_nglink();
};
?>