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