1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20use Illuminate\Database\Capsule\Manager as DB; 21use Symfony\Component\HttpFoundation\Request; 22 23/** 24 * Record webtrees events in the database 25 */ 26class Log 27{ 28 // We can log the following types of message in the wt_log table. 29 const TYPE_AUTHENTICATION = 'auth'; 30 const TYPE_CONFIGURATION = 'config'; 31 const TYPE_EDIT = 'edit'; 32 const TYPE_ERROR = 'error'; 33 const TYPE_MEDIA = 'media'; 34 const TYPE_SEARCH = 'search'; 35 36 /** 37 * Store a new message (of the appropriate type) in the message log. 38 * 39 * @param string $message 40 * @param string $log_type 41 * @param Tree|null $tree 42 * 43 * @return void 44 */ 45 private static function addLog($message, $log_type, Tree $tree = null) 46 { 47 $request = Request::createFromGlobals(); 48 $ip_address = $request->getClientIp() ?? '127.0.0.1'; 49 $tree_id = $tree ? $tree->id() : null; 50 51 DB::table('log')->insert([ 52 'log_type' => $log_type, 53 'log_message' => $message, 54 'ip_address' => $ip_address, 55 'user_id' => Auth::id(), 56 'gedcom_id' => $tree_id, 57 ]); 58 } 59 60 /** 61 * Store an authentication message in the message log. 62 * 63 * @param string $message 64 * 65 * @return void 66 */ 67 public static function addAuthenticationLog($message) 68 { 69 self::addLog($message, self::TYPE_AUTHENTICATION, null); 70 } 71 72 /** 73 * Store a configuration message in the message log. 74 * 75 * @param string $message 76 * @param Tree|null $tree 77 * 78 * @return void 79 */ 80 public static function addConfigurationLog($message, Tree $tree = null) 81 { 82 self::addLog($message, self::TYPE_CONFIGURATION, $tree); 83 } 84 85 /** 86 * Store an edit message in the message log. 87 * 88 * @param string $message 89 * @param Tree $tree 90 * 91 * @return void 92 */ 93 public static function addEditLog($message, Tree $tree) 94 { 95 self::addLog($message, self::TYPE_EDIT, $tree); 96 } 97 98 /** 99 * Store an error message in the message log. 100 * 101 * @param string $message 102 * 103 * @return void 104 */ 105 public static function addErrorLog($message) 106 { 107 self::addLog($message, self::TYPE_ERROR); 108 } 109 110 /** 111 * Store an media management message in the message log. 112 * 113 * @param string $message 114 * 115 * @return void 116 */ 117 public static function addMediaLog($message) 118 { 119 self::addLog($message, self::TYPE_MEDIA, null); 120 } 121 122 /** 123 * Store a search event in the message log. 124 * Unlike most webtrees activity, search is not restricted to a single tree, 125 * so we need to record which trees were searchecd. 126 * 127 * @param string $message 128 * @param Tree[] $trees Which trees were searched 129 * 130 * @return void 131 */ 132 public static function addSearchLog($message, array $trees) 133 { 134 foreach ($trees as $tree) { 135 self::addLog($message, self::TYPE_SEARCH, $tree); 136 } 137 } 138} 139