null,
'protocol' => null,
'rootUrl' => null,
'pageUrl' => null,
'date' => null,
'year' => null,
'dateTimestamp' => null,
];
// 快速獲取請(qǐng)求路徑(優(yōu)化 parse_url 調(diào)用)
$requestUri = $_SERVER['REQUEST_URI'] ?? '/';
$path = $requestUri;
if (($pos = strpos($requestUri, '?')) !== false) {
$path = substr($requestUri, 0, $pos);
}
if ($path === '' || $path === '/') {
$path = '/index.html';
}
// 構(gòu)建文件路徑(避免 ltrim 和多次字符串操作)
$requestPath = $path[0] === '/' ? substr($path, 1) : $path;
$filePath = TEMPLATE_DIR . '/' . $requestPath;
// 快速文件存在檢查(先檢查文件,再檢查路徑安全)
if (!file_exists($filePath)) {
http_response_code(404);
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
echo '
女人与公拘交酡视频,亚洲精品久久久中文字幕,wwwav在线免费观看
404
';
return;
}
// 安全檢查:快速路徑驗(yàn)證(避免 realpath 系統(tǒng)調(diào)用)
$realPath = realpath($filePath);
if ($realPath === false || !str_starts_with($realPath, TEMPLATE_DIR)) {
http_response_code(404);
header('Cache-Control: no-cache, no-store, must-revalidate');
echo '404404
';
return;
}
// 讀取文件內(nèi)容(一次性讀取,避免多次 I/O)
$content = file_get_contents($filePath);
if ($content === false) {
http_response_code(500);
echo '500500
';
return;
}
// 快速獲取服務(wù)器變量(避免多次訪問 $_SERVER)
$host = $_SERVER['HTTP_HOST'] ?? '';
$https = $_SERVER['HTTPS'] ?? '';
$port = $_SERVER['SERVER_PORT'] ?? '';
// 緩存域名(移除端口號(hào))
if ($cache['domain'] === null) {
$cache['domain'] = ($pos = strpos($host, ':')) !== false ? substr($host, 0, $pos) : $host;
}
// 緩存協(xié)議判斷(優(yōu)化條件判斷)
if ($cache['protocol'] === null) {
$cache['protocol'] = ($https !== '' && $https !== 'off') || $port === '443' ? 'https://' : 'http://';
}
// 緩存根地址
if ($cache['rootUrl'] === null) {
$cache['rootUrl'] = $cache['protocol'] . $cache['domain'] . '/';
}
// 緩存頁面地址
if ($cache['pageUrl'] === null) {
$cache['pageUrl'] = $cache['protocol'] . $cache['domain'] . $path;
}
// 緩存日期和年份(同一天內(nèi)復(fù)用,減少 date() 調(diào)用)
if ($cache['date'] === null) {
$cache['dateTimestamp'] = time();
$cache['date'] = date('Y-m-d', $cache['dateTimestamp']);
$cache['year'] = date('Y', $cache['dateTimestamp']);
}
// 獲取文件擴(kuò)展名(優(yōu)化 pathinfo 調(diào)用)
$extension = '';
if (($pos = strrpos($filePath, '.')) !== false) {
$extension = strtolower(substr($filePath, $pos + 1));
}
// 靜態(tài)文件檢查:CSS、JS、圖片等應(yīng)由 Nginx 直接服務(wù),不經(jīng)過 PHP
$staticExtensions = ['css', 'js', 'js1', 'png', 'jpg', 'jpeg', 'gif', 'svg', 'ico', 'woff', 'woff2', 'ttf', 'eot', 'mp4', 'webm', 'mp3', 'pdf', 'zip'];
if (in_array($extension, $staticExtensions, true)) {
http_response_code(404);
header('Cache-Control: no-cache, no-store, must-revalidate');
echo '404404
';
return;
}
// 設(shè)置 Content-Type(快速查找)
$contentType = match($extension) {
'html' => 'text/html; charset=UTF-8',
'xml' => 'application/xml; charset=UTF-8',
'txt' => 'text/plain; charset=UTF-8',
default => 'text/html; charset=UTF-8',
};
// 生成 ETag(基于文件路徑和日期)
$etag = md5($filePath . $cache['date']);
// 計(jì)算 Last-Modified(基于當(dāng)前日期的 00:00:00)
$lastModified = strtotime($cache['date'] . ' 00:00:00');
// 一次性設(shè)置所有 header(減少系統(tǒng)調(diào)用)
header('Content-Type: ' . $contentType);
header('ETag: "' . $etag . '"');
header('Cache-Control: public, max-age=86400, must-revalidate');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
// 執(zhí)行標(biāo)簽替換(使用 strtr 代替多次 str_replace,性能更好)
$content = strtr($content, [
'www.morettiandmore.com' => $cache['domain'],
'http://www.morettiandmore.com/' => $cache['rootUrl'],
'http://www.morettiandmore.com/index.php' => $cache['pageUrl'],
'2026-01-18' => $cache['date'],
'2026' => $cache['year'],
]);
// 輸出內(nèi)容
echo $content;
}
// 執(zhí)行主處理邏輯
processRequest();