xref: /webtrees/app/Http/RequestHandlers/RobotsTxt.php (revision 16a40a66d9d5430ce8d1b0a796eecf4876efb58e)
1d0bad8beSGreg Roach<?php
2d0bad8beSGreg Roach
3d0bad8beSGreg Roach/**
4d0bad8beSGreg Roach * webtrees: online genealogy
5d0bad8beSGreg Roach * Copyright (C) 2019 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
15d0bad8beSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16d0bad8beSGreg Roach */
17d0bad8beSGreg Roach
18d0bad8beSGreg Roachdeclare(strict_types=1);
19d0bad8beSGreg Roach
20d0bad8beSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d0bad8beSGreg Roach
22d0bad8beSGreg Roachuse Fisharebest\Webtrees\Module\SiteMapModule;
23d0bad8beSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
24d0bad8beSGreg Roachuse Psr\Http\Message\ResponseInterface;
25d0bad8beSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26d0bad8beSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
27d0bad8beSGreg Roach
28d0bad8beSGreg Roachuse function response;
29d0bad8beSGreg Roach
30d0bad8beSGreg Roachuse const PHP_URL_PATH;
31d0bad8beSGreg Roach
32d0bad8beSGreg Roach/**
33d0bad8beSGreg Roach * Generate a robots exclusion file.
34d0bad8beSGreg Roach *
35d0bad8beSGreg Roach * @link https://robotstxt.org
36d0bad8beSGreg Roach */
37d0bad8beSGreg Roachclass RobotsTxt implements RequestHandlerInterface
38d0bad8beSGreg Roach{
39d0bad8beSGreg Roach    private const DISALLOWED_PATHS = [
40d0bad8beSGreg Roach        'admin',
41d0bad8beSGreg Roach        'manager',
42d0bad8beSGreg Roach        'moderator',
43d0bad8beSGreg Roach        'editor',
44d0bad8beSGreg Roach        'account',
45d0bad8beSGreg Roach    ];
46d0bad8beSGreg Roach
47d0bad8beSGreg Roach    /** @var ModuleService */
48d0bad8beSGreg Roach    private $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 = [
68d0bad8beSGreg Roach            'base_url'         => $base_url,
69d0bad8beSGreg Roach            'base_path'        => parse_url($base_url, PHP_URL_PATH) ?? '',
70d0bad8beSGreg Roach            'disallowed_paths' => self::DISALLOWED_PATHS,
71d0bad8beSGreg Roach            'sitemap_url'      => '',
72d0bad8beSGreg Roach        ];
73d0bad8beSGreg Roach
74d0bad8beSGreg Roach        $sitemap_module = $this->module_service->findByInterface(SiteMapModule::class)->first();
75d0bad8beSGreg Roach
76d0bad8beSGreg Roach        if ($sitemap_module instanceof SiteMapModule) {
77*16a40a66SGreg Roach            $data['sitemap_url'] = route('sitemap-index');
78d0bad8beSGreg Roach        }
79d0bad8beSGreg Roach
80d0bad8beSGreg Roach        return response(view('robots-txt', $data))
81d0bad8beSGreg Roach            ->withHeader('Content-type', 'text/plain');
82d0bad8beSGreg Roach    }
83d0bad8beSGreg Roach}
84