getValues([ ['site.name:s', 'Misuzu'], 'site.desc:s', 'site.url:s', 'eeprom.path:s', 'eeprom.app:s', ['auth.secret:s', 'meow'], ['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); AuthToken::setSecretKey($globals['auth.secret']); if(isset($_COOKIE['msz_uid']) && isset($_COOKIE['msz_sid'])) { $authToken = new AuthToken; $authToken->setUserId(filter_input(INPUT_COOKIE, 'msz_uid', FILTER_SANITIZE_NUMBER_INT) ?? '0'); $authToken->setSessionToken(filter_input(INPUT_COOKIE, 'msz_sid') ?? ''); if($authToken->isValid()) $authToken->applyCookie(strtotime('1 year')); AuthToken::nukeCookieLegacy(); } if(!isset($authToken)) $authToken = AuthToken::unpack(filter_input(INPUT_COOKIE, 'msz_auth') ?? ''); $users = $msz->getUsers(); $sessions = $msz->getSessions(); if($authToken->isValid()) { try { $sessionInfo = $sessions->getSession(sessionToken: $authToken->getSessionToken()); if($sessionInfo->hasExpired()) { $sessions->deleteSessions(sessionInfos: $sessionInfo); } elseif($sessionInfo->getUserId() === $authToken->getUserId()) { $userInfo = $users->getUser($authToken->getUserId(), 'id'); if(!$userInfo->isDeleted()) { $users->recordUserActivity($userInfo, remoteAddr: $_SERVER['REMOTE_ADDR']); $sessions->recordSessionActivity(sessionInfo: $sessionInfo, remoteAddr: $_SERVER['REMOTE_ADDR']); if($sessionInfo->shouldBumpExpires()) $authToken->applyCookie($sessionInfo->getExpiresTime()); if($authToken->hasImpersonatedUserId()) { $allowToImpersonate = $userInfo->isSuperUser(); $impersonatedUserId = $authToken->getImpersonatedUserId(); if(!$allowToImpersonate) { $allowImpersonateUsers = $cfg->getArray(sprintf('impersonate.allow.u%s', $userInfo->getId())); $allowToImpersonate = in_array((string)$impersonatedUserId, $allowImpersonateUsers, true); } $removeImpersonationData = !$allowToImpersonate; if($allowToImpersonate) { $userInfoReal = $userInfo; try { $userInfo = $users->getUser($impersonatedUserId, 'id'); } catch(RuntimeException $ex) { $userInfo = $userInfoReal; $removeImpersonationData = true; } } if($removeImpersonationData) { $authToken->removeImpersonatedUserId(); $authToken->applyCookie(); } } $msz->setAuthInfo($authToken, $userInfo, $userInfoReal ?? null); } } } catch(RuntimeException $ex) { AuthToken::nukeCookie(); } } if(!empty($userInfo)) $userInfo = $users->getUser((string)$userInfo->getId(), 'id'); if(!empty($userInfoReal)) $userInfoReal = $users->getUser((string)$userInfoReal->getId(), 'id'); CSRF::init( $globals['csrf.secret'], ($msz->isLoggedIn() ? $authToken->getSessionToken() : $_SERVER['REMOTE_ADDR']) ); if(!empty($userInfo)) { Template::set('current_user', $userInfo); Template::set('current_user_ban_info', $msz->tryGetActiveBan()); } if(!empty($userInfoReal)) { Template::set('current_user_real', $userInfoReal); Template::set('current_user_real_colour', $users->getUserColour($userInfoReal)); } $inManageMode = str_starts_with($_SERVER['REQUEST_URI'], '/manage'); Template::set('header_menu', $msz->getHeaderMenu($userInfo ?? null)); Template::set('user_menu', $msz->getUserMenu($userInfo ?? null, $inManageMode)); Template::set('display_debug_info', MSZ_DEBUG || (!empty($userInfo) && $userInfo->isSuperUser())); if($inManageMode) { $hasManageAccess = $msz->isLoggedIn() && !$msz->hasActiveBan() && perms_check_user(MSZ_PERMS_GENERAL, $msz->getActiveUser()->getId(), MSZ_PERM_GENERAL_CAN_MANAGE); if(!$hasManageAccess) { echo render_error(403); exit; } Template::set('manage_menu', manage_get_menu($userInfo->getId())); } $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; } } $msz->setUpHttp(str_contains($mszRequestPath, '.php')); $msz->dispatchHttp($request);