2018-01-16 07:26:29 +00:00
|
|
|
<?php
|
2018-02-10 23:18:49 +00:00
|
|
|
use Carbon\Carbon;
|
2018-01-16 07:26:29 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Misuzu\Application;
|
|
|
|
use Misuzu\Database;
|
|
|
|
use Misuzu\Net\IP;
|
|
|
|
use Misuzu\Users\User;
|
2018-02-10 23:18:49 +00:00
|
|
|
use Misuzu\Users\Session;
|
2018-01-16 07:26:29 +00:00
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
require_once __DIR__ . '/../misuzu.php';
|
|
|
|
|
|
|
|
$username_validation_errors = [
|
|
|
|
'trim' => 'Your username may not start or end with spaces!',
|
|
|
|
'short' => "Your username is too short, it has to be at least " . User::USERNAME_MIN_LENGTH . " characters!",
|
|
|
|
'long' => "Your username is too long, it can't be longer than " . User::USERNAME_MAX_LENGTH . " characters!",
|
|
|
|
'double-spaces' => "Your username can't contain double spaces.",
|
|
|
|
'invalid' => 'Your username contains invalid characters.',
|
|
|
|
'spacing' => 'Please use either underscores or spaces, not both!',
|
|
|
|
];
|
|
|
|
|
|
|
|
$mode = $_GET['m'] ?? 'login';
|
|
|
|
$app->templating->var('auth_mode', $mode);
|
2018-02-22 16:37:10 +00:00
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
switch ($mode) {
|
|
|
|
case 'logout':
|
|
|
|
if ($app->getSession() === null) {
|
|
|
|
echo "You aren't logged in.";
|
|
|
|
} else {
|
|
|
|
echo "You've been logged out.";
|
|
|
|
set_cookie_m('uid', '', -3600);
|
|
|
|
set_cookie_m('sid', '', -3600);
|
|
|
|
$app->getSession()->delete();
|
|
|
|
$app->setSession(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '<meta http-equiv="refresh" content="1; url=/">';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'login':
|
2018-02-22 16:37:10 +00:00
|
|
|
if ($app->getSession() !== null) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo '<meta http-equiv="refresh" content="0; url=/">';
|
|
|
|
break;
|
2018-02-22 16:37:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 07:26:29 +00:00
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo $app->templating->render('auth.login');
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_POST['username'], $_POST['password'])) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => "You didn't fill all the forms!"]);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$username = $_POST['username'] ?? '';
|
|
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
|
|
|
|
try {
|
2018-02-11 12:57:01 +00:00
|
|
|
$user = User::where('username', $username)->orWhere('email', $username)->firstOrFail();
|
2018-01-16 07:26:29 +00:00
|
|
|
} catch (ModelNotFoundException $e) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'Invalid username or password!']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 12:57:01 +00:00
|
|
|
if (!$user->validatePassword($password)) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'Invalid username or password!']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
$session = Session::createSession($user, 'Misuzu T2');
|
2018-02-22 16:37:10 +00:00
|
|
|
$app->setSession($session);
|
2018-03-14 01:39:02 +00:00
|
|
|
set_cookie_m('uid', $session->user_id, 604800);
|
|
|
|
set_cookie_m('sid', $session->session_key, 604800);
|
2018-02-10 23:18:49 +00:00
|
|
|
|
|
|
|
// Temporary key generation for chat login.
|
|
|
|
// Should eventually be replaced with a callback login system.
|
|
|
|
// Also uses different cookies since $httponly is required to be false for these.
|
2018-02-11 12:57:01 +00:00
|
|
|
$user->last_ip = IP::remote();
|
2018-02-10 23:18:49 +00:00
|
|
|
$user->user_chat_key = bin2hex(random_bytes(16));
|
2018-01-16 07:26:29 +00:00
|
|
|
$user->save();
|
|
|
|
|
2018-02-10 23:18:49 +00:00
|
|
|
setcookie('msz_tmp_id', $user->user_id, time() + 604800, '/', '.flashii.net');
|
|
|
|
setcookie('msz_tmp_key', $user->user_chat_key, time() + 604800, '/', '.flashii.net');
|
2018-01-16 07:26:29 +00:00
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'You are now logged in!', 'next' => '/']);
|
|
|
|
break;
|
2018-01-28 03:32:28 +00:00
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
case 'register':
|
2018-02-22 16:37:10 +00:00
|
|
|
if ($app->getSession() !== null) {
|
|
|
|
return '<meta http-equiv="refresh" content="0; url=/">';
|
|
|
|
}
|
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
$prevent_registration = $app->config->get('Auth', 'prevent_registration', 'bool', false);
|
|
|
|
|
2018-01-16 07:26:29 +00:00
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
2018-03-13 22:04:01 +00:00
|
|
|
$app->templating->var('prevent_registration', $prevent_registration);
|
2018-03-14 01:39:02 +00:00
|
|
|
echo $app->templating->render('auth.register');
|
|
|
|
break;
|
2018-03-13 22:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($prevent_registration) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'Registration is not allowed on this instance.']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => "You didn't fill all the forms!"]);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$username = $_POST['username'] ?? '';
|
2018-02-11 13:55:24 +00:00
|
|
|
$username_validate = User::validateUsername($username);
|
2018-01-16 07:26:29 +00:00
|
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
$email = $_POST['email'] ?? '';
|
|
|
|
|
|
|
|
if ($username_validate !== '') {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => $username_validation_errors[$username_validate]]);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$existing = User::where('username', $username)->firstOrFail();
|
|
|
|
|
|
|
|
if ($existing->user_id > 0) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'This username is already taken!']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || !check_mx_record($email)) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'The e-mail address you entered is invalid!']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$existing = User::where('email', $email)->firstOrFail();
|
|
|
|
|
|
|
|
if ($existing->user_id > 0) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'This e-mail address has already been used!']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (password_entropy($password) < 32) {
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'Your password is too weak!']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 12:57:01 +00:00
|
|
|
User::createUser($username, $password, $email);
|
2018-01-16 07:26:29 +00:00
|
|
|
|
2018-03-14 01:39:02 +00:00
|
|
|
echo json_encode_m(['error' => 'Welcome to Flashii! You may now log in.', 'next' => '/auth.php?m=login']);
|
|
|
|
break;
|
2018-01-16 07:26:29 +00:00
|
|
|
}
|