<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		session_start();
		$this->load->helper('url_helper');
		//$this->output->enable_profiler(TRUE);
	}
	public function _login_view($name,$data)
	{

		$this->load->view('common/login_header',$data);
		$this->load->view('admin/'.$name,$data);
		$this->load->view('common/login_footer');		

	}

	/**
	 * [json 状态码JSON]
	 * @param  [type] $code [错误状态]
	 * @param  [type] $msg  [错误信息]
	 * @param  array  $data [错误数据]
	 * @return [type]       [返回json数据]
	 */
	public function json($code, $msg)
	{
		$data['code'] = $code;
		$data['msg'] = $msg;
		echo json_encode($data);
		exit; 
	}	

}


class Admin_Controller extends MY_Controller {

	public function __construct()
	{
		
		parent::__construct();
		$this->check_login();
		$this->checkCardStatus();
	}

	public function checkCardStatus()
	{
		$method = $this->router->fetch_class().'/'.$this->router->fetch_method();

		if( $method != 'card/edit' && $method != 'card/store' && $method != 'main/index')
		{
	
			$this->load->model('card_model','card');
	        $result = $this->card->checkCard();
	        if($result['status'] == 0)
	        {
	            redirect('admin/main/index');      
	        }
		}

	}

	/**
	 * [check_login 管理员登录检测]
	 * @return [type] [description]
	 */
	public function check_login(){
  
		if(! isset($_SESSION['userid']))
        {
            redirect(base_url());
        }
	}

	public function _view($name,$data){
		$data = array_merge($data, $this->data);

		$this->load->view('common/header',$data);
		$this->load->view('common/left_menu');
		$this->load->view('admin/'.$name,$data);
		$this->load->view('common/footer');
	}
}


class Api_Controller extends CI_Controller {

    protected $param = null;
	public function __construct(){
		parent::__construct();
		$this->load->library('user_service', array(), 'user');
		// $this->output->enable_profiler(TRUE);

        // 3DES 加密key
        $this->load->library('Mcrypt3Des', array('key' => '636d82b614235f1bcfd08969'));
	}

    /**
     * @access protected
     * 格式化参数
     */
    protected function _init_param() {
        $param = trim(file_get_contents('php://input'));
        if ($param) {
            $param = $this->mcrypt3des->dencrypt($param);
        }
        if (!empty($param)) {
            $param = json_decode($param, true);
        }
        $this->param = $param;
    }

    /**
     * @access protected
     * 获取参数
     */
    protected function get_param($name, $default = '', $type = 'string') {

        if (null === $this->param) {
            $this->_init_param();
        }

        if (isset($this->param[$name])) {
            $return_data = $this->param[$name];
        } else {
            $return_data = $default;
        }

        switch ($type) {
            case 'int':
            case 'integer':
                $return_data = (int)$return_data;
                break;
            case 'bool':
            case 'boolean':
                $return_data = ($return_data) ? true : false;
                break;
            case 'array':
                $return_data = (is_array($return_data) ? $return_data : $default);
                break;
            case 'string':
            default:
                $return_data = $return_data;
        }

        return $return_data;
    }

    //返回json格式数据，不加密（五折卡）
	public function json($code, $msg, $data = array()){
		$json['code'] = (string)$code;
		$json['msg'] = (string)$msg;
		$json['data'] = $data;
		echo json_encode($json);
		exit;
	}

    //返回加密数据（分享圈）
    function odz_error($errmsg = '', $errcode = -1, $values = array())
    {
        $this->odz_result(null, $errmsg, $errcode, $values);
    }

    //返回加密数据（分享圈）
    function odz_result($data = null, $msg = '', $code = 0, $values = array())
    {
        //去除html标签
        $msg = strip_tags($msg);
        $result = array('errcode'=>$code, 'errmsg'=>$msg);
        if($data && is_array($data)) {
            $result = array_merge($data, $result);
        }
        $result = json_encode($result);

        // 加密返回结果
//        if(MOCUZ_ENCRYPT != 'no'){
        $result = $this->mcrypt3des->encrypt($result);
//        }er('Content-Type:text/html; charset='.CHARSET);
        echo $result;
        exit;
    }

    /*function odz_encode($data) {
        if(empty($data)){
            return $data;
        }

        if(is_array($data)) {
            foreach($data as $k => $v) {
                $data[$k] = $this->odz_encode($v);
            }
        } else {
            if (is_string($data) && ODZ_CHARSET != 'utf-8') {
                $data = iconv(ODZ_CHARSET, 'utf-8//IGNORE', $data);
            }
            $data = html_entity_decode($data, ENT_COMPAT, 'utf-8');
            return $data;
        }
        return $data;
    }*/


}


class AdminApi_Controller extends Api_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->library('shop_service', array(), 'shop');

		if(!$this->shop->autoLogin() && $this->router->fetch_method() != 'login')
		{
			$this->json('2', 'no login');
		}
	}

}

class Front_Controller extends CI_Controller{

	public $auth;
	public $score_name;
	public $shop_info;

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
		$this->load->helper('discuz_helper');
		$this->load->model('score/ShopInfo_model','shop');
		$this->load->library('user_service', array(), 'user');
		$this->check_login();
		$this->shop_info = $this->shop->getShopInfo();
        $extcredits = get_extcredits();
        $this->score_name = $extcredits[$this->shop_info['type']]['title'];
	}


	public function check_login(){
		$this->auth = $this->input->server('HTTP_MOCUZAUTH');
		if(empty($this->auth)){
			$this->auth = $this->input->cookie('mocuz_auth');

		}

		if(empty($this->auth)){
			return false;
		}

		$this->user->checkLogin($this->auth);
	}

	public function _view($name,$data){
		$data['score_name'] = $this->score_name;
		$this->load->view('scorefront/common/header',$data);
		$this->load->view('scorefront/'.$name,$data);
	}

	public function login(){
		echo 'login';
		exit;
	}





}

/**
 * 积分商城继承的控制器
 */
class Score_Controller extends CI_Controller{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
		session_start();
		$this->checkScoreStatus();
		$this->check_login();	
	}
	public function check_login(){
  
		if(! isset($_SESSION['userid']))
        {
            redirect(base_url());
        }
	}
	public function checkScoreStatus()
	{
		$method = $this->router->fetch_class().'/'.$this->router->fetch_method();
		if( $method != 'shop/index' && $method != 'shop/add' && $method != 'shop/save')
		{
			$this->load->model('score/shopInfo_model','info');
	        $result = $this->info->getShopInfo();
	        if(empty($result))
	        {
	            redirect('score/shop/index');      
	        }
	 	}
	}
	public function _view($name,$data){
		$data = array_merge($data, $this->data);
		$this->load->view('common/header',$data);
		$this->load->view('common/score_left_menu');
		$this->load->view('admin/score/'.$name,$data);
		$this->load->view('common/footer');
	}
	/**
	 * [json 状态码JSON]
	 * @param  [type] $code [错误状态]
	 * @param  [type] $msg  [错误信息]
	 * @param  array  $data [错误数据]
	 * @return [type]       [返回json数据]
	 */
	public function json($code, $msg)
	{
		$data['code'] = $code;
		$data['msg'] = $msg;
		echo json_encode($data);
		exit; 
	}		

}

/**
 * 客户端界面设置继承控制器
 */
class Mocuz_Controller extends CI_Controller{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
		session_start();
		$this->check_login();
	}
	public function check_login(){
  
		if(! isset($_SESSION['userid']))
        {
            redirect(base_url());
        }
	}
	public function _view($name,$data){
		$this->load->view('common/innerheader',$data);
		$this->load->view('admin/mocuz/'.$name,$data);
	}
	/**
	 * [json 状态码JSON]
	 * @param  [type] $code [错误状态]
	 * @param  [type] $msg  [错误信息]
	 * @param  array  $data [错误数据]
	 * @return [type]       [返回json数据]
	 */
	public function json($code, $msg, $data = array())
	{
		$result['code'] = $code;
		$result['msg'] = $msg;
		$result['data'] = $data;
		echo json_encode($result);
		exit; 
	}		
}


/**
 * 客户端功能配置继承控制器
 */
class Mocuzfun_Controller extends CI_Controller{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
		session_start();
		$this->check_login();

		//给头部添加样式
		$directory = $this->router->fetch_directory();
		if($directory == 'mocuzfun/')
		{
			$this->data['headernow'] = 'mocuzfun';
		}
	}
	public function check_login(){
  
		if(! isset($_SESSION['userid']))
        {
            redirect(base_url());
        }
	}

	public function _view($name,$data){
		$data = array_merge($data, $this->data);
		$this->load->view('common/header',$data);
		$this->load->view('common/mocuzfun_left_menu');
		$this->load->view('admin/mocuzfun/'.$name,$data);
		$this->load->view('common/footer');
	}
	/**
	 * [json 状态码JSON]
	 * @param  [type] $code [错误状态]
	 * @param  [type] $msg  [错误信息]
	 * @param  array  $data [错误数据]
	 * @return [type]       [返回json数据]
	 */
	public function json($code, $msg)
	{
		$data['code'] = $code;
		$data['msg'] = $msg;
		echo json_encode($data);
		exit; 
	}		
}
/* End of file MY_Controller.php */
/* Location: ./core/admin/main.php */