xref: /webtrees/app/Log.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1a25f0a04SGreg Roach<?php
2*3976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
19dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
20a25f0a04SGreg Roach
21b48bb5e9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
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.
3016d6367aSGreg Roach    private const TYPE_AUTHENTICATION = 'auth';
3116d6367aSGreg Roach    private const TYPE_CONFIGURATION  = 'config';
3216d6367aSGreg Roach    private const TYPE_EDIT           = 'edit';
3316d6367aSGreg Roach    private const TYPE_ERROR          = 'error';
3416d6367aSGreg Roach    private const TYPE_MEDIA          = 'media';
3516d6367aSGreg Roach    private const 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     */
446ccdf4f0SGreg Roach    public static function addAuthenticationLog($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     */
58e364afe4SGreg Roach    private static function addLog($message, $log_type, Tree $tree = null): void
59c1010edaSGreg Roach    {
60a75b3337SGreg Roach        if (app()->has(ServerRequestInterface::class)) {
616ccdf4f0SGreg Roach            $request    = app(ServerRequestInterface::class);
62add3fa41SGreg Roach            $ip_address = $request->getAttribute('client_ip');
63a75b3337SGreg Roach        } else {
64a75b3337SGreg Roach            $ip_address = '127.0.0.1';
65a75b3337SGreg Roach        }
66b48bb5e9SGreg Roach        $tree_id    = $tree ? $tree->id() : null;
674c891c40SGreg Roach
68b48bb5e9SGreg Roach        DB::table('log')->insert([
69b48bb5e9SGreg Roach            'log_type'    => $log_type,
70b48bb5e9SGreg Roach            'log_message' => $message,
71b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
72b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
73b48bb5e9SGreg Roach            'gedcom_id'   => $tree_id,
7413abd6f3SGreg Roach        ]);
75a25f0a04SGreg Roach    }
76a25f0a04SGreg Roach
77a25f0a04SGreg Roach    /**
78a25f0a04SGreg Roach     * Store a configuration message in the message log.
79a25f0a04SGreg Roach     *
80a25f0a04SGreg Roach     * @param string    $message
81a25f0a04SGreg Roach     * @param Tree|null $tree
8218d7a90dSGreg Roach     *
8318d7a90dSGreg Roach     * @return void
84a25f0a04SGreg Roach     */
85e364afe4SGreg Roach    public static function addConfigurationLog($message, Tree $tree = null): void
86c1010edaSGreg Roach    {
87a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
88a25f0a04SGreg Roach    }
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach    /**
91a25f0a04SGreg Roach     * Store an edit message in the message log.
92a25f0a04SGreg Roach     *
93a25f0a04SGreg Roach     * @param string $message
94847d5489SGreg Roach     * @param Tree   $tree
9518d7a90dSGreg Roach     *
9618d7a90dSGreg Roach     * @return void
97a25f0a04SGreg Roach     */
98e364afe4SGreg Roach    public static function addEditLog($message, Tree $tree): void
99c1010edaSGreg Roach    {
100847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
101a25f0a04SGreg Roach    }
102a25f0a04SGreg Roach
103a25f0a04SGreg Roach    /**
104a25f0a04SGreg Roach     * Store an error message in the message log.
105a25f0a04SGreg Roach     *
106a25f0a04SGreg Roach     * @param string $message
10718d7a90dSGreg Roach     *
10818d7a90dSGreg Roach     * @return void
109a25f0a04SGreg Roach     */
110e364afe4SGreg Roach    public static function addErrorLog($message): void
111c1010edaSGreg Roach    {
112a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
113a25f0a04SGreg Roach    }
114a25f0a04SGreg Roach
115a25f0a04SGreg Roach    /**
116a25f0a04SGreg Roach     * Store an media management message in the message log.
117a25f0a04SGreg Roach     *
118a25f0a04SGreg Roach     * @param string $message
11918d7a90dSGreg Roach     *
12018d7a90dSGreg Roach     * @return void
121a25f0a04SGreg Roach     */
122e364afe4SGreg Roach    public static function addMediaLog($message): void
123c1010edaSGreg Roach    {
1246ccdf4f0SGreg Roach        self::addLog($message, self::TYPE_MEDIA);
125a25f0a04SGreg Roach    }
126a25f0a04SGreg Roach
127a25f0a04SGreg Roach    /**
128a25f0a04SGreg Roach     * Store a search event in the message log.
129a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
130a25f0a04SGreg Roach     * so we need to record which trees were searchecd.
131a25f0a04SGreg Roach     *
132a25f0a04SGreg Roach     * @param string $message
133a25f0a04SGreg Roach     * @param Tree[] $trees Which trees were searched
13418d7a90dSGreg Roach     *
13518d7a90dSGreg Roach     * @return void
136a25f0a04SGreg Roach     */
137e364afe4SGreg Roach    public static function addSearchLog($message, array $trees): void
138c1010edaSGreg Roach    {
139a25f0a04SGreg Roach        foreach ($trees as $tree) {
140a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
141a25f0a04SGreg Roach        }
142a25f0a04SGreg Roach    }
143a25f0a04SGreg Roach}
144