1<?php
2
3/**
4 * The main Logging class.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2006-2020 phpMyFAQ Team
13 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2006-08-15
16 */
17
18namespace phpMyFAQ;
19
20/**
21 * Class Logging
22 *
23 * @package phpMyFAQ
24 */
25class Logging
26{
27    /**
28     * @var Configuration
29     */
30    private $config = null;
31
32    /**
33     * Constructor.
34     *
35     * @param Configuration $config
36     */
37    public function __construct(Configuration $config)
38    {
39        $this->config = $config;
40    }
41
42    /**
43     * Returns the number of entries.
44     *
45     * @return int
46     */
47    public function getNumberOfEntries()
48    {
49        $query = sprintf(
50            '
51            SELECT
52                id
53            FROM
54                %sfaqadminlog',
55            Database::getTablePrefix()
56        );
57
58        return $this->config->getDb()->numRows(
59            $this->config->getDb()->query($query)
60        );
61    }
62
63    /**
64     * Returns all data from the admin log.
65     *
66     * @return array
67     */
68    public function getAll()
69    {
70        $data = [];
71
72        $query = sprintf(
73            '
74            SELECT
75                id, time, usr, text, ip
76            FROM
77                %sfaqadminlog
78            ORDER BY id DESC',
79            Database::getTablePrefix()
80        );
81
82        $result = $this->config->getDb()->query($query);
83        while ($row = $this->config->getDb()->fetchObject($result)) {
84            $data[$row->id] = array(
85                'time' => $row->time,
86                'usr' => $row->usr,
87                'text' => $row->text,
88                'ip' => $row->ip,
89            );
90        }
91
92        return $data;
93    }
94
95    /**
96     * Adds a new admin log entry.
97     *
98     * @param User   $user    User object
99     * @param string $logText Logged string
100     *
101     * @return bool
102     */
103    public function logAdmin(User $user, $logText = '')
104    {
105        if ($this->config->get('main.enableAdminLog')) {
106            $query = sprintf(
107                "
108                INSERT INTO
109                    %sfaqadminlog
110                (id, time, usr, text, ip)
111                    VALUES
112                (%d, %d, %d, '%s', '%s')",
113                Database::getTablePrefix(),
114                $this->config->getDb()->nextId(Database::getTablePrefix() . 'faqadminlog', 'id'),
115                $_SERVER['REQUEST_TIME'],
116                $user->userdata->get('user_id'),
117                $this->config->getDb()->escape(nl2br($logText)),
118                $_SERVER['REMOTE_ADDR']
119            );
120
121            return $this->config->getDb()->query($query);
122        } else {
123            return false;
124        }
125    }
126
127    /**
128     * Deletes logging data older than 30 days.
129     *
130     * @return bool
131     */
132    public function delete(): bool
133    {
134        $query = sprintf(
135            'DELETE FROM %sfaqadminlog WHERE time < %d',
136            Database::getTablePrefix(),
137            $_SERVER['REQUEST_TIME'] - 30 * 86400
138        );
139
140        return $this->config->getDb()->query($query);
141    }
142}
143