<?php
include_once( "../config.inc.php"); 

/* 初始化设置 */
@ini_set('memory_limit',          '16M');
@ini_set('session.cache_expire',  180);
@ini_set('session.use_trans_sid', 0);
@ini_set('session.use_cookies',   1);
@ini_set('session.auto_start',    0);
@ini_set('display_errors',        1);
session_start();

if (defined('DEBUG_MODE') == false) {
    define('DEBUG_MODE', 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;
}

include_once( ROOT_PATH."includes/language.php");
include_once( ROOT_PATH."includes/funcomm.php"); 
include_once( ROOT_PATH."class/mydb.php");
$oPub = new mydb($dbhost,$dbuser,$dbpw,$dbname); 
$dbhost = $dbuser = $dbpw = $dbname = NULL;

foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
	foreach($$_request as $_key => $_value) {
		$_key{0} != '_' && $$_key = daddslashes($_value);
	}
}

$PHP_SELF = dhtmlspecialchars($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
$BASESCRIPT = basename($PHP_SELF);
list($BASEFILENAME) = explode('.', $BASESCRIPT);
$boardurl = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api|archiver|wap)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/');

define('DOCUMENT_ROOT', str_replace('\\','/',strtolower($_SERVER["DOCUMENT_ROOT"])));
$SUBPATH = (substr(DOCUMENT_ROOT,-1) == '/')?str_replace(DOCUMENT_ROOT, '', strtolower(ROOT_PATH)):str_replace(DOCUMENT_ROOT.'/', '',strtolower(ROOT_PATH)); 

/* 通过用户输入的域名取得网站配置信息 */
$havedomin = FALSE;
$db_table = $pre."sysconfig";
$_SERVER["SERVER_NAME"] = ($_SERVER["SERVER_PORT"] != 80)?$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"]:$_SERVER["SERVER_NAME"];
$sql = "SELECT * FROM ".$db_table." WHERE main_domin='".$_SERVER["SERVER_NAME"]."' AND states <> 1 ORDER BY scid ASC LIMIT 1";
$Anorm = $oPub->getRow($sql);
if($Anorm) {
    $Anorm['domain_url']  = 'http://'.$Anorm['main_domin'].'/'.$SUBPATH;
	$havedomin = true;
} elseif(strpos($_SERVER["SERVER_NAME"],$Aconf['mail_url'] ) ) {
    $user_name = str_replace('.'.$Aconf['mail_url'],'',$_SERVER["SERVER_NAME"]);
    $sql = "SELECT * FROM ".$db_table." WHERE user_name='".$user_name."' AND states <> 1 ORDER BY scid ASC LIMIT 1";
    $Anorm = $oPub->getRow($sql);
	if($Anorm) { 
	    $Anorm['domain_url']  = 'http://'.$Anorm['user_name'].'.'.$Aconf['mail_url'].'/'.$SUBPATH;
		$_SERVER["SERVER_NAME"] = $Anorm['user_name'].'.'.$Aconf['mail_url'];
		$havedomin = true;
	}
} else if($_SERVER["SERVER_NAME"] == $Aconf['mail_url']) {
    $sql = "SELECT * FROM ".$db_table." WHERE main_domin='www.".$_SERVER["SERVER_NAME"]."' AND states <> 1 ORDER BY scid ASC LIMIT 1";
    $Anorm = $oPub->getRow($sql);
	if($Anorm) {
		$Anorm['domain_url']  = 'http://'.$_SERVER["SERVER_NAME"].'/'.$SUBPATH;
		$havedomin = true;
	} 
}

if(!$havedomin ) {
   $urlstr  =  '<br/><span style="color:#C00;font-weight: bold">友情提示:域名输入错误</span><br/><br/>';
   $urlstr .=  '<A HREF="http://www.'.$Aconf['mail_url'].'/'.$SUBPATH.'">进入本站默认首页>></A></span>';
   $title   = '友情提示:域名输入错误';
   $strShow = showMessage($urlstr,$title);
   echo $strShow;
   exit;
} else {
	/* 基本配置信息 */
	$Asets = explode("{|}",$Anorm['sets']);
	if(count($Asets))
    foreach ($Asets AS $v) {
	   $At = array();
	   $At = explode("[|]",$v);
	   if($At[0]) {
	       $Anorm[$At[0]] = $At[1];
	   }
	}
  

	$Anorm["big_thumb_w"]   = ($Anorm["big_thumb_w"])?$Anorm["big_thumb_w"]:240;
	$Anorm["big_thumb_h"]   = ($Anorm["big_thumb_h"])?$Anorm["big_thumb_h"]:180;
	$Anorm["min_thumb_w"]   = ($Anorm["min_thumb_w"])?$Anorm["min_thumb_w"]:90;
	$Anorm["min_thumb_h"]   = ($Anorm["min_thumb_h"])?$Anorm["min_thumb_h"]:60; 

	$Anorm['web_title']      = $Anorm['header_title']; 
	$Anorm['domain_id']      = $Anorm['scid'];
	$Anorm['domain_user_id'] = $Anorm['user_id']; 
	$Anorm['allow_home'] = $Aconf['allow_home'];
	$Anorm["mail_url"] = $Aconf["mail_url"];
	$Aconf = $Anorm;  
	unset($Anorm);
} 


/* 用户登陆状态判断 */

$un_domain_url       =  'domain_url';
$un_domain_id        =  'domain_id';
$un_domain_user_id = 'domain_user_id';

$un_auser_id       = 'auser_id';
$un_auser_name     = 'auser_name'; 
$un_aaction_list   = 'aaction_list'; 
$un_alogin_fail    = 'alogin_fail';
$un_apraid         = 'apraid'; //经销商

$_SESSION[$un_domain_url]       = $Aconf['mainurl'] ;
$_SESSION[$un_domain_id]        = $Aconf['domain_id'];
$_SESSION[$un_domain_user_id]   = $Aconf['domain_user_id'];

if (empty($_SESSION[$un_auser_id])) {
   $_SESSION[$un_auser_id]    = 0; 
   $_SESSION[$un_apraid]      = 0;
   $_SESSION[$un_auser_name]   = ''; 
   $_SESSION[$un_aaction_list]   = '';
   $_SESSION[$un_aarticlecat_list]   = '';
   if (!isset($_SESSION[$un_alogin_fail]))  {
        $_SESSION[$un_alogin_fail] = 0;
    }
} else {
	$db_table = $pre.'admin_user';
	$sql = "SELECT * FROM ".$db_table." where user_id = ".$_SESSION[$un_auser_id];
	$Auser = $oPub->getRow($sql);
	if($Auser['user_id'] > 0) {
       $_SESSION[$un_auser_id]    = $Auser['user_id']; 
       $_SESSION[$un_auser_name]   = $Auser['user_name']; 
       $_SESSION[$un_aaction_list]   = $Auser['action_list'];
	   $_SESSION[$un_aarticlecat_list]   = $Auser['articlecat_list'];
	   $_SESSION[$un_apraid] = $Auser['praid']; 
	   $_SESSION[$un_alogin_fail] = 1; 
       /* 判断是否为真实域名 */
	   $db_table = $pre.'sysconfig';
	   $sql = "SELECT main_domin FROM ".$db_table." where user_id = ".$Aconf['domain_user_id'];
       $Atmp = $oPub->getRow($sql);
	   $tmp_main_domin = 'http://'.$Atmp['main_domin'].'/'.$SUBPATH;
	   if(  $Aconf['domain_url'] !=  $tmp_main_domin) {
		   session_destroy();
           unset($_SESSION);
           header("Location: $tmp_main_domin");  
		} 
	}
	unset($Auser);
}
//没有成功登陆则退出

$str = '/'.$SUBPATH.'login.php'; 
if( !$_SESSION[$un_alogin_fail] ) {
    if ($_SERVER["SCRIPT_NAME"] != $str) { 
      session_destroy();
      unset($_SESSION);
      header("Location: login.php"); 
    }
} else {
   /* 判断登录session 记录的IP与当前的IP是否符合 */
   $last_ip = real_ip();
   $Alast_ip = explode('.',$last_ip);
   $last_ip  = $Alast_ip[0].'.'.$Alast_ip[1].'.'.$Alast_ip[2]; 
   if($_SESSION['admin_login_ip'] != $last_ip)
   {
	   /*记录操作日志 */
	   $db_table = $pre.'account_log';
	   $change_desc = real_ip().' |  '.date("m月d日 h:i").' |  domain_id:'.$_SESSION[$un_domain_id];
	   $change_desc .= ' | '.$_SESSION[$un_auser_name].' 登录的IP地址与当前IP地址不同，操作被禁止';
       $Afields=array('user_id'=>$Auser['user_id'],'type'=>'noRealIp','change_desc'=>$change_desc,'domain_id'=>$_SESSION[$un_domain_id]);
       $oPub->install($db_table,$Afields);
       session_destroy();
       unset($_SESSION);
       echo "<SCRIPT language='javascript'>\nalert('noRealIp');top.location='/';</script>";
	   exit;
   }

}
/* 
  文件名对应表用于权限管理 
  注：需要同时修改 $Aprive $admin_nave 两数组，及nave.php文件的20行
*/
$Aprive = array(
'list_br_0'       => '文章管理', 
'articlesend'     => '文章添加',
'articlelist'     => '文章列表',
'article_comms'   => '文章评论',
'pic'             => '图库',
'articlecat'      => '文章分类',
'arti_attr'       => '文章属性',
'tj'			  => '推荐管理',
'tjcat'           => '推荐分类',

'list_br_1'       => '专题管理',
'ztlist'         => '专题列表',
'ztconfig'          => '专题添加编辑', 
'ztart'          => '专题内容管理',
'ztbaoming'          => '专题报名设置',
'ztliuyan'          => '专题留言设置',
'ztbaomingout'          => '专题报名导出',
'ztliuyanout'          => '专题留言导出',

'list_br_2'       => '产品管理',
'productsend'  => '商品添加',
'productlist'     => '商品列表',
'productcat'    => '商品分类',
'product_comms'   => '商品评论',
'product_attrib'  => '商品属性分类',
'product_attriblist'  => '商品属性列表',
'product_brand'   => '商品品牌编辑', 

'list_br_3'         => '经销商专区',
'prav_productlist'  => '商品信息列表',
'prav_productsend'  => '商品信息添加',
'prav_articlelist'  => '促销信息列表',
'prav_articlesend'  => '促销信息添加',
'pravail_price'     => '总站产品报价',
'prav_productcat'   => '经销商产品分类',
'prav_other_notice' => '经销商公告',
'prav_other_about'  => '经销商简介',
'prav_sysconfig'    => '基本资料编辑', 

'list_br_4'       => '系统管理',
'sysconfig'       => '系统属性设置', 
'navigator'       => '自定义导航栏',
'pravailability'  => '经销商管理',
'acount_log'      => '操作日志',
'articlelist_total_users'  => '发文章统计',
 

'list_br_5'        => '模版管理',   
'template_set'    => '模板选择',
'template_item'    => '页面显示设置',
'template_edit'   => '自定义模板', 

'list_br_6'       => '用户权限',
'adminuser_base'  => '基本资料编辑',
'adminuser'       => '用户权限设置', 


'list_br_7'       => '广告管理',
'ad_send'         => '广告添加',
'ad_list'     => '广告列表', 

'list_br_8'        => '网上调查',
'vote_title'        => '调查项添加',
'vote_item'        =>'选项添加',
'vote_group'      =>'可选组编辑',

'list_br_9'       => '常规管理',
'links'           => '友情链接',
'support'         => '用户留言管理',
'other_notice'    => '网站公告',
'other_about'     => '关于本站',

'list_br_10'       => '超级用户管理',
'messages'           => '站内短信',
'inducat'           => '行业分类列表',
'citycat'                        => '地区城市列表',
'sysconfig_edit'               => '已申请网站管理',
'filter'          => '关键词及IP过滤', 

'sys_ad_position_send'    => '广告位置添加',
'sys_ad_position'            => '广告位置编辑',
'sys_ad_list'                  => '所有广告列表',

//'sys_prav_sysconfig'    => '合作商家基本资料',
//'sys_prav_productlist'  => '合作商家商品信息',
//'sys_prav_articlelist'  => '合作商家促销信息',

//'sys_articlelist'     => '所有文章查看',
//'sys_article_comms'     => '所有文章留言',
//'sys_productlist'     => '所有商品查看',
//'sys_product_comms'     => '所有商品留言',
//'sys_support'     => '所有访客留言', 
);
/* 导航条显示数组 */
$admin_nave[list_br_0] = array('articlesend','articlelist','article_comms','pic','articlecat' ,'arti_attr','tj','tjcat');
$admin_nave[list_br_1] = array('ztlist','ztconfig','ztbaomingout','ztliuyanout');  
$admin_nave[list_br_2] = array('productsend','productlist','product_comms','productcat','product_attrib','product_attriblist','product_brand'); 
$admin_nave[list_br_3] = array('prav_productlist','prav_productsend','prav_articlelist','prav_articlesend','pravail_price','prav_productcat','prav_other_notice','prav_other_about','prav_sysconfig'); 
$admin_nave[list_br_4] = array('sysconfig','navigator','pravailability','acount_log','articlelist_total_users');  
$admin_nave[list_br_5] = array('template_set','template_item','template_edit');
$admin_nave[list_br_6] = array('adminuser_base','adminuser');
$admin_nave[list_br_7] = array('ad_send','ad_list');
$admin_nave[list_br_8] = array('vote_title');
$admin_nave[list_br_9] = array('links' ,'support','other_notice','other_about'); 
if($Aconf['allow_home'] == $_SESSION[$un_auser_id]){
	$admin_nave[list_br_10] = array('messages','inducat','citycat','sysconfig_edit','sys_ad_position_send','sys_ad_position','sys_ad_list','filter');   
}else{
	//$admin_nave[list_br_10] =array();
}
/* 当前访问的文件名字 */
$str       = $_SERVER["SCRIPT_NAME"]; $Atmp = explode('/',$str );
$count   =count($Atmp); $count = $count -1; $nowfile = $Atmp[$count];
$nowfile = str_replace('.php','',$nowfile);

 /* 判断用户权限 */
if ( $_SESSION[$un_aaction_list] != 'all') { 
	$_SESSION[$un_aaction_list] .=',nave,login'; 
	$Aaction_list = explode(',',$_SESSION[$un_aaction_list]); 
	if(!in_array($nowfile,$Aaction_list)) {
        $Aconf['priveMessage'] = '<A HREF="index.php" target="_top">你无此权限，请与管理员联系。</a><br/>'.$Aconf['footer_title'];
	}
} 
$nowName = '<A HREF="../index.php">'.$Aconf['web_title'].'首页</A>><A HREF="default.php">后台管理</A>>'.$Aprive[$nowfile]; 

//查阅短信提醒
$haveMessages = false; 
$sql = "SELECT count(*) as count  FROM ".$pre."messages where touser_id='".$_SESSION[$un_auser_id]."' and type<1 and states<1";//个人短信
$count = $oPub->getOne($sql);
if($count < 1){ //1=>'系统消息',2=>'群发消息'
	$sql = "SELECT count(*) as count  FROM ".$pre."messages where type >= 1";
	$count = $oPub->getOne($sql); 
	if($count >= 1) {
		$sql = "SELECT count(*) as count  FROM ".$pre."messagesread  where user_id = '".$_SESSION[$un_auser_id]."'"; 
		$countre = $oPub->getOne($sql);
		if($countre < $count ){ 
			$haveMessages = true;
		}else{
			$haveMessages = false;	
		}
	}
}else{
	$haveMessages = true;
}

if($haveMessages){
	$straud = '有新的未读短信';
}else{
	$straud ='';
}

header('content-type: text/html; charset=utf-8'); 
if ((DEBUG_MODE & 1) == 1) {
    error_reporting(E_ALL);
} else {
    error_reporting(E_ALL ^ E_NOTICE);
}

/* 判断是否支持gzip模式 */
$mystring = strtolower($_SERVER['SERVER_SOFTWARE']);
$findme   = 'apache';
$pos = strpos($mystring, $findme);
if ($pos === false) { 
} else { 
   if (gzip_enabled()) { //ob_start('ob_gzhandler');
	   ob_start("compress");
   }  else  {
       ob_start();
   } 
} 
?> 