xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision d11be7027e34e3121be11cc025421873364403f9)
157bfa969SGreg Roach<?php
257bfa969SGreg Roach
357bfa969SGreg Roach/**
457bfa969SGreg Roach * webtrees: online genealogy
5*d11be702SGreg 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
2257bfa969SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2357bfa969SGreg Roachuse Fisharebest\Webtrees\I18N;
24d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
2557bfa969SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
2657bfa969SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
2757bfa969SGreg Roachuse Fisharebest\Webtrees\Tree;
2857bfa969SGreg Roachuse Fisharebest\Webtrees\User;
296c21f8beSGreg Roachuse Fisharebest\Webtrees\Validator;
3057bfa969SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3157bfa969SGreg Roachuse Psr\Http\Message\ResponseInterface;
3257bfa969SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3357bfa969SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3457bfa969SGreg Roach
3557bfa969SGreg Roachuse function max;
3657bfa969SGreg Roachuse function min;
3757bfa969SGreg Roach
3857bfa969SGreg Roach/**
3957bfa969SGreg Roach * Show logs.
4057bfa969SGreg Roach */
4157bfa969SGreg Roachclass SiteLogsPage implements RequestHandlerInterface
4257bfa969SGreg Roach{
4357bfa969SGreg Roach    use ViewResponseTrait;
4457bfa969SGreg Roach
45c4943cffSGreg Roach    private TreeService $tree_service;
4657bfa969SGreg Roach
47c4943cffSGreg Roach    private UserService $user_service;
4857bfa969SGreg Roach
4957bfa969SGreg Roach    /**
5057bfa969SGreg Roach     * @param TreeService $tree_service
5157bfa969SGreg Roach     * @param UserService $user_service
5257bfa969SGreg Roach     */
5357bfa969SGreg Roach    public function __construct(TreeService $tree_service, UserService $user_service)
5457bfa969SGreg Roach    {
5557bfa969SGreg Roach        $this->tree_service = $tree_service;
5657bfa969SGreg Roach        $this->user_service = $user_service;
5757bfa969SGreg Roach    }
5857bfa969SGreg Roach
5957bfa969SGreg Roach    /**
6057bfa969SGreg Roach     * @param ServerRequestInterface $request
6157bfa969SGreg Roach     *
6257bfa969SGreg Roach     * @return ResponseInterface
6357bfa969SGreg Roach     */
6457bfa969SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6557bfa969SGreg Roach    {
6657bfa969SGreg Roach        $this->layout = 'layouts/administration';
6757bfa969SGreg Roach
6857bfa969SGreg Roach        $earliest = DB::table('log')->min('log_time');
6957bfa969SGreg Roach        $latest   = DB::table('log')->max('log_time');
7057bfa969SGreg Roach
716c21f8beSGreg Roach        $earliest = Registry::timestampFactory()->fromString($earliest)->toDateString();
726c21f8beSGreg Roach        $latest   = Registry::timestampFactory()->fromString($latest)->toDateString();
7357bfa969SGreg Roach
746c21f8beSGreg Roach        $action   = Validator::queryParams($request)->string('action', '');
756c21f8beSGreg Roach        $from     = Validator::queryParams($request)->string('from', $earliest);
766c21f8beSGreg Roach        $to       = Validator::queryParams($request)->string('to', $latest);
776c21f8beSGreg Roach        $type     = Validator::queryParams($request)->string('type', '');
786c21f8beSGreg Roach        $text     = Validator::queryParams($request)->string('text', '');
796c21f8beSGreg Roach        $ip       = Validator::queryParams($request)->string('ip', '');
806c21f8beSGreg Roach        $username = Validator::queryParams($request)->string('username', '');
816c21f8beSGreg Roach        $tree     = Validator::queryParams($request)->string('tree', '');
8257bfa969SGreg Roach
8357bfa969SGreg Roach        $from = max($from, $earliest);
8457bfa969SGreg Roach        $to   = min(max($from, $to), $latest);
8557bfa969SGreg Roach
8657bfa969SGreg Roach        $user_options = $this->user_service->all()->mapWithKeys(static function (User $user): array {
8757bfa969SGreg Roach            return [$user->userName() => $user->userName()];
8857bfa969SGreg Roach        });
891ebbf27aSGreg Roach        $user_options->prepend('', '');
9057bfa969SGreg Roach
9157bfa969SGreg Roach        $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array {
9257bfa969SGreg Roach            return [$tree->name() => $tree->title()];
9357bfa969SGreg Roach        });
941ebbf27aSGreg Roach        $tree_options->prepend('', '');
9557bfa969SGreg Roach
9657bfa969SGreg Roach        $title = I18N::translate('Website logs');
9757bfa969SGreg Roach
9857bfa969SGreg Roach        return $this->viewResponse('admin/site-logs', [
9957bfa969SGreg Roach            'action'       => $action,
10057bfa969SGreg Roach            'earliest'     => $earliest,
10157bfa969SGreg Roach            'from'         => $from,
10257bfa969SGreg Roach            'tree'         => $tree,
10357bfa969SGreg Roach            'ip'           => $ip,
10457bfa969SGreg Roach            'latest'       => $latest,
10557bfa969SGreg Roach            'tree_options' => $tree_options,
10657bfa969SGreg Roach            'title'        => $title,
10757bfa969SGreg Roach            'to'           => $to,
10857bfa969SGreg Roach            'text'         => $text,
10957bfa969SGreg Roach            'type'         => $type,
11057bfa969SGreg Roach            'username'     => $username,
11157bfa969SGreg Roach            'user_options' => $user_options,
11257bfa969SGreg Roach        ]);
11357bfa969SGreg Roach    }
11457bfa969SGreg Roach}
115