<?php
/*************************************************************\
+-------------------------------------------------------------+
| 通灵佛教网在线烧香程序 System                                 |
| Copyright (c) 2003-2010 Alfred China                        |
| All rights reserved.                                        |
+-------------------------------------------------------------+
\*************************************************************/
if (!defined('NetGou_START'))
	define('NetGou_START',1);

function getdirname($path=null){
	if (!empty($path)) {
		if (strpos($path,'\\')!==false) {
			return substr($path,0,strrpos($path,'\\')).'/';
		} elseif (strpos($path,'/')!==false) {
			return substr($path,0,strrpos($path,'/')).'/';
		}
	}
	return './';
}

define('ng_dir',getdirname(__FILE__));

set_magic_quotes_runtime(0);

if(PHP_VERSION < '4.1.0') {
	$_GET = $HTTP_GET_VARS;
	$_POST = $HTTP_POST_VARS;
	$_COOKIE = $HTTP_COOKIE_VARS;
	$_SERVER = $HTTP_SERVER_VARS;
	$_ENV = $HTTP_ENV_VARS;
	$_FILES = $HTTP_POST_FILES;
}elseif (PHP_VERSION > '5.0.0'){
	date_default_timezone_set('PRC');
}

if (isset($_REQUEST['GLOBALS']) OR isset($_FILES['GLOBALS'])) {
	exit('Request tainting attempted.');
}

if (!ini_get('register_globals')) {
    $superglobals = array($_SERVER, $_ENV,$_FILES, $_COOKIE, $_POST, $_GET);
    if (isset($_SESSION)) {
        array_unshift($superglobals, $_SESSION);
    }
    foreach ($superglobals as $superglobal) {
        extract($superglobal, EXTR_SKIP);
    }
}

$ckvs = Array('_GET','_POST','_COOKIE','_FILES');
$ckvs4 = Array('HTTP_GET_VARS','HTTP_POST_VARS','HTTP_COOKIE_VARS','HTTP_POST_FILES');

//PHP小于4.1版本的兼容性处理
$phpold = 0;
foreach($ckvs4 as $_k=>$_v){ 
	if(!@is_array(${$_v})) continue;
	if(!@is_array(${$ckvs[$_k]})){ 
		${$ckvs[$_k]} = ${$_v}; unset(${$_v}); $phpold=1;
	}
}
//全局安全检测
//foreach($ckvs as $ckv){
   //foreach($$ckv AS $_k => $_v){ 
     // if(eregi("^(_|globals|)",$_k)) unset(${$ckv}[$_k]);
   //}
//}

if($phpold==1){ //低版本强制检查变量
	$ng_isMagic = false;
	$ng_registerGlobals = false;
}else{
	$ng_registerGlobals = @ini_get("register_globals");
  $ng_isMagic = @ini_get("magic_quotes_gpc");
}


//检测系统是否注册外部变量
if(!$ng_isMagic) require_once(ng_dir."/include/rglobals_magic.php");
else if(!$ng_registerGlobals) require_once(ng_dir."/include/rglobals.php");


if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
	$REMOTE_ADDR = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ($_SERVER['HTTP_client_ip']) {
	$REMOTE_ADDR = $_SERVER['HTTP_client_ip'];
} else {
	$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
}
$REMOTE_ADDR  = preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$REMOTE_ADDR) ? $REMOTE_ADDR : 'Unknown';

$netgou["max_nav_pages"] = 10;
$cache_dir = ng_dir."/include/cache";
include_once $cache_dir."/ngcache.php";

include_once ng_dir."/sql_config.php";

mysql_connect($sql_host, $sql_user, $sql_password);
mysql_select_db($sql_db) || die("Could not connect to SQL db");
mysql_query("SET character_set_connection=".$sql_code.", character_set_results=".$sql_code.", character_set_client=binary");


function db_query($query) {
		global $query_num;
		$result = mysql_query($query);
		$query_num++;
		return $result;
}

function db_fetch_array($result, $flag=MYSQL_ASSOC) {
    return mysql_fetch_array($result, $flag);
}

function ng_query($query) {

        $result = false;
        if ($p_result = db_query($query)) {
 	       while($arr = db_fetch_array($p_result))
				$result[]=$arr;
				mysql_free_result($p_result);
        }
        return $result;
}

function ng_query_first($query) {

		if ($p_result = db_query($query)) {
			$result = db_fetch_array($p_result);
			mysql_free_result($p_result);
        }
        return is_array($result)?$result:array();
}

function ng_query_first_cell($query) {
	if ($p_result = db_query($query)) {
		$result = mysql_fetch_row($p_result);
		mysql_free_result($p_result);
	}
	return is_array($result)?$result[0]:false;
}

$sql_tbl=array(
	"admins" => $sql_prefix."admins",
	"bottom" => $sql_prefix."bottom",
	"config" => $sql_prefix."config",
	"deposits" => $sql_prefix."deposits",
	"gao" => $sql_prefix."gao",
	"imglinks" => $sql_prefix."imglinks",
	"links" => $sql_prefix."links",
	"logins" => $sql_prefix."logins",
	"newslists" => $sql_prefix."newslists",
	"newsletter" => $sql_prefix."newsletter",
	"newsbody" => $sql_prefix."newsbody",
	"top" => $sql_prefix."top",
	"users" => $sql_prefix."users",
	"sx" => $sql_prefix."sx",	
	"gongde" => $sql_prefix."gongde",
	"guestbook" => $sql_prefix."guestbook",

	"zuihou" => $sql_prefix."zuihou"
	
);

function ng_header_location($location) {

	global $_SERVER;

	$header_location = (strpos($_SERVER["HTTP_USER_AGENT"],'Opera')!==false || @preg_match("/Microsoft|WebSTAR|Xitami/", getenv("SERVER_SOFTWARE")) ? "Refresh: 0; URL=" : "Location: ");
	if (!headers_sent()) {
		header($header_location.$location);
		exit();
	}
	else {
		echo "<BR><BR>".$lng_inc["txt_header_location_note"];
		echo "<META http-equiv=\"Refresh\" content=\"0;URL=$location\">";
	}

	ng_flush();
	exit();
}

function price_format($price) {
	return sprintf("%.2f",round((double)$price+0.00000000001,2));
}
/**
 * 截取中文字符串
 */
function msubstr($str, $start, $len, $plusp=false) { 
	$strlen = $start + $len; 
	for($i = 0; $i < $strlen; $i++) { 
	if(ord(substr($str, $i, 1)) > 0xa0) { 
		$tmpstr .= substr($str, $i, 2); 
		$i++; 
	} else 
		$tmpstr .= substr($str, $i, 1); 
	} 
	$length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
	if($plusp == true)
		$tmpstr .= '...';
	return $tmpstr; 
} 

function get_date($timestamp,$timeformat='Y-m-d'){
	return date($timeformat,$timestamp);
}

function get_datetime($timestamp,$timeformat='Y-m-d H:i:s'){
	return date($timeformat,$timestamp);
}
?>