1*d7de6d4cSGreg Roach<?php 2*d7de6d4cSGreg Roach 3*d7de6d4cSGreg Roach/** 4*d7de6d4cSGreg Roach * webtrees: online genealogy 5*d7de6d4cSGreg Roach * Copyright (C) 2023 webtrees development team 6*d7de6d4cSGreg Roach * This program is free software: you can redistribute it and/or modify 7*d7de6d4cSGreg Roach * it under the terms of the GNU General Public License as published by 8*d7de6d4cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*d7de6d4cSGreg Roach * (at your option) any later version. 10*d7de6d4cSGreg Roach * This program is distributed in the hope that it will be useful, 11*d7de6d4cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*d7de6d4cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*d7de6d4cSGreg Roach * GNU General Public License for more details. 14*d7de6d4cSGreg Roach * You should have received a copy of the GNU General Public License 15*d7de6d4cSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16*d7de6d4cSGreg Roach */ 17*d7de6d4cSGreg Roach 18*d7de6d4cSGreg Roachdeclare(strict_types=1); 19*d7de6d4cSGreg Roach 20*d7de6d4cSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 21*d7de6d4cSGreg Roach 22*d7de6d4cSGreg Roachuse Fig\Http\Message\StatusCodeInterface; 23*d7de6d4cSGreg Roachuse Fisharebest\Webtrees\Mime; 24*d7de6d4cSGreg Roachuse Fisharebest\Webtrees\Webtrees; 25*d7de6d4cSGreg Roachuse Psr\Http\Message\ResponseInterface; 26*d7de6d4cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 27*d7de6d4cSGreg Roachuse Psr\Http\Server\MiddlewareInterface; 28*d7de6d4cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 29*d7de6d4cSGreg Roach 30*d7de6d4cSGreg Roachuse function file_exists; 31*d7de6d4cSGreg Roachuse function file_get_contents; 32*d7de6d4cSGreg Roachuse function pathinfo; 33*d7de6d4cSGreg Roachuse function response; 34*d7de6d4cSGreg Roachuse function str_starts_with; 35*d7de6d4cSGreg Roachuse function strtoupper; 36*d7de6d4cSGreg Roach 37*d7de6d4cSGreg Roachuse const PATHINFO_EXTENSION; 38*d7de6d4cSGreg Roach 39*d7de6d4cSGreg Roach/** 40*d7de6d4cSGreg Roach * Provide access to files in the folder /public, for cli-server requests and in case the web-server doesn't do this. 41*d7de6d4cSGreg Roach */ 42*d7de6d4cSGreg Roachclass PublicFiles implements MiddlewareInterface 43*d7de6d4cSGreg Roach{ 44*d7de6d4cSGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 45*d7de6d4cSGreg Roach { 46*d7de6d4cSGreg Roach $path = $request->getUri()->getPath(); 47*d7de6d4cSGreg Roach if (str_starts_with($path, '/public/') && !str_contains($path, '..')) { 48*d7de6d4cSGreg Roach $file = Webtrees::ROOT_DIR . $path; 49*d7de6d4cSGreg Roach 50*d7de6d4cSGreg Roach if (file_exists($file)) { 51*d7de6d4cSGreg Roach $content = file_get_contents($file); 52*d7de6d4cSGreg Roach $extension = strtoupper(pathinfo($file, PATHINFO_EXTENSION)); 53*d7de6d4cSGreg Roach $mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE; 54*d7de6d4cSGreg Roach 55*d7de6d4cSGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 56*d7de6d4cSGreg Roach 'cache-control' => 'public,max-age=31536000', 57*d7de6d4cSGreg Roach 'content-type' => $mime_type, 58*d7de6d4cSGreg Roach ]); 59*d7de6d4cSGreg Roach } 60*d7de6d4cSGreg Roach } 61*d7de6d4cSGreg Roach 62*d7de6d4cSGreg Roach return $handler->handle($request); 63*d7de6d4cSGreg Roach } 64*d7de6d4cSGreg Roach} 65