xref: /webtrees/app/Log.php (revision a79ebb440d002378ab8a7447241da23d6aab0ee6)
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	// We can log the following types of message in the wt_log table.
25	const TYPE_AUTHENTICATION = 'auth';
26	const TYPE_CONFIGURATION  = 'config';
27	const TYPE_DEBUG          = 'debug';
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		global $WT_TREE;
42
43		if (!$tree) {
44			$tree = $WT_TREE;
45		}
46
47		$request = Request::create('', '', [], [], [], $_SERVER);
48
49		Database::prepare(
50			"INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)"
51		)->execute([
52					$log_type,
53					$message,
54					$request->getClientIp(),
55					Auth::id(),
56					$tree ? $tree->getTreeId() : null,
57		]);
58	}
59
60	/**
61	 * Store an authentication message in the message log.
62	 *
63	 * @param string $message
64	 */
65	public static function addAuthenticationLog($message) {
66		self::addLog($message, self::TYPE_AUTHENTICATION);
67	}
68
69	/**
70	 * Store a configuration message in the message log.
71	 *
72	 * @param string       $message
73	 * @param Tree|null $tree
74	 */
75	public static function addConfigurationLog($message, Tree $tree = null) {
76		self::addLog($message, self::TYPE_CONFIGURATION, $tree);
77	}
78
79	/**
80	 * Store a debug message in the message log.
81	 *
82	 * @param string $message
83	 */
84	public static function addDebugLog($message) {
85		self::addLog($message, self::TYPE_DEBUG);
86	}
87
88	/**
89	 * Store an edit message in the message log.
90	 *
91	 * @param string $message
92	 */
93	public static function addEditLog($message) {
94		self::addLog($message, self::TYPE_EDIT);
95	}
96
97	/**
98	 * Store an error message in the message log.
99	 *
100	 * @param string $message
101	 */
102	public static function addErrorLog($message) {
103		self::addLog($message, self::TYPE_ERROR);
104	}
105
106	/**
107	 * Store an media management message in the message log.
108	 *
109	 * @param string $message
110	 */
111	public static function addMediaLog($message) {
112		self::addLog($message, self::TYPE_MEDIA);
113	}
114
115	/**
116	 * Store a search event in the message log.
117	 *
118	 * Unlike most webtrees activity, search is not restricted to a single tree,
119	 * so we need to record which trees were searchecd.
120	 *
121	 * @param string    $message
122	 * @param Tree[] $trees Which trees were searched
123	 */
124	public static function addSearchLog($message, array $trees) {
125		foreach ($trees as $tree) {
126			self::addLog($message, self::TYPE_SEARCH, $tree);
127		}
128	}
129}
130