xref: /webtrees/app/Http/RequestHandlers/RobotsTxt.php (revision afa67798854828b1edc33dd077960ec2b18e6140)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
23use Fisharebest\Webtrees\Module\SiteMapModule;
24use Fisharebest\Webtrees\Services\ModuleService;
25use Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27use Psr\Http\Server\RequestHandlerInterface;
28
29use function response;
30
31use const PHP_URL_PATH;
32
33/**
34 * Generate a robots exclusion file.
35 *
36 * @link https://robotstxt.org
37 */
38class RobotsTxt implements RequestHandlerInterface
39{
40    private const DISALLOWED_PATHS = [
41        'admin',
42        'manager',
43        'moderator',
44        'editor',
45        'account',
46    ];
47
48    /** @var ModuleService */
49    private $module_service;
50
51    /**
52     * @param ModuleService $module_service
53     */
54    public function __construct(ModuleService $module_service)
55    {
56        $this->module_service = $module_service;
57    }
58
59    /**
60     * @param ServerRequestInterface $request
61     *
62     * @return ResponseInterface
63     */
64    public function handle(ServerRequestInterface $request): ResponseInterface
65    {
66        $base_url = $request->getAttribute('base_url');
67
68        $data = [
69            'bad_user_agents'  => BadBotBlocker::BAD_ROBOTS,
70            'base_url'         => $base_url,
71            'base_path'        => parse_url($base_url, PHP_URL_PATH) ?? '',
72            'disallowed_paths' => self::DISALLOWED_PATHS,
73            'sitemap_url'      => '',
74        ];
75
76        $sitemap_module = $this->module_service->findByInterface(SiteMapModule::class)->first();
77
78        if ($sitemap_module instanceof SiteMapModule) {
79            $data['sitemap_url'] = route('sitemap-index');
80        }
81
82        return response(view('robots-txt', $data))
83            ->withHeader('Content-type', 'text/plain');
84    }
85}
86