1762f4807SAndreas Gohr<?php 2762f4807SAndreas Gohr 3762f4807SAndreas Gohrnamespace dokuwiki\plugin\statistics; 4762f4807SAndreas Gohr 541d1fffcSAndreas Gohr/** 641d1fffcSAndreas Gohr * Exception thrown when logging should be ignored 741d1fffcSAndreas Gohr */ 841d1fffcSAndreas Gohrclass IgnoreException extends \RuntimeException 941d1fffcSAndreas Gohr{ 1041d1fffcSAndreas Gohr} 1141d1fffcSAndreas Gohr 12762f4807SAndreas Gohruse DeviceDetector\DeviceDetector; 13762f4807SAndreas Gohruse DeviceDetector\Parser\Client\Browser; 14762f4807SAndreas Gohruse DeviceDetector\Parser\Device\AbstractDeviceParser; 15762f4807SAndreas Gohruse DeviceDetector\Parser\OperatingSystem; 1641d1fffcSAndreas Gohruse dokuwiki\Input\Input; 17762f4807SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB; 18762f4807SAndreas Gohruse helper_plugin_popularity; 19762f4807SAndreas Gohruse helper_plugin_statistics; 20762f4807SAndreas Gohr 21762f4807SAndreas Gohrclass Logger 22762f4807SAndreas Gohr{ 23762f4807SAndreas Gohr /** @var helper_plugin_statistics The statistics helper plugin instance */ 24762f4807SAndreas Gohr protected helper_plugin_statistics $hlp; 25762f4807SAndreas Gohr 26762f4807SAndreas Gohr /** @var SQLiteDB The SQLite database instance */ 27762f4807SAndreas Gohr protected SQLiteDB $db; 28762f4807SAndreas Gohr 29762f4807SAndreas Gohr /** @var string The full user agent string */ 30762f4807SAndreas Gohr protected string $uaAgent; 31762f4807SAndreas Gohr 32762f4807SAndreas Gohr /** @var string The type of user agent (browser, robot, feedreader) */ 33762f4807SAndreas Gohr protected string $uaType = 'browser'; 34762f4807SAndreas Gohr 35762f4807SAndreas Gohr /** @var string The browser/client name */ 36762f4807SAndreas Gohr protected string $uaName; 37762f4807SAndreas Gohr 38762f4807SAndreas Gohr /** @var string The browser/client version */ 39762f4807SAndreas Gohr protected string $uaVersion; 40762f4807SAndreas Gohr 41762f4807SAndreas Gohr /** @var string The operating system/platform */ 42762f4807SAndreas Gohr protected string $uaPlatform; 43762f4807SAndreas Gohr 444a163f50SAndreas Gohr /** @var string|null The user name, if available */ 454a163f50SAndreas Gohr protected ?string $user = null; 464a163f50SAndreas Gohr 47762f4807SAndreas Gohr /** @var string The unique user identifier */ 48762f4807SAndreas Gohr protected string $uid; 49762f4807SAndreas Gohr 504a163f50SAndreas Gohr /** @var string The session identifier */ 514a163f50SAndreas Gohr protected string $session; 524a163f50SAndreas Gohr 534a163f50SAndreas Gohr /** @var int|null The ID of the main access log entry if any */ 544a163f50SAndreas Gohr protected ?int $hit = null; 554a163f50SAndreas Gohr 564a163f50SAndreas Gohr // region lifecycle 57762f4807SAndreas Gohr 58762f4807SAndreas Gohr /** 59762f4807SAndreas Gohr * Constructor 60762f4807SAndreas Gohr * 61762f4807SAndreas Gohr * Parses browser info and set internal vars 62762f4807SAndreas Gohr */ 63ba6b3b10SAndreas Gohr public function __construct(helper_plugin_statistics $hlp) 64762f4807SAndreas Gohr { 6541d1fffcSAndreas Gohr /** @var Input $INPUT */ 66762f4807SAndreas Gohr global $INPUT; 67762f4807SAndreas Gohr 68762f4807SAndreas Gohr $this->hlp = $hlp; 69762f4807SAndreas Gohr $this->db = $this->hlp->getDB(); 70762f4807SAndreas Gohr 714a163f50SAndreas Gohr // FIXME if we already have a session, we should not re-parse the user agent 72762f4807SAndreas Gohr 734a163f50SAndreas Gohr $ua = trim($INPUT->server->str('HTTP_USER_AGENT')); 74762f4807SAndreas Gohr AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR); 75762f4807SAndreas Gohr $dd = new DeviceDetector($ua); // FIXME we could use client hints, but need to add headers 76762f4807SAndreas Gohr $dd->discardBotInformation(); 77762f4807SAndreas Gohr $dd->parse(); 78762f4807SAndreas Gohr 7900f786d8SAndreas Gohr if ($dd->isFeedReader()) { 8000f786d8SAndreas Gohr $this->uaType = 'feedreader'; 8100f786d8SAndreas Gohr } elseif ($dd->isBot()) { 82762f4807SAndreas Gohr $this->uaType = 'robot'; 83762f4807SAndreas Gohr // for now ignore bots 84c5d2f052SAndreas Gohr throw new IgnoreException('Bot detected, not logging'); 85762f4807SAndreas Gohr } 86762f4807SAndreas Gohr 87762f4807SAndreas Gohr $this->uaAgent = $ua; 8805786d83SAndreas Gohr $this->uaName = Browser::getBrowserFamily($dd->getClient('name')) ?: 'Unknown'; 8900f786d8SAndreas Gohr $this->uaVersion = $dd->getClient('version') ?: '0'; 9005786d83SAndreas Gohr $this->uaPlatform = OperatingSystem::getOsFamily($dd->getOs('name')) ?: 'Unknown'; 91762f4807SAndreas Gohr $this->uid = $this->getUID(); 924a163f50SAndreas Gohr $this->session = $this->getSession(); 93*d550a4adSAndreas Gohr 94*d550a4adSAndreas Gohr if (!$this->hlp->getConf('nousers')) { 9541d1fffcSAndreas Gohr $this->user = $INPUT->server->str('REMOTE_USER', null, true); 96762f4807SAndreas Gohr } 97*d550a4adSAndreas Gohr } 98762f4807SAndreas Gohr 99762f4807SAndreas Gohr /** 100762f4807SAndreas Gohr * Should be called before logging 101762f4807SAndreas Gohr * 1024a163f50SAndreas Gohr * This starts a transaction, so all logging is done in one go. It also logs the user and session data. 103762f4807SAndreas Gohr */ 104762f4807SAndreas Gohr public function begin(): void 105762f4807SAndreas Gohr { 106762f4807SAndreas Gohr $this->hlp->getDB()->getPdo()->beginTransaction(); 1074a163f50SAndreas Gohr 1084a163f50SAndreas Gohr $this->logUser(); 1094a163f50SAndreas Gohr $this->logGroups(); 1104a163f50SAndreas Gohr $this->logDomain(); 1114a163f50SAndreas Gohr $this->logSession(); 112762f4807SAndreas Gohr } 113762f4807SAndreas Gohr 114762f4807SAndreas Gohr /** 115762f4807SAndreas Gohr * Should be called after logging 116762f4807SAndreas Gohr * 117762f4807SAndreas Gohr * This commits the transaction started in begin() 118762f4807SAndreas Gohr */ 119762f4807SAndreas Gohr public function end(): void 120762f4807SAndreas Gohr { 121762f4807SAndreas Gohr $this->hlp->getDB()->getPdo()->commit(); 122762f4807SAndreas Gohr } 123762f4807SAndreas Gohr 1244a163f50SAndreas Gohr // endregion 1254a163f50SAndreas Gohr // region data gathering 1264a163f50SAndreas Gohr 127762f4807SAndreas Gohr /** 128762f4807SAndreas Gohr * Get the unique user ID 129762f4807SAndreas Gohr * 13004928db4SAndreas Gohr * The user ID is stored in the user preferences and should stay there forever. 131762f4807SAndreas Gohr * @return string The unique user identifier 132762f4807SAndreas Gohr */ 133762f4807SAndreas Gohr protected function getUID(): string 134762f4807SAndreas Gohr { 13504928db4SAndreas Gohr if (!isset($_SESSION[DOKU_COOKIE]['statistics']['uid'])) { 13604928db4SAndreas Gohr // when there is no session UID set, we assume this was deliberate and we simply abort all logging 13704928db4SAndreas Gohr // @todo we may later make UID generation optional 13804928db4SAndreas Gohr throw new IgnoreException('No user ID found'); 13904928db4SAndreas Gohr } 140762f4807SAndreas Gohr 14104928db4SAndreas Gohr return $_SESSION[DOKU_COOKIE]['statistics']['uid']; 142762f4807SAndreas Gohr } 143762f4807SAndreas Gohr 144762f4807SAndreas Gohr /** 145762f4807SAndreas Gohr * Return the user's session ID 146762f4807SAndreas Gohr * 147762f4807SAndreas Gohr * @return string The session identifier 148762f4807SAndreas Gohr */ 149762f4807SAndreas Gohr protected function getSession(): string 150762f4807SAndreas Gohr { 15104928db4SAndreas Gohr if (!isset($_SESSION[DOKU_COOKIE]['statistics']['id'])) { 15204928db4SAndreas Gohr // when there is no session ID set, we assume this was deliberate and we simply abort all logging 15304928db4SAndreas Gohr throw new IgnoreException('No session ID found'); 15404928db4SAndreas Gohr } 155762f4807SAndreas Gohr 15604928db4SAndreas Gohr return $_SESSION[DOKU_COOKIE]['statistics']['id']; 157762f4807SAndreas Gohr } 158762f4807SAndreas Gohr 1594a163f50SAndreas Gohr // endregion 1604a163f50SAndreas Gohr // region automatic logging 161762f4807SAndreas Gohr 1624a163f50SAndreas Gohr /** 1634a163f50SAndreas Gohr * Log the user was seen 1644a163f50SAndreas Gohr */ 1654a163f50SAndreas Gohr protected function logUser(): void 1664a163f50SAndreas Gohr { 1674a163f50SAndreas Gohr if (!$this->user) return; 168762f4807SAndreas Gohr 169762f4807SAndreas Gohr $this->db->exec( 1704a163f50SAndreas Gohr 'INSERT INTO users (user, dt) 1714a163f50SAndreas Gohr VALUES (?, CURRENT_TIMESTAMP) 1724a163f50SAndreas Gohr ON CONFLICT (user) DO UPDATE SET 1734a163f50SAndreas Gohr dt = CURRENT_TIMESTAMP 1744a163f50SAndreas Gohr WHERE excluded.user = users.user 1754a163f50SAndreas Gohr ', 1764a163f50SAndreas Gohr $this->user 1774a163f50SAndreas Gohr ); 1784a163f50SAndreas Gohr 1794a163f50SAndreas Gohr } 1804a163f50SAndreas Gohr 1814a163f50SAndreas Gohr /** 1824a163f50SAndreas Gohr * Log the session and user agent information 1834a163f50SAndreas Gohr */ 1844a163f50SAndreas Gohr protected function logSession(): void 1854a163f50SAndreas Gohr { 1864a163f50SAndreas Gohr $this->db->exec( 1874a163f50SAndreas Gohr 'INSERT INTO sessions (session, dt, end, uid, user, ua, ua_info, ua_type, ua_ver, os) 1884a163f50SAndreas Gohr VALUES (?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?) 1894a163f50SAndreas Gohr ON CONFLICT (session) DO UPDATE SET 19041d1fffcSAndreas Gohr end = CURRENT_TIMESTAMP, 19141d1fffcSAndreas Gohr user = excluded.user, 19241d1fffcSAndreas Gohr uid = excluded.uid 1934a163f50SAndreas Gohr WHERE excluded.session = sessions.session 1944a163f50SAndreas Gohr ', 1954a163f50SAndreas Gohr $this->session, 1964a163f50SAndreas Gohr $this->uid, 1974a163f50SAndreas Gohr $this->user, 1984a163f50SAndreas Gohr $this->uaAgent, 1994a163f50SAndreas Gohr $this->uaName, 2004a163f50SAndreas Gohr $this->uaType, 2014a163f50SAndreas Gohr $this->uaVersion, 2024a163f50SAndreas Gohr $this->uaPlatform 203762f4807SAndreas Gohr ); 204762f4807SAndreas Gohr } 205762f4807SAndreas Gohr 206762f4807SAndreas Gohr /** 2074a163f50SAndreas Gohr * Log all groups for the user 208762f4807SAndreas Gohr * 2094a163f50SAndreas Gohr * @todo maybe this should be done only once per session? 210762f4807SAndreas Gohr */ 2114a163f50SAndreas Gohr protected function logGroups(): void 212762f4807SAndreas Gohr { 2134a163f50SAndreas Gohr global $USERINFO; 214762f4807SAndreas Gohr 2154a163f50SAndreas Gohr if (!$this->user) return; 2164a163f50SAndreas Gohr if (!isset($USERINFO['grps'])) return; 2174a163f50SAndreas Gohr if (!is_array($USERINFO['grps'])) return; 2184a163f50SAndreas Gohr $groups = $USERINFO['grps']; 219fced2f86SAnna Dabrowska 2204a163f50SAndreas Gohr $this->db->exec('DELETE FROM groups WHERE user = ?', $this->user); 221762f4807SAndreas Gohr 22241d1fffcSAndreas Gohr if (empty($groups)) { 22341d1fffcSAndreas Gohr return; 22441d1fffcSAndreas Gohr } 22541d1fffcSAndreas Gohr 22602aa9b73SAndreas Gohr $placeholders = implode(',', array_fill(0, count($groups), '(?, ?)')); 227762f4807SAndreas Gohr $params = []; 2284a163f50SAndreas Gohr $sql = "INSERT INTO groups (`user`, `group`) VALUES $placeholders"; 229762f4807SAndreas Gohr foreach ($groups as $group) { 2304a163f50SAndreas Gohr $params[] = $this->user; 231762f4807SAndreas Gohr $params[] = $group; 232762f4807SAndreas Gohr } 233762f4807SAndreas Gohr $this->db->exec($sql, $params); 234762f4807SAndreas Gohr } 235762f4807SAndreas Gohr 236762f4807SAndreas Gohr /** 2374a163f50SAndreas Gohr * Log email domain 2380b8b7abaSAnna Dabrowska * 2394a163f50SAndreas Gohr * @todo maybe this should be done only once per session? 2400b8b7abaSAnna Dabrowska */ 2414a163f50SAndreas Gohr protected function logDomain(): void 2420b8b7abaSAnna Dabrowska { 2434a163f50SAndreas Gohr global $USERINFO; 2444a163f50SAndreas Gohr if (!$this->user) return; 2454a163f50SAndreas Gohr if (!isset($USERINFO['mail'])) return; 2464a163f50SAndreas Gohr $mail = $USERINFO['mail']; 2470b8b7abaSAnna Dabrowska 2480b8b7abaSAnna Dabrowska $pos = strrpos($mail, '@'); 2490b8b7abaSAnna Dabrowska if (!$pos) return; 2500b8b7abaSAnna Dabrowska $domain = substr($mail, $pos + 1); 2510b8b7abaSAnna Dabrowska if (empty($domain)) return; 2520b8b7abaSAnna Dabrowska 2534a163f50SAndreas Gohr $sql = 'UPDATE users SET domain = ? WHERE user = ?'; 2544a163f50SAndreas Gohr $this->db->exec($sql, [$domain, $this->user]); 2550b8b7abaSAnna Dabrowska } 2560b8b7abaSAnna Dabrowska 2574a163f50SAndreas Gohr // endregion 2584a163f50SAndreas Gohr // region internal loggers called by the dispatchers 2594a163f50SAndreas Gohr 2600b8b7abaSAnna Dabrowska /** 2614a163f50SAndreas Gohr * Log the given referer URL 262762f4807SAndreas Gohr * 2632a30f557SAndreas Gohr * Note: we DO log empty referers. These are external accesses that did not provide a referer URL. 2642a30f557SAndreas Gohr * We do not log referers that are our own pages though. 2652a30f557SAndreas Gohr * 2662a30f557SAndreas Gohr * engine set -> a search engine referer 2672a30f557SAndreas Gohr * no engine set, url empty -> a direct access (bookmark, direct link, etc.) 2682a30f557SAndreas Gohr * no engine set, url not empty -> a referer from another page (not a wiki page) 2692a30f557SAndreas Gohr * null returned -> referer was a wiki page 2702a30f557SAndreas Gohr * 2714a163f50SAndreas Gohr * @param $referer 2722a30f557SAndreas Gohr * @return int|null The referer ID or null if no referer was logged 2732a30f557SAndreas Gohr * @todo we could check against a blacklist here 274762f4807SAndreas Gohr */ 2754a163f50SAndreas Gohr public function logReferer($referer): ?int 276762f4807SAndreas Gohr { 2772a30f557SAndreas Gohr $referer = trim($referer); 278762f4807SAndreas Gohr 279569a5066SAndreas Gohr // do not log our own pages as referers (empty referer is OK though) 280569a5066SAndreas Gohr if (!empty($referer)) { 281569a5066SAndreas Gohr $selfre = '^' . preg_quote(DOKU_URL, '/'); 2822a30f557SAndreas Gohr if (preg_match("/$selfre/", $referer)) { 2832a30f557SAndreas Gohr return null; 2842a30f557SAndreas Gohr } 285569a5066SAndreas Gohr } 286762f4807SAndreas Gohr 2872a30f557SAndreas Gohr // is it a search engine? 2884a163f50SAndreas Gohr $se = new SearchEngines($referer); 28941d1fffcSAndreas Gohr $engine = $se->getEngine(); 290762f4807SAndreas Gohr 29141d1fffcSAndreas Gohr $sql = 'INSERT OR IGNORE INTO referers (url, engine, dt) VALUES (?, ?, CURRENT_TIMESTAMP)'; 292569a5066SAndreas Gohr $this->db->exec($sql, [$referer, $engine]); 293569a5066SAndreas Gohr return (int)$this->db->queryValue('SELECT id FROM referers WHERE url = ?', $referer); 294762f4807SAndreas Gohr } 295762f4807SAndreas Gohr 296762f4807SAndreas Gohr /** 297762f4807SAndreas Gohr * Resolve IP to country/city and store in database 298762f4807SAndreas Gohr * 2994a163f50SAndreas Gohr * @return string The IP address as stored 300762f4807SAndreas Gohr */ 3014a163f50SAndreas Gohr public function logIp(): string 302762f4807SAndreas Gohr { 3034a163f50SAndreas Gohr $ip = clientIP(true); 30469fb56a2SAndreas Gohr 30569fb56a2SAndreas Gohr // anonymize the IP address for storage? 30669fb56a2SAndreas Gohr if ($this->hlp->getConf('anonips')) { 30769fb56a2SAndreas Gohr $hash = md5($ip . strrev($ip)); // we use the reversed IP as salt to avoid common rainbow tables 30869fb56a2SAndreas Gohr $host = ''; 30969fb56a2SAndreas Gohr } else { 31069fb56a2SAndreas Gohr $hash = $ip; 31169fb56a2SAndreas Gohr $host = gethostbyaddr($ip); 31269fb56a2SAndreas Gohr } 3134a163f50SAndreas Gohr 314ba6b3b10SAndreas Gohr if ($this->hlp->getConf('nolocation')) { 315ba6b3b10SAndreas Gohr // if we don't resolve location data, we just return the IP address 316ba6b3b10SAndreas Gohr return $hash; 317ba6b3b10SAndreas Gohr } 318ba6b3b10SAndreas Gohr 319762f4807SAndreas Gohr // check if IP already known and up-to-date 320762f4807SAndreas Gohr $result = $this->db->queryValue( 321762f4807SAndreas Gohr "SELECT ip 322762f4807SAndreas Gohr FROM iplocation 323762f4807SAndreas Gohr WHERE ip = ? 324762f4807SAndreas Gohr AND lastupd > date('now', '-30 days')", 3254a163f50SAndreas Gohr $hash 326762f4807SAndreas Gohr ); 3274a163f50SAndreas Gohr if ($result) return $hash; // already known and up-to-date 328762f4807SAndreas Gohr 329762f4807SAndreas Gohr 330ba6b3b10SAndreas Gohr // resolve the IP address to location data 331762f4807SAndreas Gohr try { 332ba6b3b10SAndreas Gohr $data = $this->hlp->resolveIP($ip); 333ba6b3b10SAndreas Gohr } catch (IpResolverException $e) { 334ba6b3b10SAndreas Gohr \dokuwiki\Logger::error('Statistics Plugin: ' . $e->getMessage(), $e->details); 335ba6b3b10SAndreas Gohr $data = []; 336762f4807SAndreas Gohr } 337762f4807SAndreas Gohr 338762f4807SAndreas Gohr $this->db->exec( 339762f4807SAndreas Gohr 'INSERT OR REPLACE INTO iplocation ( 340762f4807SAndreas Gohr ip, country, code, city, host, lastupd 341762f4807SAndreas Gohr ) VALUES ( 342762f4807SAndreas Gohr ?, ?, ?, ?, ?, CURRENT_TIMESTAMP 343762f4807SAndreas Gohr )', 3444a163f50SAndreas Gohr $hash, 34502aa9b73SAndreas Gohr $data['country'] ?? '', 34602aa9b73SAndreas Gohr $data['countryCode'] ?? '', 34702aa9b73SAndreas Gohr $data['city'] ?? '', 3482adee4c6SAndreas Gohr $host 349762f4807SAndreas Gohr ); 3504a163f50SAndreas Gohr 3514a163f50SAndreas Gohr return $hash; 3524a163f50SAndreas Gohr } 3534a163f50SAndreas Gohr 3544a163f50SAndreas Gohr // endregion 3554a163f50SAndreas Gohr // region log dispatchers 3564a163f50SAndreas Gohr 3574a163f50SAndreas Gohr public function logPageView(): void 3584a163f50SAndreas Gohr { 3594a163f50SAndreas Gohr global $INPUT; 3604a163f50SAndreas Gohr 3614a163f50SAndreas Gohr if (!$INPUT->str('p')) return; 3624a163f50SAndreas Gohr 3634a163f50SAndreas Gohr 3644a163f50SAndreas Gohr $referer = $INPUT->filter('trim')->str('r'); 3654a163f50SAndreas Gohr $ip = $this->logIp(); // resolve the IP address 3664a163f50SAndreas Gohr 3674a163f50SAndreas Gohr $data = [ 3684a163f50SAndreas Gohr 'page' => $INPUT->filter('cleanID')->str('p'), 3694a163f50SAndreas Gohr 'ip' => $ip, 3704a163f50SAndreas Gohr 'ref_id' => $this->logReferer($referer), 3714a163f50SAndreas Gohr 'sx' => $INPUT->int('sx'), 3724a163f50SAndreas Gohr 'sy' => $INPUT->int('sy'), 3734a163f50SAndreas Gohr 'vx' => $INPUT->int('vx'), 3744a163f50SAndreas Gohr 'vy' => $INPUT->int('vy'), 3754a163f50SAndreas Gohr 'session' => $this->session, 3764a163f50SAndreas Gohr ]; 3774a163f50SAndreas Gohr 3784a163f50SAndreas Gohr $this->db->exec(' 3794a163f50SAndreas Gohr INSERT INTO pageviews ( 3804a163f50SAndreas Gohr dt, page, ip, ref_id, screen_x, screen_y, view_x, view_y, session 3814a163f50SAndreas Gohr ) VALUES ( 3824a163f50SAndreas Gohr CURRENT_TIMESTAMP, :page, :ip, :ref_id, :sx, :sy, :vx, :vy, :session 3834a163f50SAndreas Gohr ) 3844a163f50SAndreas Gohr ', 3854a163f50SAndreas Gohr $data 3864a163f50SAndreas Gohr ); 387762f4807SAndreas Gohr } 388762f4807SAndreas Gohr 389762f4807SAndreas Gohr /** 390762f4807SAndreas Gohr * Log a click on an external link 391762f4807SAndreas Gohr * 392762f4807SAndreas Gohr * Called from log.php 393762f4807SAndreas Gohr */ 394762f4807SAndreas Gohr public function logOutgoing(): void 395762f4807SAndreas Gohr { 396762f4807SAndreas Gohr global $INPUT; 397762f4807SAndreas Gohr 398762f4807SAndreas Gohr if (!$INPUT->str('ol')) return; 399762f4807SAndreas Gohr 4004a163f50SAndreas Gohr $link = $INPUT->filter('trim')->str('ol'); 4014a163f50SAndreas Gohr $session = $this->session; 4024a163f50SAndreas Gohr $page = $INPUT->filter('cleanID')->str('p'); 403762f4807SAndreas Gohr 404762f4807SAndreas Gohr $this->db->exec( 405762f4807SAndreas Gohr 'INSERT INTO outlinks ( 4064a163f50SAndreas Gohr dt, session, page, link 407762f4807SAndreas Gohr ) VALUES ( 40841d1fffcSAndreas Gohr CURRENT_TIMESTAMP, ?, ?, ? 409762f4807SAndreas Gohr )', 4102adee4c6SAndreas Gohr $session, 4112adee4c6SAndreas Gohr $page, 4122adee4c6SAndreas Gohr $link 413762f4807SAndreas Gohr ); 414762f4807SAndreas Gohr } 415762f4807SAndreas Gohr 416762f4807SAndreas Gohr /** 417762f4807SAndreas Gohr * Log access to a media file 418762f4807SAndreas Gohr * 419762f4807SAndreas Gohr * Called from action.php 420762f4807SAndreas Gohr * 421762f4807SAndreas Gohr * @param string $media The media ID 422762f4807SAndreas Gohr * @param string $mime The media's mime type 423762f4807SAndreas Gohr * @param bool $inline Is this displayed inline? 424762f4807SAndreas Gohr * @param int $size Size of the media file 425762f4807SAndreas Gohr */ 426762f4807SAndreas Gohr public function logMedia(string $media, string $mime, bool $inline, int $size): void 427762f4807SAndreas Gohr { 428762f4807SAndreas Gohr [$mime1, $mime2] = explode('/', strtolower($mime)); 429762f4807SAndreas Gohr $inline = $inline ? 1 : 0; 430762f4807SAndreas Gohr 431762f4807SAndreas Gohr 4324a163f50SAndreas Gohr $data = [ 4334a163f50SAndreas Gohr 'media' => cleanID($media), 4344a163f50SAndreas Gohr 'ip' => $this->logIp(), // resolve the IP address 4354a163f50SAndreas Gohr 'session' => $this->session, 4364a163f50SAndreas Gohr 'size' => $size, 4374a163f50SAndreas Gohr 'mime1' => $mime1, 4384a163f50SAndreas Gohr 'mime2' => $mime2, 4394a163f50SAndreas Gohr 'inline' => $inline, 4404a163f50SAndreas Gohr ]; 4414a163f50SAndreas Gohr 4424a163f50SAndreas Gohr $this->db->exec(' 4434a163f50SAndreas Gohr INSERT INTO media ( dt, media, ip, session, size, mime1, mime2, inline ) 4444a163f50SAndreas Gohr VALUES (CURRENT_TIMESTAMP, :media, :ip, :session, :size, :mime1, :mime2, :inline) 4454a163f50SAndreas Gohr ', 4464a163f50SAndreas Gohr $data 447762f4807SAndreas Gohr ); 448762f4807SAndreas Gohr } 449762f4807SAndreas Gohr 450762f4807SAndreas Gohr /** 451762f4807SAndreas Gohr * Log page edits 452762f4807SAndreas Gohr * 4534a163f50SAndreas Gohr * called from action.php 4544a163f50SAndreas Gohr * 455762f4807SAndreas Gohr * @param string $page The page that was edited 456762f4807SAndreas Gohr * @param string $type The type of edit (create, edit, etc.) 457762f4807SAndreas Gohr */ 458762f4807SAndreas Gohr public function logEdit(string $page, string $type): void 459762f4807SAndreas Gohr { 4604a163f50SAndreas Gohr $data = [ 4614a163f50SAndreas Gohr 'page' => cleanID($page), 4624a163f50SAndreas Gohr 'type' => $type, 4634a163f50SAndreas Gohr 'ip' => $this->logIp(), // resolve the IP address 4644a163f50SAndreas Gohr 'session' => $this->session 4654a163f50SAndreas Gohr ]; 466762f4807SAndreas Gohr 46741d1fffcSAndreas Gohr $this->db->exec( 468762f4807SAndreas Gohr 'INSERT INTO edits ( 4694a163f50SAndreas Gohr dt, page, type, ip, session 470762f4807SAndreas Gohr ) VALUES ( 4714a163f50SAndreas Gohr CURRENT_TIMESTAMP, :page, :type, :ip, :session 472762f4807SAndreas Gohr )', 4734a163f50SAndreas Gohr $data 474762f4807SAndreas Gohr ); 475762f4807SAndreas Gohr } 476762f4807SAndreas Gohr 477762f4807SAndreas Gohr /** 478762f4807SAndreas Gohr * Log login/logoffs and user creations 479762f4807SAndreas Gohr * 480af93d154SAndreas Gohr * @param string $type The type of login event (login, logout, create, failed) 481af93d154SAndreas Gohr * @param string $user The username 482762f4807SAndreas Gohr */ 483762f4807SAndreas Gohr public function logLogin(string $type, string $user = ''): void 484762f4807SAndreas Gohr { 485762f4807SAndreas Gohr global $INPUT; 486762f4807SAndreas Gohr 487762f4807SAndreas Gohr if (!$user) $user = $INPUT->server->str('REMOTE_USER'); 488762f4807SAndreas Gohr 489762f4807SAndreas Gohr $ip = clientIP(true); 490762f4807SAndreas Gohr 491762f4807SAndreas Gohr $this->db->exec( 492762f4807SAndreas Gohr 'INSERT INTO logins ( 493af93d154SAndreas Gohr dt, ip, user, type 494762f4807SAndreas Gohr ) VALUES ( 495af93d154SAndreas Gohr CURRENT_TIMESTAMP, ?, ?, ? 496762f4807SAndreas Gohr )', 4972adee4c6SAndreas Gohr $ip, 4982adee4c6SAndreas Gohr $user, 499af93d154SAndreas Gohr $type 500762f4807SAndreas Gohr ); 501762f4807SAndreas Gohr } 502762f4807SAndreas Gohr 503762f4807SAndreas Gohr /** 50402aa9b73SAndreas Gohr * Log search data to the search related tables 50502aa9b73SAndreas Gohr * 50602aa9b73SAndreas Gohr * @param string $query The search query 50702aa9b73SAndreas Gohr * @param string[] $words The query split into words 50802aa9b73SAndreas Gohr */ 50902aa9b73SAndreas Gohr public function logSearch(string $query, array $words): void 51002aa9b73SAndreas Gohr { 51102aa9b73SAndreas Gohr if (!$query) return; 51202aa9b73SAndreas Gohr 51302aa9b73SAndreas Gohr $sid = $this->db->exec( 51402aa9b73SAndreas Gohr 'INSERT INTO search (dt, ip, session, query) VALUES (CURRENT_TIMESTAMP, ?, ? , ?)', 51502aa9b73SAndreas Gohr $this->logIp(), // resolve the IP address 51602aa9b73SAndreas Gohr $this->session, 51702aa9b73SAndreas Gohr $query, 51802aa9b73SAndreas Gohr ); 51902aa9b73SAndreas Gohr 52002aa9b73SAndreas Gohr foreach ($words as $word) { 52102aa9b73SAndreas Gohr if (!$word) continue; 52202aa9b73SAndreas Gohr $this->db->exec( 52302aa9b73SAndreas Gohr 'INSERT INTO searchwords (sid, word) VALUES (?, ?)', 52402aa9b73SAndreas Gohr $sid, 52502aa9b73SAndreas Gohr $word 52602aa9b73SAndreas Gohr ); 52702aa9b73SAndreas Gohr } 52802aa9b73SAndreas Gohr } 52902aa9b73SAndreas Gohr 53002aa9b73SAndreas Gohr /** 531762f4807SAndreas Gohr * Log the current page count and size as today's history entry 532762f4807SAndreas Gohr */ 533762f4807SAndreas Gohr public function logHistoryPages(): void 534762f4807SAndreas Gohr { 535762f4807SAndreas Gohr global $conf; 536762f4807SAndreas Gohr 537762f4807SAndreas Gohr // use the popularity plugin's search method to find the wanted data 538762f4807SAndreas Gohr /** @var helper_plugin_popularity $pop */ 539762f4807SAndreas Gohr $pop = plugin_load('helper', 'popularity'); 540b188870fSAndreas Gohr $list = $this->initEmptySearchList(); 541762f4807SAndreas Gohr search($list, $conf['datadir'], [$pop, 'searchCountCallback'], ['all' => false], ''); 542762f4807SAndreas Gohr $page_count = $list['file_count']; 543762f4807SAndreas Gohr $page_size = $list['file_size']; 544762f4807SAndreas Gohr 545762f4807SAndreas Gohr $this->db->exec( 546762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 547762f4807SAndreas Gohr info, value, dt 548762f4807SAndreas Gohr ) VALUES ( 549483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 550762f4807SAndreas Gohr )', 5512adee4c6SAndreas Gohr 'page_count', 5522adee4c6SAndreas Gohr $page_count 553762f4807SAndreas Gohr ); 554762f4807SAndreas Gohr $this->db->exec( 555762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 556762f4807SAndreas Gohr info, value, dt 557762f4807SAndreas Gohr ) VALUES ( 558483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 559762f4807SAndreas Gohr )', 5602adee4c6SAndreas Gohr 'page_size', 5612adee4c6SAndreas Gohr $page_size 562762f4807SAndreas Gohr ); 563762f4807SAndreas Gohr } 564762f4807SAndreas Gohr 565762f4807SAndreas Gohr /** 566762f4807SAndreas Gohr * Log the current media count and size as today's history entry 567762f4807SAndreas Gohr */ 568762f4807SAndreas Gohr public function logHistoryMedia(): void 569762f4807SAndreas Gohr { 570762f4807SAndreas Gohr global $conf; 571762f4807SAndreas Gohr 572762f4807SAndreas Gohr // use the popularity plugin's search method to find the wanted data 573762f4807SAndreas Gohr /** @var helper_plugin_popularity $pop */ 574762f4807SAndreas Gohr $pop = plugin_load('helper', 'popularity'); 575b188870fSAndreas Gohr $list = $this->initEmptySearchList(); 576762f4807SAndreas Gohr search($list, $conf['mediadir'], [$pop, 'searchCountCallback'], ['all' => true], ''); 577762f4807SAndreas Gohr $media_count = $list['file_count']; 578762f4807SAndreas Gohr $media_size = $list['file_size']; 579762f4807SAndreas Gohr 580762f4807SAndreas Gohr $this->db->exec( 581762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 582762f4807SAndreas Gohr info, value, dt 583762f4807SAndreas Gohr ) VALUES ( 584483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 585762f4807SAndreas Gohr )', 5862adee4c6SAndreas Gohr 'media_count', 5872adee4c6SAndreas Gohr $media_count 588762f4807SAndreas Gohr ); 589762f4807SAndreas Gohr $this->db->exec( 590762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 591762f4807SAndreas Gohr info, value, dt 592762f4807SAndreas Gohr ) VALUES ( 593483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 594762f4807SAndreas Gohr )', 5952adee4c6SAndreas Gohr 'media_size', 5962adee4c6SAndreas Gohr $media_size 597762f4807SAndreas Gohr ); 598762f4807SAndreas Gohr } 599b188870fSAndreas Gohr 6004a163f50SAndreas Gohr // endregion 6014a163f50SAndreas Gohr 602b188870fSAndreas Gohr /** 603b188870fSAndreas Gohr * @todo can be dropped in favor of helper_plugin_popularity::initEmptySearchList() once it's public 604b188870fSAndreas Gohr * @return array 605b188870fSAndreas Gohr */ 606b188870fSAndreas Gohr protected function initEmptySearchList() 607b188870fSAndreas Gohr { 608b188870fSAndreas Gohr return array_fill_keys([ 609b188870fSAndreas Gohr 'file_count', 610b188870fSAndreas Gohr 'file_size', 611b188870fSAndreas Gohr 'file_max', 612b188870fSAndreas Gohr 'file_min', 613b188870fSAndreas Gohr 'dir_count', 614b188870fSAndreas Gohr 'dir_nest', 615b188870fSAndreas Gohr 'file_oldest' 616b188870fSAndreas Gohr ], 0); 617b188870fSAndreas Gohr } 618762f4807SAndreas Gohr} 619