<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends salebase {
    public function action_login(){
    //    $is_verify_code = $this->get_system_config("login_verify_code");
          TPL::display($this->cptpl.'login.tpl');
    }
    	// 登录检测
	public function action_check_login() {
		ob_end_clean();
        ob_start();
		$info = $this->_validAdminInfo();
     //    echo md5($info['password']);die;
		$model = parent::model('login', 'sa');
		$flag = $model->doLogin($info['username'], $info['password']);
                unset($model);

                if ($flag == 0) {
                    $this->saveLoginLog($info['username'], '登录失败', 0);
                    $this->_json(0, "未知错误");
                }
                elseif ($flag == 1) {
                    $this->saveLoginLog($info['username'], '登录失败，帐号被锁定', 0);
                    $this->_json(0, "帐号被锁定");
                }
                elseif ($flag == 2) {
                    $this->saveLoginLog($info['username'], '登录失败，密码错误', 0);
                    $this->_json(0, "登录失败");
                }
                elseif ($flag == 3) {
                    $this->saveLoginLog($info['username'], '登录成功', 1);
                    $this->_json(1, "登录成功");
                }
	}

    public function action_logout() {
        if (!empty(parent::$wrap_admin['username'])) {
            $this->saveLoginLog(parent::$wrap_admin['username'], '退出登录', 1);
        }
        $model = parent::model('login', 'sa');
        $model->logout();
        unset($model);
        echo "1";
    }


    private function _validAdminInfo() {
        $args = XRequest::getGpc(
            array('username', 'password', 'checkcode')
        );
        if (false === XValid::isUserArgs($args['username'])) {
            $this->_json(0, "帐号格式不正确");
        }
        if (empty($args['password'])) {
            $this->_json(0, "密码不能为空");
        }
        parent::loadUtil('session');
        if ($args['checkcode'] != XSession::get('verifycode')) {
            $this->_json(0, "验证码不正确");
        }
        unset($args['checkcode']);
        return $args;
    }

    private function _json($response=0, $result='') {
        echo json_encode(array("response"=>$response, "result"=>$result));
        die();
    }

  //   public function action_logout() {
  //       	parent::loadUtil('cookie');
		// XCookie::del('sale_userinfo');
  //               $data['code']=1;
  //               echo json_encode($data);
  //   }
    
 /**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装） 
 * @return mixed
 */
 public function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
    
}
?>
   