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