xref: /webtrees/app/Log.php (revision fc747c0fe6ab8c4d4d65bbb60eb220a1c792d7f6)
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{
25    // We can log the following types of message in the wt_log table.
26    const TYPE_AUTHENTICATION = 'auth';
27    const TYPE_CONFIGURATION  = 'config';
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     * @return void
41     */
42    private static function addLog($message, $log_type, Tree $tree = null)
43    {
44        $request = Request::create('', '', [], [], [], $_SERVER);
45
46        Database::prepare(
47            "INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)"
48        )->execute([
49            $log_type,
50            $message,
51            $request->getClientIp(),
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     * @return void
63     */
64    public static function addAuthenticationLog($message)
65    {
66        self::addLog($message, self::TYPE_AUTHENTICATION, null);
67    }
68
69    /**
70     * Store a configuration message in the message log.
71     *
72     * @param string    $message
73     * @param Tree|null $tree
74     *
75     * @return void
76     */
77    public static function addConfigurationLog($message, Tree $tree = null)
78    {
79        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
80    }
81
82    /**
83     * Store an edit message in the message log.
84     *
85     * @param string $message
86     * @param Tree   $tree
87     *
88     * @return void
89     */
90    public static function addEditLog($message, Tree $tree)
91    {
92        self::addLog($message, self::TYPE_EDIT, $tree);
93    }
94
95    /**
96     * Store an error message in the message log.
97     *
98     * @param string $message
99     *
100     * @return void
101     */
102    public static function addErrorLog($message)
103    {
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     * @return void
113     */
114    public static function addMediaLog($message)
115    {
116        self::addLog($message, self::TYPE_MEDIA, null);
117    }
118
119    /**
120     * Store a search event in the message log.
121     *
122     * Unlike most webtrees activity, search is not restricted to a single tree,
123     * so we need to record which trees were searchecd.
124     *
125     * @param string $message
126     * @param Tree[] $trees Which trees were searched
127     *
128     * @return void
129     */
130    public static function addSearchLog($message, array $trees)
131    {
132        foreach ($trees as $tree) {
133            self::addLog($message, self::TYPE_SEARCH, $tree);
134        }
135    }
136}
137