xref: /webtrees/app/Log.php (revision e873f434551745f888937263ff89e80db3b0f785)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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
226ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
234c891c40SGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * Record webtrees events in the database
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass Log
28c1010edaSGreg Roach{
29a25f0a04SGreg Roach    // We can log the following types of message in the wt_log table.
30*e873f434SGreg Roach    private const string TYPE_AUTHENTICATION = 'auth';
31*e873f434SGreg Roach    private const string TYPE_CONFIGURATION  = 'config';
32*e873f434SGreg Roach    private const string TYPE_EDIT           = 'edit';
33*e873f434SGreg Roach    private const string TYPE_ERROR          = 'error';
34*e873f434SGreg Roach    private const string TYPE_MEDIA          = 'media';
35*e873f434SGreg Roach    private const string TYPE_SEARCH         = 'search';
36a25f0a04SGreg Roach
37a25f0a04SGreg Roach    /**
386ccdf4f0SGreg Roach     * Store an authentication message in the message log.
396ccdf4f0SGreg Roach     *
406ccdf4f0SGreg Roach     * @param string $message
416ccdf4f0SGreg Roach     *
426ccdf4f0SGreg Roach     * @return void
436ccdf4f0SGreg Roach     */
4424f2a3afSGreg Roach    public static function addAuthenticationLog(string $message): void
456ccdf4f0SGreg Roach    {
466ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_AUTHENTICATION);
476ccdf4f0SGreg Roach    }
486ccdf4f0SGreg Roach
496ccdf4f0SGreg Roach    /**
50a25f0a04SGreg Roach     * Store a new message (of the appropriate type) in the message log.
51a25f0a04SGreg Roach     *
52a25f0a04SGreg Roach     * @param string    $message
53a25f0a04SGreg Roach     * @param string    $log_type
54a25f0a04SGreg Roach     * @param Tree|null $tree
5518d7a90dSGreg Roach     *
5618d7a90dSGreg Roach     * @return void
57a25f0a04SGreg Roach     */
582c6f1bd5SGreg Roach    private static function addLog(string $message, string $log_type, Tree|null $tree = null): void
59c1010edaSGreg Roach    {
60d35568b4SGreg Roach        if (Registry::container()->has(ServerRequestInterface::class)) {
61d35568b4SGreg Roach            $request    = Registry::container()->get(ServerRequestInterface::class);
62b55cbc6bSGreg Roach            $ip_address = Validator::attributes($request)->string('client-ip');
63a75b3337SGreg Roach        } else {
64a75b3337SGreg Roach            $ip_address = '127.0.0.1';
65a75b3337SGreg Roach        }
664d7096d3SGreg Roach
67b48bb5e9SGreg Roach        DB::table('log')->insert([
68b48bb5e9SGreg Roach            'log_type'    => $log_type,
69b48bb5e9SGreg Roach            'log_message' => $message,
70b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
71b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
7281bf3221SGreg Roach            'gedcom_id'   => $tree?->id(),
7313abd6f3SGreg Roach        ]);
74a25f0a04SGreg Roach    }
75a25f0a04SGreg Roach
76a25f0a04SGreg Roach    /**
77a25f0a04SGreg Roach     * Store a configuration message in the message log.
78a25f0a04SGreg Roach     *
79a25f0a04SGreg Roach     * @param string    $message
80a25f0a04SGreg Roach     * @param Tree|null $tree
8118d7a90dSGreg Roach     *
8218d7a90dSGreg Roach     * @return void
83a25f0a04SGreg Roach     */
842c6f1bd5SGreg Roach    public static function addConfigurationLog(string $message, Tree|null $tree = null): void
85c1010edaSGreg Roach    {
86a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
87a25f0a04SGreg Roach    }
88a25f0a04SGreg Roach
89a25f0a04SGreg Roach    /**
90a25f0a04SGreg Roach     * Store an edit message in the message log.
91a25f0a04SGreg Roach     *
92a25f0a04SGreg Roach     * @param string $message
93847d5489SGreg Roach     * @param Tree   $tree
9418d7a90dSGreg Roach     *
9518d7a90dSGreg Roach     * @return void
96a25f0a04SGreg Roach     */
9724f2a3afSGreg Roach    public static function addEditLog(string $message, Tree $tree): void
98c1010edaSGreg Roach    {
99847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
100a25f0a04SGreg Roach    }
101a25f0a04SGreg Roach
102a25f0a04SGreg Roach    /**
103a25f0a04SGreg Roach     * Store an error message in the message log.
104a25f0a04SGreg Roach     *
105a25f0a04SGreg Roach     * @param string $message
10618d7a90dSGreg Roach     *
10718d7a90dSGreg Roach     * @return void
108a25f0a04SGreg Roach     */
10924f2a3afSGreg Roach    public static function addErrorLog(string $message): void
110c1010edaSGreg Roach    {
111a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
112a25f0a04SGreg Roach    }
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach    /**
115a25f0a04SGreg Roach     * Store an media management message in the message log.
116a25f0a04SGreg Roach     *
117a25f0a04SGreg Roach     * @param string $message
11818d7a90dSGreg Roach     *
11918d7a90dSGreg Roach     * @return void
120a25f0a04SGreg Roach     */
12124f2a3afSGreg Roach    public static function addMediaLog(string $message): void
122c1010edaSGreg Roach    {
1236ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_MEDIA);
124a25f0a04SGreg Roach    }
125a25f0a04SGreg Roach
126a25f0a04SGreg Roach    /**
127a25f0a04SGreg Roach     * Store a search event in the message log.
128a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
129fceda430SGreg Roach     * so we need to record which trees were searched.
130a25f0a04SGreg Roach     *
131a25f0a04SGreg Roach     * @param string      $message
13209482a55SGreg Roach     * @param array<Tree> $trees Which trees were searched
13318d7a90dSGreg Roach     *
13418d7a90dSGreg Roach     * @return void
135a25f0a04SGreg Roach     */
13624f2a3afSGreg Roach    public static function addSearchLog(string $message, array $trees): void
137c1010edaSGreg Roach    {
138a25f0a04SGreg Roach        foreach ($trees as $tree) {
139a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
140a25f0a04SGreg Roach        }
141a25f0a04SGreg Roach    }
142a25f0a04SGreg Roach}
143