diff --git a/src/Info/InfoRoutes.php b/src/Info/InfoRoutes.php index df7fdcd..971ba5c 100644 --- a/src/Info/InfoRoutes.php +++ b/src/Info/InfoRoutes.php @@ -36,6 +36,44 @@ class InfoRoutes extends RouteHandler { ); } + private static function findDocPath(string $projectPath, string $name): string { + // User provided case, without extension + $attempt = sprintf('%s/%s', $projectPath, $name); + if(is_file($attempt)) + return $attempt; + + // User provided case, with extension + $attempt = sprintf('%s/%s.md', $projectPath, $name); + if(is_file($attempt)) + return $attempt; + + $name = strtoupper($name); + + // Uppercase, without extension + $attempt = sprintf('%s/%s', $projectPath, $name); + if(is_file($attempt)) + return $attempt; + + // Uppercase, with extension + $attempt = sprintf('%s/%s.md', $projectPath, $name); + if(is_file($attempt)) + return $attempt; + + $name = strtolower($name); + + // Lowercase, without extension + $attempt = sprintf('%s/%s', $projectPath, $name); + if(is_file($attempt)) + return $attempt; + + // Lowercase, with extension + $attempt = sprintf('%s/%s.md', $projectPath, $name); + if(is_file($attempt)) + return $attempt; + + return ''; + } + #[Route('GET', '/info/:project/:name')] public function getProjectPage($response, $request, string $project, string $name) { if(!array_key_exists($project, self::PROJECT_PATHS)) @@ -46,37 +84,19 @@ class InfoRoutes extends RouteHandler { $projectPath = self::PROJECT_PATHS[$project]; $titleSuffix = array_key_exists($project, self::PROJECT_SUFFIXES) ? self::PROJECT_SUFFIXES[$project] : ''; - $attempts = 0; - $licenceHack = false; - for(;;) { - $path = match(++$attempts) { - 1 => sprintf('%s/%s', $projectPath, $name), - 2 => sprintf('%s/%s.md', $projectPath, $name), - 3 => sprintf('%s/%s', $projectPath, strtoupper($name)), - 4 => sprintf('%s/%s.md', $projectPath, strtoupper($name)), - 5 => sprintf('%s/%s', $projectPath, strtolower($name)), - 6 => sprintf('%s/%s.md', $projectPath, strtolower($name)), - default => '', - }; + $path = self::findDocPath($projectPath, $name); + if($path === '') { + $isBritishLicense = strtolower($name) === 'licence'; + $isAmericanLicence = strtolower($name) === 'license'; - if($path === '') { - if(!$licenceHack) { - $isBritish = strtolower($name) === 'licence'; - $isAmerican = strtolower($name) === 'license'; - - if($isBritish || $isAmerican) { - $attempts = 0; - $licenceHack = true; - $name = $isAmerican ? 'licence' : 'license'; - continue; - } - } + if($isBritishLicense || $isAmericanLicence) { + $name = $isAmericanLicence ? 'licence' : 'license'; + $path = self::findDocPath($projectPath, $name); + if($path === '') + return 404; + } else return 404; - } - - if(is_file($path)) - break; } return $this->serveMarkdownDocument($path, $titleSuffix); diff --git a/src/MisuzuContext.php b/src/MisuzuContext.php index 6da062f..063732d 100644 --- a/src/MisuzuContext.php +++ b/src/MisuzuContext.php @@ -1,6 +1,7 @@ getWebAssetInfo(); $globals['auth_info'] = $this->authInfo; $globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->getUserInfo()); - $globals['display_timings_info'] = MSZ_DEBUG - || $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW); + $globals['display_timings_info'] = $isDebug || $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW); - $templating = new SasaeEnvironment( + $this->templating = new SasaeEnvironment( MSZ_TEMPLATES, - cache: MSZ_DEBUG ? null : ['Misuzu', GitInfo::hash(true)], - debug: MSZ_DEBUG + cache: $isDebug ? null : ['Misuzu', GitInfo::hash(true)], + debug: $isDebug ); - $templating->addExtension(new MisuzuSasaeExtension($this)); - $templating->addGlobal('globals', $globals); + $this->templating->addExtension(new MisuzuSasaeExtension($this)); + $this->templating->addGlobal('globals', $globals); - Template::init($templating); + Template::init($this->templating); } public function startRouter(): void { diff --git a/src/Perms/Permissions.php b/src/Perms/Permissions.php index e50b7f3..3981b2c 100644 --- a/src/Perms/Permissions.php +++ b/src/Perms/Permissions.php @@ -5,6 +5,7 @@ use stdClass; use InvalidArgumentException; use RuntimeException; use Index\DateTime; +use Index\Environment; use Index\Data\DbStatementCache; use Index\Data\DbTools; use Index\Data\IDbConnection; @@ -370,7 +371,7 @@ class Permissions { } private static function precalculatePermissionsLog(string $fmt, ...$args): void { - if(!MSZ_CLI) + if(!Environment::isConsole()) return; echo DateTime::now()->format('[H:i:s.u] ');