1d0bad8beSGreg Roach<?php 2d0bad8beSGreg Roach 3d0bad8beSGreg Roach/** 4d0bad8beSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6d0bad8beSGreg Roach * This program is free software: you can redistribute it and/or modify 7d0bad8beSGreg Roach * it under the terms of the GNU General Public License as published by 8d0bad8beSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d0bad8beSGreg Roach * (at your option) any later version. 10d0bad8beSGreg Roach * This program is distributed in the hope that it will be useful, 11d0bad8beSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d0bad8beSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d0bad8beSGreg Roach * GNU General Public License for more details. 14d0bad8beSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16d0bad8beSGreg Roach */ 17d0bad8beSGreg Roach 18d0bad8beSGreg Roachdeclare(strict_types=1); 19d0bad8beSGreg Roach 20d0bad8beSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21d0bad8beSGreg Roach 22ffa287a1SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker; 23d0bad8beSGreg Roachuse Fisharebest\Webtrees\Module\SiteMapModule; 24d0bad8beSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 25*e96e110dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 26*e96e110dSGreg Roachuse Fisharebest\Webtrees\Tree; 27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 28d0bad8beSGreg Roachuse Psr\Http\Message\ResponseInterface; 29d0bad8beSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 30d0bad8beSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 31d0bad8beSGreg Roach 32d0bad8beSGreg Roachuse function response; 33d0bad8beSGreg Roach 34d0bad8beSGreg Roachuse const PHP_URL_PATH; 35d0bad8beSGreg Roach 36d0bad8beSGreg Roach/** 37d0bad8beSGreg Roach * Generate a robots exclusion file. 38d0bad8beSGreg Roach * 39d0bad8beSGreg Roach * @link https://robotstxt.org 40d0bad8beSGreg Roach */ 41d0bad8beSGreg Roachclass RobotsTxt implements RequestHandlerInterface 42d0bad8beSGreg Roach{ 43d0bad8beSGreg Roach private const DISALLOWED_PATHS = [ 44d0bad8beSGreg Roach 'admin', 45d0bad8beSGreg Roach 'manager', 46d0bad8beSGreg Roach 'moderator', 47d0bad8beSGreg Roach 'editor', 48d0bad8beSGreg Roach 'account', 49d0bad8beSGreg Roach ]; 50d0bad8beSGreg Roach 51c4943cffSGreg Roach private ModuleService $module_service; 52d0bad8beSGreg Roach 53*e96e110dSGreg Roach private TreeService $tree_service; 54*e96e110dSGreg Roach 55d0bad8beSGreg Roach /** 56d0bad8beSGreg Roach * @param ModuleService $module_service 57d0bad8beSGreg Roach */ 58*e96e110dSGreg Roach public function __construct(ModuleService $module_service, TreeService $tree_service) 59d0bad8beSGreg Roach { 60d0bad8beSGreg Roach $this->module_service = $module_service; 61*e96e110dSGreg Roach $this->tree_service = $tree_service; 62d0bad8beSGreg Roach } 63d0bad8beSGreg Roach 64d0bad8beSGreg Roach /** 65d0bad8beSGreg Roach * @param ServerRequestInterface $request 66d0bad8beSGreg Roach * 67d0bad8beSGreg Roach * @return ResponseInterface 68d0bad8beSGreg Roach */ 69d0bad8beSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 70d0bad8beSGreg Roach { 71b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 72*e96e110dSGreg Roach $trees = $this->tree_service->all()->map(static fn (Tree $tree): string => $tree->name()); 73d0bad8beSGreg Roach 74d0bad8beSGreg Roach $data = [ 75ffa287a1SGreg Roach 'bad_user_agents' => BadBotBlocker::BAD_ROBOTS, 76d0bad8beSGreg Roach 'base_url' => $base_url, 77d0bad8beSGreg Roach 'base_path' => parse_url($base_url, PHP_URL_PATH) ?? '', 78d0bad8beSGreg Roach 'disallowed_paths' => self::DISALLOWED_PATHS, 79d0bad8beSGreg Roach 'sitemap_url' => '', 80*e96e110dSGreg Roach 'trees' => $trees, 81d0bad8beSGreg Roach ]; 82d0bad8beSGreg Roach 83d0bad8beSGreg Roach $sitemap_module = $this->module_service->findByInterface(SiteMapModule::class)->first(); 84d0bad8beSGreg Roach 85d0bad8beSGreg Roach if ($sitemap_module instanceof SiteMapModule) { 8616a40a66SGreg Roach $data['sitemap_url'] = route('sitemap-index'); 87d0bad8beSGreg Roach } 88d0bad8beSGreg Roach 89d0bad8beSGreg Roach return response(view('robots-txt', $data)) 906172e7f6SGreg Roach ->withHeader('content-type', 'text/plain'); 91d0bad8beSGreg Roach } 92d0bad8beSGreg Roach} 93