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