xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision 5cac87ae725131e0533df3938e6bed4a6eda5c1b)
157bfa969SGreg Roach<?php
257bfa969SGreg Roach
357bfa969SGreg Roach/**
457bfa969SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
657bfa969SGreg Roach * This program is free software: you can redistribute it and/or modify
757bfa969SGreg Roach * it under the terms of the GNU General Public License as published by
857bfa969SGreg Roach * the Free Software Foundation, either version 3 of the License, or
957bfa969SGreg Roach * (at your option) any later version.
1057bfa969SGreg Roach * This program is distributed in the hope that it will be useful,
1157bfa969SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1257bfa969SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1357bfa969SGreg Roach * GNU General Public License for more details.
1457bfa969SGreg 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/>.
1657bfa969SGreg Roach */
1757bfa969SGreg Roach
1857bfa969SGreg Roachdeclare(strict_types=1);
1957bfa969SGreg Roach
2057bfa969SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2157bfa969SGreg Roach
22*5cac87aeSGreg Roachuse DateTimeImmutable;
23*5cac87aeSGreg Roachuse DateTimeZone;
24*5cac87aeSGreg Roachuse Fisharebest\Webtrees\Auth;
25*5cac87aeSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
266f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2757bfa969SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2857bfa969SGreg Roachuse Fisharebest\Webtrees\I18N;
29d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
3057bfa969SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
3157bfa969SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
3257bfa969SGreg Roachuse Fisharebest\Webtrees\Tree;
3357bfa969SGreg Roachuse Fisharebest\Webtrees\User;
346c21f8beSGreg Roachuse Fisharebest\Webtrees\Validator;
3557bfa969SGreg Roachuse Psr\Http\Message\ResponseInterface;
3657bfa969SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3757bfa969SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3857bfa969SGreg Roach
39*5cac87aeSGreg Roachuse function date;
4057bfa969SGreg Roachuse function max;
4157bfa969SGreg Roachuse function min;
4257bfa969SGreg Roach
4357bfa969SGreg Roach/**
4457bfa969SGreg Roach * Show logs.
4557bfa969SGreg Roach */
4657bfa969SGreg Roachclass SiteLogsPage implements RequestHandlerInterface
4757bfa969SGreg Roach{
4857bfa969SGreg Roach    use ViewResponseTrait;
4957bfa969SGreg Roach
50c4943cffSGreg Roach    private TreeService $tree_service;
5157bfa969SGreg Roach
52c4943cffSGreg Roach    private UserService $user_service;
5357bfa969SGreg Roach
5457bfa969SGreg Roach    /**
5557bfa969SGreg Roach     * @param TreeService $tree_service
5657bfa969SGreg Roach     * @param UserService $user_service
5757bfa969SGreg Roach     */
5857bfa969SGreg Roach    public function __construct(TreeService $tree_service, UserService $user_service)
5957bfa969SGreg Roach    {
6057bfa969SGreg Roach        $this->tree_service = $tree_service;
6157bfa969SGreg Roach        $this->user_service = $user_service;
6257bfa969SGreg Roach    }
6357bfa969SGreg Roach
6457bfa969SGreg Roach    /**
6557bfa969SGreg Roach     * @param ServerRequestInterface $request
6657bfa969SGreg Roach     *
6757bfa969SGreg Roach     * @return ResponseInterface
6857bfa969SGreg Roach     */
6957bfa969SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
7057bfa969SGreg Roach    {
7157bfa969SGreg Roach        $this->layout = 'layouts/administration';
7257bfa969SGreg Roach
73*5cac87aeSGreg Roach        // First and last change in the database
74*5cac87aeSGreg Roach        $earliest = DB::table('log')->min('log_time') ?? date('Y-m-d H:i:s');;
75*5cac87aeSGreg Roach        $latest   = DB::table('log')->max('log_time') ?? date('Y-m-d H:i:s');;
7657bfa969SGreg Roach
77*5cac87aeSGreg Roach        $earliest = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $earliest, new DateTimeZone('UTC'))
78*5cac87aeSGreg Roach            ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
79*5cac87aeSGreg Roach            ->format('Y-m-d');
80*5cac87aeSGreg Roach
81*5cac87aeSGreg Roach        $latest = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $latest, new DateTimeZone('UTC'))
82*5cac87aeSGreg Roach            ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
83*5cac87aeSGreg Roach            ->format('Y-m-d');
8457bfa969SGreg Roach
856c21f8beSGreg Roach        $action   = Validator::queryParams($request)->string('action', '');
866c21f8beSGreg Roach        $from     = Validator::queryParams($request)->string('from', $earliest);
876c21f8beSGreg Roach        $to       = Validator::queryParams($request)->string('to', $latest);
886c21f8beSGreg Roach        $type     = Validator::queryParams($request)->string('type', '');
896c21f8beSGreg Roach        $text     = Validator::queryParams($request)->string('text', '');
906c21f8beSGreg Roach        $ip       = Validator::queryParams($request)->string('ip', '');
916c21f8beSGreg Roach        $username = Validator::queryParams($request)->string('username', '');
926c21f8beSGreg Roach        $tree     = Validator::queryParams($request)->string('tree', '');
9357bfa969SGreg Roach
9457bfa969SGreg Roach        $from = max($from, $earliest);
9557bfa969SGreg Roach        $to   = min(max($from, $to), $latest);
9657bfa969SGreg Roach
9757bfa969SGreg Roach        $user_options = $this->user_service->all()->mapWithKeys(static function (User $user): array {
9857bfa969SGreg Roach            return [$user->userName() => $user->userName()];
9957bfa969SGreg Roach        });
1001ebbf27aSGreg Roach        $user_options->prepend('', '');
10157bfa969SGreg Roach
10257bfa969SGreg Roach        $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array {
10357bfa969SGreg Roach            return [$tree->name() => $tree->title()];
10457bfa969SGreg Roach        });
1051ebbf27aSGreg Roach        $tree_options->prepend('', '');
10657bfa969SGreg Roach
10757bfa969SGreg Roach        $title = I18N::translate('Website logs');
10857bfa969SGreg Roach
10957bfa969SGreg Roach        return $this->viewResponse('admin/site-logs', [
11057bfa969SGreg Roach            'action'       => $action,
11157bfa969SGreg Roach            'earliest'     => $earliest,
11257bfa969SGreg Roach            'from'         => $from,
11357bfa969SGreg Roach            'tree'         => $tree,
11457bfa969SGreg Roach            'ip'           => $ip,
11557bfa969SGreg Roach            'latest'       => $latest,
11657bfa969SGreg Roach            'tree_options' => $tree_options,
11757bfa969SGreg Roach            'title'        => $title,
11857bfa969SGreg Roach            'to'           => $to,
11957bfa969SGreg Roach            'text'         => $text,
12057bfa969SGreg Roach            'type'         => $type,
12157bfa969SGreg Roach            'username'     => $username,
12257bfa969SGreg Roach            'user_options' => $user_options,
12357bfa969SGreg Roach        ]);
12457bfa969SGreg Roach    }
12557bfa969SGreg Roach}
126