<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: common.php 10981 2009-01-14 03:05:20Z liguode $
*/

@define('IN_UCHOME', TRUE);
define('X_VER', '1.5');
define('X_RELEASE', '20090114');
define('D_BUG', '0');

D_BUG?error_reporting(7):error_reporting(0);
$_SGLOBAL = $_SCONFIG = $_SBLOCK = $_TPL = $_SCOOKIE = $_SN = $space = array();

//程序目录
define('S_ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);

//基本文件
if(!@include_once(S_ROOT.'./config.php')) {
	header("Location: install/index.php");//安装
	exit();
}

//通用函数
include_once(S_ROOT.'./source/function_common.php');

//时间
$mtime = explode(' ', microtime());
$_SGLOBAL['timestamp'] = $mtime[1];
$_SGLOBAL['supe_starttime'] = $_SGLOBAL['timestamp'] + $mtime[0];

//GPC过滤
$magic_quote = get_magic_quotes_gpc();
if(empty($magic_quote)) {
	$_GET = saddslashes($_GET);
	$_POST = saddslashes($_POST);
}

//本站URL
if(empty($_SC['siteurl'])) $_SC['siteurl'] = getsiteurl();

//链接数据库
dbconnect();

//缓存文件
foreach (array('config', 'app', 'userapp', 'ad') as $value) {
	if(!@include_once(S_ROOT.'./data/data_'.$value.'.php')) {
		include_once(S_ROOT.'./source/function_cache.php');
		$cache_func = $value.'_cache';
		$cache_func();
	}
}

//COOKIE
$prelength = strlen($_SC['cookiepre']);
foreach($_COOKIE as $key => $val) {
	if(substr($key, 0, $prelength) == $_SC['cookiepre']) {
		$_SCOOKIE[(substr($key, $prelength))] = empty($magic_quote) ? saddslashes($val) : $val;
	}
}

//启用GIP
if ($_SC['gzipcompress'] && function_exists('ob_gzhandler')) {
	ob_start('ob_gzhandler');
} else {
	ob_start();
}

//初始化
$_SGLOBAL['supe_uid'] = 0;
$_SGLOBAL['supe_username'] = '';
$_SGLOBAL['inajax'] = empty($_GET['inajax'])?0:intval($_GET['inajax']);
$_SGLOBAL['ajaxmenuid'] = empty($_GET['ajaxmenuid'])?'':$_GET['ajaxmenuid'];
$_SGLOBAL['refer'] = empty($_SERVER['HTTP_REFERER'])?'':$_SERVER['HTTP_REFERER'];

//登录注册防灌水机
if(empty($_SCONFIG['login_action'])) $_SCONFIG['login_action'] = md5('login'.md5($_SCONFIG['sitekey']));
if(empty($_SCONFIG['register_action'])) $_SCONFIG['register_action'] = md5('register'.md5($_SCONFIG['sitekey']));

//整站风格
if(empty($_SCONFIG['template'])) {
	$_SCONFIG['template'] = 'default';
}
if($_SCOOKIE['mytemplate']) {
	$_SCOOKIE['mytemplate'] = str_replace('.','',trim($_SCOOKIE['mytemplate']));
	if(file_exists(S_ROOT.'./template/'.$_SCOOKIE['mytemplate'].'/style.css')) {
		$_SCONFIG['template'] = $_SCOOKIE['mytemplate'];
	} else {
		ssetcookie('mytemplate', '');
	}
}

//处理REQUEST_URI
if(!isset($_SERVER['REQUEST_URI'])) {  
	$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
	if(isset($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
}
	
//判断用户登录状态
checkauth();

//用户菜单
getuserapp();

//处理UC应用
$_SGLOBAL['appmenus'] = $_SGLOBAL['appmenu'] = array();
if($_SGLOBAL['app']) {
	foreach ($_SGLOBAL['app'] as $value) {
		if($value['open']) {
			if(empty($_SGLOBAL['appmenu'])) {
				$_SGLOBAL['appmenu'] = $value;
			} else {
				$_SGLOBAL['appmenus'][] = $value;
			}
		}
	}
}
$life = 86400 * 365;
$var = 'sitekey';
if ( !$_COOKIE[$_SC['cookiepre'].$var] )
{
	$value = md5($_SGLOBAL['timestamp']);
	setcookie($_SC['cookiepre'].$var, $value, $life?($_SGLOBAL['timestamp']+$life):0, $_SC['cookiepath'], $_SC['cookiedomain'], $_SERVER['SERVER_PORT']==443?1:0);
	$setarr = array(
		'cookie' => $value,
		'createtime' => $_SGLOBAL['timestamp']
	);
	$aid = inserttable('site_visitor', $setarr, 1);
	$_SC['site_key'] = $value;
}
else
{
	$_SC['site_key'] = $_COOKIE[$_SC['cookiepre'].$var];
}
$host = explode('.',$_SERVER['HTTP_HOST']);
if ( count($host) == 3 )
{
	if ( $host[0] != 'www' )
	{
		$domain = addslashes(strip_tags(trim(strtolower($host[0]))));
		if ( !ereg("^[a-z0-9]*$",$domain) ) 
		{  
			header("Location: http://".$_SC['default_domain']);
			exit;
		}
		if ( strlen($domain) < 4 )
		{
			header("Location: http://".$_SC['default_domain']);
			exit;
		}
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_domain')." WHERE domain='".$domain."' limit 1");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$member[] = $value;
		}
		if( $member )
		{
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_desktop')." WHERE uid=".$member[0]['uid']." and active=1 limit 1");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$desktop[] = $value;
			}
			$_SC['domain_desktop_id'] = $desktop[0]['desktop_id'];
			$_SC['domain_uid'] = $member[0]['uid'];
			$_SC['domain'] = true;
			$_SC['domain_name'] = $domain;
		}
		else
		{
			header("Location: http://".$_SC['default_domain']);
			exit;
		}
	}
	else
	{
		if( !empty($_SGLOBAL['supe_uid']) )
		{
			$query = $_SGLOBAL['db']->query("SELECT domain FROM ".tname('site_domain')." WHERE uid=".$_SGLOBAL['supe_uid']." limit 1");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$member[] = $value;
			}
			if( $member )
			{
				$_SC['domain_name'] = $member[0]['domain'];
			}
		}
	}
}
else
{
	if( !empty($_SGLOBAL['supe_uid']) )
	{
		$query = $_SGLOBAL['db']->query("SELECT domain FROM ".tname('site_domain')." WHERE uid=".$_SGLOBAL['supe_uid']." limit 1");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$member[] = $value;
		}
		if( $member )
		{
			$_SC['domain_name'] = $member[0]['domain'];
		}
	}
}
?>