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