<?php 
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：phpwms.base.php 
 * 摘    要：base 基类
 * 适用范围：PHPWMS 1.0
 * 版    本：1.0 
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS')) {exit('php188');}
@session_start();
require_once 'phpwms.global.function.php';
/**
 * base 基类
 */
abstract class base{
	public $tpl = null;
	public $cfg = null;
	public $db = NULL;
	protected $_parseItem = array();//同下
	protected $_parseModels = array();//需解析的URL 
	protected $_msg = '';
	protected $web_base = '/';
	protected $_tpl_name = '';
	function __construct(){
		global $db;
		if(!class_exists('php188StaticCache'))require PUB_CLASS.'/phpwms.CacheLite.php';
		$this->cache = php188StaticCache::getInstance();
		$this->db = $db;
		//关闭站点
		$this->cfg =  get_cache(array('siteBase'));
		$t = get_cache('templete');
		$t = empty($t)?'default':$t;
		$this->_tpl_name = $t;
		if($this->cfg['sys_onoff']=='1'){
			$re = $this->cfg['off_reason'];
			$reson = empty($re)?'系统维护中...':$re;
			exit($reson);
		}
		$this->web_base =  str_replace('index.php', '' ,$_SERVER['PHP_SELF']);
		$query_string = $_SERVER['QUERY_STRING'];
		if(!empty($query_string)){
			$models =  array_filter(array_map("trim",explode('-',array_shift(explode('.',$query_string)))));
			$parseMode = isset($models[0])&&!empty($models[0])?$models['0']:'default_wms';
			$parseMode = explode('_',$models[0]);
			$this->_parseModels = array('model'=>isset($parseMode['0']) && !empty($parseMode['0'])?$parseMode[0]:'default','action'=>isset($parseMode['1']) && !empty($parseMode['1'])?$parseMode['1']:'wms');
			$this->_parseItem = array_slice($models,1);
		}
		$this->init();
		setcookie('vist_ip',get_ip(),time()+7200,'/');
		$this->_setTemplate();
	}
	/**
	 * 获得执行数据
	 *
	 * @param boolean $db
	 * @return string
	 */
	function getRunInfo(){
		return get_debug_info();
	}
	protected function _exception($msg = '',$location = false){
		$this->_msg = $msg;
		if($location){
			$url = get_self();
			header("location:$url");
			exit();
		}
		exit($this->bgmsg($msg,''));
	}
	protected function init(){}
	abstract function wmsAction();
	private function _setTemplate(){
		require(INCLUDE_PATH.'tpl/Smarty.class.php');
		$this->tpl = new Smarty();
		if(SYS_DEBUG)$this->tpl->error_reporting = E_ALL;
		$GLOBALS['smarty'] = $this->tpl;
		$config = filter_site_url_decode(get_cache('siteBase'));
		$default_skin = get_cache('templete');
		$default_skin  = empty($default_skin)?'default':$default_skin;
		if(!file_exists(SYS_ROOT.'template/'.$default_skin.'/wms.xml'))$default_skin = 'default';
		$this->tpl -> config_dir   = SYS_ROOT."include";
		$this->tpl -> template_dir = SYS_ROOT."template/".$default_skin.'/';
		$pub_compile_path = SYS_ROOT.'cache/pub/compile/';
		if(!is_dir($pub_compile_path))mkdirs($pub_compile_path);
		$this->tpl -> compile_dir  = $pub_compile_path;
		$pub_cache_dir = SYS_ROOT."cache/pub/cache/";
		if(!is_dir($pub_cache_dir))mkdirs($pub_cache_dir);
		$this->tpl -> cache_dir = $pub_cache_dir;
		if(!is_dir($pub_compile_path))exit('please check the path <b>cache</b> can write');
		$this->tpl->load_filter('output','filter_phptag');
		$this->tpl ->left_delimiter = '<{';
		$this->tpl -> right_delimiter = '}>';
		$this->assign(array(
				'sitecfg'=>get_cache('siteBase'),
				'footer_info'=>$config['footer_info'],
				'web_base'=>$this->web_base,
				'web_template'=>$this->web_base . "template/".$default_skin.'/',
				'outurl'=>get_out_global_url(),
				'navigation'=>get_navigation(),
				'channel_models'=>get_models(),
			)
		);
		$this->tpl->register_function('get_page','get_page');
		$this->tpl->register_function('get_content','get_content');
		$this->tpl->register_function('get_category','get_category_for_tpl');
		$this->tpl->register_function('get_ads', 'get_ads');
		$this->tpl->register_function('get_user_request', 'get_user_request');
		$this->tpl->register_function('get_flash', 'get_flash');
		$this->tpl->register_function('get_notice', 'get_notice');
		$this->tpl->register_function('select_db', 'select_db_for_smarty');
		$this->tpl->register_function('get_poll_one', 'get_poll_one');
		$this->tpl->register_function('get_friendlink', 'get_friendlink');
		$this->tpl->register_function('debug_info','system_debug_info');
		$this->tpl->register_function('building_link','building_link_for_smarty');
		if(!is_dir($this->tpl->compile_dir))mkdirs($this->tpl -> compile_dir);
		if(!is_dir($this->tpl -> cache_dir))mkdirs($this->tpl -> cache_dir);
		$this->tpl -> compile_check = true;
		$this->tpl -> debugging = false;
		require SYS_ROOT.'data/self.nav.php';
		$this->assign($GLOBALS['self_nav']);
	}
	function display($resource_name='',$self_config_file = null,  $cache_id = null, $compile_id = null){
		$s = base64_decode('PG1ldGEgbmFtZT0iR2VuZXJhdG9yIiBjb250ZW50PSJQb3dlciBCeSBwaHB3bXMtcGhwMTg4LmNvbSC98MTP6tjN+MLnvLzK9dPQz965q8u+ob6xsb6pob8=');
		$s .= "  ".SYS_VERSION;
		$s .= base64_decode('IiAvPg==');
		echo str_replace('<head>','<head>'."\n".$s,$this->fetch($resource_name,$self_config_file, $cache_id, $compile_id,false));
	}	
	
	function fetch($resource_name='',$self_config_file = null, $cache_id = null, $compile_id = null, $display = false){
		if(!empty($self_config_file)){
			$main_file = $self_config_file;
		}else{
			$main_file = TEMPLATE_MAIN_PATH.$this->_tpl_name.'/'.$resource_name;
			//设置smarty模板切换路径 如果文件不存在则使用系统默认模板文件
			if(!file_exists($main_file))$main_file = TEMPLATE_MAIN_PATH.'default/'.$resource_name;
		}
		if(!file_exists($main_file))die('tpl not exist. <b>'.(!empty($self_config_file)?$self_config_file:$resource_name).'</b>');
		return $this->tpl->fetch($main_file, $cache_id, $compile_id , $display);
	}
	function _setNav($data = array()){
		$base = array(array('link'=>$this->web_base,'name'=>'首页'));
		$data = array_merge($base,$data);
		$this->tpl->assign('crumbs',$data);
	}
	function isPost(){
		return is_post();
	}
 function building_nav($array = array(),$index_item = array('name'=>'','link'=>'/'),$connect_item = ' &raquo; ',$target="",$last_no_link = true){
	$index_item['name'] = empty($index_item['name'])?'首页':$index_item['name'];
	$string = !empty($index_item)&& is_array($index_item)?'<a href="'.$index_item['link'].'">'.$index_item['name'].'</a>':'';
	if(!is_array($array)||empty($array))return $string;
	$the_end = array();
	if($last_no_link)$the_end = array_pop($array);
	foreach ($array as $item){
		$tag = empty($target)?'':"target='$target'";
		$uri = isset($item['url'])?'<a href="'.$item['url'].'"'.$tag.'>'.$item['name'].'</a>':$item['name'];
		$string .= $connect_item.$uri;
	}
	if($last_no_link)$string .= $connect_item.$the_end['name'];
	return $string;
}
 function setSeo($data = array()){
	$this->assign(array(
		'title'=>$this->cfg['sys_name'].(!empty($data['title'])?'-'.$data['title']:''),
		'description'=>(!empty($data['description'])?$data['description'].'-':'').$this->cfg['sys_description'],
		'keywords'=>(!empty($data['keywords'])?$data['keywords'].'-':'').$this->cfg['sys_keywords'],
	));
	isset($data['xml'])?$this->tpl->assign('xml',$data['xml']):'';
}
	protected function _debug(){

	}
	function assign($array = array()){
		if(!is_array($array))return $array;
		foreach($array as $k => $item){
			$this->tpl->assign($k, $item);
		}
		return NULL;
	}
	/**
	 * 得到 当前的 用户  
	 * flg = false 返回当前 会员的信息 
	 * true 没有登录则跳转到登陆页面     否则 返回会员信息
	 */
	 function getCurUser($flg = false){
		$curruser = $_SESSION['phpwmscuruser'];
		if($flg && empty($curruser)){
				$url = building_url('member', 'login');
				$this->bgmsg('请您先登录',$url);
				header('location:' . $url);
				exit();
		}else{
			return $curruser;
		}
	}
	/**
	 * 跳转页面
	 *
	 * @param string $message 提示消息
	 * @param string $url_forward 跳转路径
	 * @param int $time 跳转时间间隔
	 */
	public function bgmsg($message,$url_forward = '',$time = 2000) {
		$forwary_file = 'msg.php';
		$this -> tpl -> assign("message",$message);
		if(empty($url_forward)){
			$url_forward=$this->web_base;
		}
		$this -> tpl -> assign("url_forward",$url_forward);
		$this -> tpl -> assign("time",$time);
		if($this -> tpl -> template_exists($forwary_file)){
		$this -> tpl -> display($forwary_file);
		} else {
			die('<font color="blue">此功能模块暂未开放，请稍后访问……</font>');
		}
		exit;
    }
    /**
     * 检查验证码
     *
     */
    public function check_code($code){
    	return check_code($code);
    }
}
?>