<?php
/**
* 系统初始化
* @author moufer<moufer@163.com>
* @copyright (C)2001-2007 Moufersoft
* @version $Id: common.inc.php 11 2008-01-23 16:17 moufer $
*/
error_reporting(0);

set_magic_quotes_runtime(0);
$mtime = explode(' ', microtime());
$mudder_starttime = $mtime[1] + $mtime[0];
$timestamp = time();

define('IN_MUDDER', TRUE);
define('DS', DIRECTORY_SEPARATOR);
define('MUDDER_ROOT', substr(dirname(__FILE__), 0, -7));
define('MUDDER_MEMBERDIR', MUDDER_ROOT.'./member/');
define('MUDDER_CACHEDIR', MUDDER_ROOT.'./data/cachefiles/');
define('MUDDER_PLUGINDIR', MUDDER_ROOT.'./plugins/');

$charset = $cookiepre = $cookiepath = $cookiedomain = null;

require_once MUDDER_ROOT.'config.inc.php';
require_once MUDDER_ROOT.'include/template.func.php';
require_once MUDDER_ROOT.'include/global.func.php';
require_once MUDDER_ROOT.'include/base.class.php';
require_once MUDDER_ROOT.'include/version.php';

$php_self = $php_domain = $php_referer = $php_scheme = $php_reuri= $php_port = $host_url =  '';

$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$php_domain = $_SERVER['SERVER_NAME'];
$php_agent = $_SERVER['HTTP_USER_AGENT'];
$php_referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$php_scheme = $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
$php_reuri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$php_port = $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
$host_url = $php_scheme . $php_domain . $php_port;

//时区设置
if(function_exists('date_default_timezone_set')) {
    @date_default_timezone_set('Asia/Shanghai');
}

//获得IP地址
$onlineip = null;
$onlineipmatches = array();
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
    $onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
    $onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
    $onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
    $onlineip = $_SERVER['REMOTE_ADDR'];
}
$onlineip = addslashes($onlineip);
@preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
$onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
unset($onlineipmatches);

//兼容性
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;
}

$pattern_arr = $replace_arr = array();
if(!defined('IN_ADMIN')) {
	$pattern_arr = array("/ union /i", "/ select /i", "/ update /i", "/ outfile /i", "/ or /i");
	$replace_arr = array('&nbsp;union&nbsp;', '&nbsp;select&nbsp;', '&nbsp;update&nbsp;', '&nbsp;outfile&nbsp;', '&nbsp;or&nbsp;');
	$_POST = strip_sql($_POST);
	$_GET = strip_sql($_GET);
	$_COOKIE = strip_sql($_COOKIE);
	unset($pattern_arr, $replace_arr);
}

$magic_quotes_gpc = get_magic_quotes_gpc();
if(!$magic_quotes_gpc) {
	$_POST = add_slashes($_POST);
	$_GET = add_slashes($_GET);
    $_COOKIE = add_slashes($_COOKIE);
    $_FILES && $_FILES = add_slashes($_FILES);
}

@extract($_POST, EXTR_SKIP);
@extract($_GET, EXTR_SKIP);
//unset($_POST, $_GET);

$_DCOOKIE = array();
$prelen = strlen($cookiepre);
foreach($_COOKIE as $key => $value) {
    if(substr($key,0,$prelen) == $cookiepre){
        $var = substr($key,$prelen);
        $_DCOOKIE[$var] = $value;
    }
}
unset($key, $value, $prelen);

require_once MUDDER_ROOT.'include/mysql.class.php';
$db = new Db_class($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);

require_once MUDDER_ROOT.'include/cache.func.php';

$_config = array();
if(!@include MUDDER_CACHEDIR.'cache_config.php'){
    cache_all();
    exit('<p>Settings caches successfully created, please refresh.</p>');
}

if($_config['siteclose'] && !defined('IN_ADMIN')) {
    diex($_config['closenote']);
}

if($attackevasive && !defined('IN_ADMIN')) {
	require_once(MUDDER_ROOT.'include/fense.php');
}

if($_config['useripaccess'] && !check_ipaccess($onlineip, explode("\r\n",$_config['useripaccess']))) {
    diex('您的IP不在网站受访IP列表以内。');
}

if($_config['ban_ip'] && check_ipaccess($onlineip, explode("\r\n",$_config['ban_ip']))) {
    diex('您的IP已被禁止访问本站。');
}

$_config['siteurl'] = $_config['siteurl'].(substr($_config['siteurl'],-1) != '/' ? '/' : '');
$_auditing = $_expleve = $_cache = $_picture_thumb = $_space = $_seccode = array();
$_auditing = &$_config['auditing'];
$_expleve = &$_config['expleve'];
$_cache = &$_config['cache'];
$_cache_num = &$_config['cache_num'];
$_picture_thumb = &$_config['picture_thumb'];
$_space = &$_config['space'];
$_seccode = &$_config['seccode'];
$_passport = &$_config['passport'];
$_membercard = &$_config['membercard'];
$_exchange = &$_config['exchange'];
$_inputlimit = &$_config['inputlimit'];
$_search = &$_config['search'];
$_googlesearch = &$_config['googlesearch'];
$_googlesearch['website'] = $_googlesearch['website'] ? $_googlesearch['website'] : 'modoer.com';
$_video = &$_config['video'];
$_mail = &$_config['mail'];
$_rewrite = &$_config['rewrite'];
$_coupon = &$_config['coupon'];

if(!$_config['passport_discuz_passportkey'] || !$_config['passport_discuz_passportpath']) {
    $_config['passport_discuz_enable'] = 0;
}

/*
$apache_mod = array();
if(function_exists('apache_get_modules')) {
    $apache_mod = apache_get_modules();
    if (!in_array('mod_rewrite', $apache_mod)) {
        $_config['rewrite_enable'] = 0;
    }
}
unset($apache_mod);
*/

if($_config['gzipcompress'] && function_exists('ob_gzhandler')) {
    @ob_start('ob_gzhandler');
} else {
    $_config['gzipcompress'] = 0;
    ob_start();
}

$php_gd = function_exists('GetImageSize') && function_exists('ImageChar') && 
	function_exists('ImageColorTransparent');
if($_config['watermark'] && !$php_gd) $_config['watermark'] = 0;

// 启用UCenter
$ucenter_enable = &$_config['ucenter_enable'];
if($ucenter_enable) {
    if(!@include MUDDER_ROOT.'./uc_client/client.php') {
        $ucenter_enable = 0;
    }
}

if(defined('IN_ADMIN') && SCRIPTNAV == 'admincp') {

    require_once MUDDER_ROOT.'./include/admin.class.php';
    $admin = new Admin_class();
    $admin->is_founder && error_reporting(E_ALL ^ E_NOTICE);

} else {

    if($ucenter_enable) {
        require_once(MUDDER_ROOT.'./include/user_uc.class.php');
        $user = new User_UC_class();
        $user->Auto_Login();
    } else {
        require_once(MUDDER_ROOT.'include/user.class.php');
        $user = new User_class();
    }
    $user->groupid == 2 && diex('您已经被管理员设置为禁止访问本站的会员。');

}

$_areacodes = $_boroughs = $_streets = $_areacode_option = $_classcodes = $_classes = $_classcode_option = $_panels = $_panel_config = $_templates = $_usergroups = $_userranks = $_plugins = $_webtotal = array();

$cachelost = '';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_areacode.php') ? '' : 'areacode,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_modules.php') ? '' : 'modules,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_classcode.php') ? '' : 'classcode,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_panels.php') ? '' : 'panels,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_templates.php') ? '' : 'templates,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_usergroups.php') ? '' : 'usergroups,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_userranks.php') ? '' : 'userranks,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_faces.php') ? '' : 'faces,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_plugins.php') ? '' : 'plugins,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_webtotal.php') ? '' : 'webtotal,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_datacall.php') ? '' : 'datacall,';
$cachelost .= (@include MUDDER_CACHEDIR.'cache_menus.php') ? '' : 'menus,';


if($cachelost) {
    $cachelost = explode(',', $cachelost);
    echo '<p>Cache List:</p><p>';
    foreach($cachelost as $name) {
        $name && cache($name);
        echo $name.'<br>';
    }
    exit('</p><p>Caches successfully created, please refresh.</p>');
}
unset($caches, $name, $cachelost);

extract($_webtotal, EXTR_SKIP);

if($_config['default_city']) {
    $city    = $_config['default_city'];
    $_default_mappoint = explode(',',$_default_mappoints[$city]);
} else {
    $result = $db->get_one("SELECT areacode,default_mappoint FROM {$dbpre}areacode WHERE areasort=1");
    if($result) {
        $_config['default_city'] = $city = $result['areacode'];
        $_default_mappoint = explode(',',$result['default_mappoint']); //city_py
    } elseif(!defined('IN_ADMIN')) {
        diex('没有设置点评城市，请到后台添加点评城市。');
    }
}
// 默认标注
$_default_mappoint[0] = trim($_default_mappoint[0]);
$_default_mappoint[1] = trim($_default_mappoint[1]);

// template
$templateid = $templateid ? $templateid : ($_DCOOKIE['templateid'] ? $_DCOOKIE['templateid'] : $_config['templateid']);
if(is_array($_templates['main']) && !(array_key_exists($templateid, $_templates['main']) && $_config['templateid'] = $templateid)) {
    $templateid='1';
}
$template_dir = $_templates['main'][$templateid]['directory'];
substr($template_dir,-1,1) != '/' && $template_dir .= '/';
$template_url = $_config['siteurl'].substr($_templates['main'][$templateid]['directory'], 2).'/';
if(!file_exists(MUDDER_ROOT.$template_dir)) {
    $templateid = '1';
    $template_dir = './templates/main/default/';
    $template_url = $_config['siteurl'].'templates/main/default/';
}
$_config['templateid'] = $templateid;
set_cookie('templateid', $_config['templateid'], 3600*24*30);

// 载入缓存处理
require_once MUDDER_ROOT.'include/cache.inc.php';
// 模板标签查询处理
require_once MUDDER_ROOT.'include/query.inc.php';
//页面编码
header('Content-type: text/html; charset='.$charset);
//翻页
$page = @intval($page);
$page = $page <= 0 ? 1 : $page;
$offset = @intval($offset);
$offset = $offset <= 0 ? 10 : $offset;
$limit = get_limit($page, $offset);
?>