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