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