<?php
/**
 * Copyright (C) LJCMS All rights reserved.
  * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class control extends adminbase {
    
	public function action_run() {
		TPL::display($this->cptpl.'login.tpl');
	}

	public function action_adminlogin() {
		$info = $this->_validAdminInfo();
     //    echo md5($info['password']);die;
		$model = parent::model('login', 'am');
		$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) {
                    $_SESSION['uploadname']=$info['username'];
                    $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', 'am');
        $model->logout();
        unset($model);
        XHandle::redirect($this->cpfile.'?c=login');
    }

    
    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_ajax_get_action()
     {     
     	$advContrl = get_class_methods('control');
   
        
     	$html = '';
     	foreach ($advContrl as $val){
            if(strpos($val,'action') !== false){
                   
               $html .= "<option value='".$val."'>".$val."</option>";
                    
            } else {
               
            }
     		
     	}
     	exit($html);
     }
}
?>
