xref: /webtrees/app/Log.php (revision 1e71bdc0ba6fc5add8fed9a3beb51cfca09e47dd)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
16 */
17
18namespace Fisharebest\Webtrees;
19
20/**
21 * Class Log - 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_REQUEST, $WT_TREE;
42
43		if (!$tree) {
44			$tree = $WT_TREE;
45		}
46
47		Database::prepare(
48			"INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)"
49		)->execute(array(
50					$log_type,
51					$message,
52					$WT_REQUEST->getClientIp(),
53					Auth::id(),
54					$tree ? $tree->getTreeId() : null
55		));
56	}
57
58	/**
59	 * Store an authentication message in the message log.
60	 *
61	 * @param string $message
62	 */
63	public static function addAuthenticationLog($message) {
64		self::addLog($message, self::TYPE_AUTHENTICATION);
65	}
66
67	/**
68	 * Store a configuration message in the message log.
69	 *
70	 * @param string       $message
71	 * @param Tree|null $tree
72	 */
73	public static function addConfigurationLog($message, Tree $tree = null) {
74		self::addLog($message, self::TYPE_CONFIGURATION, $tree);
75	}
76
77	/**
78	 * Store a debug message in the message log.
79	 *
80	 * @param string $message
81	 */
82	public static function addDebugLog($message) {
83		self::addLog($message, self::TYPE_DEBUG);
84	}
85
86	/**
87	 * Store an edit message in the message log.
88	 *
89	 * @param string $message
90	 */
91	public static function addEditLog($message) {
92		self::addLog($message, self::TYPE_EDIT);
93	}
94
95	/**
96	 * Store an error message in the message log.
97	 *
98	 * @param string $message
99	 */
100	public static function addErrorLog($message) {
101		self::addLog($message, self::TYPE_ERROR);
102	}
103
104	/**
105	 * Store an media management message in the message log.
106	 *
107	 * @param string $message
108	 */
109	public static function addMediaLog($message) {
110		self::addLog($message, self::TYPE_MEDIA);
111	}
112
113	/**
114	 * Store a search event in the message log.
115	 *
116	 * Unlike most webtrees activity, search is not restricted to a single tree,
117	 * so we need to record which trees were searchecd.
118	 *
119	 * @param string    $message
120	 * @param Tree[] $trees Which trees were searched
121	 */
122	public static function addSearchLog($message, array $trees) {
123		foreach ($trees as $tree) {
124			self::addLog($message, self::TYPE_SEARCH, $tree);
125		}
126	}
127}
128