Files
zhamao-framework/src/ZM/Http/StaticFileHandler.php

36 lines
1.0 KiB
PHP
Raw Normal View History

2020-05-23 17:23:29 +08:00
<?php
namespace ZM\Http;
2020-10-03 23:00:18 +08:00
use ZM\Config\ZMConfig;
2020-08-31 10:11:06 +08:00
use ZM\Console\Console;
2020-09-29 15:07:43 +08:00
use ZM\Utils\HttpUtil;
2020-05-23 17:23:29 +08:00
class StaticFileHandler
{
public function __construct($filename, $path) {
$full_path = realpath($path . "/" . $filename);
$response = ctx()->getResponse();
2021-02-09 17:09:09 +08:00
Console::debug("Full path: " . $full_path);
2020-05-23 17:23:29 +08:00
if ($full_path !== false) {
if (strpos($full_path, $path) !== 0) {
$response->status(403);
$response->end("403 Forbidden");
return true;
} else {
2021-02-09 17:09:09 +08:00
if (is_file($full_path)) {
2020-05-23 17:23:29 +08:00
$exp = strtolower(pathinfo($full_path)['extension'] ?? "unknown");
2020-10-03 23:00:18 +08:00
$response->setHeader("Content-Type", ZMConfig::get("file_header")[$exp] ?? "application/octet-stream");
2020-05-23 17:23:29 +08:00
$response->end(file_get_contents($full_path));
return true;
}
}
}
$response->status(404);
2020-09-29 15:07:43 +08:00
$response->end(HttpUtil::getHttpCodePage(404));
2020-05-23 17:23:29 +08:00
return true;
}
}