<?php
define('IN_GATEWEI', TRUE);
define('GATEWEI_ROOT', substr(dirname(__FILE__), 0, -6));
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
$microtime = explode(' ', microtime());
$g_run_starttime = $microtime[1] + $microtime[0];
error_reporting(0);
//error_reporting(E_ALL);
set_magic_quotes_runtime(0);
require_once GATEWEI_ROOT.'./engine/global.func.php';
require_once GATEWEI_ROOT.'./engine/project.inc.php';
require_once GATEWEI_ROOT.'./engine/cache.func.php';

define('ISROBOT', getrobot());

//定义了NOROBOT同时ISROBOT为真;
if(defined('NOROBOT') && ISROBOT) {
	exit(header("HTTP/1.1 403 Forbidden"));
}
//不允许访问的客户端链接
if(!isallowclient()) {
	exit(header("HTTP/1.1 403 Forbidden"));
}
$ipost=$action=$position=$modifyinfo['content']='';
$articles=	$toparticles=$allarticle=$products=$topproducts=$allproduct=$threads=$topthreads=$allthread=$replys=$allattachs=$replyattachs=$downattachs=$displayattachs=$replydownattachs=array();

foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
	foreach($$_request as $_key => $_value) {
		$_key{0} != '_' && $$_key = gaddslashes($_value);
	}
}
$sid=isset($sid)?$sid:0;

if (!MAGIC_QUOTES_GPC && $_FILES) {
	$_FILES = gaddslashes($_FILES);
}

if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	$currentip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	$currentip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	$currentip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	$currentip = $_SERVER['REMOTE_ADDR'];
}
$timestamp=time();
if(!defined('IN_GATEWEI')) {
	exit('Access Denied');
}

require_once(GATEWEI_ROOT.'./config.inc.php');
require_once(GATEWEI_ROOT.'./engine/project.inc.php');
require_once(GATEWEI_ROOT.'./engine/db_mysql.class.php');
require_once(GATEWEI_ROOT.'./clientdata/cache/cache_navigation.php');

$db = new db;
$db->connect($db_hostname,$db_username,$db_password,$db_name);

$cachelost = (@include GATEWEI_ROOT.'./clientdata/cache/cache_settings.php') ? '' : 'settings';
@extract($_GCACHE['settings']);
$templatedir=(substr($templatedir,-1)=="/")?$templatedir:$templatedir."/";
if(!is_dir($templatedir)){
	showmsg("模板目录不存在");
}

$allowedauthcode=sprintf('%06b', $allowedauthcode);//注意是否修改
//$allowedupload=sprintf('%03b', $allowedupload);//注意是否修改
//$allowedreply=sprintf('%03b', $allowedreply);//注意是否修改
$siteheadtitle=$sitename;
$page = isset($page) ? max(1, intval($page)) : 1;
$pid = isset($pid) && is_numeric($pid) ? $pid : 0;
$categoryid = isset($categoryid) && is_numeric($categoryid) ? $categoryid : 0;
$forid = isset($forid) && is_numeric($forid) ? $forid : 0;
$bid = isset($bid) && is_numeric($bid) ? $bid : 0;
$aid = isset($aid) && is_numeric($aid) ? $aid : 0;
$productid = isset($productid) && is_numeric($productid) ? $productid : 0;
$tid = isset($tid) && is_numeric($tid) ? $tid : 0;
$fid = isset($fid) && is_numeric($fid) ? $fid : 0;


$cachelost = (@include GATEWEI_ROOT.'./clientdata/cache/cache_articlecategory.php') ? '' : 'articlecategory';
$articlecategory = $_GCACHE['articlecategory'];

$cachelost = (@include GATEWEI_ROOT.'./clientdata/cache/cache_productcategory.php') ? '' : 'productcategory';
$productcategory = $_GCACHE['productcategory'];

$cachelost = (@include GATEWEI_ROOT.'./clientdata/cache/cache_forumcategory.php') ? '' : 'forumcategory';
if(!$cachelost){
$articlecategory = $_GCACHE['articlecategory'];
$productcategory = $_GCACHE['productcategory'];
$forumcategory = $_GCACHE['forumcategory'];
}else{
//gexit("缓存读取失败");
}
$pervalue=$userpermission[4];
$adminlogin=$uid=0;
$gid=4;

if(!$sid && !ISROBOT){
	$sid = random(6);
	@SetCookie('sid',$sid,$timestamp+43200);
	$db->query("INSERT INTO g_sessions ( sid , uid , userid , gid , pervalue , lastloginip , lastlogintime , uptime , newmsgcount ,adminlogin,authcode,allowedforum) VALUES ('$sid', 0, '访客', 4 , $pervalue , '$currentip' , $timestamp , $timestamp , 0 , 0 , '' , '')");
}
$q=$db->query("select * from g_sessions where sid='$sid' limit 1");
if(!$_GSESSION=$db->fetch_array($q)){
	if($sid){
		@SetCookie('sid','',$timestamp-3153600);
		@gheader("location:index.php");
	}
		showmsg("用户会话已经过期或无效，请从新登陆","index.php");
	}else{
		$_GSESSION['pervalue']=sprintf('%019b', $_GSESSION['pervalue']);
		@extract($_GSESSION);
	}

if($pervalue==$userpermission[4]){
	$pervalue=sprintf('%019b', $userpermission[4]);
}

$allowedattachload=(CURSCRIPT=='article'&&$pervalue{13})||(CURSCRIPT=='product'&&$pervalue{11})||(CURSCRIPT=='post'&&$pervalue{9})||(CURSCRIPT=='thread'&&$pervalue{7});//post是论坛主题 , thread是主题回帖


$sitelogo="<img src=\"".$templatedir."images/".$sitelogo."\" alt=\"".$sitename."\">";

if(CURSCRIPT=='article'){
	$resetcategory=$articlecategory;
	$curscriptisforum=0;
}elseif(CURSCRIPT=='product'){
	$resetcategory=$productcategory;
	$curscriptisforum=0;
}else{
	$resetcategory=$forumcategory;
	$curscriptisforum=1;
}
$maincategory=$subcategory=array();
	foreach($resetcategory as $key => $val){
		if($curscriptisforum){
			$val['categorytitle']=$val['forumtitle'];
		}
		if($val['pid']==0){
			$maincategory[$key]=$val;
		}else{
		    $subcategory[$val['pid']][$key]=$val;
		}
	}

 if($pervalue{17}||$pervalue{16}||$pervalue{15}){
	 $echoselectform=form_select_var();
 }
?>