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-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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!MSZ_DEBUG) {
|
|
|
|
$twigCacheDirSfx = GitInfo::hash(true);
|
|
|
|
if(empty($twigCacheDirSfx))
|
|
|
|
$twigCacheDirSfx = md5(MSZ_ROOT);
|
|
|
|
|
|
|
|
$twigCache = sys_get_temp_dir() . '/msz-tpl-' . $twigCacheDirSfx;
|
|
|
|
if(!is_dir($twigCache))
|
|
|
|
mkdir($twigCache, 0775, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$globals = $cfg->getValues([
|
|
|
|
['site.name:s', 'Misuzu'],
|
|
|
|
'site.desc:s',
|
|
|
|
'site.url:s',
|
|
|
|
'eeprom.path:s',
|
|
|
|
'eeprom.app:s',
|
|
|
|
['csrf.secret:s', 'soup'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
Template::init($msz, $twigCache ?? null, MSZ_DEBUG);
|
|
|
|
|
|
|
|
Template::set('globals', [
|
|
|
|
'site_name' => $globals['site.name'],
|
|
|
|
'site_description' => $globals['site.desc'],
|
|
|
|
'site_url' => $globals['site.url'],
|
|
|
|
'eeprom' => [
|
|
|
|
'path' => $globals['eeprom.path'],
|
|
|
|
'app' => $globals['eeprom.app'],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$mszAssetsInfo = json_decode(file_get_contents(MSZ_ASSETS . '/current.json'));
|
|
|
|
if(!empty($mszAssetsInfo))
|
|
|
|
Template::set('assets', $mszAssetsInfo);
|
|
|
|
unset($mszAssetsInfo);
|
|
|
|
|
|
|
|
Template::addPath(MSZ_TEMPLATES);
|
|
|
|
|
2023-08-03 01:35:08 +00:00
|
|
|
$tokenPacker = $msz->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()) {
|
|
|
|
$users = $msz->getUsers();
|
|
|
|
$sessions = $msz->getSessions();
|
|
|
|
$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-08-03 01:35:08 +00:00
|
|
|
$msz->getAuthInfo()->setInfo($tokenInfo, $userInfo, $sessionInfo, $userInfoReal);
|
|
|
|
|
2023-08-02 22:12:47 +00:00
|
|
|
if(!empty($userInfo))
|
|
|
|
$userInfo = $users->getUser((string)$userInfo->getId(), 'id');
|
|
|
|
if(!empty($userInfoReal))
|
|
|
|
$userInfoReal = $users->getUser((string)$userInfoReal->getId(), 'id');
|
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
CSRF::init(
|
|
|
|
$globals['csrf.secret'],
|
2023-08-03 01:35:08 +00:00
|
|
|
($msz->isLoggedIn() ? $sessionInfo->getToken() : $_SERVER['REMOTE_ADDR'])
|
2023-07-19 19:03:53 +00:00
|
|
|
);
|
|
|
|
|
2023-07-26 18:19:46 +00:00
|
|
|
if(!empty($userInfo)) {
|
2023-07-19 19:03:53 +00:00
|
|
|
Template::set('current_user', $userInfo);
|
2023-07-26 18:19:46 +00:00
|
|
|
Template::set('current_user_ban_info', $msz->tryGetActiveBan());
|
|
|
|
}
|
2023-08-02 22:12:47 +00:00
|
|
|
|
|
|
|
if(!empty($userInfoReal)) {
|
2023-07-19 19:03:53 +00:00
|
|
|
Template::set('current_user_real', $userInfoReal);
|
2023-08-02 22:12:47 +00:00
|
|
|
Template::set('current_user_real_colour', $users->getUserColour($userInfoReal));
|
|
|
|
}
|
2023-07-19 19:03:53 +00:00
|
|
|
|
|
|
|
$inManageMode = str_starts_with($_SERVER['REQUEST_URI'], '/manage');
|
|
|
|
|
2023-08-02 22:12:47 +00:00
|
|
|
Template::set('header_menu', $msz->getHeaderMenu($userInfo ?? null));
|
|
|
|
Template::set('user_menu', $msz->getUserMenu($userInfo ?? null, $inManageMode));
|
2023-08-31 00:37:09 +00:00
|
|
|
Template::set('display_timings_info', MSZ_DEBUG || $msz->getAuthInfo()->getPerms('global')->check(Perm::G_TIMINGS_VIEW));
|
2023-07-29 18:15:30 +00:00
|
|
|
|
2023-07-19 19:03:53 +00:00
|
|
|
if($inManageMode) {
|
2023-08-28 13:45:36 +00:00
|
|
|
$hasManageAccess = false;
|
|
|
|
|
|
|
|
if($msz->isLoggedIn() && !$msz->hasActiveBan()) {
|
|
|
|
$manageUser = $msz->getActiveUser();
|
|
|
|
$manageUserId = $manageUser->getId();
|
2023-08-30 22:37:21 +00:00
|
|
|
$manageGlobalPerms = $msz->getAuthInfo()->getPerms('global');
|
2023-08-28 13:45:36 +00:00
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_IS_JANITOR)) {
|
2023-08-28 13:45:36 +00:00
|
|
|
$hasManageAccess = true;
|
|
|
|
$manageMenu = [
|
|
|
|
'General' => [
|
|
|
|
'Overview' => url('manage-general-overview'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_LOGS_VIEW))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['General']['Logs'] = url('manage-general-logs');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_EMOTES_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['General']['Emoticons'] = url('manage-general-emoticons');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_CONFIG_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['General']['Settings'] = url('manage-general-settings');
|
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
$manageUserPerms = $msz->getAuthInfo()->getPerms('user');
|
|
|
|
if($manageUserPerms->check(Perm::U_USERS_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Users & Roles']['Users'] = url('manage-users');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageUserPerms->check(Perm::U_ROLES_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Users & Roles']['Roles'] = url('manage-roles');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageUserPerms->check(Perm::U_NOTES_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Users & Roles']['Notes'] = url('manage-users-notes');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageUserPerms->check(Perm::U_WARNINGS_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Users & Roles']['Warnings'] = url('manage-users-warnings');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageUserPerms->check(Perm::U_BANS_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Users & Roles']['Bans'] = url('manage-users-bans');
|
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_NEWS_POSTS_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['News']['Posts'] = url('manage-news-posts');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_NEWS_CATEGORIES_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['News']['Categories'] = url('manage-news-categories');
|
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_FORUM_CATEGORIES_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Forum']['Permission Calculator'] = url('manage-forum-categories');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_FORUM_TOPIC_REDIRS_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Forum']['Topic Redirects'] = url('manage-forum-topic-redirs');
|
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_CL_CHANGES_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Changelog']['Changes'] = url('manage-changelog-changes');
|
2023-08-30 22:37:21 +00:00
|
|
|
if($manageGlobalPerms->check(Perm::G_CL_TAGS_MANAGE))
|
2023-08-28 13:45:36 +00:00
|
|
|
$manageMenu['Changelog']['Tags'] = url('manage-changelog-tags');
|
|
|
|
|
|
|
|
Template::set('manage_menu', $manageMenu);
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 22:12:47 +00:00
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$hasManageAccess)
|
|
|
|
Template::throwError(403);
|
2023-07-19 19:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$mszRequestPath = $request->getPath();
|
|
|
|
$mszLegacyPathPrefix = MSZ_PUBLIC . '-legacy/';
|
|
|
|
$mszLegacyPath = realpath($mszLegacyPathPrefix . $mszRequestPath);
|
|
|
|
|
|
|
|
if(!empty($mszLegacyPath) && str_starts_with($mszLegacyPath, $mszLegacyPathPrefix)) {
|
|
|
|
if(is_dir($mszLegacyPath))
|
|
|
|
$mszLegacyPath .= '/index.php';
|
|
|
|
|
|
|
|
if(is_file($mszLegacyPath)) {
|
|
|
|
require_once $mszLegacyPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:51:02 +00:00
|
|
|
$msz->setUpHttp();
|
2023-01-06 20:35:03 +00:00
|
|
|
$msz->dispatchHttp($request);
|