xref: /webtrees/app/Log.php (revision 67994fb087e1b24564a780e4ae8aeff801733e35)
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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Symfony\Component\HttpFoundation\Request;
21
22/**
23 * Record webtrees events in the database
24 */
25class Log
26{
27    // We can log the following types of message in the wt_log table.
28    const TYPE_AUTHENTICATION = 'auth';
29    const TYPE_CONFIGURATION  = 'config';
30    const TYPE_EDIT           = 'edit';
31    const TYPE_ERROR          = 'error';
32    const TYPE_MEDIA          = 'media';
33    const TYPE_SEARCH         = 'search';
34
35    /**
36     * Store a new message (of the appropriate type) in the message log.
37     *
38     * @param string    $message
39     * @param string    $log_type
40     * @param Tree|null $tree
41     *
42     * @return void
43     */
44    private static function addLog($message, $log_type, Tree $tree = null)
45    {
46        $request = Request::create('', '', [], [], [], $_SERVER);
47
48        Database::prepare(
49            "INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)"
50        )->execute([
51            $log_type,
52            $message,
53            $request->getClientIp(),
54            Auth::id(),
55            $tree ? $tree->getTreeId() : null,
56        ]);
57    }
58
59    /**
60     * Store an authentication message in the message log.
61     *
62     * @param string $message
63     *
64     * @return void
65     */
66    public static function addAuthenticationLog($message)
67    {
68        self::addLog($message, self::TYPE_AUTHENTICATION, null);
69    }
70
71    /**
72     * Store a configuration message in the message log.
73     *
74     * @param string    $message
75     * @param Tree|null $tree
76     *
77     * @return void
78     */
79    public static function addConfigurationLog($message, Tree $tree = null)
80    {
81        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
82    }
83
84    /**
85     * Store an edit message in the message log.
86     *
87     * @param string $message
88     * @param Tree   $tree
89     *
90     * @return void
91     */
92    public static function addEditLog($message, Tree $tree)
93    {
94        self::addLog($message, self::TYPE_EDIT, $tree);
95    }
96
97    /**
98     * Store an error message in the message log.
99     *
100     * @param string $message
101     *
102     * @return void
103     */
104    public static function addErrorLog($message)
105    {
106        self::addLog($message, self::TYPE_ERROR);
107    }
108
109    /**
110     * Store an media management message in the message log.
111     *
112     * @param string $message
113     *
114     * @return void
115     */
116    public static function addMediaLog($message)
117    {
118        self::addLog($message, self::TYPE_MEDIA, null);
119    }
120
121    /**
122     * Store a search event in the message log.
123     *
124     * Unlike most webtrees activity, search is not restricted to a single tree,
125     * so we need to record which trees were searchecd.
126     *
127     * @param string $message
128     * @param Tree[] $trees Which trees were searched
129     *
130     * @return void
131     */
132    public static function addSearchLog($message, array $trees)
133    {
134        foreach ($trees as $tree) {
135            self::addLog($message, self::TYPE_SEARCH, $tree);
136        }
137    }
138}
139