<?php
if (IS_POST)
{
    session_start();
    $_POST['name'] == '' && yun_error(mlang('请填写用户名'), 'yun_onfocus("name");');
    $_POST['pass'] == '' && yun_error(mlang('请填写密码'), 'yun_onfocus("pass");');
    $captcha = strtolower($_POST['captcha']);
    strlen($captcha) < 1 && yun_error(mlang('请填写验证码'), 'yun_onfocus("captcha");');
    $_SESSION['code'] != $captcha && yun_error(mlang('验证码不符'),
        'yun_onfocus("captcha");recaptcha();');
    str_is_int($_POST['name']) && yun_error(mlang('错误的用户名'), 'yun_onfocus("name");');
    $u = get_user($_POST['name']);
    if ($u['id'] < 1)
        yun_error(mlang('用户不存在'), 'yun_onfocus("name");');
    strlen($_POST['pass']) < 5 && yun_error(mlang('密码不能少于5位'),
        'yun_onfocus("pass");');
    $pass = md5(md5($_POST['pass']));
    $key = encrypt($u['id'] . "\t" . TIME . "\t" . $pass . "\t" . $u['id']);
    $link = $conf['sys_host'] . ROOTPATH . '?mod=user&action=pass&tokey=' .
        urlencode($key);
    $title = xlang($conf['sys_webname']) . ',密码找回';
    $body = '<p>您好,' . html($u['name']) .
        ':<br /><br />请在24小时内,点击链接 <a href="http://' . $link . '" target="_blank">' .
        html('http://' . $link) . '</a> 更换新密码.</p>';
    ob_start();
    $msg = yun_mail($u['email'], $title, $body, $u['name']);
    ob_end_clean();
    if ($msg !== true)
        yun_error($msg);
    $_SESSION['code'] = randstr(10);
    yun_succeed(mlang('发送成功,请查收邮件'));
}

if (isset($_GET['tokey']))
{
    $k = explode("\t", decrypt($_GET['tokey']));
    $k[0] = intval($k[0]);
    $k[1] = intval($k[1]);
    $k[3] = intval($k[3]);
    $pass = $k[2];
    $k[0] == $k[3] or yun_error(mlang('无效的激活码'), ROOTPATH);
    (TIME - 24 * 3600) > $k[1] && yun_error(mlang('该链接已过期'), ROOTPATH);
    $u = get_user($k[0]);
    if ($u['id'] < 1)
        yun_error(mlang('用户不存在'), ROOTPATH);

    if ($db->query("update " . PF . "user set pass='" . addslashes($pass) .
        "' where id='$u[id]'"))
    {
        yun_succeed(mlang('密码修改成功'), ROOTPATH);
    } else
    {
        yun_error(mlang('密码修改失败'), ROOTPATH);
    }
}
?>