2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-07-22 15:02:41 +00:00
|
|
|
use RuntimeException;
|
2023-08-03 01:35:08 +00:00
|
|
|
use Misuzu\Auth\AuthTokenBuilder;
|
|
|
|
use Misuzu\Auth\AuthTokenCookie;
|
|
|
|
use Misuzu\Auth\AuthTokenInfo;
|
2023-08-31 21:33:34 +00:00
|
|
|
use Sasae\SasaeEnvironment;
|
2023-07-19 19:03:53 +00:00
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
require_once __DIR__ . '/../misuzu.php';
|
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
set_exception_handler(function(\Throwable $ex) {
|
2023-09-10 20:46:58 +00:00
|
|
|
\Sentry\captureException($ex);
|
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
http_response_code(500);
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
if(MSZ_DEBUG) {
|
|
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
|
|
echo (string)$ex;
|
|
|
|
} else {
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
|
|
echo file_get_contents(MSZ_TEMPLATES . '/500.html');
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
});
|
|
|
|
|
|
|
|
// The whole wall of shit before the router setup and dispatch should be worked away
|
|
|
|
// Lockdown things should be middleware when there's no more legacy files
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
$request = \Index\Http\HttpRequest::fromRequest();
|
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
ob_start();
|
|
|
|
|
|
|
|
if(file_exists(MSZ_ROOT . '/.migrating')) {
|
|
|
|
http_response_code(503);
|
|
|
|
if(!isset($_GET['_check'])) {
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
|
|
echo file_get_contents(MSZ_TEMPLATES . '/503.html');
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2023-09-08 00:43:00 +00:00
|
|
|
$tokenPacker = $msz->getAuthContext()->createAuthTokenPacker();
|
2023-07-19 19:03:53 +00:00
|
|
|
|
2023-08-03 01:35:08 +00:00
|
|
|
if(filter_has_var(INPUT_COOKIE, 'msz_auth'))
|
|
|
|
$tokenInfo = $tokenPacker->unpack(filter_input(INPUT_COOKIE, 'msz_auth'));
|
|
|
|
elseif(filter_has_var(INPUT_COOKIE, 'msz_uid') && filter_has_var(INPUT_COOKIE, 'msz_sid')) {
|
|
|
|
$tokenBuilder = new AuthTokenBuilder;
|
|
|
|
$tokenBuilder->setUserId((string)filter_input(INPUT_COOKIE, 'msz_uid', FILTER_SANITIZE_NUMBER_INT));
|
|
|
|
$tokenBuilder->setSessionToken((string)filter_input(INPUT_COOKIE, 'msz_sid'));
|
|
|
|
$tokenInfo = $tokenBuilder->toInfo();
|
|
|
|
$tokenBuilder = null;
|
|
|
|
} else
|
|
|
|
$tokenInfo = AuthTokenInfo::empty();
|
2023-07-19 19:03:53 +00:00
|
|
|
|
2023-08-03 01:35:08 +00:00
|
|
|
$userInfo = null;
|
|
|
|
$sessionInfo = null;
|
|
|
|
$userInfoReal = null;
|
2023-07-19 19:03:53 +00:00
|
|
|
|
2023-08-03 01:35:08 +00:00
|
|
|
if($tokenInfo->hasUserId() && $tokenInfo->hasSessionToken()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$users = $msz->getUsersContext()->getUsers();
|
2023-09-08 00:43:00 +00:00
|
|
|
$sessions = $msz->getAuthContext()->getSessions();
|
2023-08-03 01:35:08 +00:00
|
|
|
$tokenBuilder = new AuthTokenBuilder($tokenInfo);
|
2023-07-19 19:03:53 +00:00
|
|
|
|
|
|
|
try {
|
2023-08-03 01:35:08 +00:00
|
|
|
$sessionInfo = $sessions->getSession(sessionToken: $tokenInfo->getSessionToken());
|
2023-07-28 20:06:12 +00:00
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
if($sessionInfo->hasExpired()) {
|
2023-08-03 01:35:08 +00:00
|
|
|
$tokenBuilder->removeUserId();
|
|
|
|
$tokenBuilder->removeSessionToken();
|
|
|
|
} elseif($sessionInfo->getUserId() === $tokenInfo->getUserId()) {
|
|
|
|
$userInfo = $users->getUser($tokenInfo->getUserId(), 'id');
|
|
|
|
|
|
|
|
if($userInfo->isDeleted()) {
|
|
|
|
$tokenBuilder->removeUserId();
|
|
|
|
$tokenBuilder->removeSessionToken();
|
|
|
|
} else {
|
2023-08-02 22:12:47 +00:00
|
|
|
$users->recordUserActivity($userInfo, remoteAddr: $_SERVER['REMOTE_ADDR']);
|
|
|
|
$sessions->recordSessionActivity(sessionInfo: $sessionInfo, remoteAddr: $_SERVER['REMOTE_ADDR']);
|
2023-07-28 20:06:12 +00:00
|
|
|
if($sessionInfo->shouldBumpExpires())
|
2023-08-03 01:35:08 +00:00
|
|
|
$tokenBuilder->setEdited();
|
2023-07-19 19:03:53 +00:00
|
|
|
|
2023-08-03 01:35:08 +00:00
|
|
|
if($tokenInfo->hasImpersonatedUserId()) {
|
2023-08-02 22:12:47 +00:00
|
|
|
$allowToImpersonate = $userInfo->isSuperUser();
|
2023-08-03 01:35:08 +00:00
|
|
|
$impersonatedUserId = $tokenInfo->getImpersonatedUserId();
|
2023-07-28 21:20:19 +00:00
|
|
|
|
|
|
|
if(!$allowToImpersonate) {
|
|
|
|
$allowImpersonateUsers = $cfg->getArray(sprintf('impersonate.allow.u%s', $userInfo->getId()));
|
|
|
|
$allowToImpersonate = in_array((string)$impersonatedUserId, $allowImpersonateUsers, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($allowToImpersonate) {
|
|
|
|
$userInfoReal = $userInfo;
|
|
|
|
|
|
|
|
try {
|
2023-08-02 22:12:47 +00:00
|
|
|
$userInfo = $users->getUser($impersonatedUserId, 'id');
|
2023-07-28 21:20:19 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
|
|
|
$userInfo = $userInfoReal;
|
2023-08-03 01:35:08 +00:00
|
|
|
$userInfoReal = null;
|
|
|
|
$tokenBuilder->removeImpersonatedUserId();
|
2023-07-28 21:20:19 +00:00
|
|
|
}
|
2023-08-03 01:35:08 +00:00
|
|
|
} else $tokenBuilder->removeImpersonatedUserId();
|
2023-07-19 19:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 15:02:41 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
2023-08-03 01:35:08 +00:00
|
|
|
$tokenBuilder->removeUserId();
|
|
|
|
$tokenBuilder->removeSessionToken();
|
|
|
|
$tokenBuilder->removeImpersonatedUserId();
|
|
|
|
$userInfo = null;
|
|
|
|
$sessionInfo = null;
|
|
|
|
$userInfoReal = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($tokenBuilder->isEdited()) {
|
|
|
|
$tokenInfo = $tokenBuilder->toInfo();
|
|
|
|
AuthTokenCookie::apply($tokenPacker->pack($tokenInfo));
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
2023-07-19 19:03:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$authInfo = $msz->getAuthInfo();
|
|
|
|
$authInfo->setInfo($tokenInfo, $userInfo, $sessionInfo, $userInfoReal);
|
2023-08-03 01:35:08 +00:00
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
CSRF::init(
|
2023-08-31 21:33:34 +00:00
|
|
|
$cfg->getString('csrf.secret', 'soup'),
|
2023-09-06 20:06:07 +00:00
|
|
|
($authInfo->isLoggedIn() ? $sessionInfo->getToken() : $_SERVER['REMOTE_ADDR'])
|
2023-07-19 19:03:53 +00:00
|
|
|
);
|
|
|
|
|
2023-09-10 00:04:53 +00:00
|
|
|
// order for these two currently matters i think: it shouldn't.
|
|
|
|
$router = $msz->createRouting();
|
2023-08-31 21:33:34 +00:00
|
|
|
$msz->startTemplating();
|
2023-07-19 19:03:53 +00:00
|
|
|
|
2023-09-11 20:15:44 +00:00
|
|
|
$mszRequestPath = substr($request->getPath(), 1);
|
2023-07-19 19:03:53 +00:00
|
|
|
$mszLegacyPathPrefix = MSZ_PUBLIC . '-legacy/';
|
2023-09-11 20:15:44 +00:00
|
|
|
$mszLegacyPath = $mszLegacyPathPrefix . $mszRequestPath;
|
2023-07-19 19:03:53 +00:00
|
|
|
|
|
|
|
if(!empty($mszLegacyPath) && str_starts_with($mszLegacyPath, $mszLegacyPathPrefix)) {
|
2023-09-11 20:15:44 +00:00
|
|
|
$mszLegacyPathReal = realpath($mszLegacyPath);
|
2023-09-11 20:36:20 +00:00
|
|
|
if($mszLegacyPath === $mszLegacyPathReal || $mszLegacyPath === $mszLegacyPathReal . '/') {
|
|
|
|
if(str_starts_with($mszRequestPath, '/manage') && !$msz->hasManageAccess())
|
|
|
|
Template::throwError(403);
|
2023-09-11 20:15:44 +00:00
|
|
|
|
2023-09-11 20:36:20 +00:00
|
|
|
if(is_dir($mszLegacyPath))
|
|
|
|
$mszLegacyPath .= '/index.php';
|
2023-08-31 21:33:34 +00:00
|
|
|
|
2023-09-11 20:36:20 +00:00
|
|
|
if(is_file($mszLegacyPath)) {
|
|
|
|
require_once $mszLegacyPath;
|
|
|
|
return;
|
|
|
|
}
|
2023-07-19 19:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:04:53 +00:00
|
|
|
$router->dispatch($request);
|