xref: /webtrees/app/Log.php (revision 376123c414a36e5151c2e51777a7c6eb53564481)
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 Symfony\Component\HttpFoundation\Request;
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 a new message (of the appropriate type) in the message log.
38     *
39     * @param string    $message
40     * @param string    $log_type
41     * @param Tree|null $tree
42     *
43     * @return void
44     */
45    private static function addLog($message, $log_type, Tree $tree = null): void
46    {
47        // Can't use requestFromGloabls() as this will fail if we have already processed an uploaded file.
48        $request    = Request::create('', '', [], [], [], $_SERVER);
49        $ip_address = $request->getClientIp() ?? '127.0.0.1';
50        $tree_id    = $tree ? $tree->id() : null;
51
52        DB::table('log')->insert([
53            'log_type'    => $log_type,
54            'log_message' => $message,
55            'ip_address'  => $ip_address,
56            'user_id'     => Auth::id(),
57            'gedcom_id'   => $tree_id,
58        ]);
59    }
60
61    /**
62     * Store an authentication message in the message log.
63     *
64     * @param string $message
65     *
66     * @return void
67     */
68    public static function addAuthenticationLog($message): void
69    {
70        self::addLog($message, self::TYPE_AUTHENTICATION, null);
71    }
72
73    /**
74     * Store a configuration message in the message log.
75     *
76     * @param string    $message
77     * @param Tree|null $tree
78     *
79     * @return void
80     */
81    public static function addConfigurationLog($message, Tree $tree = null): void
82    {
83        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
84    }
85
86    /**
87     * Store an edit message in the message log.
88     *
89     * @param string $message
90     * @param Tree   $tree
91     *
92     * @return void
93     */
94    public static function addEditLog($message, Tree $tree): void
95    {
96        self::addLog($message, self::TYPE_EDIT, $tree);
97    }
98
99    /**
100     * Store an error message in the message log.
101     *
102     * @param string $message
103     *
104     * @return void
105     */
106    public static function addErrorLog($message): void
107    {
108        self::addLog($message, self::TYPE_ERROR);
109    }
110
111    /**
112     * Store an media management message in the message log.
113     *
114     * @param string $message
115     *
116     * @return void
117     */
118    public static function addMediaLog($message): void
119    {
120        self::addLog($message, self::TYPE_MEDIA, null);
121    }
122
123    /**
124     * Store a search event in the message log.
125     * Unlike most webtrees activity, search is not restricted to a single tree,
126     * so we need to record which trees were searchecd.
127     *
128     * @param string $message
129     * @param Tree[] $trees Which trees were searched
130     *
131     * @return void
132     */
133    public static function addSearchLog($message, array $trees): void
134    {
135        foreach ($trees as $tree) {
136            self::addLog($message, self::TYPE_SEARCH, $tree);
137        }
138    }
139}
140