xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 DateTimeImmutable;
23use DateTimeZone;
24use Fisharebest\Webtrees\Auth;
25use Fisharebest\Webtrees\Contracts\UserInterface;
26use Fisharebest\Webtrees\DB;
27use Fisharebest\Webtrees\Http\ViewResponseTrait;
28use Fisharebest\Webtrees\I18N;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Services\TreeService;
31use Fisharebest\Webtrees\Services\UserService;
32use Fisharebest\Webtrees\Tree;
33use Fisharebest\Webtrees\User;
34use Fisharebest\Webtrees\Validator;
35use Psr\Http\Message\ResponseInterface;
36use Psr\Http\Message\ServerRequestInterface;
37use Psr\Http\Server\RequestHandlerInterface;
38
39use function date;
40use function max;
41use function min;
42
43/**
44 * Show logs.
45 */
46class SiteLogsPage implements RequestHandlerInterface
47{
48    use ViewResponseTrait;
49
50    private TreeService $tree_service;
51
52    private UserService $user_service;
53
54    /**
55     * @param TreeService $tree_service
56     * @param UserService $user_service
57     */
58    public function __construct(TreeService $tree_service, UserService $user_service)
59    {
60        $this->tree_service = $tree_service;
61        $this->user_service = $user_service;
62    }
63
64    /**
65     * @param ServerRequestInterface $request
66     *
67     * @return ResponseInterface
68     */
69    public function handle(ServerRequestInterface $request): ResponseInterface
70    {
71        $this->layout = 'layouts/administration';
72
73        // First and last change in the database
74        $earliest = DB::table('log')->min('log_time') ?? date('Y-m-d H:i:s');
75        $latest   = DB::table('log')->max('log_time') ?? date('Y-m-d H:i:s');
76
77        $earliest = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $earliest, new DateTimeZone('UTC'))
78            ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
79            ->format('Y-m-d');
80
81        $latest = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $latest, new DateTimeZone('UTC'))
82            ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
83            ->format('Y-m-d');
84
85        $action   = Validator::queryParams($request)->string('action', '');
86        $from     = Validator::queryParams($request)->string('from', $earliest);
87        $to       = Validator::queryParams($request)->string('to', $latest);
88        $type     = Validator::queryParams($request)->string('type', '');
89        $text     = Validator::queryParams($request)->string('text', '');
90        $ip       = Validator::queryParams($request)->string('ip', '');
91        $username = Validator::queryParams($request)->string('username', '');
92        $tree     = Validator::queryParams($request)->string('tree', '');
93
94        $from = max($from, $earliest);
95        $to   = min(max($from, $to), $latest);
96
97        $user_options = $this->user_service->all()->mapWithKeys(static fn(User $user): array => [$user->userName() => $user->userName()]);
98        $user_options->prepend('', '');
99
100        $tree_options = $this->tree_service->all()->mapWithKeys(static fn(Tree $tree): array => [$tree->name() => $tree->title()]);
101        $tree_options->prepend('', '');
102
103        $title = I18N::translate('Website logs');
104
105        return $this->viewResponse('admin/site-logs', [
106            'action'       => $action,
107            'earliest'     => $earliest,
108            'from'         => $from,
109            'tree'         => $tree,
110            'ip'           => $ip,
111            'latest'       => $latest,
112            'tree_options' => $tree_options,
113            'title'        => $title,
114            'to'           => $to,
115            'text'         => $text,
116            'type'         => $type,
117            'username'     => $username,
118            'user_options' => $user_options,
119        ]);
120    }
121}
122