v1.0.1
AtheraStudio Database
- <?php
- declare(strict_types=1);
-
- namespace Athera;
-
- defined('ATHERA') || exit;
-
- final class App
- {
- public static function run(): void
- {
- ini_set('zlib.output_compression', '0');
- if (function_exists('header_remove')) header_remove('X-Powered-By');
- Http::noCache();
- try {
- Config::boot();
- } catch (\Throwable $e) {
- self::plain(500, 'AtheraStudio Database: ' . $e->getMessage());
- return;
- }
- try {
- self::route();
- } catch (\Throwable $e) {
- Security::log(['ev' => 'error', 'error' => mb_substr($e->getMessage() . ' @ ' . basename($e->getFile()) . ':' . $e->getLine(), 0, 500)]);
- if (!headers_sent()) self::plain(500, 'Internal error');
- }
- }
-
- private static function route(): void
- {
- if (!Security::ipAllowed()) {
- Security::log(['ev' => 'ip_denied', 'path' => mb_substr((string)($_SERVER['REQUEST_URI'] ?? ''), 0, 200)]);
- self::plain(403, 'Forbidden');
- return;
- }
- if (Config::bool('REQUIRE_HTTPS') && !Http::https()) {
- if (($_SERVER['REQUEST_METHOD'] ?? '') === 'GET' && !isset($_GET['api'])) {
- header('Location: ' . preg_replace('/^http:/', 'https:', Http::origin()) . Http::basePath(), true, 301);
- return;
- }
- self::plain(403, 'HTTPS required');
- return;
- }
- $path = (string)parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);
- $base = Http::basePath();
- if ($path !== '' && $path . '/' === $base && empty($_GET)) {
- header('Location: ' . $base, true, 301);
- return;
- }
- if (isset($_GET['unlock'])) {
- Page::unlock((string)$_GET['unlock']);
- return;
- }
- if (isset($_GET['api'])) {
- Api::handle((string)$_GET['api']);
- return;
- }
- Page::render();
- }
-
- public static function plain(int $code, string $text): void
- {
- http_response_code($code);
- Http::securityHeaders(false);
- header('Content-Type: text/plain; charset=utf-8');
- echo $text;
- }
- }
-