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