xref: /webtrees/app/Log.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22b48bb5e9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
236ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
244c891c40SGreg Roach
25a25f0a04SGreg Roach/**
2676692c8bSGreg Roach * Record webtrees events in the database
27a25f0a04SGreg Roach */
28c1010edaSGreg Roachclass Log
29c1010edaSGreg Roach{
30a25f0a04SGreg Roach    // We can log the following types of message in the wt_log table.
3116d6367aSGreg Roach    private const TYPE_AUTHENTICATION = 'auth';
3216d6367aSGreg Roach    private const TYPE_CONFIGURATION  = 'config';
3316d6367aSGreg Roach    private const TYPE_EDIT           = 'edit';
3416d6367aSGreg Roach    private const TYPE_ERROR          = 'error';
3516d6367aSGreg Roach    private const TYPE_MEDIA          = 'media';
3616d6367aSGreg Roach    private const TYPE_SEARCH         = 'search';
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /**
396ccdf4f0SGreg Roach     * Store an authentication message in the message log.
406ccdf4f0SGreg Roach     *
416ccdf4f0SGreg Roach     * @param string $message
426ccdf4f0SGreg Roach     *
436ccdf4f0SGreg Roach     * @return void
446ccdf4f0SGreg Roach     */
456ccdf4f0SGreg Roach    public static function addAuthenticationLog($message): void
466ccdf4f0SGreg Roach    {
476ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_AUTHENTICATION);
486ccdf4f0SGreg Roach    }
496ccdf4f0SGreg Roach
506ccdf4f0SGreg Roach    /**
51a25f0a04SGreg Roach     * Store a new message (of the appropriate type) in the message log.
52a25f0a04SGreg Roach     *
53a25f0a04SGreg Roach     * @param string    $message
54a25f0a04SGreg Roach     * @param string    $log_type
55a25f0a04SGreg Roach     * @param Tree|null $tree
5618d7a90dSGreg Roach     *
5718d7a90dSGreg Roach     * @return void
58a25f0a04SGreg Roach     */
59e364afe4SGreg Roach    private static function addLog($message, $log_type, Tree $tree = null): void
60c1010edaSGreg Roach    {
61a75b3337SGreg Roach        if (app()->has(ServerRequestInterface::class)) {
626ccdf4f0SGreg Roach            $request    = app(ServerRequestInterface::class);
634874f72dSGreg Roach            $ip_address = $request->getAttribute('client-ip');
64a75b3337SGreg Roach        } else {
65a75b3337SGreg Roach            $ip_address = '127.0.0.1';
66a75b3337SGreg Roach        }
67b48bb5e9SGreg Roach        $tree_id    = $tree ? $tree->id() : null;
684c891c40SGreg Roach
69b48bb5e9SGreg Roach        DB::table('log')->insert([
70b48bb5e9SGreg Roach            'log_type'    => $log_type,
71b48bb5e9SGreg Roach            'log_message' => $message,
72b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
73b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
74b48bb5e9SGreg Roach            'gedcom_id'   => $tree_id,
7513abd6f3SGreg Roach        ]);
76a25f0a04SGreg Roach    }
77a25f0a04SGreg Roach
78a25f0a04SGreg Roach    /**
79a25f0a04SGreg Roach     * Store a configuration message in the message log.
80a25f0a04SGreg Roach     *
81a25f0a04SGreg Roach     * @param string    $message
82a25f0a04SGreg Roach     * @param Tree|null $tree
8318d7a90dSGreg Roach     *
8418d7a90dSGreg Roach     * @return void
85a25f0a04SGreg Roach     */
86e364afe4SGreg Roach    public static function addConfigurationLog($message, Tree $tree = null): void
87c1010edaSGreg Roach    {
88a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
89a25f0a04SGreg Roach    }
90a25f0a04SGreg Roach
91a25f0a04SGreg Roach    /**
92a25f0a04SGreg Roach     * Store an edit message in the message log.
93a25f0a04SGreg Roach     *
94a25f0a04SGreg Roach     * @param string $message
95847d5489SGreg Roach     * @param Tree   $tree
9618d7a90dSGreg Roach     *
9718d7a90dSGreg Roach     * @return void
98a25f0a04SGreg Roach     */
99e364afe4SGreg Roach    public static function addEditLog($message, Tree $tree): void
100c1010edaSGreg Roach    {
101847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
102a25f0a04SGreg Roach    }
103a25f0a04SGreg Roach
104a25f0a04SGreg Roach    /**
105a25f0a04SGreg Roach     * Store an error message in the message log.
106a25f0a04SGreg Roach     *
107a25f0a04SGreg Roach     * @param string $message
10818d7a90dSGreg Roach     *
10918d7a90dSGreg Roach     * @return void
110a25f0a04SGreg Roach     */
111e364afe4SGreg Roach    public static function addErrorLog($message): void
112c1010edaSGreg Roach    {
113a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
114a25f0a04SGreg Roach    }
115a25f0a04SGreg Roach
116a25f0a04SGreg Roach    /**
117a25f0a04SGreg Roach     * Store an media management message in the message log.
118a25f0a04SGreg Roach     *
119a25f0a04SGreg Roach     * @param string $message
12018d7a90dSGreg Roach     *
12118d7a90dSGreg Roach     * @return void
122a25f0a04SGreg Roach     */
123e364afe4SGreg Roach    public static function addMediaLog($message): void
124c1010edaSGreg Roach    {
1256ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_MEDIA);
126a25f0a04SGreg Roach    }
127a25f0a04SGreg Roach
128a25f0a04SGreg Roach    /**
129a25f0a04SGreg Roach     * Store a search event in the message log.
130a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
131fceda430SGreg Roach     * so we need to record which trees were searched.
132a25f0a04SGreg Roach     *
133a25f0a04SGreg Roach     * @param string $message
134a25f0a04SGreg Roach     * @param Tree[] $trees Which trees were searched
13518d7a90dSGreg Roach     *
13618d7a90dSGreg Roach     * @return void
137a25f0a04SGreg Roach     */
138e364afe4SGreg Roach    public static function addSearchLog($message, array $trees): void
139c1010edaSGreg Roach    {
140a25f0a04SGreg Roach        foreach ($trees as $tree) {
141a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
142a25f0a04SGreg Roach        }
143a25f0a04SGreg Roach    }
144a25f0a04SGreg Roach}
145