xref: /webtrees/app/Log.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20b48bb5e9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
214c891c40SGreg Roachuse Symfony\Component\HttpFoundation\Request;
224c891c40SGreg Roach
23a25f0a04SGreg Roach/**
2476692c8bSGreg Roach * Record webtrees events in the database
25a25f0a04SGreg Roach */
26c1010edaSGreg Roachclass Log
27c1010edaSGreg Roach{
28a25f0a04SGreg Roach    // We can log the following types of message in the wt_log table.
2916d6367aSGreg Roach    private const TYPE_AUTHENTICATION = 'auth';
3016d6367aSGreg Roach    private const TYPE_CONFIGURATION  = 'config';
3116d6367aSGreg Roach    private const TYPE_EDIT           = 'edit';
3216d6367aSGreg Roach    private const TYPE_ERROR          = 'error';
3316d6367aSGreg Roach    private const TYPE_MEDIA          = 'media';
3416d6367aSGreg Roach    private const TYPE_SEARCH         = 'search';
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach    /**
37a25f0a04SGreg Roach     * Store a new message (of the appropriate type) in the message log.
38a25f0a04SGreg Roach     *
39a25f0a04SGreg Roach     * @param string    $message
40a25f0a04SGreg Roach     * @param string    $log_type
41a25f0a04SGreg Roach     * @param Tree|null $tree
4218d7a90dSGreg Roach     *
4318d7a90dSGreg Roach     * @return void
44a25f0a04SGreg Roach     */
45*e364afe4SGreg Roach    private static function addLog($message, $log_type, Tree $tree = null): void
46c1010edaSGreg Roach    {
47afca16feSGreg Roach        // Can't use requestFromGloabls() as this will fail if we have already processed an uploaded file.
48afca16feSGreg Roach        $request    = Request::create('', '', [], [], [], $_SERVER);
49b48bb5e9SGreg Roach        $ip_address = $request->getClientIp() ?? '127.0.0.1';
50b48bb5e9SGreg Roach        $tree_id    = $tree ? $tree->id() : null;
514c891c40SGreg Roach
52b48bb5e9SGreg Roach        DB::table('log')->insert([
53b48bb5e9SGreg Roach            'log_type'    => $log_type,
54b48bb5e9SGreg Roach            'log_message' => $message,
55b48bb5e9SGreg Roach            'ip_address'  => $ip_address,
56b48bb5e9SGreg Roach            'user_id'     => Auth::id(),
57b48bb5e9SGreg Roach            'gedcom_id'   => $tree_id,
5813abd6f3SGreg Roach        ]);
59a25f0a04SGreg Roach    }
60a25f0a04SGreg Roach
61a25f0a04SGreg Roach    /**
62a25f0a04SGreg Roach     * Store an authentication message in the message log.
63a25f0a04SGreg Roach     *
64a25f0a04SGreg Roach     * @param string $message
6518d7a90dSGreg Roach     *
6618d7a90dSGreg Roach     * @return void
67a25f0a04SGreg Roach     */
68*e364afe4SGreg Roach    public static function addAuthenticationLog($message): void
69c1010edaSGreg Roach    {
70847d5489SGreg Roach        self::addLog($message, self::TYPE_AUTHENTICATION, null);
71a25f0a04SGreg Roach    }
72a25f0a04SGreg Roach
73a25f0a04SGreg Roach    /**
74a25f0a04SGreg Roach     * Store a configuration message in the message log.
75a25f0a04SGreg Roach     *
76a25f0a04SGreg Roach     * @param string    $message
77a25f0a04SGreg Roach     * @param Tree|null $tree
7818d7a90dSGreg Roach     *
7918d7a90dSGreg Roach     * @return void
80a25f0a04SGreg Roach     */
81*e364afe4SGreg Roach    public static function addConfigurationLog($message, Tree $tree = null): void
82c1010edaSGreg Roach    {
83a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
84a25f0a04SGreg Roach    }
85a25f0a04SGreg Roach
86a25f0a04SGreg Roach    /**
87a25f0a04SGreg Roach     * Store an edit message in the message log.
88a25f0a04SGreg Roach     *
89a25f0a04SGreg Roach     * @param string $message
90847d5489SGreg Roach     * @param Tree   $tree
9118d7a90dSGreg Roach     *
9218d7a90dSGreg Roach     * @return void
93a25f0a04SGreg Roach     */
94*e364afe4SGreg Roach    public static function addEditLog($message, Tree $tree): void
95c1010edaSGreg Roach    {
96847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
97a25f0a04SGreg Roach    }
98a25f0a04SGreg Roach
99a25f0a04SGreg Roach    /**
100a25f0a04SGreg Roach     * Store an error message in the message log.
101a25f0a04SGreg Roach     *
102a25f0a04SGreg Roach     * @param string $message
10318d7a90dSGreg Roach     *
10418d7a90dSGreg Roach     * @return void
105a25f0a04SGreg Roach     */
106*e364afe4SGreg Roach    public static function addErrorLog($message): void
107c1010edaSGreg Roach    {
108a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
109a25f0a04SGreg Roach    }
110a25f0a04SGreg Roach
111a25f0a04SGreg Roach    /**
112a25f0a04SGreg Roach     * Store an media management message in the message log.
113a25f0a04SGreg Roach     *
114a25f0a04SGreg Roach     * @param string $message
11518d7a90dSGreg Roach     *
11618d7a90dSGreg Roach     * @return void
117a25f0a04SGreg Roach     */
118*e364afe4SGreg Roach    public static function addMediaLog($message): void
119c1010edaSGreg Roach    {
120847d5489SGreg Roach        self::addLog($message, self::TYPE_MEDIA, null);
121a25f0a04SGreg Roach    }
122a25f0a04SGreg Roach
123a25f0a04SGreg Roach    /**
124a25f0a04SGreg Roach     * Store a search event in the message log.
125a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
126a25f0a04SGreg Roach     * so we need to record which trees were searchecd.
127a25f0a04SGreg Roach     *
128a25f0a04SGreg Roach     * @param string $message
129a25f0a04SGreg Roach     * @param Tree[] $trees Which trees were searched
13018d7a90dSGreg Roach     *
13118d7a90dSGreg Roach     * @return void
132a25f0a04SGreg Roach     */
133*e364afe4SGreg Roach    public static function addSearchLog($message, array $trees): void
134c1010edaSGreg Roach    {
135a25f0a04SGreg Roach        foreach ($trees as $tree) {
136a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
137a25f0a04SGreg Roach        }
138a25f0a04SGreg Roach    }
139a25f0a04SGreg Roach}
140