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