1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Http\Middleware; |
21
|
|
|
|
22
|
|
|
use Aura\Router\Route; |
23
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
24
|
|
|
use Fisharebest\Webtrees\Auth; |
25
|
|
|
use Fisharebest\Webtrees\Exceptions\HttpNotFoundException; |
26
|
|
|
use Fisharebest\Webtrees\Http\RequestHandlers\HomePage; |
27
|
|
|
use Fisharebest\Webtrees\Services\TreeService; |
28
|
|
|
use Fisharebest\Webtrees\Tree; |
29
|
|
|
use Psr\Http\Message\ResponseInterface; |
30
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
31
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
32
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
33
|
|
|
|
34
|
|
|
use function app; |
35
|
|
|
use function redirect; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Transfer route parameters to the request. |
39
|
|
|
*/ |
40
|
|
|
class LoadRouteParameters implements MiddlewareInterface |
41
|
|
|
{ |
42
|
|
|
/** @var TreeService */ |
43
|
|
|
private $tree_service; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AddRouteParameters constructor. |
47
|
|
|
* |
48
|
|
|
* @param TreeService $tree_service |
49
|
|
|
*/ |
50
|
|
|
public function __construct(TreeService $tree_service) |
51
|
|
|
{ |
52
|
|
|
$this->tree_service = $tree_service; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ServerRequestInterface $request |
57
|
|
|
* @param RequestHandlerInterface $handler |
58
|
|
|
* |
59
|
|
|
* @return ResponseInterface |
60
|
|
|
*/ |
61
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
62
|
|
|
{ |
63
|
|
|
$route = $request->getAttribute('route'); |
64
|
|
|
assert($route instanceof Route); |
65
|
|
|
|
66
|
|
|
foreach ($route->attributes as $key => $value) { |
67
|
|
|
if ($key === 'tree') { |
68
|
|
|
// Convert a tree name to a tree object. |
69
|
|
|
$value = $this->tree_service->all()->get($value); |
70
|
|
|
|
71
|
|
|
// Not a valid tree, and parameter is required? |
72
|
|
|
if ($value === null && strpos($route->path, '{tree}') !== false) { |
73
|
|
|
if (Auth::check() || $request->getMethod() === RequestMethodInterface::METHOD_POST) { |
74
|
|
|
throw new HttpNotFoundException(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return redirect(HomePage::class); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
app()->instance(Tree::class, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$request = $request->withAttribute((string) $key, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $handler->handle($request); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|