misuzu/public/index.php

114 lines
4.4 KiB
PHP
Raw Normal View History

<?php
2019-09-29 00:38:39 +02:00
namespace Misuzu;
use RuntimeException;
use Index\MediaType;
use Index\Http\Content\MultipartFormContent;
use Index\Http\Content\Multipart\ValueMultipartFormData;
2025-03-28 20:19:54 +00:00
use Index\Http\Routing\Router;
2025-03-31 15:34:20 +00:00
use Index\Http\Routing\Processors\Before;
2025-03-28 20:19:54 +00:00
use Index\Http\Routing\Routes\RouteInfo;
require_once __DIR__ . '/../misuzu.php';
2024-12-02 02:28:08 +00:00
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Misuzu is not initialised.');
// 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
ob_start();
2025-02-09 20:44:10 +00:00
if(is_file($msz->dbCtx->getMigrateLockPath())) {
http_response_code(503);
2025-01-30 14:52:01 +00:00
header('Content-Type: text/html; charset=utf-8');
echo file_get_contents(sprintf('%s/error-503.html', Misuzu::PATH_PUBLIC));
exit;
}
2025-02-09 20:44:10 +00:00
$request = \Index\Http\HttpRequest::fromRequest();
$msz->timings->lap('request');
2025-04-03 14:37:19 +00:00
$msz->registerRequestRoutes($request);
2025-02-09 20:44:10 +00:00
2025-04-03 14:37:19 +00:00
if($msz->routingCtx->domainRoles->hasRole($request->getHeaderLine('Host'), 'main')) {
$mszRequestPath = substr($request->requestTarget, 1);
2025-02-09 20:44:10 +00:00
$mszLegacyPathPrefix = Misuzu::PATH_PUBLIC_LEGACY . '/';
2025-01-29 00:25:53 +00:00
$mszLegacyPath = $mszLegacyPathPrefix . $mszRequestPath;
2025-01-29 00:25:53 +00:00
if(str_starts_with($mszLegacyPath, $mszLegacyPathPrefix)) {
$mszLegacyPathReal = realpath($mszLegacyPath);
if($mszLegacyPath === $mszLegacyPathReal || $mszLegacyPath === $mszLegacyPathReal . '/') {
2025-03-28 20:19:54 +00:00
// this is here so filters can run...
2025-04-03 14:37:19 +00:00
$msz->routingCtx->router->route(RouteInfo::exact(
2025-03-31 15:34:20 +00:00
$request->method,
$request->requestTarget,
#[Before('authz:cookie')]
function() use ($msz, $mszRequestPath) {
if(str_starts_with($mszRequestPath, 'manage') && !$msz->hasManageAccess())
return 403;
},
));
2025-04-03 14:37:19 +00:00
$response = $msz->routingCtx->router->handle($request);
2025-03-28 20:19:54 +00:00
if($response->getBody()->getSize() > 0) {
Router::output($response);
exit;
}
2025-01-29 00:25:53 +00:00
if(is_dir($mszLegacyPath))
$mszLegacyPath .= '/index.php';
2023-08-31 21:33:34 +00:00
2025-01-29 00:25:53 +00:00
if(is_file($mszLegacyPath)) {
2025-03-28 20:19:54 +00:00
// Reconstruct $_POST since PHP no longer makes it for us
if($request->getBody()->isReadable() && empty($_POST)) {
$mszRequestContent = (function($contentType, $stream) {
if($contentType->equals('application/x-www-form-urlencoded')) {
parse_str((string)$stream, $postVars);
return $postVars;
}
if($contentType->equals('multipart/form-data'))
try {
return MultipartFormContent::parseStream($stream, $contentType->boundary);
} catch(RuntimeException $ex) {}
return null;
})(MediaType::parse($request->getHeaderLine('Content-Type')), $request->getBody());
$_POST = (function($requestContent) {
if(is_array($requestContent))
return $requestContent;
if($requestContent instanceof MultipartFormContent) {
$postVars = [];
foreach($requestContent->params as $name => $values) {
if(count($values) === 0)
$postVars[$name] = '';
elseif(count($values) === 1)
$postVars[$name] = $values[0] instanceof ValueMultipartFormData ? (string)$values[0] : '';
else {
$postVar = [];
foreach($values as $value)
$postVars[] = $value instanceof ValueMultipartFormData ? (string)$value : '';
$postVars[$name] = $postVar;
}
}
return $postVars;
}
return [];
})($mszRequestContent);
$_REQUEST = array_merge($_GET, $_POST);
}
2025-01-29 00:25:53 +00:00
require_once $mszLegacyPath;
return;
}
2023-09-11 20:36:20 +00:00
}
}
}
2025-04-03 14:37:19 +00:00
$msz->routingCtx->dispatch($request);