xref: /webtrees/app/Log.php (revision 81bf322104708dae4d3f2c78f87fb3e6027946a3)
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
2510e06497SGreg Roachuse function assert;
2610e06497SGreg 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        DB::table('log')->insert([
73b48bb5e9SGreg Roach            'log_type'    => $log_type,
74b48bb5e9SGreg Roach            'log_message' => $message,
75b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
76b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
77*81bf3221SGreg Roach            'gedcom_id'   => $tree?->id(),
7813abd6f3SGreg Roach        ]);
79a25f0a04SGreg Roach    }
80a25f0a04SGreg Roach
81a25f0a04SGreg Roach    /**
82a25f0a04SGreg Roach     * Store a configuration message in the message log.
83a25f0a04SGreg Roach     *
84a25f0a04SGreg Roach     * @param string    $message
85a25f0a04SGreg Roach     * @param Tree|null $tree
8618d7a90dSGreg Roach     *
8718d7a90dSGreg Roach     * @return void
88a25f0a04SGreg Roach     */
8924f2a3afSGreg Roach    public static function addConfigurationLog(string $message, Tree $tree = null): void
90c1010edaSGreg Roach    {
91a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
92a25f0a04SGreg Roach    }
93a25f0a04SGreg Roach
94a25f0a04SGreg Roach    /**
95a25f0a04SGreg Roach     * Store an edit message in the message log.
96a25f0a04SGreg Roach     *
97a25f0a04SGreg Roach     * @param string $message
98847d5489SGreg Roach     * @param Tree   $tree
9918d7a90dSGreg Roach     *
10018d7a90dSGreg Roach     * @return void
101a25f0a04SGreg Roach     */
10224f2a3afSGreg Roach    public static function addEditLog(string $message, Tree $tree): void
103c1010edaSGreg Roach    {
104847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
105a25f0a04SGreg Roach    }
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach    /**
108a25f0a04SGreg Roach     * Store an error message in the message log.
109a25f0a04SGreg Roach     *
110a25f0a04SGreg Roach     * @param string $message
11118d7a90dSGreg Roach     *
11218d7a90dSGreg Roach     * @return void
113a25f0a04SGreg Roach     */
11424f2a3afSGreg Roach    public static function addErrorLog(string $message): void
115c1010edaSGreg Roach    {
116a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
117a25f0a04SGreg Roach    }
118a25f0a04SGreg Roach
119a25f0a04SGreg Roach    /**
120a25f0a04SGreg Roach     * Store an media management message in the message log.
121a25f0a04SGreg Roach     *
122a25f0a04SGreg Roach     * @param string $message
12318d7a90dSGreg Roach     *
12418d7a90dSGreg Roach     * @return void
125a25f0a04SGreg Roach     */
12624f2a3afSGreg Roach    public static function addMediaLog(string $message): void
127c1010edaSGreg Roach    {
1286ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_MEDIA);
129a25f0a04SGreg Roach    }
130a25f0a04SGreg Roach
131a25f0a04SGreg Roach    /**
132a25f0a04SGreg Roach     * Store a search event in the message log.
133a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
134fceda430SGreg Roach     * so we need to record which trees were searched.
135a25f0a04SGreg Roach     *
136a25f0a04SGreg Roach     * @param string      $message
13709482a55SGreg Roach     * @param array<Tree> $trees Which trees were searched
13818d7a90dSGreg Roach     *
13918d7a90dSGreg Roach     * @return void
140a25f0a04SGreg Roach     */
14124f2a3afSGreg Roach    public static function addSearchLog(string $message, array $trees): void
142c1010edaSGreg Roach    {
143a25f0a04SGreg Roach        foreach ($trees as $tree) {
144a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
145a25f0a04SGreg Roach        }
146a25f0a04SGreg Roach    }
147a25f0a04SGreg Roach}
148