mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-26 17:29:21 +02:00
feature: visitors counter shows now user browser, and also if its bot
This commit is contained in:
parent
6785ecad1d
commit
a98cb66c53
@ -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
|
||||
|
@ -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'));
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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()
|
||||
|
4
system/migrations/34.php
Normal file
4
system/migrations/34.php
Normal 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 '';");
|
@ -9,6 +9,7 @@
|
||||
<th>IP</th>
|
||||
<th>Last visit</th>
|
||||
<th>Page</th>
|
||||
<th>Browser</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -17,6 +18,7 @@
|
||||
<td>{{ visitor.ip }}</td>
|
||||
<td>{{ visitor.lastvisit|date("H:i:s") }}</td>
|
||||
<td><a href="{{ visitor.page }}">{{ visitor.page|slice(0, 50) }}</a></td>
|
||||
<td>{{ visitor.browser }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
Loading…
x
Reference in New Issue
Block a user