bibis/public/register/index.php

59 行
1.9 KiB
PHP

<?php
require_once(__DIR__ . '/../../require.php');
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
do_get();
}
elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
do_post();
}
function do_get() {
if (!can_regist()) { return register_stop('新規登録は停止中。'); }
$users = load_users();
if (sizeof($users) >= USER_LIMIT) { return register_stop('ユーザー数の上限を超過(' . USER_LIMIT . '人まで)。'); }
$users = null;
output_html([], ['header.php', 'register.php']);
}
function register_stop($reason) {
output_html(compact('reason'), ['header.php', 'register-stop.php']);
}
function do_post() {
if (!has_cookie()) { return on_error(400, ['Cookie が無効']); }
if (!can_regist()) { return on_error(400, ['新規投稿は停止中。']); }
$errors = check_csrf_token(true); // true: force cookie
if ($errors) { return on_error(400, $errors); }
$id = mbtrim($_POST['id'] ?? '');
$username = mbtrim($_POST['username'] ?? '');
$password = mbtrim($_POST['password'] ?? '');
$password2 = mbtrim($_POST['password2'] ?? '');
$error_id = validate_register_id($id);
$error_username = validate_register_username($username);
if (!$error_username) {
$username = sanitize_register_username($username);
$error_username = validate_register_username($username);
}
$error_password = validate_register_password($password, $password2);
$errors = array_merge($error_id, $error_username, $error_password);
if ($errors) { return on_error(400, $errors); }
$users = load_users();
if (sizeof($users) >= USER_LIMIT) { return on_error(500, ['ユーザー数の上限を超過。']); }
$errors = add_user(compact('id', 'username', 'password'));
if ($errors) { return on_error(500, $errors); }
$username_raw = $username;
$_SESSION['user'] = compact('id', 'username', 'username_raw');
$_SESSION['messages'] = ['登録完了。よろしくね。'];
http_response_code(301);
header('Location: ' . sitebase());
}