<?php
$user['id'] > 0 && yun_error(mlang('不允许重复注册'));
$conf['sys_reg'] == 'off' && yun_error(mlang('注册已关闭'));
if (IS_POST)
{
    $arr2 = array();
    session_start();
    str_is_int($_POST['name']) && yun_error(mlang('用户名不能全为数字'),
        'yun_onfocus("name");');
    ($t = strlen($_POST['name'])) < 3 && yun_error(mlang('用户名不能少于3位'),
        'yun_onfocus("name");');
    $t > 12 && yun_error(mlang('用户名不能大于12位或4个汉字'), 'yun_onfocus("name");');
    strlen($_POST['pass']) < 5 && yun_error(mlang('密码不能少于5位'),
        'yun_onfocus("pass");');
    $_POST['pass'] == $_POST['pass2'] or yun_error(mlang('密码不相同'),
        'yun_onfocus("pass2");');
    $pass = $arr2['pass'] = md5(md5($_POST['pass']));
    ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,3}$", $_POST['email']) or
        yun_error(mlang('邮箱格式不正确'), 'yun_onfocus("email");');
    $_SESSION['code'] == $_POST['captcha'] or yun_error(mlang('验证码不符'),
        'yun_onfocus("captcha");recaptcha();');
    $arr2['team'] = $conf['sys_regteam'];

    $result = $db->query("select * from " . PF . "user where name='" . addslashes($_POST['name']) .
        '\'');
    $db->num_rows($result) && yun_error(mlang('用户已存在'), 'yun_onfocus("name");');
    $arr2['name'] = $_POST['name'];
    $result = $db->query("select * from " . PF . "user where email='" . addslashes($_POST['email']) .
        "'");
    $db->num_rows($result) && yun_error(mlang('邮箱已存在'), 'yun_onfocus("email");');
    $arr2['email'] = $_POST['email'];
    $arr2['posts'] = 0;
    $arr2['sex'] = $_POST['sex'] ? 1 : 0;
    $arr2['reg_time'] = TIME;

    if ($conf['sys_reg'] == 'email')
    {

        $uarr = $conf['sys_email_regs'];
        foreach ($uarr as $k => $v)
        {
            if ((TIME - $v['reg_time']) > 24 * 3600)
            {
                unset($uarr[$k]);
                continue;
            }
            if ($v['name'] == $arr2['name'])
                yun_error('用户已存在', 'yun_onfocus("name");');
            if ($v['email'] == $arr2['email'])
                yun_error('邮箱已存在', 'yun_onfocus("email");');
        }
        $key = encrypt(serialize($arr2));
        $link = $conf['sys_host'] . ROOTPATH . '?mod=user&action=yz&tokey=' . urlencode($key);
        $title = xlang($conf['sys_webname']) . ',会员激活';
        $body = '<p>您好,' . html($arr2['name']) .
            ':<br /><br />请在24小时内,点击链接 <a href="http://' . $link . '" target="_blank">' .
            html('http://' . $link) . '</a> 进行账号激活.</p>';
        ob_start();
        $msg = yun_mail($arr2['email'], $title, $body, $arr2['name']);
        ob_end_clean();
        if ($msg !== true)
            yun_error($msg);
        $_SESSION['code'] = randstr(10);
        $uarr[] = $arr2;
        set_conf('sys_email_regs', $uarr);
        yun_succeed(mlang('邮件已发送,请查收邮件,完成注册'), 'winclose');
    }
    $sql = sql_insert($arr2);
    $_SESSION['code'] = randstr(10);
    if ($db->query("insert into " . PF . "user $sql"))
    {
        setcookie(PF . 'yun_user', encrypt($db->insert_id() . "\t" . $_POST['name']), $_POST['temp'] ?
            0 : (TIME + 30 * 24 * 3600), ROOTPATH);
        yun_succeed(mlang('注册成功'), url(RPG . 'user/'));
    } else
    {
        yun_error(mlang('注册失败'));
    }
}
?>