xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision 928e8f1ab8bf8a4eae9e8ec7a464d6b65ee174d5)
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 function (User $user): array {
98            return [$user->userName() => $user->userName()];
99        });
100        $user_options->prepend('', '');
101
102        $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array {
103            return [$tree->name() => $tree->title()];
104        });
105        $tree_options->prepend('', '');
106
107        $title = I18N::translate('Website logs');
108
109        return $this->viewResponse('admin/site-logs', [
110            'action'       => $action,
111            'earliest'     => $earliest,
112            'from'         => $from,
113            'tree'         => $tree,
114            'ip'           => $ip,
115            'latest'       => $latest,
116            'tree_options' => $tree_options,
117            'title'        => $title,
118            'to'           => $to,
119            'text'         => $text,
120            'type'         => $type,
121            'username'     => $username,
122            'user_options' => $user_options,
123        ]);
124    }
125}
126