<?php
defined('HUCART_COM')  OR exit('No direct script access allowed');
class HC_Controller{
	/*
    protected $_model;
    protected $_controller;
    protected $_action;
    public 	  $_Templ;
	*/
	private $_Config;
	public $view;
	public $db;
	
	public $model;
	public $lang = array();
	
	private static $instance;
	
	public function __construct()
	{
		global $config;
		//self::$instance = & $this;
		$this -> _Config = $config;
		unset($config);
		$this -> load('session'); //����session
		$this -> load('input'); //����������
		$this -> load_view();   //������ͼ
		//$this -> load_lang( 'config', 0 );
    }
	
	
	public function load( $class , $parameter = array() )
	{
		if($class!=''){
			$class_dir 	= ROOT_PATH.'/system/class/'.$class.'_class.php';
		}
		if(file_exists($class_dir)){
			require_once($class_dir);
			$act_file = ucwords($class);
			if(!empty($parameter)){
				$this->$class = new $act_file( $parameter );
			}else{
				$this->$class = new $act_file();
			}
		}else{
			exit("Call to undefined '".$class."_Class'!");
		}
	}
 
	public function load_lib( $class )
	{
		if($class!=''){
			$library_dir 	= ROOT_PATH.'/system/libraries/'.$class.'_lib.php';
		}
		if(file_exists($library_dir)){
			require_once($library_dir);
			$act_file = ucwords($class."_lib");
			$class_name = $class."_lib";
			$this->$class_name = new $act_file();			
		}
	}

	private function load_view()
	{
		$this -> view = new Smarty;
		$this -> set_templ_dir();
	}
	
	private function set_templ_dir( )
	{
 
		 if($this -> _Config['present_folder']){
			$this -> view -> template_dir  =  ROOT_PATH.'/'.$this -> _Config['view']['tpl_path'].'/'.$this -> _Config['present_folder'];
			$this -> view -> compile_dir   =  ROOT_PATH.'/'.$this -> _Config['view']['cpl_path'].'/'.$this -> _Config['present_folder'];
			$this -> view -> cache_dir     =  ROOT_PATH.'/'.$this -> _Config['view']['tmp_path'].'/'.$this -> _Config['present_folder'];
			//$smarty->config_dir = "smarty/templates/config";
			//$smarty->cache_dir ="smarty/templates/cache";
		 }else{
			$this -> view -> template_dir  =  ROOT_PATH.'/'.$this -> _Config['view']['tpl_path'];
			$this -> view -> compile_dir   =  ROOT_PATH.'/'.$this -> _Config['view']['cpl_path'];
			$this -> view -> cache_dir     =  ROOT_PATH.'/'.$this -> _Config['view']['tmp_path'];
		 }
		 $this -> view -> assign('base_weburl', DOMAIN_NAME);
		 
		//var_dump($this -> view -> compile_dir);// var_dump($this); exit();
	}
	public function set_view_dir($style_dir = '')
	{
		 if($style_dir != '')
		 {
			 /*var_dump($this -> _Config);
			 echo $this -> _Config['sys_code'];
			 //echo $this -> _Config['sys_css'];
			 if($this -> _Config['sys_code']!=''){
				$this -> set_template_dir( $this -> _Config['sys_code'] ); 
			 }else{
				$this -> set_template_dir( $style_dir );
			 }*/
			 $this -> set_template_dir( $style_dir );
			 $this -> set_compile_dir( $style_dir );
			 $this -> set_cache_dir( $style_dir );
			 
			 /*
			 $this -> view -> template_dir  = $this -> view -> template_dir.'/'.$style_dir;
			 $this -> view -> compile_dir   = $this -> view -> compile_dir.'/'.$style_dir;
			 $this -> view -> cache_dir     = $this -> view -> cache_dir.'/'.$style_dir;
			 */
		 }
	}

	public function set_template_dir($style_dir = '')
	{
		 if($style_dir != '')
		 {
			 $this -> view -> template_dir  = $this -> view -> template_dir.'/'.$style_dir;
		 }
	}
	public function set_compile_dir($style_dir = '')
	{
		 if($style_dir != '')
		 {
			 $this -> view -> compile_dir  = $this -> view -> compile_dir.'/'.$style_dir;
		 }
	}
	public function set_cache_dir($style_dir = '')
	{
		 if($style_dir != '')
		 {
			 $this -> view -> cache_dir  = $this -> view -> cache_dir.'/'.$style_dir;
		 }
	}
	

	public function load_db($type='mysqli')
	{

		if( $type == 'mysqli'){
			$db_file 	= ROOT_PATH.'/system/class/mysqli_class.php';
			//echo $db_file;
			//exit();
			if(file_exists($db_file)){
				require_once($db_file);
			}
			//var_dump($this -> _Config['db']);
			$this->db = new HC_MySqli( $this -> _Config['db']['sql_localhost'] , $this -> _Config['db']['sql_user'] , $this -> _Config['db']['sql_pwd'], $this -> _Config['db']['sql_data'] ,$this -> _Config['db']['sql_port'] );
			$this->db -> prefix = $this -> _Config['db']['sql_prefix'];
		}else if( $type == 'mysql'){
			$db_file 	= ROOT_PATH.'/system/class/mysql_class.php';
			if(file_exists($db_file)){
				require_once($db_file);
			}
			$this->db = new MySql( $this -> _Config['db']['sql_localhost'] , $this -> _Config['db']['sql_user'] , $this -> _Config['db']['sql_pwd'], $this -> _Config['db']['sql_data'] ,$this -> _Config['db']['sql_port'] );
			$this->db -> prefix = $this -> _Config['db']['sql_prefix'];
		}
	}
	
	public function load_model( $model_name ='' )
	{
		if(empty($this -> db)){
			$this -> load_db();
		}
		//var_dump( $this -> db );
		if($model_name!=''){
			 $model_dir 	= ROOT_PATH.'/system/models/'.$model_name.'_model.php';
		}
		if(file_exists($model_dir)){
			require_once($model_dir);
			$act_file = ucwords($model_name."_mod");
			$model_name = $model_name."_mod";
			$this->$model_name = new $act_file();
			
				
		}

		//var_dump($this->db);
	}
	
	public function load_lang( $language_file = '',  $type_lang = 'chinese' ,$is_show = 1 )
	{
		global $_LANG ;
			if($this -> _Config['present_folder']!=''){
				$config_language 	= ROOT_PATH.'/includes/languages/'.$type_lang.'/'.$this -> _Config['present_folder'].'/config_lang.php';
				if(file_exists($config_language)){
					require_once($config_language);
				}
			}
		
		if(isset($language_file) && !empty($language_file)){
			$lang_file_arr = explode(',',$language_file);
			foreach((array)$lang_file_arr as $lkey => $val_lang_file){
				if($val_lang_file!=''){
					$language_file 	= ROOT_PATH.'/includes/languages/'.$type_lang.'/'.$val_lang_file.'_lang.php';
					if(file_exists($language_file)){
						require_once($language_file);	
					}
				}
			}
			
		}
		//echo ROOT_PATH;
		//var_dump($_LANG);
		//exit();
		if(is_array( $_LANG)){
			$this->lang = array_merge( $this->lang , $_LANG );
		}
		if($is_show==1){
			//var_dump($this->lang);
			$this -> view -> assign('lang', $this->lang );
			unset($_LANG);
		}

	}
	function error()
	{
		$this -> view -> display('error404.tpl');
		exit();
	}


	public function __destruct()
	{
	}
	
}


	function load_action($load = '',$act = '' )
	{
		global $config;
		if($load!='')
		{
			$class_path = ROOT_PATH.'/application/'.$config['present_folder'].'/'.$load.'.php';
		}else{
			$class_path = ROOT_PATH.'/application/'.$config['present_folder'].'/index.php';
		}
		
		//echo $class_path;
		if(file_exists($class_path) )
		{
			require_once($class_path);
			if($load != '' && $act != ''){
				
				$act_file = ucwords($load);
		
				if(class_exists($act_file) ){
		
					$actclass = new $act_file();
					if(method_exists($actclass,$act)){
						
						call_user_func(array($actclass, $act));
						
					}else{
						///echo $act;
						exit('Call to undefined function!');
					}
				}
			}else if($load == '' && $act == ''){
				
				if(class_exists('Index')){
					$index = new Index();
					if(method_exists('index','act_default')){
						$index -> act_default();
					}
				}
				//echo '=========';
			}else if($load != ''){
				$act_file = ucwords($load);
				if(class_exists($act_file)){
					$actclass = new $act_file();
					if(method_exists($load,'act_default')){
						call_user_func(array($actclass, 'act_default'));
					}else{
						exit('Call to undefined function!');
					}
				}
			}
		
		}else{
			exit('error 404');
		}
	}
	
	//["REQUEST_URI"]=> string(22) "/index.php?/item-9.html" 
	//["SCRIPT_NAME"]=> string(10) "/index.php" 
	//["PATH_INFO"]=> string(12) "/item-9.html" 
	//["PATH_TRANSLATED"]=> string(37) "D:\AppServ\www\hucarttest\item-9.html" 
	//["PHP_SELF"]=> string(22) "/index.php?/item-9.html"
	//["HTTP_REFERER"]=> string(48) "http://www.hucarttest.com/index.php?/item-10.html" 

	function action_uri( $is_uri = false )
	{
		if($is_uri == true){
			if( substr($_SERVER["PATH_INFO"],-5) == '.html' and $_SERVER["PATH_INFO"] !=''){
				$uri = substr($_SERVER["PATH_INFO"],0,-5);
				$arr_val = explode("/",$uri);
				if($arr_val[1]!='')
				{
					$url_arr = explode("-",$arr_val[1]);
					$re_arr['load'] = $url_arr[0];
 					$re_arr['act'] 	= $url_arr[1];
					$re_arr['id'] 	= $url_arr[2];
					return $re_arr;
				}
				
				return false;
			}
		}else{
			return false;
		}
	}
		
	function ex_str( $uri )
	{
		$arr_val = explode("/",$uri);
		if($arr_val[1]!='')
		{
			$url_arr = explode("-",$arr_val[1]);
			$re_arr['load'] = $url_arr[0];
			$re_arr['act'] 	= $url_arr[1];
			$re_arr['id'] 	= $url_arr[2];
			$re_arr['id'] = str_replace("_html","",$re_arr['id']);
			if($re_arr['act']==''){ $re_arr['load'] = str_replace("_html","",$re_arr['load']);}
			return $re_arr;
		}
		return false;
	}

	
	function del_extent(&$val){
		//'-','_',
		$delstr=array('~','*','^','%','$','#', '`',',','\'',"\"",'@','+','=','/','\\','&','!','?','<','>','(',')','[',']',' ', '\x0d','\x0a','\x20');
		$val = str_replace($delstr,'',trim($val));
		$val = substr($val,0,150);
		return  $val ;
	}

	if(isset($_GET['load']) && isset($_GET['act']) && $_GET['load']=='' && $_GET['act']=='' && $config['is_uri'] == true)
	{
		$act_arr 	= action_uri( $config['is_uri'] );
		//var_dump($act_arr);
		if($act_arr['load']!='' && $act_arr['act'] !=''){
			$load 		= del_extent($act_arr['load']);
			$act  		= del_extent($act_arr['act']);
			if( $act_arr['id'] !='' ){
				$_GET['id'] = $act_arr['id'];
			}
		}elseif($act_arr['load']!='' && $act_arr['act'] ==''){
			$load 		= del_extent($act_arr['load']);
		}
	}else{

		if(!isset($_GET['load']) && !isset($_GET['act']) && count($_GET) == 1){

			$key_url = array_keys($_GET);
			$url_arr = ex_str( $key_url[0] );
			//var_dump($url_arr);
			$load = del_extent($url_arr['load']);
			$act  = del_extent($url_arr['act']);
			$_GET['id'] = $url_arr['id'];
		}else{
			$load = del_extent($_GET['load']);
			$act = del_extent($_GET['act']);
		}
	}
	//echo 'load'.$load.'act'.$act; //exit();
	load_action( $load ,$act );
	exit();

