<?php
/**
* 用户登录
* @author moufer<moufer@163.com>
* @package modoer
* @copyright Moufer Studio(www.modoer.com)
* @version $Id: admincp.php 3 2007-9-14 11:43:54 $
*/
define('SCRIPTNAV', 'login');
require_once(substr(dirname(__FILE__), 0, -6).'./include/common.inc.php');

$forward = !$forward && $php_referer ? $php_referer : ($forward ? $forward : $_config['siteurl']);
if(strposex($forward,'logout=1') || !strposex($forward,$php_domain)) {
    $forward = $_config['siteurl'];
}
$forward = html_special_chars($forward);

// Passport 整合
if($logout == '1') {
    $_passport['enable'] && location($_passport['logout_url'], false);
} else {
    $_passport['enable'] && location($_passport['login_url'], false);
}

$tplfile = 'member_login';
$subtitle = '用户登录';

if($user->isLogin && !$logout){
    $urls = array(
        array('name' => '我的助手','url' => $_config['siteurl'].'member/index.php'),
        array('name' => '返回首页','url' => $_config['siteurl']),
        array('name' => '注销登录','url' => $_config['siteurl'].'member/login.php?logout=1'),
    );

    redirect('您目前已经登录，无需再次登录。', $urls);
}
if($logout == '1') {

    if($ucenter_enable) {
        $result = $user->Loginout_login();
        $urls = array(
            array('name' => '回到原来的页面', 'url' => $forward),
            array('name' => '返回首页', 'url' => $_config['siteurl']),
            array('name' => '重新登录', 'url' => $_config['siteurl'].'member/login.php'),
        );
        redirect('您已成功退出。'.$result, $urls); // $result -- UC同步登入的HTML，不可删除
    }
    if($user->uid > 0) {
        $uid = $user->uid;
        del_cookie(array("uid", "username", "email", "hash", "logintime", "remembertime"));
        unset($user);
        $forward = strposex($forward,'post.php') ? $_config['siteurl'] : $forward;
        if($_config['passport_discuz_passportkey']){
            //整合DZ论坛的passport API(用户同步)
            require_once(MUDDER_ROOT.'include/passport.func.php');
            $myinfo = $db->get_one("SELECT username,email,password FROM {$dbpre}members WHERE uid='{$user->uid}'");
            $member = array (
                'time'       => $timestamp,
                'username'   => $myinfo['username'],
                'password'   => $myinfo['password'],
                'email'      => $myinfo['email'],
            );
            $auth = passport_encrypt(passport_encode($member), $_config['passport_discuz_passportkey']);
            $action = 'logout';
            $verify = md5($action.$auth.$forward.$_config['passport_discuz_passportkey']);
            $url = $_config['siteurl'].$_config['passport_discuz_passportpath'].
                '?action=' .$action.
                '&auth=' .rawurlencode($auth).
                '&forward=' .rawurlencode($forward).
                '&verify=' .$verify;
            location($url, false);
        } else {
            $urls = array(
                array('name' => '回到原来的页面', 'url' => $forward),
                array('name' => '返回首页', 'url' => $_config['siteurl']),
                array('name' => '重新登录', 'url' => $_config['siteurl'].'member/login.php'),
            );
            redirect('您已成功退出。', $urls);
        }
    } else {
        location($_config['siteurl']);
    }
} elseif($forget == 'yes') {

    $tplfile = 'member_forget';
    $subtitle = '找回密码';

    if(check_submit('forgetsubmit')) {
        $username = isset($username) ? html_special_chars_ex($username) : '';
        $email = isset($email) ? html_special_chars_ex($email) : '';
        if(!$username) {
            redirect('请输入用户名。');
        } elseif(!$email) {
            redirect('请输入E-mail地址。');
        } elseif(isemail($email)) {

            if($ucenter_enable) {
                $data = uc_get_user($username);
                if($data[2] == $email) {
                    $rs['uid'] = $data[0];
                }
                unset($data);
            } else {
                $rs = $db->get_one("SELECT uid FROM {$dbpre}members WHERE username='$username' AND email='$email'");
            }

            if($rs) {

                $newpw = random(8);

                if($ucenter_enable) {
                    $result = uc_user_edit($username, '', $newpw, '', 1);
                    $result != 1 && redirect('密码初始化失败！');
                } else {
                    $new_md5pw = md5($newpw);
                    $result = $db->query("UPDATE {$dbpre}members SET password='$new_md5pw' WHERE uid='{$rs['uid']}'");
                    !$result && redirect('密码初始化失败！');
                }

                $subject = "[{$_VERSION[name]}]{$_config['sitename']}会员，找回密码";
                $message = "您的会员名:$username \r\n";
                $message .= "您的新密码:$newpw \r\n";
                $message .= "请登录网站({$_config['siteurl']}member/login.php)，尽快修改为您容易记住的密码。\n";
                $message = wordwrap($message, 70);
                if($_mail['use_stmp']) {
                    require_once MUDDER_ROOT.'include/mail.class.php';
                    $auth = $_mail['username'] && $_mail['password'] ? TRUE : FALSE;
                    $smtp = new smtp($_mail['stmp'], $_mail['port'], $auth, $_mail['username'], $_mail['password']);
                    $smtp->debug = false;
                    $result = @$smtp->sendmail($email, $_mail['email'], $subject, $message, 'TXT');
                } else {
                    $result = @mail($email, $subject, $message);
                }
                unset($new_md5pw, $newpw);
                redirect($result ? '新密码已经发送到您的邮箱。' : '邮件发送失败，请联系管理员。', $_config['siteurl'].'member/login.php');
            } else {
                redirect('用户不存在或E-mail错误，请返回修改。');
            }
        } else {
            redirect('E-mail不匹配，请返回修改.');
        }
    }

} elseif(check_submit('loginsubmit')) {

    if($_seccode['login'] && ($_DCOOKIE['seccode'] != formhash(strtolower($seccode),'',''))) {
        redirect('验证码错误，请返回修改。');
    }
	
	if($ucenter_enable){
		
		$life = isset($life) && is_numeric($life) && $life > 0 ? $life : 0;
        $result = $user->Login($username, $password, $life);
		!$result && redirect($user->errmsg, $user->backurl);
		redirect('登录成功！'.$result, $forward); // $result -- UC同步登入的HTML，不可删除
	}
	
    $where = " username = '$username' ";
    if(!$username) {
        redirect('请输入用户名！');
    } elseif(!$password) {
        redirect('请输入密码！');
    } else {
        $site_pw = md5($password);
        $where .= " AND password = '$site_pw' ";
        $c = $db->get_one("SELECT uid,username,email FROM {$dbpre}members WHERE $where");
        if(!$c) {
            $password = preg_replace("/^(.{".round(strlen($password) / 4)."})(.+?)(.{".round(strlen($password) / 6)."})$/s", "\\1***\\3", $password);
            // 登录日志记录
            /*
            $errorlog = html_special_chars(
                $timestamp."\t".
                ($site_email ? $site_email : stripslashes($site_username))."\t".
                ($password)."\t".
                $onlineip);
            //log_write('illegallog', $errorlog);
            */
            redirect('您输入的用户名或密码错误！');
            //$loginflag && $loginflag = htmlspecialchars(stripslashes($loginflag));
        } else {
            $site_uid = $c['uid'];
            $site_username = $c['username'];
            $site_email = $c['email'];
            $db->update("UPDATE {$dbpre}members SET logintime='$timestamp', loginip='$onlineip', logincount=logincount+1 WHERE uid='$site_uid'");
            $life = isset($life) && is_numeric($life) && $life > 0 ? $life : 0;
            $hash = formhash($site_uid, $site_username, $site_pw);
            set_Cookie("uid", $site_uid, $life);
            set_Cookie("username", $site_username, $life);
            set_Cookie("email", $site_email, $life);
            set_Cookie("hash", $hash, $life);
            set_Cookie("logintime", $timestamp, $life);
            set_Cookie("remembertime", $timestamp + $life, $life);
            if($_config['passport_discuz_enable']){
                //整合DZ论坛的passport API
                require_once(MUDDER_ROOT.'include/passport.func.php');
                $member = array (
                    'cookietime' => $timestamp + $life,
                    'time' => $timestamp,
                    'username' => $site_username,
                    'password' => $site_pw,
                    'email'  => $site_email,
                );
                $auth = passport_encrypt(passport_encode($member), $_config['passport_discuz_passportkey']);
                $action = 'login';
                $verify = md5($action . $auth . $forward . $_config['passport_discuz_passportkey']);
                $url = $_config['siteurl'] . $_config['passport_discuz_passportpath'] .
                    '?action=' . $action .
                    '&auth=' . rawurlencode($auth) .
                    '&forward=' . rawurlencode($forward) .
                    '&verify=' . $verify;
                location($url,false);
            } else {
                unset($username, $site_uid, $site_username, $site_pw, $site_email, $hash, $password);
                redirect('登录成功！', $forward);
            }
        }
    }

}


require_once MUDDER_ROOT.'header.php';
require_once template($tplfile);
footer();
?>
