xref: /webtrees/app/Log.php (revision dd04c183d8beed05be2226b30b7dda485ea4538a)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17a25f0a04SGreg Roach
18*dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roach/**
21a25f0a04SGreg Roach * Class Log - record webtrees events in the database
22a25f0a04SGreg Roach */
23a25f0a04SGreg Roachclass Log {
24a25f0a04SGreg Roach	// We can log the following types of message in the wt_log table.
25a25f0a04SGreg Roach	const TYPE_AUTHENTICATION = 'auth';
26a25f0a04SGreg Roach	const TYPE_CONFIGURATION  = 'config';
27a25f0a04SGreg Roach	const TYPE_DEBUG          = 'debug';
28a25f0a04SGreg Roach	const TYPE_EDIT           = 'edit';
29a25f0a04SGreg Roach	const TYPE_ERROR          = 'error';
30a25f0a04SGreg Roach	const TYPE_MEDIA          = 'media';
31a25f0a04SGreg Roach	const TYPE_SEARCH         = 'search';
32a25f0a04SGreg Roach
33a25f0a04SGreg Roach	/**
34a25f0a04SGreg Roach	 * Store a new message (of the appropriate type) in the message log.
35a25f0a04SGreg Roach	 *
36a25f0a04SGreg Roach	 * @param string    $message
37a25f0a04SGreg Roach	 * @param string    $log_type
38a25f0a04SGreg Roach	 * @param Tree|null $tree
39a25f0a04SGreg Roach	 */
40a25f0a04SGreg Roach	private static function addLog($message, $log_type, Tree $tree = null) {
41a25f0a04SGreg Roach		global $WT_REQUEST, $WT_TREE;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach		if (!$tree) {
44a25f0a04SGreg Roach			$tree = $WT_TREE;
45a25f0a04SGreg Roach		}
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach		Database::prepare(
48a25f0a04SGreg Roach			"INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)"
49a25f0a04SGreg Roach		)->execute(array(
50a25f0a04SGreg Roach					$log_type,
51a25f0a04SGreg Roach					$message,
52a25f0a04SGreg Roach					$WT_REQUEST->getClientIp(),
53a25f0a04SGreg Roach					Auth::id(),
54a25f0a04SGreg Roach					$tree ? $tree->id() : null
55a25f0a04SGreg Roach		));
56a25f0a04SGreg Roach	}
57a25f0a04SGreg Roach
58a25f0a04SGreg Roach	/**
59a25f0a04SGreg Roach	 * Store an authentication message in the message log.
60a25f0a04SGreg Roach	 *
61a25f0a04SGreg Roach	 * @param string $message
62a25f0a04SGreg Roach	 */
63a25f0a04SGreg Roach	public static function addAuthenticationLog($message) {
64a25f0a04SGreg Roach		self::addLog($message, self::TYPE_AUTHENTICATION);
65a25f0a04SGreg Roach	}
66a25f0a04SGreg Roach
67a25f0a04SGreg Roach	/**
68a25f0a04SGreg Roach	 * Store a configuration message in the message log.
69a25f0a04SGreg Roach	 *
70a25f0a04SGreg Roach	 * @param string       $message
71a25f0a04SGreg Roach	 * @param Tree|null $tree
72a25f0a04SGreg Roach	 */
73a25f0a04SGreg Roach	public static function addConfigurationLog($message, Tree $tree = null) {
74a25f0a04SGreg Roach		self::addLog($message, self::TYPE_CONFIGURATION, $tree);
75a25f0a04SGreg Roach	}
76a25f0a04SGreg Roach
77a25f0a04SGreg Roach	/**
78a25f0a04SGreg Roach	 * Store a debug message in the message log.
79a25f0a04SGreg Roach	 *
80a25f0a04SGreg Roach	 * @param string $message
81a25f0a04SGreg Roach	 */
82a25f0a04SGreg Roach	public static function addDebugLog($message) {
83a25f0a04SGreg Roach		self::addLog($message, self::TYPE_DEBUG);
84a25f0a04SGreg Roach	}
85a25f0a04SGreg Roach
86a25f0a04SGreg Roach	/**
87a25f0a04SGreg Roach	 * Store an edit message in the message log.
88a25f0a04SGreg Roach	 *
89a25f0a04SGreg Roach	 * @param string $message
90a25f0a04SGreg Roach	 */
91a25f0a04SGreg Roach	public static function addEditLog($message) {
92a25f0a04SGreg Roach		self::addLog($message, self::TYPE_EDIT);
93a25f0a04SGreg Roach	}
94a25f0a04SGreg Roach
95a25f0a04SGreg Roach	/**
96a25f0a04SGreg Roach	 * Store an error message in the message log.
97a25f0a04SGreg Roach	 *
98a25f0a04SGreg Roach	 * @param string $message
99a25f0a04SGreg Roach	 */
100a25f0a04SGreg Roach	public static function addErrorLog($message) {
101a25f0a04SGreg Roach		self::addLog($message, self::TYPE_ERROR);
102a25f0a04SGreg Roach	}
103a25f0a04SGreg Roach
104a25f0a04SGreg Roach	/**
105a25f0a04SGreg Roach	 * Store an media management message in the message log.
106a25f0a04SGreg Roach	 *
107a25f0a04SGreg Roach	 * @param string $message
108a25f0a04SGreg Roach	 */
109a25f0a04SGreg Roach	public static function addMediaLog($message) {
110a25f0a04SGreg Roach		self::addLog($message, self::TYPE_MEDIA);
111a25f0a04SGreg Roach	}
112a25f0a04SGreg Roach
113a25f0a04SGreg Roach	/**
114a25f0a04SGreg Roach	 * Store a search event in the message log.
115a25f0a04SGreg Roach	 *
116a25f0a04SGreg Roach	 * Unlike most webtrees activity, search is not restricted to a single tree,
117a25f0a04SGreg Roach	 * so we need to record which trees were searchecd.
118a25f0a04SGreg Roach	 *
119a25f0a04SGreg Roach	 * @param string    $message
120a25f0a04SGreg Roach	 * @param Tree[] $trees Which trees were searched
121a25f0a04SGreg Roach	 */
122a25f0a04SGreg Roach	public static function addSearchLog($message, array $trees) {
123a25f0a04SGreg Roach		foreach ($trees as $tree) {
124a25f0a04SGreg Roach			self::addLog($message, self::TYPE_SEARCH, $tree);
125a25f0a04SGreg Roach		}
126a25f0a04SGreg Roach	}
127a25f0a04SGreg Roach}
128