xref: /webtrees/app/Log.php (revision 4d7096d367a6529f150124b8ebfc303bb3795420)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg 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
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
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     */
4524f2a3afSGreg Roach    public static function addAuthenticationLog(string $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     */
5924f2a3afSGreg Roach    private static function addLog(string $message, string $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        }
67*4d7096d3SGreg Roach
68b48bb5e9SGreg Roach        $tree_id = $tree ? $tree->id() : null;
694c891c40SGreg Roach
70b48bb5e9SGreg Roach        DB::table('log')->insert([
71b48bb5e9SGreg Roach            'log_type'    => $log_type,
72b48bb5e9SGreg Roach            'log_message' => $message,
73b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
74b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
75b48bb5e9SGreg Roach            'gedcom_id'   => $tree_id,
7613abd6f3SGreg Roach        ]);
77a25f0a04SGreg Roach    }
78a25f0a04SGreg Roach
79a25f0a04SGreg Roach    /**
80a25f0a04SGreg Roach     * Store a configuration message in the message log.
81a25f0a04SGreg Roach     *
82a25f0a04SGreg Roach     * @param string    $message
83a25f0a04SGreg Roach     * @param Tree|null $tree
8418d7a90dSGreg Roach     *
8518d7a90dSGreg Roach     * @return void
86a25f0a04SGreg Roach     */
8724f2a3afSGreg Roach    public static function addConfigurationLog(string $message, Tree $tree = null): void
88c1010edaSGreg Roach    {
89a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
90a25f0a04SGreg Roach    }
91a25f0a04SGreg Roach
92a25f0a04SGreg Roach    /**
93a25f0a04SGreg Roach     * Store an edit message in the message log.
94a25f0a04SGreg Roach     *
95a25f0a04SGreg Roach     * @param string $message
96847d5489SGreg Roach     * @param Tree   $tree
9718d7a90dSGreg Roach     *
9818d7a90dSGreg Roach     * @return void
99a25f0a04SGreg Roach     */
10024f2a3afSGreg Roach    public static function addEditLog(string $message, Tree $tree): void
101c1010edaSGreg Roach    {
102847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
103a25f0a04SGreg Roach    }
104a25f0a04SGreg Roach
105a25f0a04SGreg Roach    /**
106a25f0a04SGreg Roach     * Store an error message in the message log.
107a25f0a04SGreg Roach     *
108a25f0a04SGreg Roach     * @param string $message
10918d7a90dSGreg Roach     *
11018d7a90dSGreg Roach     * @return void
111a25f0a04SGreg Roach     */
11224f2a3afSGreg Roach    public static function addErrorLog(string $message): void
113c1010edaSGreg Roach    {
114a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
115a25f0a04SGreg Roach    }
116a25f0a04SGreg Roach
117a25f0a04SGreg Roach    /**
118a25f0a04SGreg Roach     * Store an media management message in the message log.
119a25f0a04SGreg Roach     *
120a25f0a04SGreg Roach     * @param string $message
12118d7a90dSGreg Roach     *
12218d7a90dSGreg Roach     * @return void
123a25f0a04SGreg Roach     */
12424f2a3afSGreg Roach    public static function addMediaLog(string $message): void
125c1010edaSGreg Roach    {
1266ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_MEDIA);
127a25f0a04SGreg Roach    }
128a25f0a04SGreg Roach
129a25f0a04SGreg Roach    /**
130a25f0a04SGreg Roach     * Store a search event in the message log.
131a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
132fceda430SGreg Roach     * so we need to record which trees were searched.
133a25f0a04SGreg Roach     *
134a25f0a04SGreg Roach     * @param string      $message
13509482a55SGreg Roach     * @param array<Tree> $trees Which trees were searched
13618d7a90dSGreg Roach     *
13718d7a90dSGreg Roach     * @return void
138a25f0a04SGreg Roach     */
13924f2a3afSGreg Roach    public static function addSearchLog(string $message, array $trees): void
140c1010edaSGreg Roach    {
141a25f0a04SGreg Roach        foreach ($trees as $tree) {
142a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
143a25f0a04SGreg Roach        }
144a25f0a04SGreg Roach    }
145a25f0a04SGreg Roach}
146