<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
//ob_start();
if(!defined("IN_QKWEB")){exit("Access Denied");}
header("Content-Type:text/html;charset=utf-8");
session_start();
date_default_timezone_set("PRC");
@ini_set("display_errors","on");
define('ROOT_PATH', str_replace('includes/init.php', '', str_replace('\\', '/', __FILE__)));
if(file_exists(ROOT_PATH."configs/config.php")){ require(ROOT_PATH."configs/config.php"); }
require(ROOT_PATH."includes/config.php");
require(ROOT_PATH."includes/function.php");
require(ROOT_PATH."includes/class/Editor.class.php");
require(ROOT_PATH."includes/class/http.class.php");
require(ROOT_PATH."includes/class/Msg.class.php");
define('APP_DIR', "");
define('VERSION','V1.5.70110');
/* 对用户传入的变量进行转义操作。*/
if (!get_magic_quotes_gpc())
{
    if (!empty($_GET))
    {
        $_GET  = addslashes_deep($_GET);
    }
    if (!empty($_POST))
    {
        $_POST = addslashes_deep($_POST);
    }

    $_COOKIE   = addslashes_deep($_COOKIE);
    $_REQUEST  = addslashes_deep($_REQUEST);
}
require(ROOT_PATH."includes/model/BaseModel.class.php");
require(ROOT_PATH."includes/action/BaseAction.class.php");
require(ROOT_PATH."includes/model/SeoModel.class.php");
require(ROOT_PATH."includes/class/Dispatcher.class.php");
require(ROOT_PATH."includes/class/Pay.class.php");
require(ROOT_PATH."includes/class/Smtp.class.php");
require(ROOT_PATH."includes/class/MailTemplate.class.php");
require(ROOT_PATH."includes/class/Uploader.class.php");

define("URL_CASE_INSENSITIVE",true);

if(isset($dbhost) && isset($dbuser) && isset($dbpwd) && isset($dbname)){
	require(ROOT_PATH."includes/class/Mysql.class.php");
	$db = new Mysql($dbhost,$dbuser,$dbpwd,$dbname,"utf8");
}else{
	Msg::show("您还未安装，请执行炫彩电子商务平台安装程序");
}
$baseUrl = "/";
require(ROOT_PATH."includes/class/LoadSmarty.class.php");
$smarty = new LoadSmarty();
$smarty->assign("baseUrl","/");

if(!defined("INSTALL")){
	$lock = ROOT_PATH."data/install.lock";
	if(!$db){Msg::show("数据库连接未配置");}
	if(!$db->check()){
		if(!file_exists($lock)){
			Msg::show("您还未安装，请执行炫彩电子商务平台安装程序");
		}else{
			Msg::show("数据库连接失败 ".$db->msg);
		}
	}
}
if(!file_exists(ROOT_PATH.'data')){Msg::show('data 数据目录不存在');}
if(!file_exists(ROOT_PATH.'data/lang')){Msg::show('data/lang 语言目录不存在');}
if($cfg_lang){
	if(!file_exists(ROOT_PATH."data/lang/".$cfg_lang.".lang.php")){Msg::show('语言文件'.$cfg_lang.'不存在');}
	require_once(ROOT_PATH."data/lang/".$cfg_lang.".lang.php");
}
$_G['lang'] = $lang;
if(!defined("INSTALL") && $db->version){
	Dispatcher::dispatch();
	require(ROOT_PATH."includes/safe.php");
	$sql = "select * from qk_hotkey order by sortid asc,id desc";
	$hotkey = $db->getAll($sql);
	$smarty->assign("hotkey",$hotkey);
	$sql = "select * from qk_variable order by id desc";
	$config = $db->getAll($sql);
	$siteconfig = array();
	if($config){
		foreach($config as $item){
			$siteconfig[$item['name']] = $item['value'];
		}
	}
	unset($config);
	$smarty->assign("VARIABLE",$siteconfig);
	unset($siteconfig);
	$host = $_SERVER["HTTP_HOST"];
	$sites = $db->getAll("select * from qk_site order by id desc");
	$siteId = 0;
	foreach($sites as $site){
		if(preg_match("/^{$host}$/",$site['siteHost'])){
			$siteId = $site['id'];
			break;
		}
	}
	if(!$siteId){ $siteId = $db->getOne("select id from qk_site where isDefault=1 order by id desc"); }
	if(!$siteId){ $siteId = $db->getOne("select id from qk_site order by id desc"); }
	if(!$siteId){Msg::show('未配置站点');}
	define('SITE_ID',$siteId);
	$siteConf = $db->getRow("select * from qk_site where id='".$siteId."' order by id desc");
	$smarty->assign("siteConf",$siteConf);
	if($siteConf['theme']){$cfg_theme=$siteConf['theme'];}
	if($siteConf['template']){$cfg_tmpl=$siteConf['cfg_tmpl'];}else{
		$siteConf['template'] = $cfg_tmpl?$cfg_tmpl:"xuincn";
	}

	if(defined('MODEL_NAME') && MODEL_NAME=="Admin"){
		$caching=false;
		$smarty->setTemplateDir(ROOT_PATH."/admin/templates/");
	}else if($siteConf['template']!='' && $siteConf['template']!=$cfg_tmpl){
		$smarty->setTemplateDir(ROOT_PATH."/templates/".$siteConf['template']."/");		
	}
	$adminUrl = "/qkadmin/";
	$smarty->assign("cfg_lang",$cfg_lang);
	$smarty->assign("lang",$lang);
	$smarty->assign("config",$_G['config']);
	$smarty->assign("adminPath","qkadmin/");
	$smarty->assign("adminUrl",$adminUrl);
	$smarty->assign("staticUrl","/static/");
	$smarty->assign("basePath","/");
	$smarty->assign("dataUrl","http://".$_SERVER['HTTP_HOST']."/data/");
	$smarty->assign("dataUrl","http://img.xuin.cn/");
$smarty->assign("themeUrl","/theme/".$cfg_theme."/");
load_class("purchase");
	$pm = new PurchaseModel();
	$cartInfo = $pm->cartInfo();
	$smarty->assign("cartInfo",$cartInfo);//	print_r($cartInfo);exit;
	unset($pm);
	unset($cartInfo);
	if($_SESSION['userId']){
		$sql = "select id from qk_user where id='".$_SESSION['userId']."'";if(!$GLOBALS['db']->getOne($sql)){ load_class('user');$um=new UserModel();$um->logout(); }
	}
	if(isset($_COOKIE['userId']) && $_COOKIE['userId'] && is_numeric($_COOKIE['userId']) && (!isset($_SESSION['userId']) || !$_SESSION['userId'])){
		load_class("user");$um = new UserModel();$um->quicklogin($_COOKIE['userId'],false);
	}
}
?>