xref: /webtrees/app/Log.php (revision 4a9a60957058ca2a34e30a0d6f783e1540606590)
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        $tree_id = $tree ? $tree->id() : null;
73
74        DB::table('log')->insert([
75            'log_type'    => $log_type,
76            'log_message' => $message,
77            'ip_address'  => $ip_address,
78            'user_id'     => Auth::id(),
79            'gedcom_id'   => $tree_id,
80        ]);
81    }
82
83    /**
84     * Store a configuration message in the message log.
85     *
86     * @param string    $message
87     * @param Tree|null $tree
88     *
89     * @return void
90     */
91    public static function addConfigurationLog(string $message, Tree $tree = null): void
92    {
93        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
94    }
95
96    /**
97     * Store an edit message in the message log.
98     *
99     * @param string $message
100     * @param Tree   $tree
101     *
102     * @return void
103     */
104    public static function addEditLog(string $message, Tree $tree): void
105    {
106        self::addLog($message, self::TYPE_EDIT, $tree);
107    }
108
109    /**
110     * Store an error message in the message log.
111     *
112     * @param string $message
113     *
114     * @return void
115     */
116    public static function addErrorLog(string $message): void
117    {
118        self::addLog($message, self::TYPE_ERROR);
119    }
120
121    /**
122     * Store an media management message in the message log.
123     *
124     * @param string $message
125     *
126     * @return void
127     */
128    public static function addMediaLog(string $message): void
129    {
130        self::addLog($message, self::TYPE_MEDIA);
131    }
132
133    /**
134     * Store a search event in the message log.
135     * Unlike most webtrees activity, search is not restricted to a single tree,
136     * so we need to record which trees were searched.
137     *
138     * @param string      $message
139     * @param array<Tree> $trees Which trees were searched
140     *
141     * @return void
142     */
143    public static function addSearchLog(string $message, array $trees): void
144    {
145        foreach ($trees as $tree) {
146            self::addLog($message, self::TYPE_SEARCH, $tree);
147        }
148    }
149}
150