xref: /plugin/statistics/Logger.php (revision 1c4e3694d34de9696954292b37ba9c070a747526)
1762f4807SAndreas Gohr<?php
2762f4807SAndreas Gohr
3762f4807SAndreas Gohrnamespace dokuwiki\plugin\statistics;
4762f4807SAndreas Gohr
5*1c4e3694SAndreas Gohruse DeviceDetector\ClientHints;
6762f4807SAndreas Gohruse DeviceDetector\DeviceDetector;
7762f4807SAndreas Gohruse DeviceDetector\Parser\Client\Browser;
8762f4807SAndreas Gohruse DeviceDetector\Parser\Device\AbstractDeviceParser;
9762f4807SAndreas Gohruse DeviceDetector\Parser\OperatingSystem;
1041d1fffcSAndreas Gohruse dokuwiki\Input\Input;
11762f4807SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
12762f4807SAndreas Gohruse helper_plugin_popularity;
13762f4807SAndreas Gohruse helper_plugin_statistics;
14762f4807SAndreas Gohr
15762f4807SAndreas Gohrclass Logger
16762f4807SAndreas Gohr{
17762f4807SAndreas Gohr    /** @var helper_plugin_statistics The statistics helper plugin instance */
18762f4807SAndreas Gohr    protected helper_plugin_statistics $hlp;
19762f4807SAndreas Gohr
20762f4807SAndreas Gohr    /** @var SQLiteDB The SQLite database instance */
21762f4807SAndreas Gohr    protected SQLiteDB $db;
22762f4807SAndreas Gohr
23762f4807SAndreas Gohr    /** @var string The full user agent string */
24762f4807SAndreas Gohr    protected string $uaAgent;
25762f4807SAndreas Gohr
26762f4807SAndreas Gohr    /** @var string The type of user agent (browser, robot, feedreader) */
27762f4807SAndreas Gohr    protected string $uaType = 'browser';
28762f4807SAndreas Gohr
29762f4807SAndreas Gohr    /** @var string The browser/client name */
30762f4807SAndreas Gohr    protected string $uaName;
31762f4807SAndreas Gohr
32762f4807SAndreas Gohr    /** @var string The browser/client version */
33762f4807SAndreas Gohr    protected string $uaVersion;
34762f4807SAndreas Gohr
35762f4807SAndreas Gohr    /** @var string The operating system/platform */
36762f4807SAndreas Gohr    protected string $uaPlatform;
37762f4807SAndreas Gohr
384a163f50SAndreas Gohr    /** @var string|null The user name, if available */
394a163f50SAndreas Gohr    protected ?string $user = null;
404a163f50SAndreas Gohr
41762f4807SAndreas Gohr    /** @var string The unique user identifier */
42762f4807SAndreas Gohr    protected string $uid;
43762f4807SAndreas Gohr
444a163f50SAndreas Gohr    /** @var string The session identifier */
454a163f50SAndreas Gohr    protected string $session;
464a163f50SAndreas Gohr
474a163f50SAndreas Gohr    /** @var int|null The ID of the main access log entry if any */
484a163f50SAndreas Gohr    protected ?int $hit = null;
494a163f50SAndreas Gohr
504a163f50SAndreas Gohr    // region lifecycle
51762f4807SAndreas Gohr
52762f4807SAndreas Gohr    /**
53762f4807SAndreas Gohr     * Constructor
54762f4807SAndreas Gohr     *
55762f4807SAndreas Gohr     * Parses browser info and set internal vars
56762f4807SAndreas Gohr     */
57ba6b3b10SAndreas Gohr    public function __construct(helper_plugin_statistics $hlp)
58762f4807SAndreas Gohr    {
5941d1fffcSAndreas Gohr        /** @var Input $INPUT */
60762f4807SAndreas Gohr        global $INPUT;
61762f4807SAndreas Gohr
62762f4807SAndreas Gohr        $this->hlp = $hlp;
63762f4807SAndreas Gohr        $this->db = $this->hlp->getDB();
64762f4807SAndreas Gohr
654a163f50SAndreas Gohr        // FIXME if we already have a session, we should not re-parse the user agent
66762f4807SAndreas Gohr
674a163f50SAndreas Gohr        $ua = trim($INPUT->server->str('HTTP_USER_AGENT'));
68762f4807SAndreas Gohr        AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR);
69*1c4e3694SAndreas Gohr        $dd = new DeviceDetector($ua, ClientHints::factory($_SERVER));
70762f4807SAndreas Gohr        $dd->discardBotInformation();
71762f4807SAndreas Gohr        $dd->parse();
72762f4807SAndreas Gohr
7300f786d8SAndreas Gohr        if ($dd->isFeedReader()) {
7400f786d8SAndreas Gohr            $this->uaType = 'feedreader';
7500f786d8SAndreas Gohr        } elseif ($dd->isBot()) {
76762f4807SAndreas Gohr            $this->uaType = 'robot';
77762f4807SAndreas Gohr            // for now ignore bots
78c5d2f052SAndreas Gohr            throw new IgnoreException('Bot detected, not logging');
79762f4807SAndreas Gohr        }
80762f4807SAndreas Gohr
81762f4807SAndreas Gohr        $this->uaAgent = $ua;
82823a6144SAndreas Gohr        $this->uaName = $dd->getClient('name') ?: 'Unknown';
8300f786d8SAndreas Gohr        $this->uaVersion = $dd->getClient('version') ?: '0';
8405786d83SAndreas Gohr        $this->uaPlatform = OperatingSystem::getOsFamily($dd->getOs('name')) ?: 'Unknown';
85762f4807SAndreas Gohr        $this->uid = $this->getUID();
864a163f50SAndreas Gohr        $this->session = $this->getSession();
87d550a4adSAndreas Gohr
88d550a4adSAndreas Gohr        if (!$this->hlp->getConf('nousers')) {
8941d1fffcSAndreas Gohr            $this->user = $INPUT->server->str('REMOTE_USER', null, true);
90762f4807SAndreas Gohr        }
91d550a4adSAndreas Gohr    }
92762f4807SAndreas Gohr
93762f4807SAndreas Gohr    /**
94762f4807SAndreas Gohr     * Should be called before logging
95762f4807SAndreas Gohr     *
964a163f50SAndreas Gohr     * This starts a transaction, so all logging is done in one go. It also logs the user and session data.
97762f4807SAndreas Gohr     */
98762f4807SAndreas Gohr    public function begin(): void
99762f4807SAndreas Gohr    {
100762f4807SAndreas Gohr        $this->hlp->getDB()->getPdo()->beginTransaction();
1014a163f50SAndreas Gohr
1024a163f50SAndreas Gohr        $this->logUser();
1034a163f50SAndreas Gohr        $this->logGroups();
1044a163f50SAndreas Gohr        $this->logDomain();
1054a163f50SAndreas Gohr        $this->logSession();
106762f4807SAndreas Gohr    }
107762f4807SAndreas Gohr
108762f4807SAndreas Gohr    /**
109762f4807SAndreas Gohr     * Should be called after logging
110762f4807SAndreas Gohr     *
111762f4807SAndreas Gohr     * This commits the transaction started in begin()
112762f4807SAndreas Gohr     */
113762f4807SAndreas Gohr    public function end(): void
114762f4807SAndreas Gohr    {
115762f4807SAndreas Gohr        $this->hlp->getDB()->getPdo()->commit();
116762f4807SAndreas Gohr    }
117762f4807SAndreas Gohr
1184a163f50SAndreas Gohr    // endregion
1194a163f50SAndreas Gohr    // region data gathering
1204a163f50SAndreas Gohr
121762f4807SAndreas Gohr    /**
122762f4807SAndreas Gohr     * Get the unique user ID
123762f4807SAndreas Gohr     *
12404928db4SAndreas Gohr     * The user ID is stored in the user preferences and should stay there forever.
125762f4807SAndreas Gohr     * @return string The unique user identifier
126762f4807SAndreas Gohr     */
127762f4807SAndreas Gohr    protected function getUID(): string
128762f4807SAndreas Gohr    {
12904928db4SAndreas Gohr        if (!isset($_SESSION[DOKU_COOKIE]['statistics']['uid'])) {
13004928db4SAndreas Gohr            // when there is no session UID set, we assume this was deliberate and we simply abort all logging
13104928db4SAndreas Gohr            // @todo we may later make UID generation optional
13204928db4SAndreas Gohr            throw new IgnoreException('No user ID found');
13304928db4SAndreas Gohr        }
134762f4807SAndreas Gohr
13504928db4SAndreas Gohr        return $_SESSION[DOKU_COOKIE]['statistics']['uid'];
136762f4807SAndreas Gohr    }
137762f4807SAndreas Gohr
138762f4807SAndreas Gohr    /**
139762f4807SAndreas Gohr     * Return the user's session ID
140762f4807SAndreas Gohr     *
141762f4807SAndreas Gohr     * @return string The session identifier
142762f4807SAndreas Gohr     */
143762f4807SAndreas Gohr    protected function getSession(): string
144762f4807SAndreas Gohr    {
14504928db4SAndreas Gohr        if (!isset($_SESSION[DOKU_COOKIE]['statistics']['id'])) {
14604928db4SAndreas Gohr            // when there is no session ID set, we assume this was deliberate and we simply abort all logging
14704928db4SAndreas Gohr            throw new IgnoreException('No session ID found');
14804928db4SAndreas Gohr        }
149762f4807SAndreas Gohr
15004928db4SAndreas Gohr        return $_SESSION[DOKU_COOKIE]['statistics']['id'];
151762f4807SAndreas Gohr    }
152762f4807SAndreas Gohr
1534a163f50SAndreas Gohr    // endregion
1544a163f50SAndreas Gohr    // region automatic logging
155762f4807SAndreas Gohr
1564a163f50SAndreas Gohr    /**
1574a163f50SAndreas Gohr     * Log the user was seen
1584a163f50SAndreas Gohr     */
1594a163f50SAndreas Gohr    protected function logUser(): void
1604a163f50SAndreas Gohr    {
1614a163f50SAndreas Gohr        if (!$this->user) return;
162762f4807SAndreas Gohr
163762f4807SAndreas Gohr        $this->db->exec(
1644a163f50SAndreas Gohr            'INSERT INTO users (user, dt)
1654a163f50SAndreas Gohr                  VALUES (?, CURRENT_TIMESTAMP)
1664a163f50SAndreas Gohr            ON CONFLICT (user) DO UPDATE SET
1674a163f50SAndreas Gohr                         dt = CURRENT_TIMESTAMP
1684a163f50SAndreas Gohr                   WHERE excluded.user = users.user
1694a163f50SAndreas Gohr            ',
1704a163f50SAndreas Gohr            $this->user
1714a163f50SAndreas Gohr        );
1724a163f50SAndreas Gohr    }
1734a163f50SAndreas Gohr
1744a163f50SAndreas Gohr    /**
1754a163f50SAndreas Gohr     * Log the session and user agent information
1764a163f50SAndreas Gohr     */
1774a163f50SAndreas Gohr    protected function logSession(): void
1784a163f50SAndreas Gohr    {
1794a163f50SAndreas Gohr        $this->db->exec(
1804a163f50SAndreas Gohr            'INSERT INTO sessions (session, dt, end, uid, user, ua, ua_info, ua_type, ua_ver, os)
1814a163f50SAndreas Gohr                  VALUES (?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?)
1824a163f50SAndreas Gohr             ON CONFLICT (session) DO UPDATE SET
18341d1fffcSAndreas Gohr                         end = CURRENT_TIMESTAMP,
18441d1fffcSAndreas Gohr                         user = excluded.user,
18541d1fffcSAndreas Gohr                         uid = excluded.uid
1864a163f50SAndreas Gohr                   WHERE excluded.session = sessions.session
1874a163f50SAndreas Gohr             ',
1884a163f50SAndreas Gohr            $this->session,
1894a163f50SAndreas Gohr            $this->uid,
1904a163f50SAndreas Gohr            $this->user,
1914a163f50SAndreas Gohr            $this->uaAgent,
1924a163f50SAndreas Gohr            $this->uaName,
1934a163f50SAndreas Gohr            $this->uaType,
1944a163f50SAndreas Gohr            $this->uaVersion,
1954a163f50SAndreas Gohr            $this->uaPlatform
196762f4807SAndreas Gohr        );
197762f4807SAndreas Gohr    }
198762f4807SAndreas Gohr
199762f4807SAndreas Gohr    /**
2004a163f50SAndreas Gohr     * Log all groups for the user
201762f4807SAndreas Gohr     *
2024a163f50SAndreas Gohr     * @todo maybe this should be done only once per session?
203762f4807SAndreas Gohr     */
2044a163f50SAndreas Gohr    protected function logGroups(): void
205762f4807SAndreas Gohr    {
2064a163f50SAndreas Gohr        global $USERINFO;
207762f4807SAndreas Gohr
2084a163f50SAndreas Gohr        if (!$this->user) return;
2094a163f50SAndreas Gohr        if (!isset($USERINFO['grps'])) return;
2104a163f50SAndreas Gohr        if (!is_array($USERINFO['grps'])) return;
2114a163f50SAndreas Gohr        $groups = $USERINFO['grps'];
212fced2f86SAnna Dabrowska
2134a163f50SAndreas Gohr        $this->db->exec('DELETE FROM groups WHERE user = ?', $this->user);
214762f4807SAndreas Gohr
215bd514593SAndreas Gohr        if ($groups === []) {
21641d1fffcSAndreas Gohr            return;
21741d1fffcSAndreas Gohr        }
21841d1fffcSAndreas Gohr
21902aa9b73SAndreas Gohr        $placeholders = implode(',', array_fill(0, count($groups), '(?, ?)'));
220762f4807SAndreas Gohr        $params = [];
2214a163f50SAndreas Gohr        $sql = "INSERT INTO groups (`user`, `group`) VALUES $placeholders";
222762f4807SAndreas Gohr        foreach ($groups as $group) {
2234a163f50SAndreas Gohr            $params[] = $this->user;
224762f4807SAndreas Gohr            $params[] = $group;
225762f4807SAndreas Gohr        }
226762f4807SAndreas Gohr        $this->db->exec($sql, $params);
227762f4807SAndreas Gohr    }
228762f4807SAndreas Gohr
229762f4807SAndreas Gohr    /**
2304a163f50SAndreas Gohr     * Log email domain
2310b8b7abaSAnna Dabrowska     *
2324a163f50SAndreas Gohr     * @todo maybe this should be done only once per session?
2330b8b7abaSAnna Dabrowska     */
2344a163f50SAndreas Gohr    protected function logDomain(): void
2350b8b7abaSAnna Dabrowska    {
2364a163f50SAndreas Gohr        global $USERINFO;
2374a163f50SAndreas Gohr        if (!$this->user) return;
2384a163f50SAndreas Gohr        if (!isset($USERINFO['mail'])) return;
2394a163f50SAndreas Gohr        $mail = $USERINFO['mail'];
2400b8b7abaSAnna Dabrowska
2410b8b7abaSAnna Dabrowska        $pos = strrpos($mail, '@');
2420b8b7abaSAnna Dabrowska        if (!$pos) return;
2430b8b7abaSAnna Dabrowska        $domain = substr($mail, $pos + 1);
2440b8b7abaSAnna Dabrowska        if (empty($domain)) return;
2450b8b7abaSAnna Dabrowska
2464a163f50SAndreas Gohr        $sql = 'UPDATE users SET domain = ? WHERE user = ?';
2474a163f50SAndreas Gohr        $this->db->exec($sql, [$domain, $this->user]);
2480b8b7abaSAnna Dabrowska    }
2490b8b7abaSAnna Dabrowska
2504a163f50SAndreas Gohr    // endregion
2514a163f50SAndreas Gohr    // region internal loggers called by the dispatchers
2524a163f50SAndreas Gohr
2530b8b7abaSAnna Dabrowska    /**
2544a163f50SAndreas Gohr     * Log the given referer URL
255762f4807SAndreas Gohr     *
2562a30f557SAndreas Gohr     * Note: we DO log empty referers. These are external accesses that did not provide a referer URL.
2572a30f557SAndreas Gohr     * We do not log referers that are our own pages though.
2582a30f557SAndreas Gohr     *
2592a30f557SAndreas Gohr     * engine set -> a search engine referer
2602a30f557SAndreas Gohr     * no engine set, url empty -> a direct access (bookmark, direct link, etc.)
2612a30f557SAndreas Gohr     * no engine set, url not empty -> a referer from another page (not a wiki page)
2622a30f557SAndreas Gohr     * null returned -> referer was a wiki page
2632a30f557SAndreas Gohr     *
2644a163f50SAndreas Gohr     * @param $referer
2652a30f557SAndreas Gohr     * @return int|null The referer ID or null if no referer was logged
2662a30f557SAndreas Gohr     * @todo we could check against a blacklist here
267762f4807SAndreas Gohr     */
2684a163f50SAndreas Gohr    public function logReferer($referer): ?int
269762f4807SAndreas Gohr    {
2702a30f557SAndreas Gohr        $referer = trim($referer);
271762f4807SAndreas Gohr
272569a5066SAndreas Gohr        // do not log our own pages as referers (empty referer is OK though)
273569a5066SAndreas Gohr        if (!empty($referer)) {
274569a5066SAndreas Gohr            $selfre = '^' . preg_quote(DOKU_URL, '/');
2752a30f557SAndreas Gohr            if (preg_match("/$selfre/", $referer)) {
2762a30f557SAndreas Gohr                return null;
2772a30f557SAndreas Gohr            }
278569a5066SAndreas Gohr        }
279762f4807SAndreas Gohr
2802a30f557SAndreas Gohr        // is it a search engine?
2814a163f50SAndreas Gohr        $se = new SearchEngines($referer);
28241d1fffcSAndreas Gohr        $engine = $se->getEngine();
283762f4807SAndreas Gohr
28441d1fffcSAndreas Gohr        $sql = 'INSERT OR IGNORE INTO referers (url, engine, dt) VALUES (?, ?, CURRENT_TIMESTAMP)';
285569a5066SAndreas Gohr        $this->db->exec($sql, [$referer, $engine]);
286569a5066SAndreas Gohr        return (int)$this->db->queryValue('SELECT id FROM referers WHERE url = ?', $referer);
287762f4807SAndreas Gohr    }
288762f4807SAndreas Gohr
289762f4807SAndreas Gohr    /**
290762f4807SAndreas Gohr     * Resolve IP to country/city and store in database
291762f4807SAndreas Gohr     *
2924a163f50SAndreas Gohr     * @return string The IP address as stored
293762f4807SAndreas Gohr     */
2944a163f50SAndreas Gohr    public function logIp(): string
295762f4807SAndreas Gohr    {
2964a163f50SAndreas Gohr        $ip = clientIP(true);
29769fb56a2SAndreas Gohr
29869fb56a2SAndreas Gohr        // anonymize the IP address for storage?
29969fb56a2SAndreas Gohr        if ($this->hlp->getConf('anonips')) {
30069fb56a2SAndreas Gohr            $hash = md5($ip . strrev($ip)); // we use the reversed IP as salt to avoid common rainbow tables
30169fb56a2SAndreas Gohr            $host = '';
30269fb56a2SAndreas Gohr        } else {
30369fb56a2SAndreas Gohr            $hash = $ip;
30469fb56a2SAndreas Gohr            $host = gethostbyaddr($ip);
30569fb56a2SAndreas Gohr        }
3064a163f50SAndreas Gohr
307ba6b3b10SAndreas Gohr        if ($this->hlp->getConf('nolocation')) {
308ba6b3b10SAndreas Gohr            // if we don't resolve location data, we just return the IP address
309ba6b3b10SAndreas Gohr            return $hash;
310ba6b3b10SAndreas Gohr        }
311ba6b3b10SAndreas Gohr
312762f4807SAndreas Gohr        // check if IP already known and up-to-date
313762f4807SAndreas Gohr        $result = $this->db->queryValue(
314762f4807SAndreas Gohr            "SELECT ip
315762f4807SAndreas Gohr             FROM   iplocation
316762f4807SAndreas Gohr             WHERE  ip = ?
3177a1a7c58SAndreas Gohr               AND  dt > date('now', '-30 days')",
3184a163f50SAndreas Gohr            $hash
319762f4807SAndreas Gohr        );
3204a163f50SAndreas Gohr        if ($result) return $hash; // already known and up-to-date
321762f4807SAndreas Gohr
322762f4807SAndreas Gohr
323ba6b3b10SAndreas Gohr        // resolve the IP address to location data
324762f4807SAndreas Gohr        try {
325ba6b3b10SAndreas Gohr            $data = $this->hlp->resolveIP($ip);
326ba6b3b10SAndreas Gohr        } catch (IpResolverException $e) {
327ba6b3b10SAndreas Gohr            \dokuwiki\Logger::error('Statistics Plugin: ' . $e->getMessage(), $e->details);
328ba6b3b10SAndreas Gohr            $data = [];
329762f4807SAndreas Gohr        }
330762f4807SAndreas Gohr
331762f4807SAndreas Gohr        $this->db->exec(
332762f4807SAndreas Gohr            'INSERT OR REPLACE INTO iplocation (
3337a1a7c58SAndreas Gohr                    ip, country, code, city, host, dt
334762f4807SAndreas Gohr                 ) VALUES (
335762f4807SAndreas Gohr                    ?, ?, ?, ?, ?, CURRENT_TIMESTAMP
336762f4807SAndreas Gohr                 )',
3374a163f50SAndreas Gohr            $hash,
33802aa9b73SAndreas Gohr            $data['country'] ?? '',
33902aa9b73SAndreas Gohr            $data['countryCode'] ?? '',
34002aa9b73SAndreas Gohr            $data['city'] ?? '',
3412adee4c6SAndreas Gohr            $host
342762f4807SAndreas Gohr        );
3434a163f50SAndreas Gohr
3444a163f50SAndreas Gohr        return $hash;
3454a163f50SAndreas Gohr    }
3464a163f50SAndreas Gohr
3474a163f50SAndreas Gohr    // endregion
3484a163f50SAndreas Gohr    // region log dispatchers
3494a163f50SAndreas Gohr
3504a163f50SAndreas Gohr    public function logPageView(): void
3514a163f50SAndreas Gohr    {
3524a163f50SAndreas Gohr        global $INPUT;
3534a163f50SAndreas Gohr
3544a163f50SAndreas Gohr        if (!$INPUT->str('p')) return;
3554a163f50SAndreas Gohr
3564a163f50SAndreas Gohr
3574a163f50SAndreas Gohr        $referer = $INPUT->filter('trim')->str('r');
3584a163f50SAndreas Gohr        $ip = $this->logIp(); // resolve the IP address
3594a163f50SAndreas Gohr
3604a163f50SAndreas Gohr        $data = [
3614a163f50SAndreas Gohr            'page' => $INPUT->filter('cleanID')->str('p'),
3624a163f50SAndreas Gohr            'ip' => $ip,
3634a163f50SAndreas Gohr            'ref_id' => $this->logReferer($referer),
3644a163f50SAndreas Gohr            'sx' => $INPUT->int('sx'),
3654a163f50SAndreas Gohr            'sy' => $INPUT->int('sy'),
3664a163f50SAndreas Gohr            'vx' => $INPUT->int('vx'),
3674a163f50SAndreas Gohr            'vy' => $INPUT->int('vy'),
3684a163f50SAndreas Gohr            'session' => $this->session,
3694a163f50SAndreas Gohr        ];
3704a163f50SAndreas Gohr
371bd514593SAndreas Gohr        $this->db->exec(
372bd514593SAndreas Gohr            '
3734a163f50SAndreas Gohr        INSERT INTO pageviews (
3744a163f50SAndreas Gohr            dt, page, ip, ref_id, screen_x, screen_y, view_x, view_y, session
3754a163f50SAndreas Gohr        ) VALUES (
3764a163f50SAndreas Gohr            CURRENT_TIMESTAMP, :page, :ip, :ref_id, :sx, :sy, :vx, :vy, :session
3774a163f50SAndreas Gohr        )
3784a163f50SAndreas Gohr        ',
3794a163f50SAndreas Gohr            $data
3804a163f50SAndreas Gohr        );
381762f4807SAndreas Gohr    }
382762f4807SAndreas Gohr
383762f4807SAndreas Gohr    /**
384762f4807SAndreas Gohr     * Log a click on an external link
385762f4807SAndreas Gohr     *
38687e0f0b1SAndreas Gohr     * Called from dispatch.php
387762f4807SAndreas Gohr     */
388762f4807SAndreas Gohr    public function logOutgoing(): void
389762f4807SAndreas Gohr    {
390762f4807SAndreas Gohr        global $INPUT;
391762f4807SAndreas Gohr
392762f4807SAndreas Gohr        if (!$INPUT->str('ol')) return;
393762f4807SAndreas Gohr
3944a163f50SAndreas Gohr        $link = $INPUT->filter('trim')->str('ol');
3954a163f50SAndreas Gohr        $session = $this->session;
3964a163f50SAndreas Gohr        $page = $INPUT->filter('cleanID')->str('p');
397762f4807SAndreas Gohr
398762f4807SAndreas Gohr        $this->db->exec(
399762f4807SAndreas Gohr            'INSERT INTO outlinks (
4004a163f50SAndreas Gohr                dt, session, page, link
401762f4807SAndreas Gohr             ) VALUES (
40241d1fffcSAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?
403762f4807SAndreas Gohr             )',
4042adee4c6SAndreas Gohr            $session,
4052adee4c6SAndreas Gohr            $page,
4062adee4c6SAndreas Gohr            $link
407762f4807SAndreas Gohr        );
408762f4807SAndreas Gohr    }
409762f4807SAndreas Gohr
410762f4807SAndreas Gohr    /**
411762f4807SAndreas Gohr     * Log access to a media file
412762f4807SAndreas Gohr     *
413762f4807SAndreas Gohr     * Called from action.php
414762f4807SAndreas Gohr     *
415762f4807SAndreas Gohr     * @param string $media The media ID
416762f4807SAndreas Gohr     * @param string $mime The media's mime type
417762f4807SAndreas Gohr     * @param bool $inline Is this displayed inline?
418762f4807SAndreas Gohr     * @param int $size Size of the media file
419762f4807SAndreas Gohr     */
420762f4807SAndreas Gohr    public function logMedia(string $media, string $mime, bool $inline, int $size): void
421762f4807SAndreas Gohr    {
422762f4807SAndreas Gohr        [$mime1, $mime2] = explode('/', strtolower($mime));
423762f4807SAndreas Gohr        $inline = $inline ? 1 : 0;
424762f4807SAndreas Gohr
425762f4807SAndreas Gohr
4264a163f50SAndreas Gohr        $data = [
4274a163f50SAndreas Gohr            'media' => cleanID($media),
4284a163f50SAndreas Gohr            'ip' => $this->logIp(), // resolve the IP address
4294a163f50SAndreas Gohr            'session' => $this->session,
4304a163f50SAndreas Gohr            'size' => $size,
4314a163f50SAndreas Gohr            'mime1' => $mime1,
4324a163f50SAndreas Gohr            'mime2' => $mime2,
4334a163f50SAndreas Gohr            'inline' => $inline,
4344a163f50SAndreas Gohr        ];
4354a163f50SAndreas Gohr
436bd514593SAndreas Gohr        $this->db->exec(
437bd514593SAndreas Gohr            '
4384a163f50SAndreas Gohr                INSERT INTO media ( dt, media, ip, session, size, mime1, mime2, inline )
4394a163f50SAndreas Gohr                     VALUES (CURRENT_TIMESTAMP, :media, :ip, :session, :size, :mime1, :mime2, :inline)
4404a163f50SAndreas Gohr            ',
4414a163f50SAndreas Gohr            $data
442762f4807SAndreas Gohr        );
443762f4807SAndreas Gohr    }
444762f4807SAndreas Gohr
445762f4807SAndreas Gohr    /**
446762f4807SAndreas Gohr     * Log page edits
447762f4807SAndreas Gohr     *
4484a163f50SAndreas Gohr     * called from action.php
4494a163f50SAndreas Gohr     *
450762f4807SAndreas Gohr     * @param string $page The page that was edited
451762f4807SAndreas Gohr     * @param string $type The type of edit (create, edit, etc.)
452762f4807SAndreas Gohr     */
453762f4807SAndreas Gohr    public function logEdit(string $page, string $type): void
454762f4807SAndreas Gohr    {
4554a163f50SAndreas Gohr        $data = [
4564a163f50SAndreas Gohr            'page' => cleanID($page),
4574a163f50SAndreas Gohr            'type' => $type,
4584a163f50SAndreas Gohr            'ip' => $this->logIp(), // resolve the IP address
4594a163f50SAndreas Gohr            'session' => $this->session
4604a163f50SAndreas Gohr        ];
461762f4807SAndreas Gohr
46241d1fffcSAndreas Gohr        $this->db->exec(
463762f4807SAndreas Gohr            'INSERT INTO edits (
4644a163f50SAndreas Gohr                dt, page, type, ip, session
465762f4807SAndreas Gohr             ) VALUES (
4664a163f50SAndreas Gohr                CURRENT_TIMESTAMP, :page, :type, :ip, :session
467762f4807SAndreas Gohr             )',
4684a163f50SAndreas Gohr            $data
469762f4807SAndreas Gohr        );
470762f4807SAndreas Gohr    }
471762f4807SAndreas Gohr
472762f4807SAndreas Gohr    /**
473762f4807SAndreas Gohr     * Log login/logoffs and user creations
474762f4807SAndreas Gohr     *
475af93d154SAndreas Gohr     * @param string $type The type of login event (login, logout, create, failed)
476af93d154SAndreas Gohr     * @param string $user The username
477762f4807SAndreas Gohr     */
478762f4807SAndreas Gohr    public function logLogin(string $type, string $user = ''): void
479762f4807SAndreas Gohr    {
480762f4807SAndreas Gohr        global $INPUT;
481762f4807SAndreas Gohr
482762f4807SAndreas Gohr        if (!$user) $user = $INPUT->server->str('REMOTE_USER');
483762f4807SAndreas Gohr
484762f4807SAndreas Gohr        $ip = clientIP(true);
485762f4807SAndreas Gohr
486762f4807SAndreas Gohr        $this->db->exec(
487762f4807SAndreas Gohr            'INSERT INTO logins (
488af93d154SAndreas Gohr                dt, ip, user, type
489762f4807SAndreas Gohr             ) VALUES (
490af93d154SAndreas Gohr                CURRENT_TIMESTAMP, ?, ?, ?
491762f4807SAndreas Gohr             )',
4922adee4c6SAndreas Gohr            $ip,
4932adee4c6SAndreas Gohr            $user,
494af93d154SAndreas Gohr            $type
495762f4807SAndreas Gohr        );
496762f4807SAndreas Gohr    }
497762f4807SAndreas Gohr
498762f4807SAndreas Gohr    /**
49902aa9b73SAndreas Gohr     * Log search data to the search related tables
50002aa9b73SAndreas Gohr     *
50102aa9b73SAndreas Gohr     * @param string $query The search query
50202aa9b73SAndreas Gohr     * @param string[] $words The query split into words
50302aa9b73SAndreas Gohr     */
50402aa9b73SAndreas Gohr    public function logSearch(string $query, array $words): void
50502aa9b73SAndreas Gohr    {
50602aa9b73SAndreas Gohr        if (!$query) return;
50702aa9b73SAndreas Gohr
50802aa9b73SAndreas Gohr        $sid = $this->db->exec(
50902aa9b73SAndreas Gohr            'INSERT INTO search (dt, ip, session, query) VALUES (CURRENT_TIMESTAMP, ?, ? , ?)',
51002aa9b73SAndreas Gohr            $this->logIp(), // resolve the IP address
51102aa9b73SAndreas Gohr            $this->session,
51202aa9b73SAndreas Gohr            $query,
51302aa9b73SAndreas Gohr        );
51402aa9b73SAndreas Gohr
51502aa9b73SAndreas Gohr        foreach ($words as $word) {
51602aa9b73SAndreas Gohr            if (!$word) continue;
51702aa9b73SAndreas Gohr            $this->db->exec(
51802aa9b73SAndreas Gohr                'INSERT INTO searchwords (sid, word) VALUES (?, ?)',
51902aa9b73SAndreas Gohr                $sid,
52002aa9b73SAndreas Gohr                $word
52102aa9b73SAndreas Gohr            );
52202aa9b73SAndreas Gohr        }
52302aa9b73SAndreas Gohr    }
52402aa9b73SAndreas Gohr
52502aa9b73SAndreas Gohr    /**
526762f4807SAndreas Gohr     * Log the current page count and size as today's history entry
527762f4807SAndreas Gohr     */
528762f4807SAndreas Gohr    public function logHistoryPages(): void
529762f4807SAndreas Gohr    {
530762f4807SAndreas Gohr        global $conf;
531762f4807SAndreas Gohr
532762f4807SAndreas Gohr        // use the popularity plugin's search method to find the wanted data
533762f4807SAndreas Gohr        /** @var helper_plugin_popularity $pop */
534762f4807SAndreas Gohr        $pop = plugin_load('helper', 'popularity');
535b188870fSAndreas Gohr        $list = $this->initEmptySearchList();
536762f4807SAndreas Gohr        search($list, $conf['datadir'], [$pop, 'searchCountCallback'], ['all' => false], '');
537762f4807SAndreas Gohr        $page_count = $list['file_count'];
538762f4807SAndreas Gohr        $page_size = $list['file_size'];
539762f4807SAndreas Gohr
540762f4807SAndreas Gohr        $this->db->exec(
541762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
542762f4807SAndreas Gohr                info, value, dt
543762f4807SAndreas Gohr             ) VALUES (
544483101d3SAndreas Gohr                ?, ?, CURRENT_TIMESTAMP
545762f4807SAndreas Gohr             )',
5462adee4c6SAndreas Gohr            'page_count',
5472adee4c6SAndreas Gohr            $page_count
548762f4807SAndreas Gohr        );
549762f4807SAndreas Gohr        $this->db->exec(
550762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
551762f4807SAndreas Gohr                info, value, dt
552762f4807SAndreas Gohr             ) VALUES (
553483101d3SAndreas Gohr                ?, ?, CURRENT_TIMESTAMP
554762f4807SAndreas Gohr             )',
5552adee4c6SAndreas Gohr            'page_size',
5562adee4c6SAndreas Gohr            $page_size
557762f4807SAndreas Gohr        );
558762f4807SAndreas Gohr    }
559762f4807SAndreas Gohr
560762f4807SAndreas Gohr    /**
561762f4807SAndreas Gohr     * Log the current media count and size as today's history entry
562762f4807SAndreas Gohr     */
563762f4807SAndreas Gohr    public function logHistoryMedia(): void
564762f4807SAndreas Gohr    {
565762f4807SAndreas Gohr        global $conf;
566762f4807SAndreas Gohr
567762f4807SAndreas Gohr        // use the popularity plugin's search method to find the wanted data
568762f4807SAndreas Gohr        /** @var helper_plugin_popularity $pop */
569762f4807SAndreas Gohr        $pop = plugin_load('helper', 'popularity');
570b188870fSAndreas Gohr        $list = $this->initEmptySearchList();
571762f4807SAndreas Gohr        search($list, $conf['mediadir'], [$pop, 'searchCountCallback'], ['all' => true], '');
572762f4807SAndreas Gohr        $media_count = $list['file_count'];
573762f4807SAndreas Gohr        $media_size = $list['file_size'];
574762f4807SAndreas Gohr
575762f4807SAndreas Gohr        $this->db->exec(
576762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
577762f4807SAndreas Gohr                info, value, dt
578762f4807SAndreas Gohr             ) VALUES (
579483101d3SAndreas Gohr                ?, ?, CURRENT_TIMESTAMP
580762f4807SAndreas Gohr             )',
5812adee4c6SAndreas Gohr            'media_count',
5822adee4c6SAndreas Gohr            $media_count
583762f4807SAndreas Gohr        );
584762f4807SAndreas Gohr        $this->db->exec(
585762f4807SAndreas Gohr            'INSERT OR REPLACE INTO history (
586762f4807SAndreas Gohr                info, value, dt
587762f4807SAndreas Gohr             ) VALUES (
588483101d3SAndreas Gohr                ?, ?, CURRENT_TIMESTAMP
589762f4807SAndreas Gohr             )',
5902adee4c6SAndreas Gohr            'media_size',
5912adee4c6SAndreas Gohr            $media_size
592762f4807SAndreas Gohr        );
593762f4807SAndreas Gohr    }
594b188870fSAndreas Gohr
5954a163f50SAndreas Gohr    // endregion
5964a163f50SAndreas Gohr
597b188870fSAndreas Gohr    /**
598b188870fSAndreas Gohr     * @todo can be dropped in favor of helper_plugin_popularity::initEmptySearchList() once it's public
599b188870fSAndreas Gohr     * @return array
600b188870fSAndreas Gohr     */
601b188870fSAndreas Gohr    protected function initEmptySearchList()
602b188870fSAndreas Gohr    {
603b188870fSAndreas Gohr        return array_fill_keys([
604b188870fSAndreas Gohr            'file_count',
605b188870fSAndreas Gohr            'file_size',
606b188870fSAndreas Gohr            'file_max',
607b188870fSAndreas Gohr            'file_min',
608b188870fSAndreas Gohr            'dir_count',
609b188870fSAndreas Gohr            'dir_nest',
610b188870fSAndreas Gohr            'file_oldest'
611b188870fSAndreas Gohr        ], 0);
612b188870fSAndreas Gohr    }
613762f4807SAndreas Gohr}
614