<?php
if (!defined('IN_OUN')) {
    die('Hacking attempt');
}

include_once( "config.inc.php"); 

if (defined('DEBUG_MODE') == false) {
    define('DEBUG_MODE', 0);
} 

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

include_once( ROOT_PATH."includes/language.php");
include_once( ROOT_PATH."includes/funcomm.php");
include_once( ROOT_PATH."includes/cls_session.php");
include_once( ROOT_PATH."class/mydb.php");

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;
}

if (isset($_SERVER['PHP_SELF'])) {
    define('PHP_SELF', $_SERVER['PHP_SELF']);
} else {
    define('PHP_SELF', $_SERVER['SCRIPT_NAME']);
}

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

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));
 
$oPub = new mydb($dbhost,$dbuser,$dbpw,$dbname); 
$dbhost = $dbuser = $dbpw = $dbname = NULL;  

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

	$Anorm["domain_id"]   = $Anorm["scid"] ;
	$Anorm["web_title"]   = $Anorm["header_title"] ;

	$Anorm["domain_url"]  = 'http://'.$Anorm["main_domin"].'/'.$SUBPATH; 
	$Asets = explode("{|}",$Anorm["sets"]);
	while( @list( $k, $v ) = @each( $Asets) ) { 
		$At = array();
		$At = explode("[|]",$v);
		if($At[0]) 
		   $Anorm[$At[0]] = $At[1]; 
	} 
	$Anorm["mail_url"] = $Aconf["mail_url"]; 
	$Anorm["allow_home"] = $Aconf["allow_home"]; 

	$PHP_SELF = dhtmlspecialchars($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']); 
	$Anorm["now_url"]		= basename($PHP_SELF);
	/* 判断 logo 类型 */
	if($Anorm['logo']) {
		$Atmp = explode('.',$Anorm['logo']);
        if($Atmp[1] == 'swf') { 
           $str = '<script type="text/javascript">';
			   $str .= 'document.write(\'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
			   $str .= 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'.$Anorm["logo_w"].'" height="'.$Anorm["logo_h"].'">\');';
			   $str .= 'document.write(\'<param name="movie" value="data/weblogo/'.$Anorm['logo'].'"><param name="quality" value="high">\');';
			   $str .= 'document.write(\'<param name=wmode value="transparent">\');';
			   $str .= 'document.write(\'<embed src="data/weblogo/'.$Anorm['logo'].'"  quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"'; 
			   $str .= 'type="application/x-shockwave-flash"   wmode="transparent" width="'.$Anorm["logo_w"].'" height="'.$Anorm["logo_h"].'"></embed>\');';
			   $str .= 'document.write(\'</object>\');';
           $str .= '</script>';
		   $Aconf['logo'] = $str;
		} else {
           $Aconf['logo'] = '<IMG SRC="data/weblogo/'.$Anorm['logo'].'" BORDER="0" title="'.$Anorm['header_title'].'" width="'.$Anorm["logo_w"].'" height="'.$Anorm["logo_h"].'">';
		}
	} else {
        $Aconf['logo'] = false;
	}
	$Anorm["logo"]          = $Aconf['logo'];
	$Anorm["cache_time"]    = $Anorm["cache_time"]?$Anorm["cache_time"]:0; 

	$Anorm["template"]      = ($Anorm["user_template"] > 0 )?'../templates/user_themes/'.$Anorm["scid"].'/':$Anorm["template"].'/'; //模板 
	$Anorm["template_path"] = ($Anorm["user_template"] > 0 )?'templates/user_themes/'.$Anorm["scid"].'/':'themes/'.$Anorm["template"];
	$Anorm["template_base"]   = ($Anorm["user_template"])?'templates/user_themes/'.$Anorm["scid"].'/':'themes/'.$Anorm["template"];   
	$Anorm["logo_title"]    = $Anorm["header_title"];

	// 主站URL  
	if($Aconf["allow_home"]) {
		$sql = "SELECT main_domin,header_title FROM ".$pre."sysconfig WHERE scid='".$Aconf["allow_home"]."' LIMIT 1";
	}else { 
		$sql = "SELECT main_domin,header_title FROM ".$pre."sysconfig order by scid asc LIMIT 1";
	}
	$row = $oPub->getRow($sql); 
	$Anorm["admin_main_domin"] = 'http://'.$row["main_domin"].'/'.$SUBPATH;
	$Anorm["admin_header_title"] = $row["header_title"]; 

	$Aconf = array_merge($Anorm,$Aconf); 
	unset($Anorm);unset($row); 
} 

if (is_spider()) {
    /* 如果是蜘蛛的访问，那么默认为访客方式，并且不记录到日志中 */
    if (!defined('INIT_NO_USERS')) {
        define('INIT_NO_USERS', true);
    }
    $_SESSION = array();
}
 
if (!defined('INIT_NO_SMARTY')) {
    header('Cache-control: private');
    header('Content-type: text/html; charset=utf-8');

    /* 创建 Smarty 对象。*/
    require(ROOT_PATH . 'includes/cls_template.php');
    $smarty = new cls_template; 
    $smarty->cache_lifetime = $Aconf['cache_time'];
    $smarty->template_dir   = ROOT_PATH . 'themes/' . $Aconf['template'];
	$base_chache_dir        = ROOT_PATH . 'templates/caches/';
	$base_compile_dir       = ROOT_PATH . 'templates/compiled/';
    $smarty->cache_dir      = $base_chache_dir .$Aconf['domain_id'];
    $smarty->compile_dir    = $base_compile_dir.$Aconf['domain_id'];

    if ((DEBUG_MODE & 2) == 2) {
        $smarty->direct_output = true;
        $smarty->force_compile = true;
    }  else {
        $smarty->direct_output = false;
        $smarty->force_compile = false;
    }
}

$Aconf["preFile"] = preg_replace (array("'/".$SUBPATH."'","'.php'"),array("",""),PHP_SELF);//当前文件名前缀  
$Aconf["nowFile"] = $Aconf['rewrite'] ?$Aconf["preFile"].".html":$Aconf["preFile"].".php"; //当前使用的文件名 
$Aconf["displayFile"] = $Aconf["preFile"].".html"; //显示的模版文件名

/* 当前网站公用 session */
$un_domain_id = 'domain_id'; $un_domain_url = 'domain_url';
$_SESSION[$un_domain_url]       = $Aconf['domain_url']; 
$_SESSION[$un_domain_id]       = $Aconf['domain_id'];  
 

/* 判断是否支持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();
   } 
} 
?>
