misuzu/public/settings.php

183 lines
7.2 KiB
PHP
Raw Normal View History

<?php
require_once '../misuzu.php';
if (!user_session_active()) {
echo render_error(401);
return;
}
2018-10-29 19:12:06 +00:00
$errors = [];
2018-10-29 19:12:06 +00:00
$currentEmail = user_email_get(user_session_current('user_id'));
$isRestricted = user_warning_check_restriction(user_session_current('user_id'));
2018-03-24 04:31:42 +00:00
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2018-10-02 19:16:42 +00:00
if (!csrf_verify('settings', $_POST['csrf'] ?? '')) {
2018-10-29 19:12:06 +00:00
$errors[] = MSZ_TMP_USER_ERROR_STRINGS['csrf'];
} else {
2018-10-29 17:55:10 +00:00
if (!empty($_POST['session'])) {
$currentSessionKilled = false;
if (is_array($_POST['session'])) {
foreach ($_POST['session'] as $sessionId) {
$sessionId = intval($sessionId);
$session = user_session_find($sessionId);
2018-08-15 13:36:40 +00:00
2018-10-29 19:12:06 +00:00
if (!$session || (int)$session['user_id'] !== user_session_current('user_id')) {
$errors[] = "Session #{$sessionId} does not exist.";
2018-10-29 17:55:10 +00:00
break;
} elseif ((int)$session['session_id'] === user_session_current('session_id')) {
$currentSessionKilled = true;
}
user_session_delete($session['session_id']);
audit_log(MSZ_AUDIT_PERSONAL_SESSION_DESTROY, user_session_current('user_id'), [
2018-10-29 17:55:10 +00:00
$session['session_id'],
]);
}
} elseif ($_POST['session'] === 'all') {
$currentSessionKilled = true;
2018-10-29 19:12:06 +00:00
user_session_purge_all(user_session_current('user_id'));
audit_log(MSZ_AUDIT_PERSONAL_SESSION_DESTROY_ALL, user_session_current('user_id'));
2018-10-29 17:55:10 +00:00
}
2018-10-29 17:55:10 +00:00
if ($currentSessionKilled) {
2019-02-12 14:12:58 +00:00
header(sprintf('Location: %s', url('index')));
2018-09-27 22:03:43 +00:00
return;
}
}
if (!empty($_POST['role']) && !$isRestricted) {
2018-11-17 20:37:18 +00:00
$roleId = (int)($_POST['role']['id'] ?? 0);
2018-10-29 19:12:06 +00:00
2018-11-17 20:37:18 +00:00
if ($roleId > 0 && user_role_has(user_session_current('user_id'), $roleId)) {
switch ($_POST['role']['mode'] ?? '') {
case 'display':
user_role_set_display(user_session_current('user_id'), $roleId);
break;
case 'leave':
if (user_role_can_leave($roleId)) {
user_role_remove(user_session_current('user_id'), $roleId);
} else {
$errors[] = "You're not allow to leave this role, an administrator has to remove it for you.";
}
break;
}
} else {
$errors[] = "You're trying to modify a role that hasn't been assigned to you.";
}
}
if (!empty($_POST['current_password'])) {
2018-10-29 19:12:06 +00:00
if (!user_password_verify_db(user_session_current('user_id'), $_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 ($currentEmail === mb_strtolower($_POST['email']['confirm'])) {
$errors[] = 'This is already your e-mail address!';
} else {
2018-10-29 19:12:06 +00:00
$checkMail = user_validate_email($_POST['email']['new'], true);
2018-10-29 19:12:06 +00:00
if ($checkMail !== '') {
switch ($checkMail) {
case 'dns':
$errors[] = 'No valid MX record exists for this domain.';
break;
2018-10-29 19:12:06 +00:00
case 'format':
$errors[] = 'The given e-mail address was incorrectly formatted.';
break;
2018-10-29 19:12:06 +00:00
case 'in-use':
$errors[] = 'This e-mail address is already in use.';
break;
2018-10-29 19:12:06 +00:00
default:
$errors[] = 'Unknown e-mail validation error.';
}
2018-10-29 19:12:06 +00:00
} else {
user_email_set(user_session_current('user_id'), $_POST['email']['new']);
audit_log(MSZ_AUDIT_PERSONAL_EMAIL_CHANGE, user_session_current('user_id'), [
2018-10-29 19:12:06 +00:00
$_POST['email']['new'],
]);
}
2018-10-29 19:12:06 +00:00
}
}
2018-10-29 19:12:06 +00:00
// 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_validate_password($_POST['password']['new']);
2018-10-29 19:12:06 +00:00
if ($checkPassword !== '') {
$errors[] = 'The given passwords was too weak.';
} else {
user_password_set(user_session_current('user_id'), $_POST['password']['new']);
audit_log(MSZ_AUDIT_PERSONAL_PASSWORD_CHANGE, user_session_current('user_id'));
}
}
}
}
}
}
}
2018-10-29 19:12:06 +00:00
$sessions = [
'list' => [],
'active' => user_session_current('session_id'),
2019-01-03 00:33:02 +00:00
'pagination' => pagination_create(user_session_count(user_session_current('user_id')), 15),
2018-10-29 19:12:06 +00:00
];
$logins = [
'list' => [],
2019-01-03 00:33:02 +00:00
'pagination' => pagination_create(user_login_attempts_count(user_session_current('user_id')), 15),
2018-10-29 19:12:06 +00:00
];
$logs = [
'list' => [],
2019-01-03 00:33:02 +00:00
'pagination' => pagination_create(audit_log_count(user_session_current('user_id')), 15),
2018-12-15 18:14:23 +00:00
'strings' => MSZ_AUDIT_LOG_STRINGS,
2018-10-29 19:12:06 +00:00
];
2019-01-03 00:33:02 +00:00
foreach (['sessions', 'logins', 'logs'] as $section) {
if (!pagination_is_valid_offset(pagination_offset(($$section)['pagination'], pagination_param("{$section}_page")))) {
($$section)['pagination']['offset'] = 0;
($$section)['pagination']['page'] = 1;
}
}
$sessions['list'] = user_session_list(
$sessions['pagination']['offset'],
$sessions['pagination']['range'],
user_session_current('user_id')
);
$logins['list'] = user_login_attempts_list(
$logins['pagination']['offset'],
$logins['pagination']['range'],
user_session_current('user_id')
);
$logs['list'] = audit_log_list(
$logs['pagination']['offset'],
$logs['pagination']['range'],
user_session_current('user_id')
);
2018-10-29 19:12:06 +00:00
$userRoles = user_role_all_user(user_session_current('user_id'));
2018-11-16 17:54:56 +00:00
2018-10-29 19:12:06 +00:00
echo tpl_render('user.settings', [
'errors' => $errors,
'current_email' => $currentEmail,
'sessions' => $sessions,
'logins' => $logins,
'logs' => $logs,
2018-11-17 20:37:18 +00:00
'user_roles' => $userRoles,
'user_display_role' => user_role_get_display(user_session_current('user_id')),
'is_restricted' => $isRestricted,
2018-10-29 19:12:06 +00:00
]);