Files
zyt/admin/scripts/hospital-preview-router.php
T
2026-11-11 13:53:14 +08:00

88 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
// 仅供回环预览:静态文件来自隔离副本,PHP 只从固定原入口加载。
$project = realpath(__DIR__ . '/../..');
$public = $project . '/server/public';
$preview = $project . '/.test-runtime/hospital-preview/admin';
$path = (string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
header('Referrer-Policy: no-referrer');
header('X-Content-Type-Options: nosniff');
header('Cache-Control: no-store');
function missing(): never
{
http_response_code(404);
header('Content-Type: application/json; charset=utf-8');
echo '{"code":0,"msg":"接口不存在","data":{}}';
exit;
}
if (!in_array($_SERVER['REMOTE_ADDR'] ?? '', ['127.0.0.1', '::1'], true)
|| !in_array($_SERVER['HTTP_HOST'] ?? '', ['127.0.0.1:8090', 'localhost:8090'], true)
|| array_key_exists('s', $_GET) || preg_match('/[\\\\\x00-\x20\x7f]/', $path)) {
missing();
}
if (in_array($method, ['GET', 'HEAD'], true) && ($path === '/admin' || str_starts_with($path, '/admin/'))) {
if ($path === '/admin/group-login') {
$file = $preview . '/group-login.html';
$type = 'text/html; charset=utf-8';
} elseif ($path === '/admin/group-sso.mjs') {
$file = $preview . '/group-sso.mjs';
$type = 'text/javascript; charset=utf-8';
} elseif (preg_match('~^/admin/(assets/[A-Za-z0-9_.@/-]+|ai-chat-icon\.png)$~D', $path)) {
$file = realpath($preview . substr($path, strlen('/admin')));
$base = realpath($preview);
$normalizedFile = is_string($file) ? str_replace('\\', '/', $file) : '';
$normalizedBase = is_string($base) ? str_replace('\\', '/', $base) . '/' : '';
if (!$file || !$base || !str_starts_with($normalizedFile, $normalizedBase) || !is_file($file)) { missing(); }
$types = ['js' => 'text/javascript', 'mjs' => 'text/javascript', 'css' => 'text/css', 'png' => 'image/png',
'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml',
'woff' => 'font/woff', 'woff2' => 'font/woff2', 'ttf' => 'font/ttf', 'ico' => 'image/x-icon'];
$type = $types[strtolower(pathinfo($file, PATHINFO_EXTENSION))] ?? null;
if ($type === null) { missing(); }
} elseif (preg_match('~^/admin(?:/[A-Za-z0-9_-]+)*/?$~D', $path)) {
$file = $preview . '/index.html';
$type = 'text/html; charset=utf-8';
} else { missing(); }
if (!is_file($file)) { missing(); }
header('Content-Type: ' . $type);
if ($method !== 'HEAD') { readfile($file); }
exit;
}
// 不透传任意磁盘路径、安装器、测试脚本或 PHP 文件。
if (!in_array($method, ['GET', 'POST', 'OPTIONS'], true)
|| !preg_match('~^/adminapi/[A-Za-z][A-Za-z0-9_.]*/[A-Za-z][A-Za-z0-9_]*$~D', $path)) {
missing();
}
$root = $project . '/server';
if (!is_file($root . '/vendor/autoload.php') || !is_file($root . '/config/install.lock')) {
http_response_code(503);
header('Content-Type: application/json; charset=utf-8');
echo '{"code":0,"msg":"原项目服务尚未配置","data":{}}';
exit;
}
require $root . '/vendor/autoload.php';
$_SERVER['SCRIPT_FILENAME'] = $public . '/index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['PATH_INFO'] = $path;
foreach (['HTTP_X_FORWARDED_HOST', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP'] as $key) { unset($_SERVER[$key]); }
$application = new class($root . DIRECTORY_SEPARATOR) extends think\App {
public function loadEnv(string $envName = ''): void
{
parent::loadEnv($envName);
$this->env->set('APP_DEBUG', false);
$this->env->set('LOCAL_DEBUG_LOGIN_SKIP_PASSWORD', false);
}
};
$application->initialize();
$application->request->setPathinfo(ltrim($path, '/'));
$http = $application->http;
$response = $http->run();
$response->send();
$http->end($response);