<?php
/**
 * 密碼重設
 * 
 * @version        $Id: resetpassword.php 1 8:38 2010年7月9日Z tianya $
 * @package        DedeCMS.Member
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/config.php");
require_once(DEDEMEMBER."/inc/inc_pwd_functions.php");
if(empty($dopost)) $dopost = "";

if($dopost == "")
{
    include(dirname(__FILE__)."/templets/resetpassword.htm");
}
elseif($dopost == "getpwd")
{

    //驗證驗證碼
    if(!isset($vdcode)) $vdcode = '';

    $svali = GetCkVdValue();
    if(strtolower($vdcode) != $svali || $svali=='')
    {
        ResetVdValue();
        ShowMsg("對不起，驗證碼輸入錯誤！","-1");
        exit();
    }

    //驗證郵箱，用戶名
    if(empty($mail) && empty($userid))
    {
        showmsg('對不起，請輸入用戶名或郵箱', '-1');
        exit;
    } else if (!preg_match("#(.*)@(.*)\.(.*)#", $mail))
    {
        showmsg('對不起，請輸入正確的郵箱格式', '-1');
        exit;
    } else if (CheckUserID($userid, '', false) != 'ok')
    {
        ShowMsg("你輸入的用戶名 {$userid} 不合法！","-1");
        exit();
    }
    $member = member($mail, $userid);

    //以郵件方式取回密碼；
    if($type == 1)
    {
        //判斷系統郵件服務是否開啟
        if($cfg_sendmail_bysmtp == "Y")
        {
            sn($member['mid'],$userid,$member['email']);
        }else
        {
            showmsg('對不起郵件服務暫未開啟，請聯繫管理員', 'login.php');
            exit();
        }

        //以安全問題取回密碼；
    } else if ($type == 2)
    {
        if($member['safequestion'] == 0)
        {
            showmsg('對不起您尚未設置安全密碼，請通過郵件方式重設密碼', 'login.php');
            exit;
        }
        require_once(dirname(__FILE__)."/templets/resetpassword3.htm");
    }
    exit();
}
else if($dopost == "safequestion")
{
    $mid = preg_replace("#[^0-9]#", "", $id);
    $sql = "SELECT safequestion,safeanswer,userid,email FROM #@__member WHERE mid = '$mid'";
    $row = $db->GetOne($sql);
    if(empty($safequestion)) $safequestion = '';

    if(empty($safeanswer)) $safeanswer = '';

    if($row['safequestion'] == $safequestion && $row['safeanswer'] == $safeanswer)
    {
        sn($mid, $row['userid'], $row['email'], 'N');
        exit();
    }
    else
    {
        ShowMsg("對不起，您的安全問題或答案回答錯誤","-1");
        exit();
    }

}
else if($dopost == "getpasswd")
{
    //修改密碼
    if(empty($id))
    {
        ShowMsg("對不起，請不要非法提交","login.php");
        exit();
    }
    $mid = preg_replace("#[^0-9]#", "", $id);
    $row = $db->GetOne("SELECT * FROM #@__pwd_tmp WHERE mid = '$mid'");
    if(empty($row))
    {
        ShowMsg("對不起，請不要非法提交","login.php");
        exit();
    }
    if(empty($setp))
    {
        $tptim= (60*60*24*3);
        $dtime = time();
        if($dtime - $tptim > $row['mailtime'])
        {
            $db->executenonequery("DELETE FROM `#@__pwd_tmp` WHERE `md` = '$id';");
            ShowMsg("對不起，臨時密碼修改期限已過期","login.php");
            exit();
        }
        require_once(dirname(__FILE__)."/templets/resetpassword2.htm");
    }
    elseif($setp == 2)
    {
        if(isset($key)) $pwdtmp = $key;

        $sn = md5(trim($pwdtmp));
        if($row['pwd'] == $sn)
        {
            if($pwd != "")
            {
                if($pwd == $pwdok)
                {
                    $pwdok = md5($pwdok);
                    $sql = "DELETE FROM `#@__pwd_tmp` WHERE `mid` = '$id';";
                    $db->executenonequery($sql);
                    $sql = "UPDATE `#@__member` SET `pwd` = '$pwdok' WHERE `mid` = '$id';";
                    if($db->executenonequery($sql))
                    {
                        showmsg('更改密碼成功，請牢記新密碼', 'login.php');
                        exit;
                    }
                }
            }
            showmsg('對不起，新密碼為空或填寫不一致', '-1');
            exit;
        }
        showmsg('對不起，臨時密碼錯誤', '-1');
        exit;
    }
}