<?php
/**
 * 核心文件
 *
 * 核心初始化类，不允许继承
 *
 */
defined('InShopNC') or exit('Access Invalid!');

require_once (BASE_ROOT_PATH . '/helper/performance_helper.php');

final class Base
{
	const CPURL = '';

	public static function init() 
	{
	    global $setting_config;     
	    self::parse_conf($setting_config);
	    define('MD5_KEY',md5($setting_config['md5_key']));

	    if(function_exists('date_default_timezone_set')) 
	    {
	        if (is_numeric($setting_config['time_zone'])){
	            @date_default_timezone_set('Asia/Shanghai');
	        } else {
	            @date_default_timezone_set($setting_config['time_zone']);
	        }
	    }

	    //session start
	    self::start_session();
	    //output to the template
	    Tpl::output('setting_config',$setting_config);	    
	    //read language
	    Language::read('core_lang_index');
	}
        
    public static function fini() {
        session_destroy();
    }
    
    public static function run_util()
    {
       	self::cp();
		global $setting_config;
		self::parse_conf($setting_config);
		define('MD5_KEY',md5($setting_config['md5_key']));

		if(function_exists('date_default_timezone_set'))
		{
			if (is_numeric($setting_config['time_zone'])){
				@date_default_timezone_set('Asia/Shanghai');
			}else{
				@date_default_timezone_set($setting_config['time_zone']);
			}
		}
		Tpl::output('setting_config',$setting_config);
		Language::read('core_lang_index');
    }

	public static function mobile_init()
	{
		self::cp();

		global $setting_config;
		self::parse_conf($setting_config);
		define('MD5_KEY',md5($setting_config['md5_key']));

		if(function_exists('date_default_timezone_set'))
		{
			if (is_numeric($setting_config['time_zone'])){
				@date_default_timezone_set('Asia/Shanghai');
			}else{
				@date_default_timezone_set($setting_config['time_zone']);
			}
		}
		Tpl::output('setting_config',$setting_config);
		Language::read('core_lang_index');

		session::instance()->init();
	}

	public static function mobile_control()
	{
		if ($GLOBALS['setting_config']['enabled_subdomain'] == '1' && $_GET['act'] == 'index' && $_GET['op'] == 'index')
		{
			$store_id = subdomain();
			if ($store_id > 0) $_GET['act'] = 'show_store';
		}

		$act_file = realpath(BASE_PATH.'/control/'.$_GET['act'].'.php');
		$class_name = $_GET['act'].'Control';

		if (!file_exists($act_file) || !@require_once($act_file)) {
			throw new Exception("Base Error: access file={$act_file} isn't exists!");
		}

		if(class_exists($class_name)) {
			$main = new $class_name();
		} else {
			$error = "Base Error: class {$class_name} isn't exists!";
			throw new Exception($error);
		}

		if(!empty($main))
		{
			$function = $_GET['op'].'Op';
			if (method_exists($main,$function)) {
				$main->$function();
			} elseif (method_exists($main,'indexOp')) {
				$main->indexOp();
			} else {
				$error = "Base Error: function {$function} not in $class_name!";
				throw new Exception($error);
			}
		} else {
			$error = "Base Error: class {$class_name} isn't exists!";
			throw new Exception($error);
		}
	}


	/**
	 * run
	 */
	public static function run() {
		performance_helper::clear();
        self::cp();
        self::init();
        self::control();
    }

	/**
	 * get setting
	 */
	private static function parse_conf(&$setting_config) 
	{            
		$nc_config = $GLOBALS['config'];
		if(is_array($nc_config['db']['slave']) && !empty($nc_config['db']['slave'])) 
		{
			$dbslave = $nc_config['db']['slave'];
			$sid     = array_rand($dbslave);
			$nc_config['db']['slave'] = $dbslave[$sid];
		} else {
			$nc_config['db']['slave'] = $nc_config['db'][1]; //数据库在此初始化
		}

		$nc_config['db']['master'] = $nc_config['db'][1];
		$setting_config = $nc_config;
		$setting = ($setting = rkcache('setting')) ? $setting : rkcache('setting',true);
		$setting['shopnc_version'] = 'Copyright 2015</a>';
		$setting_config = array_merge_recursive($setting,$nc_config);
	}

	/**
	 * 控制器调度
	 *
	 */
	private static function control()
	{
		if ($GLOBALS['setting_config']['enabled_subdomain'] == '1' && $_GET['act'] == 'index' && $_GET['op'] == 'index')
		{
			$store_id = subdomain();
			if ($store_id > 0) $_GET['act'] = 'show_store';
		}

		$act_file = realpath(BASE_PATH.'/control/'.$_GET['act'].'.php');
		$class_name = $_GET['act'].'Control';
		if (!@include_once($act_file))
		{
		    if (C('debug')) {
		        throw_exception("Base Error: access file isn't exists!");
		    } else {
		        showMessage('抱歉！您访问的页面不存在','','html','error');
		    }
		}

		if (class_exists($class_name))
		{
			$main = new $class_name();
			$function = $_GET['op'].'Op';
            Log::record("act={$class_name},op={$function}",Log::DEBUG);

			if (method_exists($main,$function)){                       
				$main->$function();
			} elseif (method_exists($main,'indexOp')){
				$main->indexOp();
			} else {
				$error = "Base Error: function $function not in $class_name!";
				throw_exception($error);
			}
		} else {
			$error = "Base Error: class $class_name isn't exists!";
			throw_exception($error);
		}
	}

	/**
	 * 开启session
	 *
	 */
	private static function start_session()
	{
		global $config;
		
		if (SUBDOMAIN_SUFFIX){
			$subdomain_suffix = SUBDOMAIN_SUFFIX;
		}else{
			if (preg_match("/^[0-9.]+$/",$_SERVER['HTTP_HOST'])){
				$subdomain_suffix = $_SERVER['HTTP_HOST'];
			}else{
				$split_url = explode('.',$_SERVER['HTTP_HOST']);
				if($split_url[2] != '') unset($split_url[0]);
				$subdomain_suffix = implode('.',$split_url);
			}
		}
		//session.name强制定制成PHPSESSID,不请允许更改
		@ini_set('session.name','PHPSESSID');
		$subdomain_suffix = str_replace('http://','',$subdomain_suffix);
		if ($subdomain_suffix !== 'localhost') {
		    @ini_set('session.cookie_domain', $subdomain_suffix);
		}

		//默认以文件形式存储session信息
		if (strcmp($config['session_type'], 'files') === 0) {
			session_save_path(BASE_DATA_PATH.'/session');
		} else if (strcmp($config['session_type'], 'redis') === 0) {
			session_save_path($config['session_save_path']);
		}
		
		session_start();
	}

	public static function autoload($class)
	{
		$class = strtolower($class);
		if (ucwords(substr($class,-5)) == 'Class' ){
			if (!@include_once(BASE_PATH.'/framework/libraries/'.substr($class,0,-5).'.class.php')){
				exit("Class Error: {$class}.isn't exists!");
			}
		}elseif (ucwords(substr($class,0,5)) == 'Cache' && $class != 'cache'){
			if (!@include_once(BASE_CORE_PATH.'/framework/cache/'.substr($class,0,5).'.'.substr($class,5).'.php')){
				exit("Class Error: {$class}.isn't exists!");
			}
		}elseif ($class == 'db'){
			if (!@include_once(BASE_CORE_PATH.'/framework/db/'.strtolower(DBDRIVER).'.php')){
				exit("Class Error: {$class}.isn't exists!");
			}
		}else{
			if (!@include_once(BASE_CORE_PATH.'/framework/libraries/'.$class.'.php')){
				exit("Class Error: {$class}.isn't exists!");
			}
		}
	}

	public static function mobile_autoload($class)
	{
		$class = strtolower($class);
		if (ucwords(substr($class,-5)) == 'Class' ){
			if (!@include_once(BASE_PATH.'/framework/libraries/'.substr($class,0,-5).'.class.php')){
				throw new Exception("Class Error: {$class}.isn't exists!");
			}
		}elseif (ucwords(substr($class,0,5)) == 'Cache' && $class != 'cache'){
			if (!@include_once(BASE_CORE_PATH.'/framework/cache/'.substr($class,0,5).'.'.substr($class,5).'.php')){
				throw new Exception("Class Error: {$class}.isn't exists!");
			}
		}elseif ($class == 'db'){
			if (!@include_once(BASE_CORE_PATH.'/framework/db/'.strtolower(DBDRIVER).'.php')){
				throw new Exception("Class Error: {$class}.isn't exists!");
			}
		}else{
			if (!@include_once(BASE_CORE_PATH.'/framework/libraries/'.$class.'.php')){
				throw new Exception("Class Error: {$class}.isn't exists!");
			}
		}
	}

	/**
	 * 合法性验证
	 *
	 */
	private static function cp()
    {
		if (self::CPURL == '') return;
		if ($_SERVER['HTTP_HOST'] == 'localhost') return;
		if ($_SERVER['HTTP_HOST'] == '127.0.0.1') return;
		if (strpos(self::CPURL,'||') !== false)
        {
			$a = explode('||',self::CPURL);
			foreach ($a as $v)
			{
				$d = strtolower(stristr($_SERVER['HTTP_HOST'],$v));
				if ($d == strtolower($v)){
					return;
				}else{
					continue;
				}
			}
			return;
			//header('location: http://abc.abc.com');exit();
		} 
		else
        {
			$d = strtolower(stristr($_SERVER['HTTP_HOST'],self::CPURL));
			if ($d != strtolower(self::CPURL)){
				 return;
			}
		}
	}
}