xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
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
225cac87aeSGreg Roachuse DateTimeImmutable;
235cac87aeSGreg Roachuse DateTimeZone;
245cac87aeSGreg Roachuse Fisharebest\Webtrees\Auth;
255cac87aeSGreg 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
395cac87aeSGreg 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
735cac87aeSGreg Roach        // First and last change in the database
744705d87cSGreg Roach        $earliest = DB::table('log')->min('log_time') ?? date('Y-m-d H:i:s');
754705d87cSGreg Roach        $latest   = DB::table('log')->max('log_time') ?? date('Y-m-d H:i:s');
7657bfa969SGreg Roach
775cac87aeSGreg Roach        $earliest = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $earliest, new DateTimeZone('UTC'))
785cac87aeSGreg Roach            ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
795cac87aeSGreg Roach            ->format('Y-m-d');
805cac87aeSGreg Roach
815cac87aeSGreg Roach        $latest = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $latest, new DateTimeZone('UTC'))
825cac87aeSGreg Roach            ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
835cac87aeSGreg 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
97*f25fc0f9SGreg Roach        $user_options = $this->user_service->all()->mapWithKeys(static fn (User $user): array => [$user->userName() => $user->userName()]);
981ebbf27aSGreg Roach        $user_options->prepend('', '');
9957bfa969SGreg Roach
100*f25fc0f9SGreg Roach        $tree_options = $this->tree_service->all()->mapWithKeys(static fn (Tree $tree): array => [$tree->name() => $tree->title()]);
1011ebbf27aSGreg Roach        $tree_options->prepend('', '');
10257bfa969SGreg Roach
10357bfa969SGreg Roach        $title = I18N::translate('Website logs');
10457bfa969SGreg Roach
10557bfa969SGreg Roach        return $this->viewResponse('admin/site-logs', [
10657bfa969SGreg Roach            'action'       => $action,
10757bfa969SGreg Roach            'earliest'     => $earliest,
10857bfa969SGreg Roach            'from'         => $from,
10957bfa969SGreg Roach            'tree'         => $tree,
11057bfa969SGreg Roach            'ip'           => $ip,
11157bfa969SGreg Roach            'latest'       => $latest,
11257bfa969SGreg Roach            'tree_options' => $tree_options,
11357bfa969SGreg Roach            'title'        => $title,
11457bfa969SGreg Roach            'to'           => $to,
11557bfa969SGreg Roach            'text'         => $text,
11657bfa969SGreg Roach            'type'         => $type,
11757bfa969SGreg Roach            'username'     => $username,
11857bfa969SGreg Roach            'user_options' => $user_options,
11957bfa969SGreg Roach        ]);
12057bfa969SGreg Roach    }
12157bfa969SGreg Roach}
122