xref: /webtrees/app/Log.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
17*e7f56f2aSGreg Roach
18dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
204c891c40SGreg Roachuse Symfony\Component\HttpFoundation\Request;
214c891c40SGreg Roach
22a25f0a04SGreg Roach/**
2376692c8bSGreg Roach * Record webtrees events in the database
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass Log
26c1010edaSGreg Roach{
27a25f0a04SGreg Roach    // We can log the following types of message in the wt_log table.
28a25f0a04SGreg Roach    const TYPE_AUTHENTICATION = 'auth';
29a25f0a04SGreg Roach    const TYPE_CONFIGURATION  = 'config';
30a25f0a04SGreg Roach    const TYPE_EDIT           = 'edit';
31a25f0a04SGreg Roach    const TYPE_ERROR          = 'error';
32a25f0a04SGreg Roach    const TYPE_MEDIA          = 'media';
33a25f0a04SGreg Roach    const TYPE_SEARCH         = 'search';
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach    /**
36a25f0a04SGreg Roach     * Store a new message (of the appropriate type) in the message log.
37a25f0a04SGreg Roach     *
38a25f0a04SGreg Roach     * @param string    $message
39a25f0a04SGreg Roach     * @param string    $log_type
40a25f0a04SGreg Roach     * @param Tree|null $tree
4118d7a90dSGreg Roach     *
4218d7a90dSGreg Roach     * @return void
43a25f0a04SGreg Roach     */
44c1010edaSGreg Roach    private static function addLog($message, $log_type, Tree $tree = null)
45c1010edaSGreg Roach    {
46f97c7170SGreg Roach        $request = Request::create('', '', [], [], [], $_SERVER);
474c891c40SGreg Roach
48a25f0a04SGreg Roach        Database::prepare(
49a25f0a04SGreg Roach            "INSERT INTO `##log` (log_type, log_message, ip_address, user_id, gedcom_id) VALUES (?, ?, ?, ?, ?)"
5013abd6f3SGreg Roach        )->execute([
51a25f0a04SGreg Roach            $log_type,
52a25f0a04SGreg Roach            $message,
534c891c40SGreg Roach            $request->getClientIp(),
54a25f0a04SGreg Roach            Auth::id(),
55cbc1590aSGreg Roach            $tree ? $tree->getTreeId() : null,
5613abd6f3SGreg Roach        ]);
57a25f0a04SGreg Roach    }
58a25f0a04SGreg Roach
59a25f0a04SGreg Roach    /**
60a25f0a04SGreg Roach     * Store an authentication message in the message log.
61a25f0a04SGreg Roach     *
62a25f0a04SGreg Roach     * @param string $message
6318d7a90dSGreg Roach     *
6418d7a90dSGreg Roach     * @return void
65a25f0a04SGreg Roach     */
66c1010edaSGreg Roach    public static function addAuthenticationLog($message)
67c1010edaSGreg Roach    {
68847d5489SGreg Roach        self::addLog($message, self::TYPE_AUTHENTICATION, null);
69a25f0a04SGreg Roach    }
70a25f0a04SGreg Roach
71a25f0a04SGreg Roach    /**
72a25f0a04SGreg Roach     * Store a configuration message in the message log.
73a25f0a04SGreg Roach     *
74a25f0a04SGreg Roach     * @param string    $message
75a25f0a04SGreg Roach     * @param Tree|null $tree
7618d7a90dSGreg Roach     *
7718d7a90dSGreg Roach     * @return void
78a25f0a04SGreg Roach     */
79c1010edaSGreg Roach    public static function addConfigurationLog($message, Tree $tree = null)
80c1010edaSGreg Roach    {
81a25f0a04SGreg Roach        self::addLog($message, self::TYPE_CONFIGURATION, $tree);
82a25f0a04SGreg Roach    }
83a25f0a04SGreg Roach
84a25f0a04SGreg Roach    /**
85a25f0a04SGreg Roach     * Store an edit message in the message log.
86a25f0a04SGreg Roach     *
87a25f0a04SGreg Roach     * @param string $message
88847d5489SGreg Roach     * @param Tree   $tree
8918d7a90dSGreg Roach     *
9018d7a90dSGreg Roach     * @return void
91a25f0a04SGreg Roach     */
92c1010edaSGreg Roach    public static function addEditLog($message, Tree $tree)
93c1010edaSGreg Roach    {
94847d5489SGreg Roach        self::addLog($message, self::TYPE_EDIT, $tree);
95a25f0a04SGreg Roach    }
96a25f0a04SGreg Roach
97a25f0a04SGreg Roach    /**
98a25f0a04SGreg Roach     * Store an error message in the message log.
99a25f0a04SGreg Roach     *
100a25f0a04SGreg Roach     * @param string $message
10118d7a90dSGreg Roach     *
10218d7a90dSGreg Roach     * @return void
103a25f0a04SGreg Roach     */
104c1010edaSGreg Roach    public static function addErrorLog($message)
105c1010edaSGreg Roach    {
106a25f0a04SGreg Roach        self::addLog($message, self::TYPE_ERROR);
107a25f0a04SGreg Roach    }
108a25f0a04SGreg Roach
109a25f0a04SGreg Roach    /**
110a25f0a04SGreg Roach     * Store an media management message in the message log.
111a25f0a04SGreg Roach     *
112a25f0a04SGreg Roach     * @param string $message
11318d7a90dSGreg Roach     *
11418d7a90dSGreg Roach     * @return void
115a25f0a04SGreg Roach     */
116c1010edaSGreg Roach    public static function addMediaLog($message)
117c1010edaSGreg Roach    {
118847d5489SGreg Roach        self::addLog($message, self::TYPE_MEDIA, null);
119a25f0a04SGreg Roach    }
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach    /**
122a25f0a04SGreg Roach     * Store a search event in the message log.
123a25f0a04SGreg Roach     *
124a25f0a04SGreg Roach     * Unlike most webtrees activity, search is not restricted to a single tree,
125a25f0a04SGreg Roach     * so we need to record which trees were searchecd.
126a25f0a04SGreg Roach     *
127a25f0a04SGreg Roach     * @param string $message
128a25f0a04SGreg Roach     * @param Tree[] $trees Which trees were searched
12918d7a90dSGreg Roach     *
13018d7a90dSGreg Roach     * @return void
131a25f0a04SGreg Roach     */
132c1010edaSGreg Roach    public static function addSearchLog($message, array $trees)
133c1010edaSGreg Roach    {
134a25f0a04SGreg Roach        foreach ($trees as $tree) {
135a25f0a04SGreg Roach            self::addLog($message, self::TYPE_SEARCH, $tree);
136a25f0a04SGreg Roach        }
137a25f0a04SGreg Roach    }
138a25f0a04SGreg Roach}
139