xref: /plugin/statistics/Logger.php (revision a10aed88c50dc33e6ca4199888a06200a919160e)
1762f4807SAndreas Gohr<?php
2762f4807SAndreas Gohr
3762f4807SAndreas Gohrnamespace dokuwiki\plugin\statistics;
4762f4807SAndreas Gohr
5762f4807SAndreas Gohruse DeviceDetector\DeviceDetector;
6762f4807SAndreas Gohruse DeviceDetector\Parser\Client\Browser;
7762f4807SAndreas Gohruse DeviceDetector\Parser\Device\AbstractDeviceParser;
8762f4807SAndreas Gohruse DeviceDetector\Parser\OperatingSystem;
9762f4807SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient;
10762f4807SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
11762f4807SAndreas Gohruse dokuwiki\Utf8\Clean;
12762f4807SAndreas Gohruse dokuwiki\Utf8\PhpString;
13762f4807SAndreas Gohruse helper_plugin_popularity;
14762f4807SAndreas Gohruse helper_plugin_statistics;
15762f4807SAndreas Gohr
16762f4807SAndreas Gohr
17762f4807SAndreas Gohrclass Logger
18762f4807SAndreas Gohr{
19762f4807SAndreas Gohr    /** @var helper_plugin_statistics The statistics helper plugin instance */
20762f4807SAndreas Gohr    protected helper_plugin_statistics $hlp;
21762f4807SAndreas Gohr
22762f4807SAndreas Gohr    /** @var SQLiteDB The SQLite database instance */
23762f4807SAndreas Gohr    protected SQLiteDB $db;
24762f4807SAndreas Gohr
25762f4807SAndreas Gohr    /** @var string The full user agent string */
26762f4807SAndreas Gohr    protected string $uaAgent;
27762f4807SAndreas Gohr
28762f4807SAndreas Gohr    /** @var string The type of user agent (browser, robot, feedreader) */
29762f4807SAndreas Gohr    protected string $uaType = 'browser';
30762f4807SAndreas Gohr
31762f4807SAndreas Gohr    /** @var string The browser/client name */
32762f4807SAndreas Gohr    protected string $uaName;
33762f4807SAndreas Gohr
34762f4807SAndreas Gohr    /** @var string The browser/client version */
35762f4807SAndreas Gohr    protected string $uaVersion;
36762f4807SAndreas Gohr
37762f4807SAndreas Gohr    /** @var string The operating system/platform */
38762f4807SAndreas Gohr    protected string $uaPlatform;
39762f4807SAndreas Gohr
40762f4807SAndreas Gohr    /** @var string The unique user identifier */
41762f4807SAndreas Gohr    protected string $uid;
42762f4807SAndreas Gohr
43762f4807SAndreas Gohr
44762f4807SAndreas Gohr    /**
45762f4807SAndreas Gohr     * Constructor
46762f4807SAndreas Gohr     *
47762f4807SAndreas Gohr     * Parses browser info and set internal vars
48762f4807SAndreas Gohr     */
49762f4807SAndreas Gohr    public function __construct(helper_plugin_statistics $hlp)
50762f4807SAndreas Gohr    {
51762f4807SAndreas Gohr        global $INPUT;
52762f4807SAndreas Gohr
53762f4807SAndreas Gohr        $this->hlp = $hlp;
54762f4807SAndreas Gohr        $this->db = $this->hlp->getDB();
55762f4807SAndreas Gohr
56762f4807SAndreas Gohr        $ua = trim($INPUT->server->str('HTTP_USER_AGENT'));
57762f4807SAndreas Gohr
58762f4807SAndreas Gohr        AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR);
59762f4807SAndreas Gohr        $dd = new DeviceDetector($ua); // FIXME we could use client hints, but need to add headers
60762f4807SAndreas Gohr        $dd->discardBotInformation();
61762f4807SAndreas Gohr        $dd->parse();
62762f4807SAndreas Gohr
63762f4807SAndreas Gohr        if ($dd->isBot()) {
64762f4807SAndreas Gohr            $this->uaType = 'robot';
65762f4807SAndreas Gohr
66762f4807SAndreas Gohr            // for now ignore bots
67762f4807SAndreas Gohr            throw new \RuntimeException('Bot detected, not logging');
68762f4807SAndreas Gohr        }
69762f4807SAndreas Gohr
70762f4807SAndreas Gohr        $this->uaAgent = $ua;
71762f4807SAndreas Gohr        $this->uaName = Browser::getBrowserFamily($dd->getClient('name'));
72762f4807SAndreas Gohr        $this->uaVersion = $dd->getClient('version');
73762f4807SAndreas Gohr        $this->uaPlatform = OperatingSystem::getOsFamily($dd->getOs('name'));
74762f4807SAndreas Gohr        $this->uid = $this->getUID();
75762f4807SAndreas Gohr
76762f4807SAndreas Gohr        if ($dd->isFeedReader()) {
77762f4807SAndreas Gohr            $this->uaType = 'feedreader';
78762f4807SAndreas Gohr        }
79762f4807SAndreas Gohr
80762f4807SAndreas Gohr        $this->logLastseen();
81762f4807SAndreas Gohr    }
82762f4807SAndreas Gohr
83762f4807SAndreas Gohr    /**
84762f4807SAndreas Gohr     * Should be called before logging
85762f4807SAndreas Gohr     *
86762f4807SAndreas Gohr     * This starts a transaction, so all logging is done in one go
87762f4807SAndreas Gohr     */
88762f4807SAndreas Gohr    public function begin(): void
89762f4807SAndreas Gohr    {
90762f4807SAndreas Gohr        $this->hlp->getDB()->getPdo()->beginTransaction();
91762f4807SAndreas Gohr    }
92762f4807SAndreas Gohr
93762f4807SAndreas Gohr    /**
94762f4807SAndreas Gohr     * Should be called after logging
95762f4807SAndreas Gohr     *
96762f4807SAndreas Gohr     * This commits the transaction started in begin()
97762f4807SAndreas Gohr     */
98762f4807SAndreas Gohr    public function end(): void
99762f4807SAndreas Gohr    {
100762f4807SAndreas Gohr        $this->hlp->getDB()->getPdo()->commit();
101762f4807SAndreas Gohr    }
102762f4807SAndreas Gohr
103762f4807SAndreas Gohr    /**
104762f4807SAndreas Gohr     * Get the unique user ID
105762f4807SAndreas Gohr     *
106762f4807SAndreas Gohr     * @return string The unique user identifier
107762f4807SAndreas Gohr     */
108762f4807SAndreas Gohr    protected function getUID(): string
109762f4807SAndreas Gohr    {
110762f4807SAndreas Gohr        global $INPUT;
111762f4807SAndreas Gohr
112762f4807SAndreas Gohr        $uid = $INPUT->str('uid');
113762f4807SAndreas Gohr        if (!$uid) $uid = get_doku_pref('plgstats', false);
114762f4807SAndreas Gohr        if (!$uid) $uid = session_id();
115762f4807SAndreas Gohr        return $uid;
116762f4807SAndreas Gohr    }
117762f4807SAndreas Gohr
118762f4807SAndreas Gohr    /**
119762f4807SAndreas Gohr     * Return the user's session ID
120762f4807SAndreas Gohr     *
121762f4807SAndreas Gohr     * This is usually our own managed session, not a PHP session (only in fallback)
122762f4807SAndreas Gohr     *
123762f4807SAndreas Gohr     * @return string The session identifier
124762f4807SAndreas Gohr     */
125762f4807SAndreas Gohr    protected function getSession(): string
126762f4807SAndreas Gohr    {
127762f4807SAndreas Gohr        global $INPUT;
128762f4807SAndreas Gohr
129762f4807SAndreas Gohr        $ses = $INPUT->str('ses');
130762f4807SAndreas Gohr        if (!$ses) $ses = get_doku_pref('plgstatsses', false);
131762f4807SAndreas Gohr        if (!$ses) $ses = session_id();
132762f4807SAndreas Gohr        return $ses;
133762f4807SAndreas Gohr    }
134762f4807SAndreas Gohr
135762f4807SAndreas Gohr    /**
136762f4807SAndreas Gohr     * Log that we've seen the user (authenticated only)
137762f4807SAndreas Gohr     */
138762f4807SAndreas Gohr    public function logLastseen(): void
139762f4807SAndreas Gohr    {
140762f4807SAndreas Gohr        global $INPUT;
141762f4807SAndreas Gohr
142762f4807SAndreas Gohr        if (empty($INPUT->server->str('REMOTE_USER'))) return;
143762f4807SAndreas Gohr
144762f4807SAndreas Gohr        $this->db->exec(
145762f4807SAndreas Gohr            'REPLACE INTO lastseen (user, dt) VALUES (?, CURRENT_TIMESTAMP)',
146762f4807SAndreas Gohr            $INPUT->server->str('REMOTE_USER'),
147762f4807SAndreas Gohr        );
148762f4807SAndreas Gohr    }
149762f4807SAndreas Gohr
150762f4807SAndreas Gohr    /**
151762f4807SAndreas Gohr     * Log actions by groups
152762f4807SAndreas Gohr     *
153762f4807SAndreas Gohr     * @param string $type The type of access to log ('view','edit')
154762f4807SAndreas Gohr     * @param array $groups The groups to log
155762f4807SAndreas Gohr     */
156762f4807SAndreas Gohr    public function logGroups(string $type, array $groups): void
157762f4807SAndreas Gohr    {
158762f4807SAndreas Gohr        if (!is_array($groups)) {
159762f4807SAndreas Gohr            return;
160762f4807SAndreas Gohr        }
161762f4807SAndreas Gohr
162762f4807SAndreas Gohr        $tolog = (array)$this->hlp->getConf('loggroups');
163762f4807SAndreas Gohr        $groups = array_intersect($groups, $tolog);
164762f4807SAndreas Gohr        if ($groups === []) {
165762f4807SAndreas Gohr            return;
166762f4807SAndreas Gohr        }
167762f4807SAndreas Gohr
168762f4807SAndreas Gohr
169762f4807SAndreas Gohr        $params = [];
170762f4807SAndreas Gohr        $sql = "INSERT INTO groups (`type`, `group`) VALUES ";
171762f4807SAndreas Gohr        foreach ($groups as $group) {
172762f4807SAndreas Gohr            $sql .= '(?, ?),';
173762f4807SAndreas Gohr            $params[] = $type;
174762f4807SAndreas Gohr            $params[] = $group;
175762f4807SAndreas Gohr        }
176762f4807SAndreas Gohr        $sql = rtrim($sql, ',');
177762f4807SAndreas Gohr        $this->db->exec($sql, $params);
178762f4807SAndreas Gohr    }
179762f4807SAndreas Gohr
180762f4807SAndreas Gohr    /**
181762f4807SAndreas Gohr     * Log external search queries
182762f4807SAndreas Gohr     *
183762f4807SAndreas Gohr     * Will not write anything if the referer isn't a search engine
184762f4807SAndreas Gohr     *
185762f4807SAndreas Gohr     * @param string $referer The HTTP referer URL
186762f4807SAndreas Gohr     * @param string $type Reference to the type variable that will be modified
187762f4807SAndreas Gohr     */
188762f4807SAndreas Gohr    public function logExternalSearch(string $referer, string &$type): void
189762f4807SAndreas Gohr    {
190762f4807SAndreas Gohr        global $INPUT;
191762f4807SAndreas Gohr
192762f4807SAndreas Gohr        $searchEngine = new SearchEngines($referer);
193762f4807SAndreas Gohr
194762f4807SAndreas Gohr        if (!$searchEngine->isSearchEngine()) {
195762f4807SAndreas Gohr            return; // not a search engine
196762f4807SAndreas Gohr        }
197762f4807SAndreas Gohr
198762f4807SAndreas Gohr        $type = 'search';
199762f4807SAndreas Gohr        $query = $searchEngine->getQuery();
200762f4807SAndreas Gohr
201762f4807SAndreas Gohr        // log it!
202762f4807SAndreas Gohr        $words = explode(' ', Clean::stripspecials($query, ' ', '\._\-:\*'));
203762f4807SAndreas Gohr        $this->logSearch($INPUT->str('p'), $query, $words, $searchEngine->getEngine());
204762f4807SAndreas Gohr    }
205762f4807SAndreas Gohr
206762f4807SAndreas Gohr    /**
207762f4807SAndreas Gohr     * Log search data to the search related tables
208762f4807SAndreas Gohr     *
209762f4807SAndreas Gohr     * @param string $page The page being searched from
210762f4807SAndreas Gohr     * @param string $query The search query
211762f4807SAndreas Gohr     * @param array $words Array of search words
212762f4807SAndreas Gohr     * @param string $engine The search engine name
213762f4807SAndreas Gohr     */
214762f4807SAndreas Gohr    public function logSearch(string $page, string $query, array $words, string $engine): void
215762f4807SAndreas Gohr    {
216762f4807SAndreas Gohr        $sid = $this->db->exec(
217762f4807SAndreas Gohr            'INSERT INTO search (dt, page, query, engine) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)',
218762f4807SAndreas Gohr            $page, $query, $engine
219762f4807SAndreas Gohr        );
220762f4807SAndreas Gohr        if (!$sid) return;
221762f4807SAndreas Gohr
222762f4807SAndreas Gohr        foreach ($words as $word) {
223762f4807SAndreas Gohr            if (!$word) continue;
224762f4807SAndreas Gohr            $this->db->exec(
225762f4807SAndreas Gohr                'INSERT INTO searchwords (sid, word) VALUES (?, ?)',
226762f4807SAndreas Gohr                $sid, $word
227762f4807SAndreas Gohr            );
228762f4807SAndreas Gohr        }
229762f4807SAndreas Gohr    }
230762f4807SAndreas Gohr
231762f4807SAndreas Gohr    /**
232762f4807SAndreas Gohr     * Log that the session was seen
233762f4807SAndreas Gohr     *
234762f4807SAndreas Gohr     * This is used to calculate the time people spend on the whole site
235762f4807SAndreas Gohr     * during their session
236762f4807SAndreas Gohr     *
237762f4807SAndreas Gohr     * Viewcounts are used for bounce calculation
238762f4807SAndreas Gohr     *
239762f4807SAndreas Gohr     * @param int $addview set to 1 to count a view
240762f4807SAndreas Gohr     */
241762f4807SAndreas Gohr    public function logSession(int $addview = 0): void
242762f4807SAndreas Gohr    {
243762f4807SAndreas Gohr        // only log browser sessions
244762f4807SAndreas Gohr        if ($this->uaType != 'browser') return;
245762f4807SAndreas Gohr
246762f4807SAndreas Gohr        $session = $this->getSession();
247762f4807SAndreas Gohr        $this->db->exec(
248762f4807SAndreas Gohr            'INSERT OR REPLACE INTO session (
249762f4807SAndreas Gohr                session, dt, end, views, uid
250762f4807SAndreas Gohr             ) VALUES (
251762f4807SAndreas Gohr                ?,
252762f4807SAndreas Gohr                CURRENT_TIMESTAMP,
253762f4807SAndreas Gohr                CURRENT_TIMESTAMP,
254762f4807SAndreas Gohr                COALESCE((SELECT views FROM session WHERE session = ?) + ?, ?),
255762f4807SAndreas Gohr                ?
256762f4807SAndreas Gohr             )',
257762f4807SAndreas Gohr            $session, $session, $addview, $addview, $this->uid
258762f4807SAndreas Gohr        );
259762f4807SAndreas Gohr    }
260762f4807SAndreas Gohr
261762f4807SAndreas Gohr    /**
262762f4807SAndreas Gohr     * Resolve IP to country/city and store in database
263762f4807SAndreas Gohr     *
264762f4807SAndreas Gohr     * @param string $ip The IP address to resolve
265762f4807SAndreas Gohr     */
266762f4807SAndreas Gohr    public function logIp(string $ip): void
267762f4807SAndreas Gohr    {
268762f4807SAndreas Gohr        // check if IP already known and up-to-date
269762f4807SAndreas Gohr        $result = $this->db->queryValue(
270762f4807SAndreas Gohr            "SELECT ip
271762f4807SAndreas Gohr             FROM   iplocation
272762f4807SAndreas Gohr             WHERE  ip = ?
273762f4807SAndreas Gohr               AND  lastupd > date('now', '-30 days')",
274762f4807SAndreas Gohr            $ip
275762f4807SAndreas Gohr        );
276762f4807SAndreas Gohr        if ($result) return;
277762f4807SAndreas Gohr
278762f4807SAndreas Gohr        $http = new DokuHTTPClient();
279762f4807SAndreas Gohr        $http->timeout = 10;
280762f4807SAndreas Gohr        $json = $http->get('http://ip-api.com/json/' . $ip); // yes, it's HTTP only
281762f4807SAndreas Gohr
282762f4807SAndreas Gohr        if (!$json) return; // FIXME log error
283762f4807SAndreas Gohr        try {
284762f4807SAndreas Gohr            $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
285762f4807SAndreas Gohr        } catch (\JsonException $e) {
286762f4807SAndreas Gohr            return; // FIXME log error
287762f4807SAndreas Gohr        }
288*a10aed88SAndreas Gohr        if(!isset($data['status']) || $data['status'] !== 'success') {
289*a10aed88SAndreas Gohr            return; // FIXME log error
290*a10aed88SAndreas Gohr        }
291762f4807SAndreas Gohr
292762f4807SAndreas Gohr        $host = gethostbyaddr($ip);
293762f4807SAndreas Gohr        $this->db->exec(
294762f4807SAndreas Gohr            'INSERT OR REPLACE INTO iplocation (
295762f4807SAndreas Gohr                    ip, country, code, city, host, lastupd
296762f4807SAndreas Gohr                 ) VALUES (
297762f4807SAndreas Gohr                    ?, ?, ?, ?, ?, CURRENT_TIMESTAMP
298762f4807SAndreas Gohr                 )',
299762f4807SAndreas Gohr            $ip, $data['country'], $data['countryCode'], $data['city'], $host
300762f4807SAndreas Gohr        );
301762f4807SAndreas Gohr    }
302762f4807SAndreas Gohr
303762f4807SAndreas Gohr    /**
304762f4807SAndreas Gohr     * Log a click on an external link
305762f4807SAndreas Gohr     *
306762f4807SAndreas Gohr     * Called from log.php
307762f4807SAndreas Gohr     */
308762f4807SAndreas Gohr    public function logOutgoing(): void
309762f4807SAndreas Gohr    {
310762f4807SAndreas Gohr        global $INPUT;
311762f4807SAndreas Gohr
312762f4807SAndreas Gohr        if (!$INPUT->str('ol')) return;
313762f4807SAndreas Gohr
314762f4807SAndreas Gohr        $link = $INPUT->str('ol');
315762f4807SAndreas Gohr        $link_md5 = md5($link);
316762f4807SAndreas Gohr        $session = $this->getSession();
317762f4807SAndreas Gohr        $page = $INPUT->str('p');
318762f4807SAndreas Gohr
319762f4807SAndreas Gohr        $this->db->exec(
320762f4807SAndreas Gohr            'INSERT INTO outlinks (
321762f4807SAndreas Gohr                dt, session, page, link_md5, link
322762f4807SAndreas Gohr             ) VALUES (
323762f4807SAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?, ?
324762f4807SAndreas Gohr             )',
325762f4807SAndreas Gohr            $session, $page, $link_md5, $link
326762f4807SAndreas Gohr        );
327762f4807SAndreas Gohr    }
328762f4807SAndreas Gohr
329762f4807SAndreas Gohr    /**
330762f4807SAndreas Gohr     * Log a page access
331762f4807SAndreas Gohr     *
332762f4807SAndreas Gohr     * Called from log.php
333762f4807SAndreas Gohr     */
334762f4807SAndreas Gohr    public function logAccess(): void
335762f4807SAndreas Gohr    {
336762f4807SAndreas Gohr        global $INPUT, $USERINFO;
337762f4807SAndreas Gohr
338762f4807SAndreas Gohr        if (!$INPUT->str('p')) return;
339762f4807SAndreas Gohr
340762f4807SAndreas Gohr        # FIXME check referer against blacklist and drop logging for bad boys
341762f4807SAndreas Gohr
342762f4807SAndreas Gohr        // handle referer
343762f4807SAndreas Gohr        $referer = trim($INPUT->str('r'));
344762f4807SAndreas Gohr        if ($referer) {
345762f4807SAndreas Gohr            $ref = $referer;
346762f4807SAndreas Gohr            $ref_md5 = md5($referer);
347762f4807SAndreas Gohr            if (str_starts_with($referer, DOKU_URL)) {
348762f4807SAndreas Gohr                $ref_type = 'internal';
349762f4807SAndreas Gohr            } else {
350762f4807SAndreas Gohr                $ref_type = 'external';
351762f4807SAndreas Gohr                $this->logExternalSearch($referer, $ref_type);
352762f4807SAndreas Gohr            }
353762f4807SAndreas Gohr        } else {
354762f4807SAndreas Gohr            $ref = '';
355762f4807SAndreas Gohr            $ref_md5 = '';
356762f4807SAndreas Gohr            $ref_type = '';
357762f4807SAndreas Gohr        }
358762f4807SAndreas Gohr
359762f4807SAndreas Gohr        $page = $INPUT->str('p');
360762f4807SAndreas Gohr        $ip = clientIP(true);
361762f4807SAndreas Gohr        $sx = $INPUT->int('sx');
362762f4807SAndreas Gohr        $sy = $INPUT->int('sy');
363762f4807SAndreas Gohr        $vx = $INPUT->int('vx');
364762f4807SAndreas Gohr        $vy = $INPUT->int('vy');
365762f4807SAndreas Gohr        $js = $INPUT->int('js');
366762f4807SAndreas Gohr        $user = $INPUT->server->str('REMOTE_USER');
367762f4807SAndreas Gohr        $session = $this->getSession();
368762f4807SAndreas Gohr
369762f4807SAndreas Gohr        $this->db->exec(
370762f4807SAndreas Gohr            'INSERT INTO access (
371762f4807SAndreas Gohr                dt, page, ip, ua, ua_info, ua_type, ua_ver, os, ref, ref_md5, ref_type,
372762f4807SAndreas Gohr                screen_x, screen_y, view_x, view_y, js, user, session, uid
373762f4807SAndreas Gohr             ) VALUES (
374762f4807SAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
375762f4807SAndreas Gohr                ?, ?, ?, ?, ?, ?, ?, ?
376762f4807SAndreas Gohr             )',
377762f4807SAndreas Gohr            $page, $ip, $this->uaAgent, $this->uaName, $this->uaType, $this->uaVersion, $this->uaPlatform,
378762f4807SAndreas Gohr            $ref, $ref_md5, $ref_type, $sx, $sy, $vx, $vy, $js, $user, $session, $this->uid
379762f4807SAndreas Gohr        );
380762f4807SAndreas Gohr
381762f4807SAndreas Gohr        if ($ref_md5) {
382762f4807SAndreas Gohr            $this->db->exec(
383762f4807SAndreas Gohr                'INSERT OR IGNORE INTO refseen (
384762f4807SAndreas Gohr                    ref_md5, dt
385762f4807SAndreas Gohr                 ) VALUES (
386762f4807SAndreas Gohr                    ?, CURRENT_TIMESTAMP
387762f4807SAndreas Gohr                 )',
388762f4807SAndreas Gohr                $ref_md5
389762f4807SAndreas Gohr            );
390762f4807SAndreas Gohr        }
391762f4807SAndreas Gohr
392762f4807SAndreas Gohr        // log group access
393762f4807SAndreas Gohr        if (isset($USERINFO['grps'])) {
394762f4807SAndreas Gohr            $this->logGroups('view', $USERINFO['grps']);
395762f4807SAndreas Gohr        }
396762f4807SAndreas Gohr
397762f4807SAndreas Gohr        // resolve the IP
398762f4807SAndreas Gohr        $this->logIp(clientIP(true));
399762f4807SAndreas Gohr    }
400762f4807SAndreas Gohr
401762f4807SAndreas Gohr    /**
402762f4807SAndreas Gohr     * Log access to a media file
403762f4807SAndreas Gohr     *
404762f4807SAndreas Gohr     * Called from action.php
405762f4807SAndreas Gohr     *
406762f4807SAndreas Gohr     * @param string $media The media ID
407762f4807SAndreas Gohr     * @param string $mime The media's mime type
408762f4807SAndreas Gohr     * @param bool $inline Is this displayed inline?
409762f4807SAndreas Gohr     * @param int $size Size of the media file
410762f4807SAndreas Gohr     */
411762f4807SAndreas Gohr    public function logMedia(string $media, string $mime, bool $inline, int $size): void
412762f4807SAndreas Gohr    {
413762f4807SAndreas Gohr        global $INPUT;
414762f4807SAndreas Gohr
415762f4807SAndreas Gohr        [$mime1, $mime2] = explode('/', strtolower($mime));
416762f4807SAndreas Gohr        $inline = $inline ? 1 : 0;
417762f4807SAndreas Gohr        $size = (int)$size;
418762f4807SAndreas Gohr
419762f4807SAndreas Gohr        $ip = clientIP(true);
420762f4807SAndreas Gohr        $user = $INPUT->server->str('REMOTE_USER');
421762f4807SAndreas Gohr        $session = $this->getSession();
422762f4807SAndreas Gohr
423762f4807SAndreas Gohr        $this->db->exec(
424762f4807SAndreas Gohr            'INSERT INTO media (
425762f4807SAndreas Gohr                dt, media, ip, ua, ua_info, ua_type, ua_ver, os, user, session, uid,
426762f4807SAndreas Gohr                size, mime1, mime2, inline
427762f4807SAndreas Gohr             ) VALUES (
428762f4807SAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
429762f4807SAndreas Gohr                ?, ?, ?, ?
430762f4807SAndreas Gohr             )',
431762f4807SAndreas Gohr            $media, $ip, $this->uaAgent, $this->uaName, $this->uaType, $this->uaVersion, $this->uaPlatform,
432762f4807SAndreas Gohr            $user, $session, $this->uid, $size, $mime1, $mime2, $inline
433762f4807SAndreas Gohr        );
434762f4807SAndreas Gohr    }
435762f4807SAndreas Gohr
436762f4807SAndreas Gohr    /**
437762f4807SAndreas Gohr     * Log page edits
438762f4807SAndreas Gohr     *
439762f4807SAndreas Gohr     * @param string $page The page that was edited
440762f4807SAndreas Gohr     * @param string $type The type of edit (create, edit, etc.)
441762f4807SAndreas Gohr     */
442762f4807SAndreas Gohr    public function logEdit(string $page, string $type): void
443762f4807SAndreas Gohr    {
444762f4807SAndreas Gohr        global $INPUT, $USERINFO;
445762f4807SAndreas Gohr
446762f4807SAndreas Gohr        $ip = clientIP(true);
447762f4807SAndreas Gohr        $user = $INPUT->server->str('REMOTE_USER');
448762f4807SAndreas Gohr        $session = $this->getSession();
449762f4807SAndreas Gohr
450762f4807SAndreas Gohr        $this->db->exec(
451762f4807SAndreas Gohr            'INSERT INTO edits (
452762f4807SAndreas Gohr                dt, page, type, ip, user, session, uid
453762f4807SAndreas Gohr             ) VALUES (
454762f4807SAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?
455762f4807SAndreas Gohr             )',
456762f4807SAndreas Gohr            $page, $type, $ip, $user, $session, $this->uid
457762f4807SAndreas Gohr        );
458762f4807SAndreas Gohr
459762f4807SAndreas Gohr        // log group access
460762f4807SAndreas Gohr        if (isset($USERINFO['grps'])) {
461762f4807SAndreas Gohr            $this->logGroups('edit', $USERINFO['grps']);
462762f4807SAndreas Gohr        }
463762f4807SAndreas Gohr    }
464762f4807SAndreas Gohr
465762f4807SAndreas Gohr    /**
466762f4807SAndreas Gohr     * Log login/logoffs and user creations
467762f4807SAndreas Gohr     *
468762f4807SAndreas Gohr     * @param string $type The type of login event (login, logout, create)
469762f4807SAndreas Gohr     * @param string $user The username (optional, will use current user if empty)
470762f4807SAndreas Gohr     */
471762f4807SAndreas Gohr    public function logLogin(string $type, string $user = ''): void
472762f4807SAndreas Gohr    {
473762f4807SAndreas Gohr        global $INPUT;
474762f4807SAndreas Gohr
475762f4807SAndreas Gohr        if (!$user) $user = $INPUT->server->str('REMOTE_USER');
476762f4807SAndreas Gohr
477762f4807SAndreas Gohr        $ip = clientIP(true);
478762f4807SAndreas Gohr        $session = $this->getSession();
479762f4807SAndreas Gohr
480762f4807SAndreas Gohr        $this->db->exec(
481762f4807SAndreas Gohr            'INSERT INTO logins (
482762f4807SAndreas Gohr                dt, type, ip, user, session, uid
483762f4807SAndreas Gohr             ) VALUES (
484762f4807SAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?, ?, ?
485762f4807SAndreas Gohr             )',
486762f4807SAndreas Gohr            $type, $ip, $user, $session, $this->uid
487762f4807SAndreas Gohr        );
488762f4807SAndreas Gohr    }
489762f4807SAndreas Gohr
490762f4807SAndreas Gohr    /**
491762f4807SAndreas Gohr     * Log the current page count and size as today's history entry
492762f4807SAndreas Gohr     */
493762f4807SAndreas Gohr    public function logHistoryPages(): void
494762f4807SAndreas Gohr    {
495762f4807SAndreas Gohr        global $conf;
496762f4807SAndreas Gohr
497762f4807SAndreas Gohr        // use the popularity plugin's search method to find the wanted data
498762f4807SAndreas Gohr        /** @var helper_plugin_popularity $pop */
499762f4807SAndreas Gohr        $pop = plugin_load('helper', 'popularity');
500762f4807SAndreas Gohr        $list = [];
501762f4807SAndreas Gohr        search($list, $conf['datadir'], [$pop, 'searchCountCallback'], ['all' => false], '');
502762f4807SAndreas Gohr        $page_count = $list['file_count'];
503762f4807SAndreas Gohr        $page_size = $list['file_size'];
504762f4807SAndreas Gohr
505762f4807SAndreas Gohr        $this->db->exec(
506762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
507762f4807SAndreas Gohr                info, value, dt
508762f4807SAndreas Gohr             ) VALUES (
509762f4807SAndreas Gohr                ?, ?, date("now")
510762f4807SAndreas Gohr             )',
511762f4807SAndreas Gohr            'page_count', $page_count
512762f4807SAndreas Gohr        );
513762f4807SAndreas Gohr        $this->db->exec(
514762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
515762f4807SAndreas Gohr                info, value, dt
516762f4807SAndreas Gohr             ) VALUES (
517762f4807SAndreas Gohr                ?, ?, date("now")
518762f4807SAndreas Gohr             )',
519762f4807SAndreas Gohr            'page_size', $page_size
520762f4807SAndreas Gohr        );
521762f4807SAndreas Gohr    }
522762f4807SAndreas Gohr
523762f4807SAndreas Gohr    /**
524762f4807SAndreas Gohr     * Log the current media count and size as today's history entry
525762f4807SAndreas Gohr     */
526762f4807SAndreas Gohr    public function logHistoryMedia(): void
527762f4807SAndreas Gohr    {
528762f4807SAndreas Gohr        global $conf;
529762f4807SAndreas Gohr
530762f4807SAndreas Gohr        // use the popularity plugin's search method to find the wanted data
531762f4807SAndreas Gohr        /** @var helper_plugin_popularity $pop */
532762f4807SAndreas Gohr        $pop = plugin_load('helper', 'popularity');
533762f4807SAndreas Gohr        $list = [];
534762f4807SAndreas Gohr        search($list, $conf['mediadir'], [$pop, 'searchCountCallback'], ['all' => true], '');
535762f4807SAndreas Gohr        $media_count = $list['file_count'];
536762f4807SAndreas Gohr        $media_size = $list['file_size'];
537762f4807SAndreas Gohr
538762f4807SAndreas Gohr        $this->db->exec(
539762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
540762f4807SAndreas Gohr                info, value, dt
541762f4807SAndreas Gohr             ) VALUES (
542762f4807SAndreas Gohr                ?, ?, date("now")
543762f4807SAndreas Gohr             )',
544762f4807SAndreas Gohr            'media_count', $media_count
545762f4807SAndreas Gohr        );
546762f4807SAndreas Gohr        $this->db->exec(
547762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
548762f4807SAndreas Gohr                info, value, dt
549762f4807SAndreas Gohr             ) VALUES (
550762f4807SAndreas Gohr                ?, ?, date("now")
551762f4807SAndreas Gohr             )',
552762f4807SAndreas Gohr            'media_size', $media_size
553762f4807SAndreas Gohr        );
554762f4807SAndreas Gohr    }
555762f4807SAndreas Gohr}
556