1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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; 21 22use Illuminate\Database\Capsule\Manager as DB; 23use Psr\Http\Message\ServerRequestInterface; 24 25/** 26 * Record webtrees events in the database 27 */ 28class Log 29{ 30 // We can log the following types of message in the wt_log table. 31 private const TYPE_AUTHENTICATION = 'auth'; 32 private const TYPE_CONFIGURATION = 'config'; 33 private const TYPE_EDIT = 'edit'; 34 private const TYPE_ERROR = 'error'; 35 private const TYPE_MEDIA = 'media'; 36 private const TYPE_SEARCH = 'search'; 37 38 /** 39 * Store an authentication message in the message log. 40 * 41 * @param string $message 42 * 43 * @return void 44 */ 45 public static function addAuthenticationLog(string $message): void 46 { 47 self::addLog($message, self::TYPE_AUTHENTICATION); 48 } 49 50 /** 51 * Store a new message (of the appropriate type) in the message log. 52 * 53 * @param string $message 54 * @param string $log_type 55 * @param Tree|null $tree 56 * 57 * @return void 58 */ 59 private static function addLog(string $message, string $log_type, Tree $tree = null): void 60 { 61 if (app()->has(ServerRequestInterface::class)) { 62 $request = app(ServerRequestInterface::class); 63 assert($request instanceof ServerRequestInterface); 64 65 $ip_address = Validator::attributes($request)->string('client-ip'); 66 } else { 67 $ip_address = '127.0.0.1'; 68 } 69 70 $tree_id = $tree ? $tree->id() : null; 71 72 DB::table('log')->insert([ 73 'log_type' => $log_type, 74 'log_message' => $message, 75 'ip_address' => $ip_address, 76 'user_id' => Auth::id(), 77 'gedcom_id' => $tree_id, 78 ]); 79 } 80 81 /** 82 * Store a configuration message in the message log. 83 * 84 * @param string $message 85 * @param Tree|null $tree 86 * 87 * @return void 88 */ 89 public static function addConfigurationLog(string $message, Tree $tree = null): void 90 { 91 self::addLog($message, self::TYPE_CONFIGURATION, $tree); 92 } 93 94 /** 95 * Store an edit message in the message log. 96 * 97 * @param string $message 98 * @param Tree $tree 99 * 100 * @return void 101 */ 102 public static function addEditLog(string $message, Tree $tree): void 103 { 104 self::addLog($message, self::TYPE_EDIT, $tree); 105 } 106 107 /** 108 * Store an error message in the message log. 109 * 110 * @param string $message 111 * 112 * @return void 113 */ 114 public static function addErrorLog(string $message): void 115 { 116 self::addLog($message, self::TYPE_ERROR); 117 } 118 119 /** 120 * Store an media management message in the message log. 121 * 122 * @param string $message 123 * 124 * @return void 125 */ 126 public static function addMediaLog(string $message): void 127 { 128 self::addLog($message, self::TYPE_MEDIA); 129 } 130 131 /** 132 * Store a search event in the message log. 133 * Unlike most webtrees activity, search is not restricted to a single tree, 134 * so we need to record which trees were searched. 135 * 136 * @param string $message 137 * @param array<Tree> $trees Which trees were searched 138 * 139 * @return void 140 */ 141 public static function addSearchLog(string $message, array $trees): void 142 { 143 foreach ($trees as $tree) { 144 self::addLog($message, self::TYPE_SEARCH, $tree); 145 } 146 } 147} 148