feature: visitors counter shows now user browser, and also if its bot

This commit is contained in:
slawkens 2023-02-17 18:41:25 +01:00
parent 6785ecad1d
commit a98cb66c53
7 changed files with 49 additions and 12 deletions

View File

@ -8,6 +8,11 @@
* @link https://my-aac.org * @link https://my-aac.org
*/ */
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\OperatingSystem;
$title = 'Visitors'; $title = 'Visitors';
$use_datatable = true; $use_datatable = true;
@ -30,6 +35,28 @@ function compare($a, $b)
$tmp = $visitors->getVisitors(); $tmp = $visitors->getVisitors();
usort($tmp, 'compare'); 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( $twig->display('admin.visitors.html.twig', array(
'config_visitors_counter_ttl' => $config['visitors_counter_ttl'], 'config_visitors_counter_ttl' => $config['visitors_counter_ttl'],
'visitors' => $tmp 'visitors' => $tmp

View File

@ -27,7 +27,7 @@ if (version_compare(phpversion(), '7.2.5', '<')) die('PHP version 7.2.5 or highe
const MYAAC = true; const MYAAC = true;
const MYAAC_VERSION = '0.9.0-dev'; const MYAAC_VERSION = '0.9.0-dev';
const DATABASE_VERSION = 33; const DATABASE_VERSION = 34;
const TABLE_PREFIX = 'myaac_'; const TABLE_PREFIX = 'myaac_';
define('START_TIME', microtime(true)); define('START_TIME', microtime(true));
define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX')); define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX'));

View File

@ -10,6 +10,7 @@
"composer/semver": "^3.2", "composer/semver": "^3.2",
"twig/twig": "^2.0", "twig/twig": "^2.0",
"erusev/parsedown": "^1.7", "erusev/parsedown": "^1.7",
"nikic/fast-route": "^1.3" "nikic/fast-route": "^1.3",
"matomo/device-detector": "^6.0"
} }
} }

View File

@ -1,4 +1,4 @@
SET @myaac_database_version = 33; SET @myaac_database_version = 34;
CREATE TABLE `myaac_account_actions` CREATE TABLE `myaac_account_actions`
( (
@ -330,6 +330,7 @@ CREATE TABLE `myaac_visitors`
`ip` VARCHAR(45) NOT NULL, `ip` VARCHAR(45) NOT NULL,
`lastvisit` INT(11) NOT NULL DEFAULT 0, `lastvisit` INT(11) NOT NULL DEFAULT 0,
`page` VARCHAR(2048) NOT NULL, `page` VARCHAR(2048) NOT NULL,
`user_agent` VARCHAR(255) NOT NULL DEFAULT '',
UNIQUE (`ip`) UNIQUE (`ip`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

View File

@ -34,10 +34,12 @@ class Visitors
$this->cleanVisitors(); $this->cleanVisitors();
$ip = $_SERVER['REMOTE_ADDR']; $ip = $_SERVER['REMOTE_ADDR'];
$userAgentShortened = substr($_SERVER['HTTP_USER_AGENT'] ?? 'unknown', 0, 255);
if($this->visitorExists($ip)) if($this->visitorExists($ip))
$this->updateVisitor($ip, $_SERVER['REQUEST_URI']); $this->updateVisitor($ip, $_SERVER['REQUEST_URI'], $userAgentShortened);
else else
$this->addVisitor($ip, $_SERVER['REQUEST_URI']); $this->addVisitor($ip, $_SERVER['REQUEST_URI'], $userAgentShortened);
} }
public function __destruct() 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)); $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) { if($this->cacheEnabled) {
$this->data[$ip] = array('page' => $page, 'lastvisit' => time()); $this->data[$ip] = array('page' => $page, 'lastvisit' => time(), 'user_agent' => $userAgent);
return; return;
} }
global $db; 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) { if($this->cacheEnabled) {
$this->data[$ip] = array('page' => $page, 'lastvisit' => time()); $this->data[$ip] = array('page' => $page, 'lastvisit' => time(), 'user_agent' => $userAgent);
return; return;
} }
global $db; 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() public function getVisitors()
@ -107,7 +109,7 @@ class Visitors
} }
global $db; 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() public function getAmountVisitors()

4
system/migrations/34.php Normal file
View File

@ -0,0 +1,4 @@
<?php
// add user_agent column into visitors
$db->exec('ALTER TABLE `' . TABLE_PREFIX . "visitors` ADD `user_agent` VARCHAR(255) NOT NULL DEFAULT '';");

View File

@ -9,6 +9,7 @@
<th>IP</th> <th>IP</th>
<th>Last visit</th> <th>Last visit</th>
<th>Page</th> <th>Page</th>
<th>Browser</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -17,6 +18,7 @@
<td>{{ visitor.ip }}</td> <td>{{ visitor.ip }}</td>
<td>{{ visitor.lastvisit|date("H:i:s") }}</td> <td>{{ visitor.lastvisit|date("H:i:s") }}</td>
<td><a href="{{ visitor.page }}">{{ visitor.page|slice(0, 50) }}</a></td> <td><a href="{{ visitor.page }}">{{ visitor.page|slice(0, 50) }}</a></td>
<td>{{ visitor.browser }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>