misuzu/public-legacy/settings/account.php

147 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use RuntimeException;
2022-09-13 13:14:49 +00:00
use Misuzu\Users\User;
use Misuzu\Users\UserSession;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
if(!UserSession::hasCurrent()) {
echo render_error(401);
return;
}
$errors = [];
2023-07-27 23:26:05 +00:00
$users = $msz->getUsers();
$roles = $msz->getRoles();
2022-09-13 13:14:49 +00:00
$currentUser = User::getCurrent();
$currentUserId = $currentUser->getId();
$isRestricted = $msz->hasActiveBan();
2022-09-13 13:14:49 +00:00
$isVerifiedRequest = CSRF::validateRequest();
if(!$isRestricted && $isVerifiedRequest && !empty($_POST['role'])) {
try {
2023-07-27 23:26:05 +00:00
$roleInfo = $roles->getRole(($_POST['role']['id'] ?? 0));
} catch(RuntimeException $ex) {}
2022-09-13 13:14:49 +00:00
2023-07-27 23:26:05 +00:00
if(empty($roleInfo) || !$users->hasRole($userInfo, $roleInfo))
2022-09-13 13:14:49 +00:00
$errors[] = "You're trying to modify a role that hasn't been assigned to you.";
else {
switch($_POST['role']['mode'] ?? '') {
case 'display':
2023-07-27 23:26:05 +00:00
$users->updateUser(
$currentUser,
displayRoleInfo: $roleInfo
);
2022-09-13 13:14:49 +00:00
break;
case 'leave':
2023-07-27 23:26:05 +00:00
if($roleInfo->isLeavable())
$users->removeRoles($currentUser, $roleInfo);
2022-09-13 13:14:49 +00:00
else
$errors[] = "You're not allow to leave this role, an administrator has to remove it for you.";
break;
}
}
}
if($isVerifiedRequest && isset($_POST['tfa']['enable']) && $currentUser->hasTOTP() !== (bool)$_POST['tfa']['enable']) {
if((bool)$_POST['tfa']['enable']) {
$tfaKey = TOTP::generateKey();
2023-07-18 21:48:44 +00:00
$tfaIssuer = $cfg->getString('site.name', 'Misuzu');
2022-09-13 13:14:49 +00:00
$tfaQrcode = (new QRCode(new QROptions([
'version' => 5,
'outputType' => QRCode::OUTPUT_IMAGE_JPG,
'eccLevel' => QRCode::ECC_L,
])))->render(sprintf('otpauth://totp/%s:%s?%s', $tfaIssuer, $currentUser->getUsername(), http_build_query([
'secret' => $tfaKey,
'issuer' => $tfaIssuer,
])));
Template::set([
'settings_2fa_code' => $tfaKey,
'settings_2fa_image' => $tfaQrcode,
]);
$currentUser->setTOTPKey($tfaKey);
} else {
$currentUser->removeTOTPKey();
}
}
if($isVerifiedRequest && !empty($_POST['current_password'])) {
if(!$currentUser->checkPassword($_POST['current_password'] ?? '')) {
$errors[] = 'Your password was incorrect.';
} else {
// Changing e-mail
if(!empty($_POST['email']['new'])) {
if(empty($_POST['email']['confirm']) || $_POST['email']['new'] !== $_POST['email']['confirm']) {
$errors[] = 'The addresses you entered did not match each other.';
} elseif($currentUser->getEMailAddress() === mb_strtolower($_POST['email']['confirm'])) {
$errors[] = 'This is already your e-mail address!';
} else {
$checkMail = User::validateEMailAddress($_POST['email']['new'], true);
if($checkMail !== '') {
switch($checkMail) {
case 'dns':
$errors[] = 'No valid MX record exists for this domain.';
break;
case 'format':
$errors[] = 'The given e-mail address was incorrectly formatted.';
break;
case 'in-use':
$errors[] = 'This e-mail address is already in use.';
break;
default:
$errors[] = 'Unknown e-mail validation error.';
}
} else {
$currentUser->setEMailAddress($_POST['email']['new']);
$msz->createAuditLog('PERSONAL_EMAIL_CHANGE', [
2022-09-13 13:14:49 +00:00
$_POST['email']['new'],
]);
}
}
}
// Changing password
if(!empty($_POST['password']['new'])) {
if(empty($_POST['password']['confirm']) || $_POST['password']['new'] !== $_POST['password']['confirm']) {
$errors[] = 'The new passwords you entered did not match each other.';
} else {
$checkPassword = User::validatePassword($_POST['password']['new']);
if($checkPassword !== '') {
$errors[] = 'The given passwords was too weak.';
} else {
$currentUser->setPassword($_POST['password']['new']);
$msz->createAuditLog('PERSONAL_PASSWORD_CHANGE');
2022-09-13 13:14:49 +00:00
}
}
}
}
}
// THIS FUCKING SUCKS AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
2023-07-27 23:26:05 +00:00
if($_SERVER['REQUEST_METHOD'] === 'POST' && $isVerifiedRequest) {
2022-09-13 13:14:49 +00:00
$currentUser->save();
2023-07-27 23:26:05 +00:00
// force a page refresh for now to deal with the User object and new shit desyncing
url_redirect('settings-account');
return;
}
$userRoles = $roles->getRoles(userInfo: $currentUser);
2022-09-13 13:14:49 +00:00
Template::render('settings.account', [
'errors' => $errors,
'settings_user' => $currentUser,
2023-07-27 23:26:05 +00:00
'settings_roles' => $userRoles,
2022-09-13 13:14:49 +00:00
'is_restricted' => $isRestricted,
]);