bibis/public/attachment/index.php

36 行
1005 B
PHP

<?php
require_once(__DIR__ . '/../../require.php');
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (!ENABLE_ATTACHMENT) { return on_error(404, ['Not Found.']); }
$id = $_GET['id'] ?? '';
if ($id == '' || !preg_match('/^[a-z0-9]{32}$/', $id)) { return on_error(400, ['URLが不正。']); }
$filepath = ATTACHMENT_DIR . $id . '.gz';
if (!file_exists($filepath)) { return on_error(404, ['Not Found.']); }
ob_start();
readgzfile($filepath);
$buffer = ob_get_clean();
$type = get_image_type($buffer);
if (!isset($type)) { return on_error(500, ['ファイルが不正。']); }
$php_time = filemtime(__FILE__);
$attachment_time = filemtime($filepath);
$etag = '"' . $php_time . '.' . $attachment_time . '"';
header('Cache-Control: max-age=86400');
header("ETag: {$etag}");
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
if ($_SERVER['HTTP_IF_NONE_MATCH'] === $etag) {
header('HTTP/1.1 304 Not Modified', true, 304);
exit();
}
}
header("Content-Type: {$type}");
echo $buffer;
}