hlp = $hlp; $this->db = $this->hlp->getDB(); $this->httpClient = $httpClient; // FIXME if we already have a session, we should not re-parse the user agent $ua = trim($INPUT->server->str('HTTP_USER_AGENT')); AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR); $dd = new DeviceDetector($ua); // FIXME we could use client hints, but need to add headers $dd->discardBotInformation(); $dd->parse(); if ($dd->isFeedReader()) { $this->uaType = 'feedreader'; } elseif ($dd->isBot()) { $this->uaType = 'robot'; // for now ignore bots throw new IgnoreException('Bot detected, not logging'); } $this->uaAgent = $ua; $this->uaName = Browser::getBrowserFamily($dd->getClient('name')) ?: 'Unknown'; $this->uaVersion = $dd->getClient('version') ?: '0'; $this->uaPlatform = OperatingSystem::getOsFamily($dd->getOs('name')) ?: 'Unknown'; $this->uid = $this->getUID(); $this->session = $this->getSession(); $this->user = $INPUT->server->str('REMOTE_USER') ?: null; } /** * Should be called before logging * * This starts a transaction, so all logging is done in one go. It also logs the user and session data. */ public function begin(): void { $this->hlp->getDB()->getPdo()->beginTransaction(); $this->logUser(); $this->logGroups(); $this->logDomain(); $this->logSession(); } /** * Should be called after logging * * This commits the transaction started in begin() */ public function end(): void { $this->hlp->getDB()->getPdo()->commit(); } // endregion // region data gathering /** * Get the unique user ID * * @return string The unique user identifier */ protected function getUID(): string { global $INPUT; $uid = $INPUT->str('uid'); if (!$uid) $uid = get_doku_pref('plgstats', false); if (!$uid) $uid = session_id(); set_doku_pref('plgstats', $uid); return $uid; } /** * Return the user's session ID * * This is usually our own managed session, not a PHP session (only in fallback) * * @return string The session identifier */ protected function getSession(): string { global $INPUT; // FIXME session setting needs work. It should be reset on user change, maybe we do rely on the PHP session? // We also want to store the user agent in the session table, so this needs also change the session ID $ses = $INPUT->str('ses'); if (!$ses) $ses = get_doku_pref('plgstatsses', false); if (!$ses) $ses = session_id(); set_doku_pref('plgstatsses', $ses); return $ses; } // endregion // region automatic logging /** * Log the user was seen */ protected function logUser(): void { if (!$this->user) return; $this->db->exec( 'INSERT INTO users (user, dt) VALUES (?, CURRENT_TIMESTAMP) ON CONFLICT (user) DO UPDATE SET dt = CURRENT_TIMESTAMP WHERE excluded.user = users.user ', $this->user ); } /** * Log the session and user agent information */ protected function logSession(): void { $this->db->exec( 'INSERT INTO sessions (session, dt, end, uid, user, ua, ua_info, ua_type, ua_ver, os) VALUES (?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (session) DO UPDATE SET end = CURRENT_TIMESTAMP WHERE excluded.session = sessions.session ', $this->session, $this->uid, $this->user, $this->uaAgent, $this->uaName, $this->uaType, $this->uaVersion, $this->uaPlatform ); } /** * Log all groups for the user * * @todo maybe this should be done only once per session? */ protected function logGroups(): void { global $USERINFO; if (!$this->user) return; if (!isset($USERINFO['grps'])) return; if (!is_array($USERINFO['grps'])) return; $groups = $USERINFO['grps']; $this->db->exec('DELETE FROM groups WHERE user = ?', $this->user); $placeholders = implode(',', array_fill(0, count($groups), '(?, ?)')); $params = []; $sql = "INSERT INTO groups (`user`, `group`) VALUES $placeholders"; foreach ($groups as $group) { $params[] = $this->user; $params[] = $group; } $this->db->exec($sql, $params); } /** * Log email domain * * @todo maybe this should be done only once per session? */ protected function logDomain(): void { global $USERINFO; if (!$this->user) return; if (!isset($USERINFO['mail'])) return; $mail = $USERINFO['mail']; $pos = strrpos($mail, '@'); if (!$pos) return; $domain = substr($mail, $pos + 1); if (empty($domain)) return; $sql = 'UPDATE users SET domain = ? WHERE user = ?'; $this->db->exec($sql, [$domain, $this->user]); } // endregion // region internal loggers called by the dispatchers /** * Log the given referer URL * * @param $referer * @return int|null The referer ID or null if no referer was given */ public function logReferer($referer): ?int { if (!$referer) return null; // FIXME we could check against a blacklist here $se = new SearchEngines($referer); $type = $se->isSearchEngine() ? 'search' : 'external'; $sql = 'INSERT OR IGNORE INTO referers (url, type, dt) VALUES (?, ?, CURRENT_TIMESTAMP)'; return $this->db->exec($sql, [$referer, $type]); // returns ID even if the insert was ignored } /** * Resolve IP to country/city and store in database * * @return string The IP address as stored */ public function logIp(): string { $ip = clientIP(true); $hash = $ip; // @todo we could anonymize here // check if IP already known and up-to-date $result = $this->db->queryValue( "SELECT ip FROM iplocation WHERE ip = ? AND lastupd > date('now', '-30 days')", $hash ); if ($result) return $hash; // already known and up-to-date $http = $this->httpClient ?: new DokuHTTPClient(); $http->timeout = 7; $json = $http->get('http://ip-api.com/json/' . $ip); // yes, it's HTTP only if (!$json) { \dokuwiki\Logger::error('Statistics Plugin - Failed talk to ip-api.com.'); return $hash; } try { $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $e) { \dokuwiki\Logger::error('Statistics Plugin - Failed to decode JSON from ip-api.com.', $e); return $hash; } if (!isset($data['status'])) { \dokuwiki\Logger::error('Statistics Plugin - Invalid ip-api.com result' . $ip, $data); return $hash; }; // we do not check for 'success' status here. when the API can't resolve the IP we still log it // without location data, so we won't re-query it in the next 30 days. $host = gethostbyaddr($ip); // @todo if we anonymize the IP, we should not do this $this->db->exec( 'INSERT OR REPLACE INTO iplocation ( ip, country, code, city, host, lastupd ) VALUES ( ?, ?, ?, ?, ?, CURRENT_TIMESTAMP )', $hash, $data['country'] ?? '', $data['countryCode'] ?? '', $data['city'] ?? '', $host ); return $hash; } // endregion // region log dispatchers public function logPageView(): void { global $INPUT; if (!$INPUT->str('p')) return; $referer = $INPUT->filter('trim')->str('r'); $ip = $this->logIp(); // resolve the IP address $data = [ 'page' => $INPUT->filter('cleanID')->str('p'), 'ip' => $ip, 'ref_id' => $this->logReferer($referer), 'sx' => $INPUT->int('sx'), 'sy' => $INPUT->int('sy'), 'vx' => $INPUT->int('vx'), 'vy' => $INPUT->int('vy'), 'session' => $this->session, ]; $this->db->exec(' INSERT INTO pageviews ( dt, page, ip, ref_id, screen_x, screen_y, view_x, view_y, session ) VALUES ( CURRENT_TIMESTAMP, :page, :ip, :ref_id, :sx, :sy, :vx, :vy, :session ) ', $data ); } /** * Log a click on an external link * * Called from log.php */ public function logOutgoing(): void { global $INPUT; if (!$INPUT->str('ol')) return; $link = $INPUT->filter('trim')->str('ol'); $session = $this->session; $page = $INPUT->filter('cleanID')->str('p'); $this->db->exec( 'INSERT INTO outlinks ( dt, session, page, link ) VALUES ( CURRENT_TIMESTAMP, ?, ?, ?, ? )', $session, $page, $link ); } /** * Log access to a media file * * Called from action.php * * @param string $media The media ID * @param string $mime The media's mime type * @param bool $inline Is this displayed inline? * @param int $size Size of the media file */ public function logMedia(string $media, string $mime, bool $inline, int $size): void { [$mime1, $mime2] = explode('/', strtolower($mime)); $inline = $inline ? 1 : 0; $data = [ 'media' => cleanID($media), 'ip' => $this->logIp(), // resolve the IP address 'session' => $this->session, 'size' => $size, 'mime1' => $mime1, 'mime2' => $mime2, 'inline' => $inline, ]; $this->db->exec(' INSERT INTO media ( dt, media, ip, session, size, mime1, mime2, inline ) VALUES (CURRENT_TIMESTAMP, :media, :ip, :session, :size, :mime1, :mime2, :inline) ', $data ); } /** * Log page edits * * called from action.php * * @param string $page The page that was edited * @param string $type The type of edit (create, edit, etc.) */ public function logEdit(string $page, string $type): void { $data = [ 'page' => cleanID($page), 'type' => $type, 'ip' => $this->logIp(), // resolve the IP address 'session' => $this->session ]; $editId = $this->db->exec( 'INSERT INTO edits ( dt, page, type, ip, session ) VALUES ( CURRENT_TIMESTAMP, :page, :type, :ip, :session )', $data ); } /** * Log login/logoffs and user creations * * @param string $type The type of login event (login, logout, create) * @param string $user The username (optional, will use current user if empty) * @fixme this is still broken, I need to figure out the session handling first */ public function logLogin(string $type, string $user = ''): void { global $INPUT; if (!$user) $user = $INPUT->server->str('REMOTE_USER'); $ip = clientIP(true); $session = $this->session; $this->db->exec( 'INSERT INTO logins ( dt, type, ip, session ) VALUES ( CURRENT_TIMESTAMP, ?, ?, ?, ?, ? )', $type, $ip, $user, $session, $this->uid ); } /** * Log search data to the search related tables * * @param string $query The search query * @param string[] $words The query split into words */ public function logSearch(string $query, array $words): void { if(!$query) return; $sid = $this->db->exec( 'INSERT INTO search (dt, ip, session, query) VALUES (CURRENT_TIMESTAMP, ?, ? , ?)', $this->logIp(), // resolve the IP address $this->session, $query, ); foreach ($words as $word) { if (!$word) continue; $this->db->exec( 'INSERT INTO searchwords (sid, word) VALUES (?, ?)', $sid, $word ); } } /** * Log the current page count and size as today's history entry */ public function logHistoryPages(): void { global $conf; // use the popularity plugin's search method to find the wanted data /** @var helper_plugin_popularity $pop */ $pop = plugin_load('helper', 'popularity'); $list = $this->initEmptySearchList(); search($list, $conf['datadir'], [$pop, 'searchCountCallback'], ['all' => false], ''); $page_count = $list['file_count']; $page_size = $list['file_size']; $this->db->exec( 'INSERT OR REPLACE INTO history ( info, value, dt ) VALUES ( ?, ?, CURRENT_TIMESTAMP )', 'page_count', $page_count ); $this->db->exec( 'INSERT OR REPLACE INTO history ( info, value, dt ) VALUES ( ?, ?, CURRENT_TIMESTAMP )', 'page_size', $page_size ); } /** * Log the current media count and size as today's history entry */ public function logHistoryMedia(): void { global $conf; // use the popularity plugin's search method to find the wanted data /** @var helper_plugin_popularity $pop */ $pop = plugin_load('helper', 'popularity'); $list = $this->initEmptySearchList(); search($list, $conf['mediadir'], [$pop, 'searchCountCallback'], ['all' => true], ''); $media_count = $list['file_count']; $media_size = $list['file_size']; $this->db->exec( 'INSERT OR REPLACE INTO history ( info, value, dt ) VALUES ( ?, ?, CURRENT_TIMESTAMP )', 'media_count', $media_count ); $this->db->exec( 'INSERT OR REPLACE INTO history ( info, value, dt ) VALUES ( ?, ?, CURRENT_TIMESTAMP )', 'media_size', $media_size ); } // endregion /** * @todo can be dropped in favor of helper_plugin_popularity::initEmptySearchList() once it's public * @return array */ protected function initEmptySearchList() { return array_fill_keys([ 'file_count', 'file_size', 'file_max', 'file_min', 'dir_count', 'dir_nest', 'file_oldest' ], 0); } }