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 */ 16namespace Fisharebest\Webtrees; 17 18use Symfony\Component\HttpFoundation\Request; 19 20/** 21 * Record webtrees events in the database 22 */ 23class Log 24{ 25 // We can log the following types of message in the wt_log table. 26 const TYPE_AUTHENTICATION = 'auth'; 27 const TYPE_CONFIGURATION = 'config'; 28 const TYPE_EDIT = 'edit'; 29 const TYPE_ERROR = 'error'; 30 const TYPE_MEDIA = 'media'; 31 const TYPE_SEARCH = 'search'; 32 33 /** 34 * Store a new message (of the appropriate type) in the message log. 35 * 36 * @param string $message 37 * @param string $log_type 38 * @param Tree|null $tree 39 */ 40 private static function addLog($message, $log_type, Tree $tree = null) 41 { 42 $request = Request::create('', '', [], [], [], $_SERVER); 43 44 Database::prepare( 45 "INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)" 46 )->execute([ 47 $log_type, 48 $message, 49 $request->getClientIp(), 50 Auth::id(), 51 $tree ? $tree->getTreeId() : null, 52 ]); 53 } 54 55 /** 56 * Store an authentication message in the message log. 57 * 58 * @param string $message 59 */ 60 public static function addAuthenticationLog($message) 61 { 62 self::addLog($message, self::TYPE_AUTHENTICATION, null); 63 } 64 65 /** 66 * Store a configuration message in the message log. 67 * 68 * @param string $message 69 * @param Tree|null $tree 70 */ 71 public static function addConfigurationLog($message, Tree $tree = null) 72 { 73 self::addLog($message, self::TYPE_CONFIGURATION, $tree); 74 } 75 76 /** 77 * Store an edit message in the message log. 78 * 79 * @param string $message 80 * @param Tree $tree 81 */ 82 public static function addEditLog($message, Tree $tree) 83 { 84 self::addLog($message, self::TYPE_EDIT, $tree); 85 } 86 87 /** 88 * Store an error message in the message log. 89 * 90 * @param string $message 91 */ 92 public static function addErrorLog($message) 93 { 94 self::addLog($message, self::TYPE_ERROR); 95 } 96 97 /** 98 * Store an media management message in the message log. 99 * 100 * @param string $message 101 */ 102 public static function addMediaLog($message) 103 { 104 self::addLog($message, self::TYPE_MEDIA, null); 105 } 106 107 /** 108 * Store a search event in the message log. 109 * 110 * Unlike most webtrees activity, search is not restricted to a single tree, 111 * so we need to record which trees were searchecd. 112 * 113 * @param string $message 114 * @param Tree[] $trees Which trees were searched 115 */ 116 public static function addSearchLog($message, array $trees) 117 { 118 foreach ($trees as $tree) { 119 self::addLog($message, self::TYPE_SEARCH, $tree); 120 } 121 } 122} 123