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