bibis/public/setting/index.php

77 行
2.1 KiB
PHP

<?php
require_once(__DIR__ . '/../../require.php');
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
do_get();
}
else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
do_post();
}
function do_get() {
if (!isset($_SESSION['user']['id'])) { return on_error('403', ['ログインが必要です。']); }
$profile = load_profile($_SESSION['user']['id']) ?? [];
$view['bio'] = htmlspecialchars($profile['bio'] ?? '');
output_html($view, ['header.php', 'setting.php']);
}
function do_post() {
if (!isset($_SESSION['user']['id'])) { return on_error('403', ['ログインが必要です。']); }
$username = mbtrim($_POST['username'] ?? '');
$bio = mbtrim($_POST['bio'] ?? '');
$password = mbtrim($_POST['password'] ?? '');
$password2 = mbtrim($_POST['password2'] ?? '');
$tripkey = $_POST['tripkey'] ?? '';
$notrip = $_POST['notrip'] ?? '';
$error_username = validate_register_username($username);
if (!$error_username) {
$username = sanitize_register_username($username);
$error_username = validate_register_username($username);
}
$error_bio = [];
if ($bio > '') {
$error_bio = validate_bio($bio);
if (!$error_bio) {
$bio = sanitize_bio($bio);
$error_bio = validate_bio($bio);
}
}
$error_password = [];
if ($password > '') {
$error_password = validate_register_password($password, $password2);
}
$errors = array_merge($error_username, $error_bio, $error_password);
if ($errors) { return on_error(400, $errors); }
$trip = '';
if (!$notrip) {
$trip = $_SESSION['user']['trip'];
}
if ($tripkey > '') {
// $notrip == 1 かつ $tripkey > '' ならトリップを變更できる
$trip = twochan_trip($tripkey);
}
$id = $_SESSION['user']['id'];
$errors = update_user(compact('id', 'username', 'password', 'trip'));
if ($errors) { return on_error(500, $errors); }
$username_raw = $username;
$username = $username . $trip;
$_SESSION['user'] = compact('id', 'username', 'username_raw', 'trip');
$_SESSION['messages'] = ['設定完了。'];
// ignore error (because it is rarecase)
save_profile($id, ['bio' => $bio ]);
http_response_code(301);
header('Location: ' . sitebase('user/?id=' . $id));
}