2015-04-06 21:23:54 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Sakura Authentication Page
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Declare Namespace
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
// Include components
|
2015-12-03 19:40:01 +00:00
|
|
|
require_once str_replace(basename(__DIR__), '', dirname(__FILE__)) . 'sakura.php';
|
2015-04-06 21:23:54 +00:00
|
|
|
|
2015-04-08 17:18:19 +00:00
|
|
|
// Page actions
|
2015-09-14 20:51:23 +00:00
|
|
|
if (isset($_REQUEST['mode'])) {
|
2015-04-17 22:51:53 +00:00
|
|
|
// Continue
|
|
|
|
$continue = true;
|
2015-04-08 17:18:19 +00:00
|
|
|
|
2015-09-14 21:41:43 +00:00
|
|
|
// Make sure we're not in activate mode since adding a timestamp
|
|
|
|
// and accessing the PHP session id is kind of hard when you're in an e-mail client
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!isset($_REQUEST['mode']) || $_REQUEST['mode'] != 'activate') {
|
2015-04-19 13:00:32 +00:00
|
|
|
// Compare time and session so we know the link isn't forged
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!isset($_REQUEST['time']) || $_REQUEST['time'] < time() - 1000) {
|
2015-04-19 13:00:32 +00:00
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('AUTH_ACTION'),
|
|
|
|
'message' => 'Timestamps differ too much, refresh the page and try again.',
|
|
|
|
'success' => 0,
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
];
|
2015-04-12 13:33:59 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
// Prevent
|
|
|
|
$continue = false;
|
|
|
|
}
|
2015-04-17 22:51:53 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
// Match session ids for the same reason
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!isset($_REQUEST['session']) || $_REQUEST['session'] != session_id()) {
|
2015-04-19 13:00:32 +00:00
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('AUTH_ACTION'),
|
|
|
|
'message' => 'Invalid session, please try again.',
|
|
|
|
'success' => 0,
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
];
|
2015-04-17 22:51:53 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
// Prevent
|
|
|
|
$continue = false;
|
|
|
|
}
|
2015-04-17 22:51:53 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 11:35:16 +00:00
|
|
|
// Login check
|
2015-09-14 20:51:23 +00:00
|
|
|
if (Users::checkLogin()) {
|
|
|
|
if (!in_array($_REQUEST['mode'], ['logout'])) {
|
2015-04-18 11:35:16 +00:00
|
|
|
$continue = false;
|
|
|
|
|
2015-04-25 20:08:44 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('SITE_HOME'),
|
|
|
|
'message' => 'You are already authenticated. Redirecting...',
|
|
|
|
'success' => 1,
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-25 20:08:44 +00:00
|
|
|
];
|
|
|
|
}
|
2015-04-18 11:35:16 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($continue) {
|
|
|
|
switch ($_REQUEST['mode']) {
|
2015-04-17 22:51:53 +00:00
|
|
|
case 'logout':
|
|
|
|
// Attempt logout
|
|
|
|
$logout = Users::logout();
|
|
|
|
|
|
|
|
// Add page specific data
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => ($logout ? $_REQUEST['redirect'] : $urls->format('SITE_LOGIN')),
|
|
|
|
'message' => $logout ? 'You are now logged out.' : 'An unknown error occurred.',
|
|
|
|
'success' => $logout ? 1 : 0,
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
2015-04-21 14:23:28 +00:00
|
|
|
case 'changepassword':
|
2015-04-25 20:08:44 +00:00
|
|
|
// Attempt change
|
2015-09-14 21:41:43 +00:00
|
|
|
$passforget = Users::resetPassword(
|
|
|
|
$_REQUEST['verk'],
|
|
|
|
$_REQUEST['uid'],
|
|
|
|
$_REQUEST['newpw'],
|
|
|
|
$_REQUEST['verpw']
|
|
|
|
);
|
2015-04-25 20:08:44 +00:00
|
|
|
|
|
|
|
// Array containing "human understandable" messages
|
|
|
|
$messages = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'INVALID_VERK' => 'The verification key supplied was invalid!',
|
|
|
|
'INVALID_CODE' => 'Invalid verification key, if you think this is an error contact the administrator.',
|
|
|
|
'INVALID_USER' => 'The used verification key is not designated for this user.',
|
|
|
|
'VERK_TOO_SHIT' => 'Your verification code is too weak, try adding some special characters.',
|
|
|
|
'PASS_TOO_SHIT' => 'Your password is too weak, try adding some special characters.',
|
|
|
|
'PASS_NOT_MATCH' => 'Passwords do not match.',
|
|
|
|
'SUCCESS' => 'Successfully changed your password, you may now log in.',
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-25 20:08:44 +00:00
|
|
|
];
|
|
|
|
|
2015-04-21 14:23:28 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-14 21:41:43 +00:00
|
|
|
'redirect' => (
|
|
|
|
$passforget[0] ?
|
|
|
|
$urls->format('SITE_LOGIN') :
|
|
|
|
$_SERVER['PHP_SELF'] . '?pw=true&uid=' . $_REQUEST['uid'] . '&verk=' . $_REQUEST['verk']
|
|
|
|
),
|
2015-09-14 20:51:23 +00:00
|
|
|
'message' => $messages[$passforget[1]],
|
|
|
|
'success' => $passforget[0],
|
2015-04-21 14:23:28 +00:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
// Activating accounts
|
|
|
|
case 'activate':
|
|
|
|
// Attempt activation
|
|
|
|
$activate = Users::activateUser($_REQUEST['u'], true, $_REQUEST['k']);
|
|
|
|
|
|
|
|
// Array containing "human understandable" messages
|
|
|
|
$messages = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'USER_NOT_EXIST' => 'The user you tried to activate does not exist.',
|
|
|
|
'USER_ALREADY_ACTIVE' => 'The user you tried to activate is already active.',
|
|
|
|
'INVALID_CODE' => 'Invalid activation code, if you think this is an error contact the administrator.',
|
|
|
|
'INVALID_USER' => 'The used activation code is not designated for this user.',
|
|
|
|
'SUCCESS' => 'Successfully activated your account, you may now log in.',
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('SITE_LOGIN'),
|
|
|
|
'message' => $messages[$activate[1]],
|
|
|
|
'success' => $activate[0],
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-19 13:00:32 +00:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Resending the activation e-mail
|
|
|
|
case 'resendactivemail':
|
2015-04-20 19:39:49 +00:00
|
|
|
// Attempt send
|
2015-04-21 14:23:28 +00:00
|
|
|
$resend = Users::resendActivationMail($_REQUEST['username'], $_REQUEST['email']);
|
|
|
|
|
|
|
|
// Array containing "human understandable" messages
|
|
|
|
$messages = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'AUTH_LOCKED' => 'Authentication is currently not allowed, try again later.',
|
|
|
|
'USER_NOT_EXIST' => 'The user you tried to activate does not exist (confirm the username/email combination).',
|
|
|
|
'USER_ALREADY_ACTIVE' => 'The user you tried to activate is already active.',
|
|
|
|
'SUCCESS' => 'The activation e-mail has been sent to the address associated with your account.',
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-21 14:23:28 +00:00
|
|
|
];
|
2015-04-20 19:39:49 +00:00
|
|
|
|
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('SITE_HOME'),
|
|
|
|
'message' => $messages[$resend[1]],
|
|
|
|
'success' => $resend[0],
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-20 19:39:49 +00:00
|
|
|
];
|
2015-04-19 13:00:32 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
// Login processing
|
|
|
|
case 'login':
|
|
|
|
// Attempt login
|
|
|
|
$login = Users::login($_REQUEST['username'], $_REQUEST['password'], isset($_REQUEST['remember']));
|
|
|
|
|
|
|
|
// Array containing "human understandable" messages
|
|
|
|
$messages = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'AUTH_LOCKED' => 'Authentication is currently not allowed, try again later.',
|
|
|
|
'USER_NOT_EXIST' => 'The user you tried to log into does not exist.',
|
|
|
|
'INCORRECT_PASSWORD' => 'The password you entered was invalid.',
|
|
|
|
'NOT_ALLOWED' => 'Your account does not have the required permissions to log in.',
|
|
|
|
'NO_LOGIN' => 'Logging into this account is disabled.',
|
2015-10-22 14:24:18 +00:00
|
|
|
'RATE_LIMIT' => 'Your IP has hit the login rate limit, try again later.',
|
2015-09-23 20:45:42 +00:00
|
|
|
'LOGIN_SUCCESS' => 'Login successful!',
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
];
|
|
|
|
|
2015-10-22 14:24:18 +00:00
|
|
|
// Check if we're not RATE_LIMIT
|
|
|
|
if ($login[1] != 'RATE_LIMIT') {
|
|
|
|
// Add to database
|
2016-02-25 16:06:29 +00:00
|
|
|
DBv2::prepare('INSERT INTO `{prefix}login_attempts` (`attempt_success`, `attempt_timestamp`, `attempt_ip`, `user_id`) VALUES (:succ, :time, :ip, :user)')
|
2016-02-18 23:28:44 +00:00
|
|
|
->execute([
|
|
|
|
'succ' => $login[0],
|
|
|
|
'time' => time(),
|
|
|
|
'ip' => Net::pton(Net::IP()),
|
|
|
|
'user' => isset($login[2]) ? $login[2] : 0,
|
2015-10-22 14:24:18 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
'redirect' => $login[0] ? (User::construct($login[2])->lastOnline ? $_REQUEST['redirect'] : $urls->format('INFO_PAGE', ['welcome'])) : $urls->format('SITE_LOGIN'),
|
2015-09-14 20:51:23 +00:00
|
|
|
'message' => $messages[$login[1]],
|
|
|
|
'success' => $login[0],
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
];
|
|
|
|
break;
|
2015-04-08 19:27:51 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
// Registration processing
|
|
|
|
case 'register':
|
2015-04-19 13:00:32 +00:00
|
|
|
// Attempt registration
|
|
|
|
$register = Users::register(
|
|
|
|
$_REQUEST['username'],
|
|
|
|
$_REQUEST['password'],
|
|
|
|
$_REQUEST['confirmpassword'],
|
|
|
|
$_REQUEST['email'],
|
|
|
|
isset($_REQUEST['tos']),
|
|
|
|
(
|
2015-12-04 14:19:10 +00:00
|
|
|
Config::get('recaptcha') ?
|
2015-04-19 13:00:32 +00:00
|
|
|
$_REQUEST['g-recaptcha-response'] :
|
|
|
|
null
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Array containing "human understandable" messages
|
|
|
|
$messages = [
|
2015-09-14 20:51:23 +00:00
|
|
|
'AUTH_LOCKED' => 'Authentication is currently not allowed, try again later.',
|
|
|
|
'DISABLED' => 'Registration is currently disabled.',
|
|
|
|
'INVALID_REG_KEY' => 'The given registration code was invalid.',
|
|
|
|
'TOS' => 'You are required to agree to the Terms of Service.',
|
|
|
|
'CAPTCHA_FAIL' => 'Captcha verification failed, please try again.',
|
|
|
|
'USER_EXISTS' => 'A user with this username already exists, if you lost your password try using the Lost Password form.',
|
|
|
|
'NAME_TOO_SHORT' => 'Your name must be at least 3 characters long.',
|
|
|
|
'NAME_TOO_LONG' => 'Your name can\'t be longer than 16 characters.',
|
|
|
|
'PASS_TOO_SHIT' => 'Your password is too weak, try adding some special characters.',
|
|
|
|
'PASS_NOT_MATCH' => 'Passwords do not match.',
|
2016-01-09 21:57:54 +00:00
|
|
|
'EMAIL_EXISTS' => 'Someone already registered using this email!', // HOW DID I MISS THIS?!
|
2015-09-14 20:51:23 +00:00
|
|
|
'INVALID_EMAIL' => 'Your e-mail address is formatted incorrectly.',
|
|
|
|
'INVALID_MX' => 'No valid MX-Record found on the e-mail address you supplied.',
|
|
|
|
'EMAILSENT' => 'Your registration went through! An activation e-mail has been sent.',
|
2015-12-04 14:19:10 +00:00
|
|
|
'SUCCESS' => 'Your registration went through! Welcome to ' . Config::get('sitename') . '!',
|
2015-04-19 13:00:32 +00:00
|
|
|
];
|
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => ($register[0] ? $urls->format('SITE_LOGIN') : $urls->format('SITE_REGISTER')),
|
|
|
|
'message' => $messages[$register[1]],
|
|
|
|
'success' => $register[0],
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
];
|
|
|
|
break;
|
2015-04-08 19:27:51 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
// Unforgetting passwords
|
|
|
|
case 'forgotpassword':
|
2015-04-25 20:08:44 +00:00
|
|
|
// Attempt send
|
|
|
|
$passforgot = Users::sendPasswordForgot($_REQUEST['username'], $_REQUEST['email']);
|
|
|
|
|
|
|
|
// Array containing "human understandable" messages
|
|
|
|
$messages = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'AUTH_LOCKED' => 'Authentication is currently not allowed, try again later.',
|
|
|
|
'USER_NOT_EXIST' => 'The requested user does not exist (confirm the username/email combination).',
|
|
|
|
'NOT_ALLOWED' => 'Your account does not have the required permissions to change your password.',
|
|
|
|
'SUCCESS' => 'The password reset e-mail has been sent to the address associated with your account.',
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-25 20:08:44 +00:00
|
|
|
];
|
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('SITE_FORGOT_PASSWORD'),
|
|
|
|
'message' => $messages[$passforgot[1]],
|
|
|
|
'success' => $passforgot[0],
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
];
|
|
|
|
break;
|
2015-04-08 17:18:19 +00:00
|
|
|
|
2015-04-17 22:51:53 +00:00
|
|
|
}
|
2015-04-08 17:18:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 13:33:59 +00:00
|
|
|
// Print page contents or if the AJAX request is set only display the render data
|
2015-11-06 22:30:37 +00:00
|
|
|
if (isset($_REQUEST['ajax'])) {
|
|
|
|
echo $renderData['page']['message'] . '|' .
|
|
|
|
$renderData['page']['success'] . '|' .
|
|
|
|
$renderData['page']['redirect'];
|
|
|
|
} else {
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
|
|
|
echo Template::render('global/information');
|
2015-11-06 22:30:37 +00:00
|
|
|
}
|
2015-04-08 19:27:51 +00:00
|
|
|
exit;
|
2015-04-08 17:18:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:23:54 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['auth'] = [
|
2015-08-09 20:27:03 +00:00
|
|
|
|
2015-09-05 16:11:04 +00:00
|
|
|
'redirect' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $urls->format('SITE_HOME'),
|
2015-04-06 21:23:54 +00:00
|
|
|
'blockRegister' => [
|
2015-08-09 20:27:03 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'do' => false,
|
2015-09-05 16:11:04 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
],
|
2015-08-09 20:27:03 +00:00
|
|
|
|
2015-04-06 21:23:54 +00:00
|
|
|
];
|
|
|
|
|
2015-04-25 20:08:44 +00:00
|
|
|
// Check if the user is already logged in
|
2015-09-14 20:51:23 +00:00
|
|
|
if (Users::checkLogin()) {
|
2015-04-25 20:08:44 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'redirect' => $urls->format('SITE_HOME'),
|
|
|
|
'message' => 'You are already logged in, log out to access this page.',
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-25 20:08:44 +00:00
|
|
|
];
|
|
|
|
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
|
|
|
echo Template::render('global/information');
|
2015-04-25 20:08:44 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-04-18 18:26:52 +00:00
|
|
|
// Check if a user has already registered from the current IP address
|
2016-02-07 19:36:13 +00:00
|
|
|
if (count($regUserIP = Users::getUsersByIP(Net::pton(Net::IP())))) {
|
2015-04-18 18:26:52 +00:00
|
|
|
$renderData['auth']['blockRegister'] = [
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'do' => true,
|
|
|
|
'username' => $regUserIP[array_rand($regUserIP)]['username'],
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-04-18 18:26:52 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-04-21 14:23:28 +00:00
|
|
|
// If password forgot things are set display password forget thing
|
2015-09-14 20:51:23 +00:00
|
|
|
if (isset($_REQUEST['pw']) && $_REQUEST['pw']) {
|
2015-04-21 14:23:28 +00:00
|
|
|
$renderData['auth']['changingPass'] = true;
|
2015-09-14 20:51:23 +00:00
|
|
|
$renderData['auth']['userId'] = $_REQUEST['uid'];
|
2015-04-21 14:23:28 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
if (isset($_REQUEST['key'])) {
|
2015-04-21 14:23:28 +00:00
|
|
|
$renderData['auth']['forgotKey'] = $_REQUEST['key'];
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2015-04-21 14:23:28 +00:00
|
|
|
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
|
|
|
echo Template::render('main/forgotpassword');
|
2015-04-21 14:23:28 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:23:54 +00:00
|
|
|
// Print page contents
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
|
|
|
echo Template::render('main/authenticate');
|