xref: /webtrees/app/Log.php (revision 3b2b4f4a7b8432a126008c761ad3119ae4788c4b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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
25use function assert;
26
27/**
28 * Record webtrees events in the database
29 */
30class Log
31{
32    // We can log the following types of message in the wt_log table.
33    private const TYPE_AUTHENTICATION = 'auth';
34    private const TYPE_CONFIGURATION  = 'config';
35    private const TYPE_EDIT           = 'edit';
36    private const TYPE_ERROR          = 'error';
37    private const TYPE_MEDIA          = 'media';
38    private const TYPE_SEARCH         = 'search';
39
40    /**
41     * Store an authentication message in the message log.
42     *
43     * @param string $message
44     *
45     * @return void
46     */
47    public static function addAuthenticationLog(string $message): void
48    {
49        self::addLog($message, self::TYPE_AUTHENTICATION);
50    }
51
52    /**
53     * Store a new message (of the appropriate type) in the message log.
54     *
55     * @param string    $message
56     * @param string    $log_type
57     * @param Tree|null $tree
58     *
59     * @return void
60     */
61    private static function addLog(string $message, string $log_type, Tree $tree = null): void
62    {
63        if (app()->has(ServerRequestInterface::class)) {
64            $request = app(ServerRequestInterface::class);
65            assert($request instanceof ServerRequestInterface);
66
67            $ip_address = Validator::attributes($request)->string('client-ip');
68        } else {
69            $ip_address = '127.0.0.1';
70        }
71
72        DB::table('log')->insert([
73            'log_type'    => $log_type,
74            'log_message' => $message,
75            'ip_address'  => $ip_address,
76            'user_id'     => Auth::id(),
77            'gedcom_id'   => $tree?->id(),
78        ]);
79    }
80
81    /**
82     * Store a configuration message in the message log.
83     *
84     * @param string    $message
85     * @param Tree|null $tree
86     *
87     * @return void
88     */
89    public static function addConfigurationLog(string $message, Tree $tree = null): void
90    {
91        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
92    }
93
94    /**
95     * Store an edit message in the message log.
96     *
97     * @param string $message
98     * @param Tree   $tree
99     *
100     * @return void
101     */
102    public static function addEditLog(string $message, Tree $tree): void
103    {
104        self::addLog($message, self::TYPE_EDIT, $tree);
105    }
106
107    /**
108     * Store an error message in the message log.
109     *
110     * @param string $message
111     *
112     * @return void
113     */
114    public static function addErrorLog(string $message): void
115    {
116        self::addLog($message, self::TYPE_ERROR);
117    }
118
119    /**
120     * Store an media management message in the message log.
121     *
122     * @param string $message
123     *
124     * @return void
125     */
126    public static function addMediaLog(string $message): void
127    {
128        self::addLog($message, self::TYPE_MEDIA);
129    }
130
131    /**
132     * Store a search event in the message log.
133     * Unlike most webtrees activity, search is not restricted to a single tree,
134     * so we need to record which trees were searched.
135     *
136     * @param string      $message
137     * @param array<Tree> $trees Which trees were searched
138     *
139     * @return void
140     */
141    public static function addSearchLog(string $message, array $trees): void
142    {
143        foreach ($trees as $tree) {
144            self::addLog($message, self::TYPE_SEARCH, $tree);
145        }
146    }
147}
148