misuzu/public/auth.php

197 lines
6.5 KiB
PHP
Raw Normal View History

<?php
2018-02-11 00:18:49 +01:00
use Carbon\Carbon;
2018-05-16 04:58:21 +02:00
use Misuzu\Database;
2018-03-16 03:01:24 +01:00
use Misuzu\Net\IPAddress;
2018-02-11 00:18:49 +01:00
use Misuzu\Users\Session;
require_once __DIR__ . '/../misuzu.php';
$config = $app->getConfig();
$templating = $app->getTemplating();
2018-05-27 02:20:35 +02:00
$usernameValidationErrors = [
'trim' => 'Your username may not start or end with spaces!',
2018-05-27 02:20:35 +02:00
'short' => sprintf('Your username is too short, it has to be at least %d characters!', MSZ_USERNAME_MIN_LENGTH),
'long' => sprintf("Your username is too long, it can't be longer than %d characters!", MSZ_USERNAME_MAX_LENGTH),
'double-spaces' => "Your username can't contain double spaces.",
'invalid' => 'Your username contains invalid characters.',
'spacing' => 'Please use either underscores or spaces, not both!',
'in-use' => 'This username is already taken!',
];
2018-05-27 02:20:35 +02:00
$authMode = $_GET['m'] ?? 'login';
$preventRegistration = $config->get('Auth', 'prevent_registration', 'bool', false);
$templating->addPath('auth', __DIR__ . '/../views/auth');
2018-03-22 02:56:41 +00:00
2018-05-27 02:20:35 +02:00
$templating->vars([
'prevent_registration' => $preventRegistration,
'auth_mode' => $authMode,
'auth_username' => $_REQUEST['username'] ?? '',
'auth_email' => $_REQUEST['email'] ?? '',
]);
2018-05-27 02:20:35 +02:00
switch ($authMode) {
case 'logout':
2018-05-16 04:58:21 +02:00
if (!$app->hasActiveSession()) {
2018-03-22 02:56:41 +00:00
header('Location: /');
return;
}
2018-03-22 19:07:02 +01:00
if (isset($_GET['s']) && tmp_csrf_verify($_GET['s'])) {
set_cookie_m('uid', '', -3600);
set_cookie_m('sid', '', -3600);
2018-05-27 02:20:35 +02:00
user_session_delete($app->getSessionId());
2018-03-22 02:56:41 +00:00
header('Location: /');
return;
}
echo $templating->render('@auth.logout');
break;
case 'login':
2018-05-16 04:58:21 +02:00
if ($app->hasActiveSession()) {
2018-03-22 02:56:41 +00:00
header('Location: /');
break;
}
2018-05-27 02:20:35 +02:00
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$authLoginError = '';
2018-03-22 02:56:41 +00:00
while ($_SERVER['REQUEST_METHOD'] === 'POST') {
2018-05-20 22:12:45 +02:00
$ipAddress = IPAddress::remote()->getString();
2018-03-22 04:45:59 +01:00
2018-03-22 02:56:41 +00:00
if (!isset($_POST['username'], $_POST['password'])) {
2018-05-27 02:20:35 +02:00
$authLoginError = "You didn't fill all the forms!";
2018-03-22 02:56:41 +00:00
break;
}
2018-05-27 02:20:35 +02:00
$remainingAttempts = user_login_attempts_remaining($ipAddress);
2018-05-16 04:58:21 +02:00
if ($remainingAttempts < 1) {
2018-05-27 02:20:35 +02:00
$authLoginError = 'Too many failed login attempts, try again later.';
2018-03-22 04:45:59 +01:00
break;
}
2018-03-22 02:56:41 +00:00
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$getUser = Database::prepare('
2018-05-16 04:58:21 +02:00
SELECT `user_id`, `password`
FROM `msz_users`
WHERE LOWER(`email`) = LOWER(:email)
OR LOWER(`username`) = LOWER(:username)
');
$getUser->bindValue('email', $username);
$getUser->bindValue('username', $username);
$userData = $getUser->execute() ? $getUser->fetch() : [];
$userId = (int)($userData['user_id'] ?? 0);
2018-05-27 02:20:35 +02:00
$loginFailedError = sprintf(
"Invalid username or password, %d attempt%s remaining.",
$remainingAttempts - 1,
$remainingAttempts === 2 ? '' : 's'
);
2018-05-16 04:58:21 +02:00
if ($userId < 1) {
2018-05-27 02:20:35 +02:00
user_login_attempt_record(false, null, $ipAddress, $userAgent);
$authLoginError = $loginFailedError;
2018-03-22 02:56:41 +00:00
break;
}
2018-05-16 04:58:21 +02:00
if (!password_verify($password, $userData['password'])) {
2018-05-27 02:20:35 +02:00
user_login_attempt_record(false, $userId, $ipAddress, $userAgent);
$authLoginError = $loginFailedError;
2018-03-22 02:56:41 +00:00
break;
}
2018-02-11 00:18:49 +01:00
2018-05-27 02:20:35 +02:00
user_login_attempt_record(true, $userId, $ipAddress, $userAgent);
$sessionKey = user_session_create($userId, $ipAddress, $userAgent);
if ($sessionKey === '') {
$authLoginError = 'Unable to create new session, contact an administrator ASAP.';
2018-05-16 04:58:21 +02:00
break;
}
2018-03-22 04:45:59 +01:00
2018-05-16 04:58:21 +02:00
$app->startSession($userId, $sessionKey);
$cookieLife = Carbon::now()->addMonth()->timestamp;
set_cookie_m('uid', $userId, $cookieLife);
set_cookie_m('sid', $sessionKey, $cookieLife);
2018-03-22 02:56:41 +00:00
header('Location: /');
return;
}
2018-05-27 02:20:35 +02:00
if (!empty($authLoginError)) {
$templating->var('auth_login_error', $authLoginError);
2018-03-22 02:56:41 +00:00
}
echo $templating->render('auth');
break;
2018-01-28 04:32:28 +01:00
case 'register':
2018-05-16 04:58:21 +02:00
if ($app->hasActiveSession()) {
2018-03-22 02:56:41 +00:00
header('Location: /');
}
2018-05-27 02:20:35 +02:00
$authRegistrationError = '';
2018-03-22 02:56:41 +00:00
while ($_SERVER['REQUEST_METHOD'] === 'POST') {
2018-05-27 02:20:35 +02:00
if ($preventRegistration) {
$authRegistrationError = 'Registration is not allowed on this instance.';
2018-03-22 02:56:41 +00:00
break;
}
2018-03-22 02:56:41 +00:00
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) {
2018-05-27 02:20:35 +02:00
$authRegistrationError = "You didn't fill all the forms!";
2018-03-22 02:56:41 +00:00
break;
}
2018-03-22 02:56:41 +00:00
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$email = $_POST['email'] ?? '';
2018-05-27 02:20:35 +02:00
$usernameValidation = user_validate_username($username, true);
if ($usernameValidation !== '') {
$authRegistrationError = $usernameValidationErrors[$usernameValidation];
2018-03-22 02:56:41 +00:00
break;
}
2018-05-27 02:20:35 +02:00
$emailValidation = user_validate_email($email, true);
if ($emailValidation !== '') {
$authRegistrationError = $emailValidation === 'in-use'
? 'This e-mail address has already been used!'
: 'The e-mail address you entered is invalid!';
break;
}
2018-05-16 04:58:21 +02:00
if (user_validate_password($password) !== '') {
2018-05-27 02:20:35 +02:00
$authRegistrationError = 'Your password is too weak!';
break;
}
2018-05-27 02:20:35 +02:00
$createUser = user_create(
$username,
$password,
$email,
IPAddress::remote()->getString()
);
if ($createUser < 1) {
$authRegistrationError = 'Something happened?';
2018-05-16 04:58:21 +02:00
break;
}
2018-05-27 02:20:35 +02:00
user_role_add($createUser, MSZ_ROLE_MAIN);
2018-05-16 04:58:21 +02:00
$templating->var('auth_register_message', 'Welcome to Flashii! You may now log in.');
break;
}
2018-05-27 02:20:35 +02:00
if (!empty($authRegistrationError)) {
$templating->var('auth_register_error', $authRegistrationError);
2018-03-22 02:56:41 +00:00
}
echo $templating->render('@auth.auth');
break;
}