<?php

/**
 * 登录类
 *
 * @author 杨金球
 */
if (C('BIND_BBS')) {
    Vendor('Ucenter.UcApi');  //载入UcApi扩展
}

class LoginAction extends PublicAction {

    //登录首页
    public function index() {
        $this->assign('title', '请登录');  //分配标题
        $this->assign('keyword', '登录|');
        $this->assign('content', '登录|');
        //$b = array_keys($_COOKIE);
        //$count = count($_COOKIE);
        //dump($b);
        /* for ($i = 0; $i < $count; $i++) {
          setcookie($b[$i], '', -3600, '/');
          } */
        //dump($_COOKIE);
        //dump(UcApi::getUserByCookie());
        $this->display();
    }

    //检查登录
    public function checklogin() {
        //if (!empty($_POST['username']) && !empty($_POST['password']) && preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $_POST['username'])) {
        if (!empty($_POST['username']) && !empty($_POST['password'])) {

            $cutName = substr($_POST['username'], 0, strpos($_POST['username'], "@")); //截取邮箱得用户名
            $user = M('user');
            if (preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $_POST['username'])) {
                $map['name'] = $cutName;
            } else {
                $map['name'] = $_POST['username'];
            }
            $userMsg = $user->where($map)->find();


            // 没有绑定论坛
            if (!C('BIND_BBS')) {
                if ($userMsg['pwd'] == md5($_POST['password'])) {
                    $_SESSION['user_id'] = $userMsg['id'];
                    $_SESSION['username'] = $userMsg['name'];
                    $_SESSION['user_status'] = $userMsg['user_status'];
                } else {
                    $this->error('用户名或密码错误');
                }
            } else {    //绑定论坛
                //判断是否邮箱登陆
                if (preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $_POST['username'])) {
                    $UCuser = new Model();
                    $UCuserdb = $UCuser->db('', "" . UC_CONNECT . "://" . UC_DBUSER . ":" . UC_DBPW . "@" . UC_DBHOST . "/" . UC_DBNAME . "")->query("SELECT username FROM `" . UC_PREFIX . "members` WHERE email = '$_POST[username]'");
                    if (!$UCuserdb) {
                        $username = $cutName;
                    } else {
                        $username = $UCuserdb['0']['username'];
                    }
                } else {
                    $username = $_POST['username'];
                }
                $login = UcApi::login($username, $_POST['password']);
                //  UCenter 登录成功
                if (is_array($login)) {
                    if ($userMsg) {   // 本地也存在用户
                        $_SESSION['user_id'] = $userMsg['id'];
                        $_SESSION['username'] = $userMsg['name'];
                        $_SESSION['user_status'] = $userMsg['user_status'];
                        $_SESSION['bbs_id'] = $userMsg['bbs_id'];
                        //检测本地密码是不是和UCenter密码一致,如不一致就修改本地密码
                        if ($userMsg['pwd'] != md5($_POST['password'])) {
                            $map['pwd'] = md5($_POST['password']);
                            $map['bbs_id'] = $login['uid']; //绑定论坛ID到本地用户表
                            $map['id'] = $userMsg['id'];
                            $user->save($map);
                        }
                    } else {    //本地不存在用户
                        //自动激活
                        $arg['name'] = $login['username'];
                        $arg['email'] = $login['email'];
                        $arg['bbs_id'] = $login['uid'];
                        $arg['pwd'] = md5(date('YmdHis'));
                        $arg['reg_date'] = time();
                        $userID = $user->add($arg);
                        $_SESSION['user_id'] = $userID;
                        $_SESSION['username'] = $login['username'];
                        $_SESSION['user_status'] = 1;
                        $_SESSION['bbs_id'] = $login['uid'];
                    }
                    $uclogin = $login['synlogin'];    //输出同步登录代码
                } else {
                    if ($login == -1) {    //UCenter不存在此用户
                        if ($userMsg) {   //UCenter不存在用户，但是本地存在用户
                            if ($userMsg['pwd'] != md5($_POST['password'])) {   //匹配本地密码不成功
                                $this->error('密码错误，请重试');
                            } else {    //匹配本地密码成功，自动向UCenter注册一个用户
                                $_SESSION['user_id'] = $userMsg['id'];
                                $_SESSION['username'] = $userMsg['name'];
                                $_SESSION['user_status'] = $userMsg['user_status'];
                                if (preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $_POST['username'])) {
                                    $UCname = $cutName;
                                } else {
                                    $UCname = $_POST['username'];
                                }

                                $reg = UcApi::reg($UCname, $_POST['password'], $userMsg['email']);
                                if ($reg > 0) {
                                    $map['bbs_id'] = $reg; //绑定论坛ID到本地用户表
                                    $map['id'] = $userMsg['id'];
                                    $user->save($map);
                                    $_SESSION['bbs_id'] = $reg;
                                    $uclogin = uc_user_synlogin($reg); //输出同步登录代码
                                }
                            }
                        } else {    //UCenter不存在用户，本地也不存在用户
                            $this->error('用户不存在');
                        }
                    } else if ($login == -2) {  //UCenter 密码错误
                        if ($userMsg) {     //本地存在用户
                            if ($userMsg['pwd'] == md5($_POST['password'])) {
                                //本地密码匹配成功，更改UCenter密码
                                uc_user_edit($userMsg['name'], '', $_POST['password'], '', 1);
                                $_SESSION['user_id'] = $userMsg['id'];
                                $_SESSION['username'] = $userMsg['name'];
                                $_SESSION['user_status'] = $userMsg['user_status'];
                                $_SESSION['bbs_id'] = $userMsg['bbs_id'];
                                $uclogin = uc_user_synlogin($userMsg['bbs_id']); //输出同步登录代码
                            } else {    //本地密码错误
                                $this->error('密码错误，请重试');
                            }
                        } else {    //本地不存在用户且UCenter登录密码错误
                            $this->error('密码错误，请重试');
                        }
                    } else {
                        $this->error('密码错误，请重试');
                    }
                }
            }
            $url = rawurldecode($_GET['url']);
            $this->assign('jumpUrl', $url);
            $this->success($uclogin.'登录成功');
        } else {
            $this->error('错误，用户名或密码错误');
        }
    }

    //注册
    public function register() {
        $this->assign('title', '注册');
        $this->display();
    }

    //检查注册
    public function check_register() {
        if (preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $_POST['username'])) {
            $UCname = substr($_POST['username'], 0, strpos($_POST['username'], "@")); //获取邮箱用户名
            $user = M('user');
            if (C('BIND_BBS')) {
                $reg = UcApi::reg($UCname, $_POST['password'], $_POST['username']);
                if ($reg <= 0) {
                    $this->error(UcApi::getError());
                }
            }
            $map['name'] = $UCname;
            if ($user->where($map)->find()) {
                $this->error('用户名已经被注册，请更改');
            } else {
                $map['pwd'] = md5($_POST['password']);
                $map['email'] = $_POST['username'];
                $map['reg_date'] = time();
                if (C('BIND_BBS')) {
                    $map['bbs_id'] = $reg;
                }
                $id = $user->add($map);
                if (!$id) {
                    $this->error('注册失败，请稍后再试');
                }
            }
            //处理注册成功并登陆
            $_SESSION['user_id'] = $id;
            $_SESSION['username'] = $UCname;
            $_SESSION['user_status'] = 1;
            if (C('BIND_BBS')) {
                $_SESSION['bbs_id'] = $reg;
                $ucreg = uc_user_synlogin($reg); //输出同步登录代码
            }
            $url = rawurldecode($_GET['url']);
            $this->assign('jumpUrl', 'index?url=' . $url);
            $this->success($ucreg.'注册成功');
        } else {
            $this->error('用户名必须为邮箱格式');
        }
    }

    //退出登录
    public function logout() {
        unset($_SESSION['user_id'], $_SESSION['username'],  $_SESSION['user_status'],  $_SESSION['bbs_id']);
        if (C('BIND_BBS')) {
            $uclogout = UcApi::logout();
        }
		$this->assign('jumpUrl', __APP__);
        $this->success($uclogout.'退出成功');
    }

}

?>
