1762f4807SAndreas Gohr<?php 2762f4807SAndreas Gohr 3762f4807SAndreas Gohrnamespace dokuwiki\plugin\statistics; 4762f4807SAndreas Gohr 51c4e3694SAndreas Gohruse DeviceDetector\ClientHints; 6762f4807SAndreas Gohruse DeviceDetector\DeviceDetector; 7762f4807SAndreas Gohruse DeviceDetector\Parser\Device\AbstractDeviceParser; 8762f4807SAndreas Gohruse DeviceDetector\Parser\OperatingSystem; 941d1fffcSAndreas Gohruse dokuwiki\Input\Input; 10762f4807SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB; 11762f4807SAndreas Gohruse helper_plugin_popularity; 12762f4807SAndreas Gohruse helper_plugin_statistics; 13762f4807SAndreas Gohr 14762f4807SAndreas Gohrclass Logger 15762f4807SAndreas Gohr{ 16762f4807SAndreas Gohr /** @var helper_plugin_statistics The statistics helper plugin instance */ 17762f4807SAndreas Gohr protected helper_plugin_statistics $hlp; 18762f4807SAndreas Gohr 19762f4807SAndreas Gohr /** @var SQLiteDB The SQLite database instance */ 20762f4807SAndreas Gohr protected SQLiteDB $db; 21762f4807SAndreas Gohr 22762f4807SAndreas Gohr /** @var string The full user agent string */ 23762f4807SAndreas Gohr protected string $uaAgent; 24762f4807SAndreas Gohr 25762f4807SAndreas Gohr /** @var string The type of user agent (browser, robot, feedreader) */ 26762f4807SAndreas Gohr protected string $uaType = 'browser'; 27762f4807SAndreas Gohr 28762f4807SAndreas Gohr /** @var string The browser/client name */ 29762f4807SAndreas Gohr protected string $uaName; 30762f4807SAndreas Gohr 31762f4807SAndreas Gohr /** @var string The browser/client version */ 32762f4807SAndreas Gohr protected string $uaVersion; 33762f4807SAndreas Gohr 34762f4807SAndreas Gohr /** @var string The operating system/platform */ 35762f4807SAndreas Gohr protected string $uaPlatform; 36762f4807SAndreas Gohr 374a163f50SAndreas Gohr /** @var string|null The user name, if available */ 384a163f50SAndreas Gohr protected ?string $user = null; 394a163f50SAndreas Gohr 40762f4807SAndreas Gohr /** @var string The unique user identifier */ 41762f4807SAndreas Gohr protected string $uid; 42762f4807SAndreas Gohr 434a163f50SAndreas Gohr /** @var string The session identifier */ 444a163f50SAndreas Gohr protected string $session; 454a163f50SAndreas Gohr 464a163f50SAndreas Gohr /** @var int|null The ID of the main access log entry if any */ 474a163f50SAndreas Gohr protected ?int $hit = null; 484a163f50SAndreas Gohr 494a163f50SAndreas Gohr // region lifecycle 50762f4807SAndreas Gohr 51762f4807SAndreas Gohr /** 52762f4807SAndreas Gohr * Constructor 53762f4807SAndreas Gohr * 54762f4807SAndreas Gohr * Parses browser info and set internal vars 55762f4807SAndreas Gohr */ 56ba6b3b10SAndreas Gohr public function __construct(helper_plugin_statistics $hlp) 57762f4807SAndreas Gohr { 5841d1fffcSAndreas Gohr /** @var Input $INPUT */ 59762f4807SAndreas Gohr global $INPUT; 60762f4807SAndreas Gohr 61762f4807SAndreas Gohr $this->hlp = $hlp; 62762f4807SAndreas Gohr $this->db = $this->hlp->getDB(); 63762f4807SAndreas Gohr 644a163f50SAndreas Gohr // FIXME if we already have a session, we should not re-parse the user agent 65762f4807SAndreas Gohr 664a163f50SAndreas Gohr $ua = trim($INPUT->server->str('HTTP_USER_AGENT')); 67762f4807SAndreas Gohr AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR); 681c4e3694SAndreas Gohr $dd = new DeviceDetector($ua, ClientHints::factory($_SERVER)); 69762f4807SAndreas Gohr $dd->discardBotInformation(); 70762f4807SAndreas Gohr $dd->parse(); 71762f4807SAndreas Gohr 7200f786d8SAndreas Gohr if ($dd->isFeedReader()) { 7300f786d8SAndreas Gohr $this->uaType = 'feedreader'; 7400f786d8SAndreas Gohr } elseif ($dd->isBot()) { 75762f4807SAndreas Gohr $this->uaType = 'robot'; 76762f4807SAndreas Gohr // for now ignore bots 77c5d2f052SAndreas Gohr throw new IgnoreException('Bot detected, not logging'); 78762f4807SAndreas Gohr } 79762f4807SAndreas Gohr 80762f4807SAndreas Gohr $this->uaAgent = $ua; 81823a6144SAndreas Gohr $this->uaName = $dd->getClient('name') ?: 'Unknown'; 8200f786d8SAndreas Gohr $this->uaVersion = $dd->getClient('version') ?: '0'; 8305786d83SAndreas Gohr $this->uaPlatform = OperatingSystem::getOsFamily($dd->getOs('name')) ?: 'Unknown'; 84762f4807SAndreas Gohr $this->uid = $this->getUID(); 854a163f50SAndreas Gohr $this->session = $this->getSession(); 86d550a4adSAndreas Gohr 87d550a4adSAndreas Gohr if (!$this->hlp->getConf('nousers')) { 8841d1fffcSAndreas Gohr $this->user = $INPUT->server->str('REMOTE_USER', null, true); 89762f4807SAndreas Gohr } 90d550a4adSAndreas Gohr } 91762f4807SAndreas Gohr 92762f4807SAndreas Gohr /** 93762f4807SAndreas Gohr * Should be called before logging 94762f4807SAndreas Gohr * 954a163f50SAndreas Gohr * This starts a transaction, so all logging is done in one go. It also logs the user and session data. 96762f4807SAndreas Gohr */ 97762f4807SAndreas Gohr public function begin(): void 98762f4807SAndreas Gohr { 99762f4807SAndreas Gohr $this->hlp->getDB()->getPdo()->beginTransaction(); 1004a163f50SAndreas Gohr 1014a163f50SAndreas Gohr $this->logUser(); 1024a163f50SAndreas Gohr $this->logGroups(); 1034a163f50SAndreas Gohr $this->logDomain(); 1044a163f50SAndreas Gohr $this->logSession(); 105*9aec20efSAndreas Gohr $this->logCampaign(); 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 /** 200*9aec20efSAndreas Gohr * Log UTM campaign data 201*9aec20efSAndreas Gohr * 202*9aec20efSAndreas Gohr * @return void 203*9aec20efSAndreas Gohr */ 204*9aec20efSAndreas Gohr protected function logCampaign(): void 205*9aec20efSAndreas Gohr { 206*9aec20efSAndreas Gohr global $INPUT; 207*9aec20efSAndreas Gohr 208*9aec20efSAndreas Gohr $campaign = $INPUT->filter('trim')->str('utm_campaign', null, true); 209*9aec20efSAndreas Gohr $source = $INPUT->filter('trim')->str('utm_source', null, true); 210*9aec20efSAndreas Gohr $medium = $INPUT->filter('trim')->str('utm_medium', null, true); 211*9aec20efSAndreas Gohr 212*9aec20efSAndreas Gohr if (!$campaign) return; 213*9aec20efSAndreas Gohr 214*9aec20efSAndreas Gohr $this->db->exec( 215*9aec20efSAndreas Gohr 'INSERT OR IGNORE INTO campaigns (session, campaign, source, medium) 216*9aec20efSAndreas Gohr VALUES (?, ?, ?, ?)', 217*9aec20efSAndreas Gohr $this->session, 218*9aec20efSAndreas Gohr $campaign, 219*9aec20efSAndreas Gohr $source, 220*9aec20efSAndreas Gohr $medium 221*9aec20efSAndreas Gohr ); 222*9aec20efSAndreas Gohr } 223*9aec20efSAndreas Gohr 224*9aec20efSAndreas Gohr /** 2254a163f50SAndreas Gohr * Log all groups for the user 226762f4807SAndreas Gohr * 2274a163f50SAndreas Gohr * @todo maybe this should be done only once per session? 228762f4807SAndreas Gohr */ 2294a163f50SAndreas Gohr protected function logGroups(): void 230762f4807SAndreas Gohr { 2314a163f50SAndreas Gohr global $USERINFO; 232762f4807SAndreas Gohr 2334a163f50SAndreas Gohr if (!$this->user) return; 2344a163f50SAndreas Gohr if (!isset($USERINFO['grps'])) return; 2354a163f50SAndreas Gohr if (!is_array($USERINFO['grps'])) return; 2364a163f50SAndreas Gohr $groups = $USERINFO['grps']; 237fced2f86SAnna Dabrowska 2384a163f50SAndreas Gohr $this->db->exec('DELETE FROM groups WHERE user = ?', $this->user); 239762f4807SAndreas Gohr 240bd514593SAndreas Gohr if ($groups === []) { 24141d1fffcSAndreas Gohr return; 24241d1fffcSAndreas Gohr } 24341d1fffcSAndreas Gohr 24402aa9b73SAndreas Gohr $placeholders = implode(',', array_fill(0, count($groups), '(?, ?)')); 245762f4807SAndreas Gohr $params = []; 2464a163f50SAndreas Gohr $sql = "INSERT INTO groups (`user`, `group`) VALUES $placeholders"; 247762f4807SAndreas Gohr foreach ($groups as $group) { 2484a163f50SAndreas Gohr $params[] = $this->user; 249762f4807SAndreas Gohr $params[] = $group; 250762f4807SAndreas Gohr } 251762f4807SAndreas Gohr $this->db->exec($sql, $params); 252762f4807SAndreas Gohr } 253762f4807SAndreas Gohr 254762f4807SAndreas Gohr /** 2554a163f50SAndreas Gohr * Log email domain 2560b8b7abaSAnna Dabrowska * 2574a163f50SAndreas Gohr * @todo maybe this should be done only once per session? 2580b8b7abaSAnna Dabrowska */ 2594a163f50SAndreas Gohr protected function logDomain(): void 2600b8b7abaSAnna Dabrowska { 2614a163f50SAndreas Gohr global $USERINFO; 2624a163f50SAndreas Gohr if (!$this->user) return; 2634a163f50SAndreas Gohr if (!isset($USERINFO['mail'])) return; 2644a163f50SAndreas Gohr $mail = $USERINFO['mail']; 2650b8b7abaSAnna Dabrowska 2660b8b7abaSAnna Dabrowska $pos = strrpos($mail, '@'); 2670b8b7abaSAnna Dabrowska if (!$pos) return; 2680b8b7abaSAnna Dabrowska $domain = substr($mail, $pos + 1); 2690b8b7abaSAnna Dabrowska if (empty($domain)) return; 2700b8b7abaSAnna Dabrowska 2714a163f50SAndreas Gohr $sql = 'UPDATE users SET domain = ? WHERE user = ?'; 2724a163f50SAndreas Gohr $this->db->exec($sql, [$domain, $this->user]); 2730b8b7abaSAnna Dabrowska } 2740b8b7abaSAnna Dabrowska 2754a163f50SAndreas Gohr // endregion 2764a163f50SAndreas Gohr // region internal loggers called by the dispatchers 2774a163f50SAndreas Gohr 2780b8b7abaSAnna Dabrowska /** 2794a163f50SAndreas Gohr * Log the given referer URL 280762f4807SAndreas Gohr * 2812a30f557SAndreas Gohr * Note: we DO log empty referers. These are external accesses that did not provide a referer URL. 2822a30f557SAndreas Gohr * We do not log referers that are our own pages though. 2832a30f557SAndreas Gohr * 2842a30f557SAndreas Gohr * engine set -> a search engine referer 2852a30f557SAndreas Gohr * no engine set, url empty -> a direct access (bookmark, direct link, etc.) 2862a30f557SAndreas Gohr * no engine set, url not empty -> a referer from another page (not a wiki page) 2872a30f557SAndreas Gohr * null returned -> referer was a wiki page 2882a30f557SAndreas Gohr * 2894a163f50SAndreas Gohr * @param $referer 2902a30f557SAndreas Gohr * @return int|null The referer ID or null if no referer was logged 2912a30f557SAndreas Gohr * @todo we could check against a blacklist here 292762f4807SAndreas Gohr */ 2934a163f50SAndreas Gohr public function logReferer($referer): ?int 294762f4807SAndreas Gohr { 2952a30f557SAndreas Gohr $referer = trim($referer); 296762f4807SAndreas Gohr 297569a5066SAndreas Gohr // do not log our own pages as referers (empty referer is OK though) 298569a5066SAndreas Gohr if (!empty($referer)) { 299569a5066SAndreas Gohr $selfre = '^' . preg_quote(DOKU_URL, '/'); 3002a30f557SAndreas Gohr if (preg_match("/$selfre/", $referer)) { 3012a30f557SAndreas Gohr return null; 3022a30f557SAndreas Gohr } 303569a5066SAndreas Gohr } 304762f4807SAndreas Gohr 3052a30f557SAndreas Gohr // is it a search engine? 3064a163f50SAndreas Gohr $se = new SearchEngines($referer); 30741d1fffcSAndreas Gohr $engine = $se->getEngine(); 308762f4807SAndreas Gohr 30941d1fffcSAndreas Gohr $sql = 'INSERT OR IGNORE INTO referers (url, engine, dt) VALUES (?, ?, CURRENT_TIMESTAMP)'; 310569a5066SAndreas Gohr $this->db->exec($sql, [$referer, $engine]); 311569a5066SAndreas Gohr return (int)$this->db->queryValue('SELECT id FROM referers WHERE url = ?', $referer); 312762f4807SAndreas Gohr } 313762f4807SAndreas Gohr 314762f4807SAndreas Gohr /** 315762f4807SAndreas Gohr * Resolve IP to country/city and store in database 316762f4807SAndreas Gohr * 3174a163f50SAndreas Gohr * @return string The IP address as stored 318762f4807SAndreas Gohr */ 3194a163f50SAndreas Gohr public function logIp(): string 320762f4807SAndreas Gohr { 3214a163f50SAndreas Gohr $ip = clientIP(true); 32269fb56a2SAndreas Gohr 32369fb56a2SAndreas Gohr // anonymize the IP address for storage? 32469fb56a2SAndreas Gohr if ($this->hlp->getConf('anonips')) { 32569fb56a2SAndreas Gohr $hash = md5($ip . strrev($ip)); // we use the reversed IP as salt to avoid common rainbow tables 32669fb56a2SAndreas Gohr $host = ''; 32769fb56a2SAndreas Gohr } else { 32869fb56a2SAndreas Gohr $hash = $ip; 32969fb56a2SAndreas Gohr $host = gethostbyaddr($ip); 33069fb56a2SAndreas Gohr } 3314a163f50SAndreas Gohr 332ba6b3b10SAndreas Gohr if ($this->hlp->getConf('nolocation')) { 333ba6b3b10SAndreas Gohr // if we don't resolve location data, we just return the IP address 334ba6b3b10SAndreas Gohr return $hash; 335ba6b3b10SAndreas Gohr } 336ba6b3b10SAndreas Gohr 337762f4807SAndreas Gohr // check if IP already known and up-to-date 338762f4807SAndreas Gohr $result = $this->db->queryValue( 339762f4807SAndreas Gohr "SELECT ip 340762f4807SAndreas Gohr FROM iplocation 341762f4807SAndreas Gohr WHERE ip = ? 3427a1a7c58SAndreas Gohr AND dt > date('now', '-30 days')", 3434a163f50SAndreas Gohr $hash 344762f4807SAndreas Gohr ); 3454a163f50SAndreas Gohr if ($result) return $hash; // already known and up-to-date 346762f4807SAndreas Gohr 347762f4807SAndreas Gohr 348ba6b3b10SAndreas Gohr // resolve the IP address to location data 349762f4807SAndreas Gohr try { 350ba6b3b10SAndreas Gohr $data = $this->hlp->resolveIP($ip); 351ba6b3b10SAndreas Gohr } catch (IpResolverException $e) { 352ba6b3b10SAndreas Gohr \dokuwiki\Logger::error('Statistics Plugin: ' . $e->getMessage(), $e->details); 353ba6b3b10SAndreas Gohr $data = []; 354762f4807SAndreas Gohr } 355762f4807SAndreas Gohr 356762f4807SAndreas Gohr $this->db->exec( 357762f4807SAndreas Gohr 'INSERT OR REPLACE INTO iplocation ( 3587a1a7c58SAndreas Gohr ip, country, code, city, host, dt 359762f4807SAndreas Gohr ) VALUES ( 360762f4807SAndreas Gohr ?, ?, ?, ?, ?, CURRENT_TIMESTAMP 361762f4807SAndreas Gohr )', 3624a163f50SAndreas Gohr $hash, 36302aa9b73SAndreas Gohr $data['country'] ?? '', 36402aa9b73SAndreas Gohr $data['countryCode'] ?? '', 36502aa9b73SAndreas Gohr $data['city'] ?? '', 3662adee4c6SAndreas Gohr $host 367762f4807SAndreas Gohr ); 3684a163f50SAndreas Gohr 3694a163f50SAndreas Gohr return $hash; 3704a163f50SAndreas Gohr } 3714a163f50SAndreas Gohr 3724a163f50SAndreas Gohr // endregion 3734a163f50SAndreas Gohr // region log dispatchers 3744a163f50SAndreas Gohr 3754a163f50SAndreas Gohr public function logPageView(): void 3764a163f50SAndreas Gohr { 3774a163f50SAndreas Gohr global $INPUT; 3784a163f50SAndreas Gohr 3794a163f50SAndreas Gohr if (!$INPUT->str('p')) return; 3804a163f50SAndreas Gohr 3814a163f50SAndreas Gohr 3824a163f50SAndreas Gohr $referer = $INPUT->filter('trim')->str('r'); 3834a163f50SAndreas Gohr $ip = $this->logIp(); // resolve the IP address 3844a163f50SAndreas Gohr 3854a163f50SAndreas Gohr $data = [ 3864a163f50SAndreas Gohr 'page' => $INPUT->filter('cleanID')->str('p'), 3874a163f50SAndreas Gohr 'ip' => $ip, 3884a163f50SAndreas Gohr 'ref_id' => $this->logReferer($referer), 3894a163f50SAndreas Gohr 'sx' => $INPUT->int('sx'), 3904a163f50SAndreas Gohr 'sy' => $INPUT->int('sy'), 3914a163f50SAndreas Gohr 'vx' => $INPUT->int('vx'), 3924a163f50SAndreas Gohr 'vy' => $INPUT->int('vy'), 3934a163f50SAndreas Gohr 'session' => $this->session, 3944a163f50SAndreas Gohr ]; 3954a163f50SAndreas Gohr 396bd514593SAndreas Gohr $this->db->exec( 397bd514593SAndreas Gohr ' 3984a163f50SAndreas Gohr INSERT INTO pageviews ( 3994a163f50SAndreas Gohr dt, page, ip, ref_id, screen_x, screen_y, view_x, view_y, session 4004a163f50SAndreas Gohr ) VALUES ( 4014a163f50SAndreas Gohr CURRENT_TIMESTAMP, :page, :ip, :ref_id, :sx, :sy, :vx, :vy, :session 4024a163f50SAndreas Gohr ) 4034a163f50SAndreas Gohr ', 4044a163f50SAndreas Gohr $data 4054a163f50SAndreas Gohr ); 406762f4807SAndreas Gohr } 407762f4807SAndreas Gohr 408762f4807SAndreas Gohr /** 409762f4807SAndreas Gohr * Log a click on an external link 410762f4807SAndreas Gohr * 41187e0f0b1SAndreas Gohr * Called from dispatch.php 412762f4807SAndreas Gohr */ 413762f4807SAndreas Gohr public function logOutgoing(): void 414762f4807SAndreas Gohr { 415762f4807SAndreas Gohr global $INPUT; 416762f4807SAndreas Gohr 417762f4807SAndreas Gohr if (!$INPUT->str('ol')) return; 418762f4807SAndreas Gohr 4194a163f50SAndreas Gohr $link = $INPUT->filter('trim')->str('ol'); 4204a163f50SAndreas Gohr $session = $this->session; 4214a163f50SAndreas Gohr $page = $INPUT->filter('cleanID')->str('p'); 422762f4807SAndreas Gohr 423762f4807SAndreas Gohr $this->db->exec( 424762f4807SAndreas Gohr 'INSERT INTO outlinks ( 4254a163f50SAndreas Gohr dt, session, page, link 426762f4807SAndreas Gohr ) VALUES ( 42741d1fffcSAndreas Gohr CURRENT_TIMESTAMP, ?, ?, ? 428762f4807SAndreas Gohr )', 4292adee4c6SAndreas Gohr $session, 4302adee4c6SAndreas Gohr $page, 4312adee4c6SAndreas Gohr $link 432762f4807SAndreas Gohr ); 433762f4807SAndreas Gohr } 434762f4807SAndreas Gohr 435762f4807SAndreas Gohr /** 436762f4807SAndreas Gohr * Log access to a media file 437762f4807SAndreas Gohr * 438762f4807SAndreas Gohr * Called from action.php 439762f4807SAndreas Gohr * 440762f4807SAndreas Gohr * @param string $media The media ID 441762f4807SAndreas Gohr * @param string $mime The media's mime type 442762f4807SAndreas Gohr * @param bool $inline Is this displayed inline? 443762f4807SAndreas Gohr * @param int $size Size of the media file 444762f4807SAndreas Gohr */ 445762f4807SAndreas Gohr public function logMedia(string $media, string $mime, bool $inline, int $size): void 446762f4807SAndreas Gohr { 447762f4807SAndreas Gohr [$mime1, $mime2] = explode('/', strtolower($mime)); 448762f4807SAndreas Gohr $inline = $inline ? 1 : 0; 449762f4807SAndreas Gohr 450762f4807SAndreas Gohr 4514a163f50SAndreas Gohr $data = [ 4524a163f50SAndreas Gohr 'media' => cleanID($media), 4534a163f50SAndreas Gohr 'ip' => $this->logIp(), // resolve the IP address 4544a163f50SAndreas Gohr 'session' => $this->session, 4554a163f50SAndreas Gohr 'size' => $size, 4564a163f50SAndreas Gohr 'mime1' => $mime1, 4574a163f50SAndreas Gohr 'mime2' => $mime2, 4584a163f50SAndreas Gohr 'inline' => $inline, 4594a163f50SAndreas Gohr ]; 4604a163f50SAndreas Gohr 461bd514593SAndreas Gohr $this->db->exec( 462bd514593SAndreas Gohr ' 4634a163f50SAndreas Gohr INSERT INTO media ( dt, media, ip, session, size, mime1, mime2, inline ) 4644a163f50SAndreas Gohr VALUES (CURRENT_TIMESTAMP, :media, :ip, :session, :size, :mime1, :mime2, :inline) 4654a163f50SAndreas Gohr ', 4664a163f50SAndreas Gohr $data 467762f4807SAndreas Gohr ); 468762f4807SAndreas Gohr } 469762f4807SAndreas Gohr 470762f4807SAndreas Gohr /** 471762f4807SAndreas Gohr * Log page edits 472762f4807SAndreas Gohr * 4734a163f50SAndreas Gohr * called from action.php 4744a163f50SAndreas Gohr * 475762f4807SAndreas Gohr * @param string $page The page that was edited 476762f4807SAndreas Gohr * @param string $type The type of edit (create, edit, etc.) 477762f4807SAndreas Gohr */ 478762f4807SAndreas Gohr public function logEdit(string $page, string $type): void 479762f4807SAndreas Gohr { 4804a163f50SAndreas Gohr $data = [ 4814a163f50SAndreas Gohr 'page' => cleanID($page), 4824a163f50SAndreas Gohr 'type' => $type, 4834a163f50SAndreas Gohr 'ip' => $this->logIp(), // resolve the IP address 4844a163f50SAndreas Gohr 'session' => $this->session 4854a163f50SAndreas Gohr ]; 486762f4807SAndreas Gohr 48741d1fffcSAndreas Gohr $this->db->exec( 488762f4807SAndreas Gohr 'INSERT INTO edits ( 4894a163f50SAndreas Gohr dt, page, type, ip, session 490762f4807SAndreas Gohr ) VALUES ( 4914a163f50SAndreas Gohr CURRENT_TIMESTAMP, :page, :type, :ip, :session 492762f4807SAndreas Gohr )', 4934a163f50SAndreas Gohr $data 494762f4807SAndreas Gohr ); 495762f4807SAndreas Gohr } 496762f4807SAndreas Gohr 497762f4807SAndreas Gohr /** 498762f4807SAndreas Gohr * Log login/logoffs and user creations 499762f4807SAndreas Gohr * 500af93d154SAndreas Gohr * @param string $type The type of login event (login, logout, create, failed) 501af93d154SAndreas Gohr * @param string $user The username 502762f4807SAndreas Gohr */ 503762f4807SAndreas Gohr public function logLogin(string $type, string $user = ''): void 504762f4807SAndreas Gohr { 505762f4807SAndreas Gohr global $INPUT; 506762f4807SAndreas Gohr 507762f4807SAndreas Gohr if (!$user) $user = $INPUT->server->str('REMOTE_USER'); 508762f4807SAndreas Gohr 509762f4807SAndreas Gohr $ip = clientIP(true); 510762f4807SAndreas Gohr 511762f4807SAndreas Gohr $this->db->exec( 512762f4807SAndreas Gohr 'INSERT INTO logins ( 513af93d154SAndreas Gohr dt, ip, user, type 514762f4807SAndreas Gohr ) VALUES ( 515af93d154SAndreas Gohr CURRENT_TIMESTAMP, ?, ?, ? 516762f4807SAndreas Gohr )', 5172adee4c6SAndreas Gohr $ip, 5182adee4c6SAndreas Gohr $user, 519af93d154SAndreas Gohr $type 520762f4807SAndreas Gohr ); 521762f4807SAndreas Gohr } 522762f4807SAndreas Gohr 523762f4807SAndreas Gohr /** 52402aa9b73SAndreas Gohr * Log search data to the search related tables 52502aa9b73SAndreas Gohr * 52602aa9b73SAndreas Gohr * @param string $query The search query 52702aa9b73SAndreas Gohr * @param string[] $words The query split into words 52802aa9b73SAndreas Gohr */ 52902aa9b73SAndreas Gohr public function logSearch(string $query, array $words): void 53002aa9b73SAndreas Gohr { 53102aa9b73SAndreas Gohr if (!$query) return; 53202aa9b73SAndreas Gohr 53302aa9b73SAndreas Gohr $sid = $this->db->exec( 53402aa9b73SAndreas Gohr 'INSERT INTO search (dt, ip, session, query) VALUES (CURRENT_TIMESTAMP, ?, ? , ?)', 53502aa9b73SAndreas Gohr $this->logIp(), // resolve the IP address 53602aa9b73SAndreas Gohr $this->session, 53702aa9b73SAndreas Gohr $query, 53802aa9b73SAndreas Gohr ); 53902aa9b73SAndreas Gohr 54002aa9b73SAndreas Gohr foreach ($words as $word) { 54102aa9b73SAndreas Gohr if (!$word) continue; 54202aa9b73SAndreas Gohr $this->db->exec( 54302aa9b73SAndreas Gohr 'INSERT INTO searchwords (sid, word) VALUES (?, ?)', 54402aa9b73SAndreas Gohr $sid, 54502aa9b73SAndreas Gohr $word 54602aa9b73SAndreas Gohr ); 54702aa9b73SAndreas Gohr } 54802aa9b73SAndreas Gohr } 54902aa9b73SAndreas Gohr 55002aa9b73SAndreas Gohr /** 551762f4807SAndreas Gohr * Log the current page count and size as today's history entry 552762f4807SAndreas Gohr */ 553762f4807SAndreas Gohr public function logHistoryPages(): void 554762f4807SAndreas Gohr { 555762f4807SAndreas Gohr global $conf; 556762f4807SAndreas Gohr 557762f4807SAndreas Gohr // use the popularity plugin's search method to find the wanted data 558762f4807SAndreas Gohr /** @var helper_plugin_popularity $pop */ 559762f4807SAndreas Gohr $pop = plugin_load('helper', 'popularity'); 560b188870fSAndreas Gohr $list = $this->initEmptySearchList(); 561762f4807SAndreas Gohr search($list, $conf['datadir'], [$pop, 'searchCountCallback'], ['all' => false], ''); 562762f4807SAndreas Gohr $page_count = $list['file_count']; 563762f4807SAndreas Gohr $page_size = $list['file_size']; 564762f4807SAndreas Gohr 565762f4807SAndreas Gohr $this->db->exec( 566762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 567762f4807SAndreas Gohr info, value, dt 568762f4807SAndreas Gohr ) VALUES ( 569483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 570762f4807SAndreas Gohr )', 5712adee4c6SAndreas Gohr 'page_count', 5722adee4c6SAndreas Gohr $page_count 573762f4807SAndreas Gohr ); 574762f4807SAndreas Gohr $this->db->exec( 575762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 576762f4807SAndreas Gohr info, value, dt 577762f4807SAndreas Gohr ) VALUES ( 578483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 579762f4807SAndreas Gohr )', 5802adee4c6SAndreas Gohr 'page_size', 5812adee4c6SAndreas Gohr $page_size 582762f4807SAndreas Gohr ); 583762f4807SAndreas Gohr } 584762f4807SAndreas Gohr 585762f4807SAndreas Gohr /** 586762f4807SAndreas Gohr * Log the current media count and size as today's history entry 587762f4807SAndreas Gohr */ 588762f4807SAndreas Gohr public function logHistoryMedia(): void 589762f4807SAndreas Gohr { 590762f4807SAndreas Gohr global $conf; 591762f4807SAndreas Gohr 592762f4807SAndreas Gohr // use the popularity plugin's search method to find the wanted data 593762f4807SAndreas Gohr /** @var helper_plugin_popularity $pop */ 594762f4807SAndreas Gohr $pop = plugin_load('helper', 'popularity'); 595b188870fSAndreas Gohr $list = $this->initEmptySearchList(); 596762f4807SAndreas Gohr search($list, $conf['mediadir'], [$pop, 'searchCountCallback'], ['all' => true], ''); 597762f4807SAndreas Gohr $media_count = $list['file_count']; 598762f4807SAndreas Gohr $media_size = $list['file_size']; 599762f4807SAndreas Gohr 600762f4807SAndreas Gohr $this->db->exec( 601762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 602762f4807SAndreas Gohr info, value, dt 603762f4807SAndreas Gohr ) VALUES ( 604483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 605762f4807SAndreas Gohr )', 6062adee4c6SAndreas Gohr 'media_count', 6072adee4c6SAndreas Gohr $media_count 608762f4807SAndreas Gohr ); 609762f4807SAndreas Gohr $this->db->exec( 610762f4807SAndreas Gohr 'INSERT OR REPLACE INTO history ( 611762f4807SAndreas Gohr info, value, dt 612762f4807SAndreas Gohr ) VALUES ( 613483101d3SAndreas Gohr ?, ?, CURRENT_TIMESTAMP 614762f4807SAndreas Gohr )', 6152adee4c6SAndreas Gohr 'media_size', 6162adee4c6SAndreas Gohr $media_size 617762f4807SAndreas Gohr ); 618762f4807SAndreas Gohr } 619b188870fSAndreas Gohr 6204a163f50SAndreas Gohr // endregion 6214a163f50SAndreas Gohr 622b188870fSAndreas Gohr /** 623b188870fSAndreas Gohr * @todo can be dropped in favor of helper_plugin_popularity::initEmptySearchList() once it's public 624b188870fSAndreas Gohr * @return array 625b188870fSAndreas Gohr */ 626b188870fSAndreas Gohr protected function initEmptySearchList() 627b188870fSAndreas Gohr { 628b188870fSAndreas Gohr return array_fill_keys([ 629b188870fSAndreas Gohr 'file_count', 630b188870fSAndreas Gohr 'file_size', 631b188870fSAndreas Gohr 'file_max', 632b188870fSAndreas Gohr 'file_min', 633b188870fSAndreas Gohr 'dir_count', 634b188870fSAndreas Gohr 'dir_nest', 635b188870fSAndreas Gohr 'file_oldest' 636b188870fSAndreas Gohr ], 0); 637b188870fSAndreas Gohr } 638762f4807SAndreas Gohr} 639