From a98cb66c5351e5a70d18ea48ce2310016bcde495 Mon Sep 17 00:00:00 2001 From: slawkens Date: Fri, 17 Feb 2023 18:41:25 +0100 Subject: [PATCH] feature: visitors counter shows now user browser, and also if its bot --- admin/pages/visitors.php | 27 +++++++++++++++++++++++ common.php | 2 +- composer.json | 3 ++- install/includes/schema.sql | 3 ++- system/libs/visitors.php | 20 +++++++++-------- system/migrations/34.php | 4 ++++ system/templates/admin.visitors.html.twig | 2 ++ 7 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 system/migrations/34.php diff --git a/admin/pages/visitors.php b/admin/pages/visitors.php index e86d647b..28efb7c5 100644 --- a/admin/pages/visitors.php +++ b/admin/pages/visitors.php @@ -8,6 +8,11 @@ * @link https://my-aac.org */ defined('MYAAC') or die('Direct access not allowed!'); + +use DeviceDetector\DeviceDetector; +use DeviceDetector\Parser\Client\Browser; +use DeviceDetector\Parser\OperatingSystem; + $title = 'Visitors'; $use_datatable = true; @@ -30,6 +35,28 @@ function compare($a, $b) $tmp = $visitors->getVisitors(); usort($tmp, 'compare'); +foreach ($tmp as &$visitor) { + $userAgent = $visitor['user_agent']; + if ($userAgent == 'unknown') { + $browser = 'Unknown'; + } + else { + $dd = new DeviceDetector($userAgent); + $dd->parse(); + if ($dd->isBot()) { + $browser = '(Bot) ' . $dd->getBot(); + } + else { + $osFamily = OperatingSystem::getOsFamily($dd->getOs('name')); + $browserFamily = Browser::getBrowserFamily($dd->getClient('name')); + + $browser = $osFamily . ', ' . $browserFamily; + } + } + + $visitor['browser'] = $browser; +} + $twig->display('admin.visitors.html.twig', array( 'config_visitors_counter_ttl' => $config['visitors_counter_ttl'], 'visitors' => $tmp diff --git a/common.php b/common.php index dfe6439d..bcc66be3 100644 --- a/common.php +++ b/common.php @@ -27,7 +27,7 @@ if (version_compare(phpversion(), '7.2.5', '<')) die('PHP version 7.2.5 or highe const MYAAC = true; const MYAAC_VERSION = '0.9.0-dev'; -const DATABASE_VERSION = 33; +const DATABASE_VERSION = 34; const TABLE_PREFIX = 'myaac_'; define('START_TIME', microtime(true)); define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX')); diff --git a/composer.json b/composer.json index 90179c29..cd16bf18 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ "composer/semver": "^3.2", "twig/twig": "^2.0", "erusev/parsedown": "^1.7", - "nikic/fast-route": "^1.3" + "nikic/fast-route": "^1.3", + "matomo/device-detector": "^6.0" } } diff --git a/install/includes/schema.sql b/install/includes/schema.sql index 8a12dd40..8a70d66a 100644 --- a/install/includes/schema.sql +++ b/install/includes/schema.sql @@ -1,4 +1,4 @@ -SET @myaac_database_version = 33; +SET @myaac_database_version = 34; CREATE TABLE `myaac_account_actions` ( @@ -330,6 +330,7 @@ CREATE TABLE `myaac_visitors` `ip` VARCHAR(45) NOT NULL, `lastvisit` INT(11) NOT NULL DEFAULT 0, `page` VARCHAR(2048) NOT NULL, + `user_agent` VARCHAR(255) NOT NULL DEFAULT '', UNIQUE (`ip`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; diff --git a/system/libs/visitors.php b/system/libs/visitors.php index b8b80dba..53cc5839 100644 --- a/system/libs/visitors.php +++ b/system/libs/visitors.php @@ -34,10 +34,12 @@ class Visitors $this->cleanVisitors(); $ip = $_SERVER['REMOTE_ADDR']; + $userAgentShortened = substr($_SERVER['HTTP_USER_AGENT'] ?? 'unknown', 0, 255); + if($this->visitorExists($ip)) - $this->updateVisitor($ip, $_SERVER['REQUEST_URI']); + $this->updateVisitor($ip, $_SERVER['REQUEST_URI'], $userAgentShortened); else - $this->addVisitor($ip, $_SERVER['REQUEST_URI']); + $this->addVisitor($ip, $_SERVER['REQUEST_URI'], $userAgentShortened); } public function __destruct() @@ -75,26 +77,26 @@ class Visitors $db->exec('DELETE FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' WHERE ' . $db->fieldName('lastvisit') . ' < ' . (time() - $this->sessionTime * 60)); } - private function updateVisitor($ip, $page) + private function updateVisitor($ip, $page, $userAgent) { if($this->cacheEnabled) { - $this->data[$ip] = array('page' => $page, 'lastvisit' => time()); + $this->data[$ip] = array('page' => $page, 'lastvisit' => time(), 'user_agent' => $userAgent); return; } global $db; - $db->exec('UPDATE ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' SET ' . $db->fieldName('lastvisit') . ' = ' . time() . ', ' . $db->fieldName('page') . ' = ' . $db->quote($page) . ' WHERE ' . $db->fieldName('ip') . ' = ' . $db->quote($ip)); + $db->update(TABLE_PREFIX . 'visitors', ['lastvisit' => time(), 'page' => $page, 'user_agent' => $userAgent], ['ip' => $ip]); } - private function addVisitor($ip, $page) + private function addVisitor($ip, $page, $userAgent) { if($this->cacheEnabled) { - $this->data[$ip] = array('page' => $page, 'lastvisit' => time()); + $this->data[$ip] = array('page' => $page, 'lastvisit' => time(), 'user_agent' => $userAgent); return; } global $db; - $db->exec('INSERT INTO ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' (' . $db->fieldName('ip') . ' ,' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ') VALUE (' . $db->quote($ip) . ', ' . time() . ', ' . $db->quote($page) . ')'); + $db->insert(TABLE_PREFIX . 'visitors', ['ip' => $ip, 'lastvisit' => time(), 'page' => $page, 'user_agent' => $userAgent]); } public function getVisitors() @@ -107,7 +109,7 @@ class Visitors } global $db; - return $db->query('SELECT ' . $db->fieldName('ip') . ', ' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' ORDER BY ' . $db->fieldName('lastvisit') . ' DESC')->fetchAll(); + return $db->query('SELECT ' . $db->fieldName('ip') . ', ' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ', ' . $db->fieldName('user_agent') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' ORDER BY ' . $db->fieldName('lastvisit') . ' DESC')->fetchAll(); } public function getAmountVisitors() diff --git a/system/migrations/34.php b/system/migrations/34.php new file mode 100644 index 00000000..6f4e6b4a --- /dev/null +++ b/system/migrations/34.php @@ -0,0 +1,4 @@ +exec('ALTER TABLE `' . TABLE_PREFIX . "visitors` ADD `user_agent` VARCHAR(255) NOT NULL DEFAULT '';"); diff --git a/system/templates/admin.visitors.html.twig b/system/templates/admin.visitors.html.twig index 5c34469d..f71c3b31 100644 --- a/system/templates/admin.visitors.html.twig +++ b/system/templates/admin.visitors.html.twig @@ -9,6 +9,7 @@ IP Last visit Page + Browser @@ -17,6 +18,7 @@ {{ visitor.ip }} {{ visitor.lastvisit|date("H:i:s") }} {{ visitor.page|slice(0, 50) }} + {{ visitor.browser }} {% endfor %}