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