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\Services; 2157bfa969SGreg Roach 225cac87aeSGreg Roachuse DateInterval; 235cac87aeSGreg Roachuse DateTimeImmutable; 245cac87aeSGreg Roachuse DateTimeZone; 255cac87aeSGreg Roachuse Fisharebest\Webtrees\Auth; 265cac87aeSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 276f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 28d97083feSGreg Roachuse Fisharebest\Webtrees\Registry; 296c21f8beSGreg Roachuse Fisharebest\Webtrees\Validator; 3057bfa969SGreg Roachuse Illuminate\Database\Query\Builder; 3157bfa969SGreg Roachuse Illuminate\Database\Query\Expression; 326c21f8beSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3357bfa969SGreg Roach 34b5961194SGreg Roachuse function addcslashes; 35b5961194SGreg Roach 3657bfa969SGreg Roach/** 3757bfa969SGreg Roach * Manage site logs 3857bfa969SGreg Roach */ 3957bfa969SGreg Roachclass SiteLogsService 4057bfa969SGreg Roach{ 4157bfa969SGreg Roach /** 4257bfa969SGreg Roach * Generate a query for filtering the changes log. 4357bfa969SGreg Roach * 446c21f8beSGreg Roach * @param ServerRequestInterface $request 4557bfa969SGreg Roach * 4657bfa969SGreg Roach * @return Builder 4757bfa969SGreg Roach */ 486c21f8beSGreg Roach public function logsQuery(ServerRequestInterface $request): Builder 4957bfa969SGreg Roach { 506c21f8beSGreg Roach $tree = Validator::queryParams($request)->string('tree'); 516c21f8beSGreg Roach $from = Validator::queryParams($request)->string('from'); 526c21f8beSGreg Roach $to = Validator::queryParams($request)->string('to'); 536c21f8beSGreg Roach $type = Validator::queryParams($request)->string('type'); 546c21f8beSGreg Roach $text = Validator::queryParams($request)->string('text'); 556c21f8beSGreg Roach $ip = Validator::queryParams($request)->string('ip'); 566c21f8beSGreg Roach $username = Validator::queryParams($request)->string('username'); 5757bfa969SGreg Roach 5857bfa969SGreg Roach $query = DB::table('log') 5957bfa969SGreg Roach ->leftJoin('user', 'user.user_id', '=', 'log.user_id') 6057bfa969SGreg Roach ->leftJoin('gedcom', 'gedcom.gedcom_id', '=', 'log.gedcom_id') 6157bfa969SGreg Roach ->select(['log.*', new Expression("COALESCE(user_name, '<none>') AS user_name"), new Expression("COALESCE(gedcom_name, '<none>') AS gedcom_name")]); 6257bfa969SGreg Roach 635cac87aeSGreg Roach $tz = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')); 645cac87aeSGreg Roach $utc = new DateTimeZone('UTC'); 655cac87aeSGreg Roach 6657bfa969SGreg Roach if ($from !== '') { 67*6bd19c8cSGreg Roach $from_time = DateTimeImmutable::createFromFormat('!Y-m-d', $from, $tz) 685cac87aeSGreg Roach ->setTimezone($utc) 695cac87aeSGreg Roach ->format('Y-m-d H:i:s'); 705cac87aeSGreg Roach 715cac87aeSGreg Roach $query->where('log_time', '>=', $from_time); 7257bfa969SGreg Roach } 7357bfa969SGreg Roach 7457bfa969SGreg Roach if ($to !== '') { 75*6bd19c8cSGreg Roach $to_time = DateTimeImmutable::createFromFormat('!Y-m-d', $to, $tz) 765cac87aeSGreg Roach ->add(new DateInterval('P1D')) 775cac87aeSGreg Roach ->setTimezone($utc) 785cac87aeSGreg Roach ->format('Y-m-d H:i:s'); 795cac87aeSGreg Roach 805cac87aeSGreg Roach $query->where('log_time', '<', $to_time); 8157bfa969SGreg Roach } 8257bfa969SGreg Roach 8357bfa969SGreg Roach if ($type !== '') { 8457bfa969SGreg Roach $query->where('log_type', '=', $type); 8557bfa969SGreg Roach } 8657bfa969SGreg Roach 8757bfa969SGreg Roach if ($text !== '') { 88b5961194SGreg Roach $query->where('log_message', 'LIKE', '%' . addcslashes($text, '\\%_') . '%'); 8957bfa969SGreg Roach } 9057bfa969SGreg Roach 9157bfa969SGreg Roach if ($ip !== '') { 92b5961194SGreg Roach $query->where('ip_address', 'LIKE', addcslashes($ip, '\\%_') . '%'); 9357bfa969SGreg Roach } 9457bfa969SGreg Roach 9557bfa969SGreg Roach if ($username !== '') { 961aeae823SGreg Roach $query->where('user_name', '=', $username); 9757bfa969SGreg Roach } 9857bfa969SGreg Roach 9957bfa969SGreg Roach if ($tree !== '') { 10057bfa969SGreg Roach $query->where('gedcom_name', '=', $tree); 10157bfa969SGreg Roach } 10257bfa969SGreg Roach 10357bfa969SGreg Roach return $query; 10457bfa969SGreg Roach } 10557bfa969SGreg Roach} 106