<commit_before><?php
    function handleRegisterPost() {
        if (!crsf_check()) {
            return '页面已过期';
        }
        if (!isset($_POST['username'])) {
            return "无效表单";
        }
        if (!isset($_POST['password'])) {
            return "无效表单";
        }
        if (!isset($_POST['email'])) {
            return "无效表单";
        }
        if(!isset($_POST['verify-code'])){
            return "无效表单";
        }

        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $verifyCode = $_POST['verify-code'];
        if (!validateUsername($username)) {
            return "失败：无效用户名。";
        }
        if (queryUser($username)) {
            return "失败：用户名已存在。";
        }
        if (!validatePassword($password)) {
            return "失败：无效密码。";
        }
        if (!validateEmail($email)) {
            return "失败：无效电子邮箱。";
        }
        if(!validateVerifyCode($verifyCode)) {
            return "失败：验证码错误";
        }

        $password = getPasswordToStore($password, $username);

        $esc_email = DB::escape($email);

        $svn_pw = uojRandString(10);
        if (!DB::selectCount("SELECT COUNT(*) FROM user_info"))
            mysql_query("insert into user_info (username, email, password, svn_password, register_time, usergroup) values ('$username', '$esc_email', '$password', '$svn_pw', now(), 'S')");
        else
            mysql_query("insert into user_info (username, email, password, svn_password, register_time) values ('$username', '$esc_email', '$password', '$svn_pw', now())");

        return "欢迎你！" . $username . "，你已成功注册。";
    }

    if (isset($_POST['register'])) {
        echo handleRegisterPost();
        die();
    } elseif (isset($_POST['check_username'])) {
        $username = $_POST['username'];
        if (validateUsername($username) && !queryUser($username)) {
            echo '{"ok" : true}';
        } else {
            echo '{"ok" : false}';
        }
        die();
    }elseif (isset($_POST['check_verify_code'])) {
        $verifyCode = $_POST['verify-code'];
        if(validateVerifyCode($verifyCode)){
            echo '{"ok" : true}';
        } else {
            echo '{"ok" : false}';
        }
    }
?>
<?php
    $REQUIRE_LIB['md5'] = '';
    $REQUIRE_LIB['dialog'] = '';
?>
<?php echoUOJPageHeader(UOJLocale::get('register')) ?>
<h2 class="page-header"><?= UOJLocale::get('register') ?></h2>
<form id="form-register" class="form-horizontal">
    <div id="div-email" class="form-group">
        <label for="input-email" class="col-sm-2 control-label"><?= UOJLocale::get('email') ?></label>
        <div class="col-sm-3">
            <input type="email" class="form-control" id="input-email" name="email" placeholder="<?= UOJLocale::get('enter your email') ?>" maxlength="50" />
            <span class="help-block" id="help-email"></span>
        </div>
    </div>
    <div id="div-username" class="form-group">
        <label for="input-username" class="col-sm-2 control-label"><?= UOJLocale::get('username') ?></label>
        <div class="col-sm-3">
            <input type="text" class="form-control" id="input-username" name="username" placeholder="<?= UOJLocale::get('enter your username') ?>" maxlength="20" />
            <span class="help-block" id="help-username"></span>
        </div>
    </div>
    <div id="div-password" class="form-group">
        <label for="input-password" class="col-sm-2 control-label"><?= UOJLocale::get('password') ?></label>
        <div class="col-sm-3">
            <input type="password" class="form-control" id="input-password" name="password" placeholder="<?= UOJLocale::get('enter your password') ?>" maxlength="20" />
            <input type="password" class="form-control top-buffer-sm" id="input-confirm_password" placeholder="<?= UOJLocale::get('re-enter your password') ?>" maxlength="20" />
            <span class="help-block" id="help-password"></span>
        </div>
    </div>
    <div id="div-verify-code" class="form-group">
        <label for="input-verify-code" class="col-sm-2 control-label"><?= UOJLocale::get('verify code') ?></label>
        <div class="col-sm-3">
            <input type="password" class="form-control" id="input-verify-code" name="verify-code" placeholder="<?= UOJLocale::get('enter verify code') ?>" maxLength="30" />
            <span class="help-block" id="help-verify-code"></span>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-3">
            <button type="submit" id="button-submit" class="btn btn-default"><?= UOJLocale::get('submit') ?></button>
        </div>
    </div>
</form>

<script type="text/javascript">
function checkUsernameNotInUse() {
    var ok = false;
    $.ajax({
        url : '/register',
        type : 'POST',
        dataType : 'json',
        async : false,

        data : {
            check_username : '',
            username : $('#input-username').val()
        },
        success : function(data) {
            ok = data.ok;
        },
        error :    function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
            ok = false;
        }
    });
    return ok;
}
function checkVerifyCode() {
    var ok = false;
    $.ajax({
        url : '/register',
        type : 'POST',
        dataType : 'json',
        async : false,

        data : {
            check_verify_code : '',
            verifyCode : $('#input-verify-code').val()
        },

        success : function(data) {
            ok = data.ok;
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
            ok = false;
        }
    });
    return ok;
}
function validateRegisterPost() {
    var ok = true;
    ok &= getFormErrorAndShowHelp('email', validateEmail);
    ok &= getFormErrorAndShowHelp('username', function(str) {
        var err = validateUsername(str);
        if (err)
            return err;
        if (!checkUsernameNotInUse())
            return '该用户名已被人使用了。';
        return '';
    })
    ok &= getFormErrorAndShowHelp('password', validateSettingPassword);
    ok &= getFormErrorAndShowHelp('verify-code',function(str) {
        if(!checkVerifyCode())
            return '验证码错误';
        return '';
    })
    return ok;
}

function submitRegisterPost() {
    if (!validateRegisterPost()) {
        return;
    }

    $.post('/register', {
        _token : "<?= crsf_token() ?>",
        register : '',
        username : $('#input-username').val(),
        email        : $('#input-email').val(),
        password : md5($('#input-password').val(), "<?= getPasswordClientSalt() ?>"),
        verify-code : $('#input-verify-code').val()
    }, function(msg) {
        if (/^欢迎你！/.test(msg)) {
            BootstrapDialog.show({<commit_msg>fix verify code<commit_after><?php
    function handleRegisterPost() {
        if (!crsf_check()) {
            return '页面已过期';
        }
        if (!isset($_POST['username'])) {
            return "无效表单";
        }
        if (!isset($_POST['password'])) {
            return "无效表单";
        }
        if (!isset($_POST['email'])) {
            return "无效表单";
        }
        if(!isset($_POST['verifyCode'])){
            return "无效表单";
        }

        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $verifyCode = $_POST['verifyCode'];
        if (!validateUsername($username)) {
            return "失败：无效用户名。";
        }
        if (queryUser($username)) {
            return "失败：用户名已存在。";
        }
        if (!validatePassword($password)) {
            return "失败：无效密码。";
        }
        if (!validateEmail($email)) {
            return "失败：无效电子邮箱。";
        }
        if(!validateVerifyCode($verifyCode)) {
            return "失败：验证码错误";
        }

        $password = getPasswordToStore($password, $username);

        $esc_email = DB::escape($email);

        $svn_pw = uojRandString(10);
        if (!DB::selectCount("SELECT COUNT(*) FROM user_info"))
            mysql_query("insert into user_info (username, email, password, svn_password, register_time, usergroup) values ('$username', '$esc_email', '$password', '$svn_pw', now(), 'S')");
        else
            mysql_query("insert into user_info (username, email, password, svn_password, register_time) values ('$username', '$esc_email', '$password', '$svn_pw', now())");

        return "欢迎你！" . $username . "，你已成功注册。";
    }

    if (isset($_POST['register'])) {
        echo handleRegisterPost();
        die();
    } elseif (isset($_POST['check_username'])) {
        $username = $_POST['username'];
        if (validateUsername($username) && !queryUser($username)) {
            echo '{"ok" : true}';
        } else {
            echo '{"ok" : false}';
        }
        die();
    }elseif (isset($_POST['check_verify_code'])) {
        $verifyCode = $_POST['verifyCode'];
        if(validateVerifyCode($verifyCode)){
            echo '{"ok" : true}';
        } else {
            echo '{"ok" : false}';
        }
    }
?>
<?php
    $REQUIRE_LIB['md5'] = '';
    $REQUIRE_LIB['dialog'] = '';
?>
<?php echoUOJPageHeader(UOJLocale::get('register')) ?>
<h2 class="page-header"><?= UOJLocale::get('register') ?></h2>
<form id="form-register" class="form-horizontal">
    <div id="div-email" class="form-group">
        <label for="input-email" class="col-sm-2 control-label"><?= UOJLocale::get('email') ?></label>
        <div class="col-sm-3">
            <input type="email" class="form-control" id="input-email" name="email" placeholder="<?= UOJLocale::get('enter your email') ?>" maxlength="50" />
            <span class="help-block" id="help-email"></span>
        </div>
    </div>
    <div id="div-username" class="form-group">
        <label for="input-username" class="col-sm-2 control-label"><?= UOJLocale::get('username') ?></label>
        <div class="col-sm-3">
            <input type="text" class="form-control" id="input-username" name="username" placeholder="<?= UOJLocale::get('enter your username') ?>" maxlength="20" />
            <span class="help-block" id="help-username"></span>
        </div>
    </div>
    <div id="div-password" class="form-group">
        <label for="input-password" class="col-sm-2 control-label"><?= UOJLocale::get('password') ?></label>
        <div class="col-sm-3">
            <input type="password" class="form-control" id="input-password" name="password" placeholder="<?= UOJLocale::get('enter your password') ?>" maxlength="20" />
            <input type="password" class="form-control top-buffer-sm" id="input-confirm_password" placeholder="<?= UOJLocale::get('re-enter your password') ?>" maxlength="20" />
            <span class="help-block" id="help-password"></span>
        </div>
    </div>
    <div id="div-verify-code" class="form-group">
        <label for="input-verify-code" class="col-sm-2 control-label"><?= UOJLocale::get('verify code') ?></label>
        <div class="col-sm-3">
            <input type="password" class="form-control" id="input-verify-code" name="verify-code" placeholder="<?= UOJLocale::get('enter verify code') ?>" maxLength="30" />
            <span class="help-block" id="help-verify-code"></span>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-3">
            <button type="submit" id="button-submit" class="btn btn-default"><?= UOJLocale::get('submit') ?></button>
        </div>
    </div>
</form>

<script type="text/javascript">
function checkUsernameNotInUse() {
    var ok = false;
    $.ajax({
        url : '/register',
        type : 'POST',
        dataType : 'json',
        async : false,

        data : {
            check_username : '',
            username : $('#input-username').val()
        },
        success : function(data) {
            ok = data.ok;
        },
        error :    function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
            ok = false;
        }
    });
    return ok;
}
function checkVerifyCode() {
    var ok = false;
    $.ajax({
        url : '/register',
        type : 'POST',
        dataType : 'json',
        async : false,

        data : {
            check_verify_code : '',
            verifyCode : $('#input-verify-code').val()
        },

        success : function(data) {
            ok = data.ok;
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
            ok = false;
        }
    });
    return ok;
}
function validateRegisterPost() {
    var ok = true;
    ok &= getFormErrorAndShowHelp('email', validateEmail);
    ok &= getFormErrorAndShowHelp('username', function(str) {
        var err = validateUsername(str);
        if (err)
            return err;
        if (!checkUsernameNotInUse())
            return '该用户名已被人使用了。';
        return '';
    })
    ok &= getFormErrorAndShowHelp('password', validateSettingPassword);
    ok &= getFormErrorAndShowHelp('verify-code',function(str) {
        if(!checkVerifyCode())
            return '验证码错误';
        return '';
    })
    return ok;
}

function submitRegisterPost() {
    if (!validateRegisterPost()) {
        return;
    }

    $.post('/register', {
        _token : "<?= crsf_token() ?>",
        register : '',
        username : $('#input-username').val(),
        email        : $('#input-email').val(),
        password : <PASSWORD>($('#input-password').val(), "<?= getPasswordClientSalt() ?>"),
        verifyCode : $('#input-verify-code').val()
    }, function(msg) {
        if (/^欢迎你！/.test(msg)) {
            BootstrapDialog.show({
{'fraction_non_alphanumeric': 0.1857049371046317, 'fraction_numerical': 0.003708281829419036, 'mean_word_length': 2.391861898890259, 'pattern_counts': {'":': 0, '<': 132, '<?xml version=': 0, '>': 132, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7458841', 'n_tokens_mistral': 4177, 'n_tokens_neox': 4262, 'n_words': 781}