<?php

// +----------------------------------------------------------------------

// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.

// +----------------------------------------------------------------------

// | Author: Tuolaji <479923197@qq.com>

// +----------------------------------------------------------------------

/**

 */

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class PublicController extends AdminbaseController {



    function _initialize() {

        C(S('sp_dynamic_config'));//加载动态配置

    }

    //后台登陆界面

    public function login() {

    	if(isset($_SESSION['ADMIN_ID'])){//已经登录

    		$this->success(L('LOGIN_SUCCESS'),U("Index/index"));

    	}else{

    	    $site_admin_url_password =C("SP_SITE_ADMIN_URL_PASSWORD");

    	    $upw=session("__SP_UPW__");

    		if(!empty($site_admin_url_password) && $upw!=$site_admin_url_password){

    			redirect(__ROOT__."/");

    		}else{

    			$this->display(":login");

    		}

    	}

    }

    

    public function logout(){

    	session('ADMIN_ID',null);

        session("__SP_UPW__",null);

    	redirect(__ROOT__."/");

    }

    

    public function dologin(){

    	$name = I("post.username");

        $error['username'] = $name;

        $error['time'] = time();

        $error['ip'] = get_client_ip();

        if(empty($name)){

            $error['remark'] = L('USERNAME_OR_EMAIL_EMPTY');

            M('admin_login_error')->add($error);

            $this->error(L('USERNAME_OR_EMAIL_EMPTY'));

        }

        $pass = I("post.password");

        if(empty($pass)){

            $error['remark'] = L('PASSWORD_REQUIRED');

            M('admin_login_error')->add($error);

            $this->error(L('PASSWORD_REQUIRED'));

        }

        $verrify = I("post.verify");

        if(empty($verrify)){

            $this->error(L('CAPTCHA_REQUIRED'));

        }

        //验证码

        if(!sp_check_verify_code()){

            $this->error(L('CAPTCHA_NOT_RIGHT'));

        }else{

            $t=time();

            $tt=$t-(10*60);

            $mapt['time']=array('between',"{$tt},{$t}");

            $mapt['ip']=get_client_ip();

            $c=M('admin_login_error')->where($mapt)->select();

            $cc=count($c);

            if($cc>=5){

                $this->error("错误次数过多，请10分钟后重试");
            }

            $user = D("Common/Users");

            if(strpos($name,"@")>0){//邮箱登陆

                $where['user_email']=$name;

            }else{

                $where['user_login']=$name;

            }

            $result = $user->where($where)->find();

            if(!empty($result) && $result['user_type']==1){

                if(sp_compare_password($pass,$result['user_pass'])){

                    $role_user_model=M("RoleUser");

                    $role_user_join = C('DB_PREFIX').'role as b on a.role_id =b.id';

                    $groups=$role_user_model->alias("a")->join($role_user_join)->where(array("user_id"=>$result["id"],"status"=>1))->getField("role_id",true);

                    if( $result["id"]!=1 && ( empty($groups) || empty($result['user_status']) ) ){

                        $error['remark'] = L('USE_DISABLED');

                        M('admin_login_error')->add($error);

                        $this->error(L('USE_DISABLED'));

                    }

                    //登入成功页面跳转

                    $_SESSION["ADMIN_ID"]=$result["id"];

                    $_SESSION['name']=$result["user_login"];

                    $result['last_login_ip']=get_client_ip();

                    $result['last_login_time']=date("Y-m-d H:i:s");

                    $user->save($result);

                    setcookie("admin_username",$name,time()+30*24*3600,"/");

                    $this->success(L('LOGIN_SUCCESS'),U("Index/index"));//redirect(U("Index/index"));//

                }else{

                    $error['remark'] = L('PASSWORD_NOT_RIGHT');

                    M('admin_login_error')->add($error);

                    $this->error(L('PASSWORD_NOT_RIGHT'));

                }

            }else{

                $error['remark'] = L('USERNAME_NOT_EXIST');

                M('admin_login_error')->add($error);

                $this->error(L('USERNAME_NOT_EXIST'));

            }

        }

    }



}