xref: /webtrees/app/Log.php (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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
25*10e06497SGreg Roachuse function assert;
26*10e06497SGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * Record webtrees events in the database
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass Log
31c1010edaSGreg Roach{
32a25f0a04SGreg Roach    // We can log the following types of message in the wt_log table.
3316d6367aSGreg Roach    private const TYPE_AUTHENTICATION = 'auth';
3416d6367aSGreg Roach    private const TYPE_CONFIGURATION  = 'config';
3516d6367aSGreg Roach    private const TYPE_EDIT           = 'edit';
3616d6367aSGreg Roach    private const TYPE_ERROR          = 'error';
3716d6367aSGreg Roach    private const TYPE_MEDIA          = 'media';
3816d6367aSGreg Roach    private const TYPE_SEARCH         = 'search';
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach    /**
416ccdf4f0SGreg Roach     * Store an authentication message in the message log.
426ccdf4f0SGreg Roach     *
436ccdf4f0SGreg Roach     * @param string $message
446ccdf4f0SGreg Roach     *
456ccdf4f0SGreg Roach     * @return void
466ccdf4f0SGreg Roach     */
4724f2a3afSGreg Roach    public static function addAuthenticationLog(string $message): void
486ccdf4f0SGreg Roach    {
496ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_AUTHENTICATION);
506ccdf4f0SGreg Roach    }
516ccdf4f0SGreg Roach
526ccdf4f0SGreg Roach    /**
53a25f0a04SGreg Roach     * Store a new message (of the appropriate type) in the message log.
54a25f0a04SGreg Roach     *
55a25f0a04SGreg Roach     * @param string    $message
56a25f0a04SGreg Roach     * @param string    $log_type
57a25f0a04SGreg Roach     * @param Tree|null $tree
5818d7a90dSGreg Roach     *
5918d7a90dSGreg Roach     * @return void
60a25f0a04SGreg Roach     */
6124f2a3afSGreg Roach    private static function addLog(string $message, string $log_type, Tree $tree = null): void
62c1010edaSGreg Roach    {
63a75b3337SGreg Roach        if (app()->has(ServerRequestInterface::class)) {
646ccdf4f0SGreg Roach            $request = app(ServerRequestInterface::class);
65b55cbc6bSGreg Roach            assert($request instanceof ServerRequestInterface);
66b55cbc6bSGreg Roach
67b55cbc6bSGreg Roach            $ip_address = Validator::attributes($request)->string('client-ip');
68a75b3337SGreg Roach        } else {
69a75b3337SGreg Roach            $ip_address = '127.0.0.1';
70a75b3337SGreg Roach        }
714d7096d3SGreg Roach
72b48bb5e9SGreg Roach        $tree_id = $tree ? $tree->id() : null;
734c891c40SGreg Roach
74b48bb5e9SGreg Roach        DB::table('log')->insert([
75b48bb5e9SGreg Roach            'log_type'    => $log_type,
76b48bb5e9SGreg Roach            'log_message' => $message,
77b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
78b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
79b48bb5e9SGreg Roach            'gedcom_id'   => $tree_id,
8013abd6f3SGreg Roach        ]);
81a25f0a04SGreg Roach    }
82a25f0a04SGreg Roach
83a25f0a04SGreg Roach    /**
84a25f0a04SGreg Roach     * Store a configuration message in the message log.
85a25f0a04SGreg Roach     *
86a25f0a04SGreg Roach     * @param string    $message
87a25f0a04SGreg Roach     * @param Tree|null $tree
8818d7a90dSGreg Roach     *
8918d7a90dSGreg Roach     * @return void
90a25f0a04SGreg Roach     */
9124f2a3afSGreg Roach    public static function addConfigurationLog(string $message, Tree $tree = null): void
92c1010edaSGreg Roach    {
93a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
94a25f0a04SGreg Roach    }
95a25f0a04SGreg Roach
96a25f0a04SGreg Roach    /**
97a25f0a04SGreg Roach     * Store an edit message in the message log.
98a25f0a04SGreg Roach     *
99a25f0a04SGreg Roach     * @param string $message
100847d5489SGreg Roach     * @param Tree   $tree
10118d7a90dSGreg Roach     *
10218d7a90dSGreg Roach     * @return void
103a25f0a04SGreg Roach     */
10424f2a3afSGreg Roach    public static function addEditLog(string $message, Tree $tree): void
105c1010edaSGreg Roach    {
106847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
107a25f0a04SGreg Roach    }
108a25f0a04SGreg Roach
109a25f0a04SGreg Roach    /**
110a25f0a04SGreg Roach     * Store an error message in the message log.
111a25f0a04SGreg Roach     *
112a25f0a04SGreg Roach     * @param string $message
11318d7a90dSGreg Roach     *
11418d7a90dSGreg Roach     * @return void
115a25f0a04SGreg Roach     */
11624f2a3afSGreg Roach    public static function addErrorLog(string $message): void
117c1010edaSGreg Roach    {
118a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
119a25f0a04SGreg Roach    }
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach    /**
122a25f0a04SGreg Roach     * Store an media management message in the message log.
123a25f0a04SGreg Roach     *
124a25f0a04SGreg Roach     * @param string $message
12518d7a90dSGreg Roach     *
12618d7a90dSGreg Roach     * @return void
127a25f0a04SGreg Roach     */
12824f2a3afSGreg Roach    public static function addMediaLog(string $message): void
129c1010edaSGreg Roach    {
1306ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_MEDIA);
131a25f0a04SGreg Roach    }
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach    /**
134a25f0a04SGreg Roach     * Store a search event in the message log.
135a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
136fceda430SGreg Roach     * so we need to record which trees were searched.
137a25f0a04SGreg Roach     *
138a25f0a04SGreg Roach     * @param string      $message
13909482a55SGreg Roach     * @param array<Tree> $trees Which trees were searched
14018d7a90dSGreg Roach     *
14118d7a90dSGreg Roach     * @return void
142a25f0a04SGreg Roach     */
14324f2a3afSGreg Roach    public static function addSearchLog(string $message, array $trees): void
144c1010edaSGreg Roach    {
145a25f0a04SGreg Roach        foreach ($trees as $tree) {
146a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
147a25f0a04SGreg Roach        }
148a25f0a04SGreg Roach    }
149a25f0a04SGreg Roach}
150