1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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 Fisharebest\Webtrees\Http\ViewResponseTrait; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\Services\TreeService; 26use Fisharebest\Webtrees\Services\UserService; 27use Fisharebest\Webtrees\Tree; 28use Fisharebest\Webtrees\User; 29use Illuminate\Database\Capsule\Manager as DB; 30use Illuminate\Support\Collection; 31use Psr\Http\Message\ResponseInterface; 32use Psr\Http\Message\ServerRequestInterface; 33use Psr\Http\Server\RequestHandlerInterface; 34 35use function max; 36use function min; 37 38/** 39 * Show logs. 40 */ 41class SiteLogsPage implements RequestHandlerInterface 42{ 43 use ViewResponseTrait; 44 45 private TreeService $tree_service; 46 47 private UserService $user_service; 48 49 /** 50 * @param TreeService $tree_service 51 * @param UserService $user_service 52 */ 53 public function __construct(TreeService $tree_service, UserService $user_service) 54 { 55 $this->tree_service = $tree_service; 56 $this->user_service = $user_service; 57 } 58 59 /** 60 * @param ServerRequestInterface $request 61 * 62 * @return ResponseInterface 63 */ 64 public function handle(ServerRequestInterface $request): ResponseInterface 65 { 66 $this->layout = 'layouts/administration'; 67 68 $earliest = DB::table('log')->min('log_time'); 69 $latest = DB::table('log')->max('log_time'); 70 71 $earliest = Registry::timestampFactory()->fromString($earliest); 72 $latest = Registry::timestampFactory()->fromString($latest); 73 74 $earliest = $earliest->toDateString(); 75 $latest = $latest->toDateString(); 76 77 $params = $request->getQueryParams(); 78 $action = $params['action'] ?? ''; 79 $from = $params['from'] ?? $earliest; 80 $to = $params['to'] ?? $latest; 81 $type = $params['type'] ?? ''; 82 $text = $params['text'] ?? ''; 83 $ip = $params['ip'] ?? ''; 84 $username = $params['username'] ?? ''; 85 $tree = $params['tree'] ?? ''; 86 87 $from = max($from, $earliest); 88 $to = min(max($from, $to), $latest); 89 90 $user_options = $this->user_service->all()->mapWithKeys(static function (User $user): array { 91 return [$user->userName() => $user->userName()]; 92 }); 93 $user_options = (new Collection(['' => '']))->merge($user_options); 94 95 $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array { 96 return [$tree->name() => $tree->title()]; 97 }); 98 $tree_options = (new Collection(['' => '']))->merge($tree_options); 99 100 $title = I18N::translate('Website logs'); 101 102 return $this->viewResponse('admin/site-logs', [ 103 'action' => $action, 104 'earliest' => $earliest, 105 'from' => $from, 106 'tree' => $tree, 107 'ip' => $ip, 108 'latest' => $latest, 109 'tree_options' => $tree_options, 110 'title' => $title, 111 'to' => $to, 112 'text' => $text, 113 'type' => $type, 114 'username' => $username, 115 'user_options' => $user_options, 116 ]); 117 } 118} 119