myaac/system/libs/visitors.php
slawkens 39fee6e57d * menus are now showed by the order they're saved into menu_categories configurable
* fixed visitors in admin panel showing, when cache is disabled
2017-11-06 08:50:06 +01:00

129 lines
3.3 KiB
PHP

<?php
/**
* Visitors class
*
* @package MyAAC
* @author Slawkens <slawkens@gmail.com>
* @copyright 2017 MyAAC
* @version 0.6.6
* @link http://my-aac.org
*/
defined('MYAAC') or die('Direct access not allowed!');
class Visitors
{
private $sessionTime; // time session will live
private $data; // cached data
private $cacheEnabled;
public function __construct($sessionTime = 10)
{
global $cache;
$this->cacheEnabled = ($cache && $cache->enabled());
if($this->cacheEnabled)
{
$tmp = '';
if($cache->fetch('visitors', $tmp))
$this->data = unserialize($tmp);
else
$this->data = array();
}
$this->sessionTime = $sessionTime;
$this->cleanVisitors();
$ip = $_SERVER['REMOTE_ADDR'];
if($this->visitorExists($ip))
$this->updateVisitor($ip, $_SERVER['REQUEST_URI']);
else
$this->addVisitor($ip, $_SERVER['REQUEST_URI']);
}
public function __destruct()
{
global $cache;
if($this->cacheEnabled)
$cache->set('visitors', serialize($this->data), 120);
}
public function visitorExists($ip)
{
if($this->cacheEnabled) {
return isset($this->data[$ip]);
}
global $db;
$users = $db->query('SELECT COUNT(`ip`) as count FROM `' . TABLE_PREFIX . 'visitors' . '` WHERE ' . $db->fieldName('ip') . ' = ' . $db->quote($ip))->fetch();
return ($users['count'] > 0);
}
private function cleanVisitors()
{
if($this->cacheEnabled)
{
$timeNow = time();
foreach($this->data as $ip => $details)
{
if($timeNow - (int)$details['lastvisit'] > $this->sessionTime * 60)
unset($this->data[$ip]);
}
return;
}
global $db;
$db->query('DELETE FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' WHERE ' . $db->fieldName('lastvisit') . ' < ' . (time() - $this->sessionTime * 60));
}
private function updateVisitor($ip, $page)
{
if($this->cacheEnabled) {
$this->data[$ip] = array('page' => $page, 'lastvisit' => time());
return;
}
global $db;
$db->query('UPDATE ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' SET ' . $db->fieldName('lastvisit') . ' = ' . time() . ', ' . $db->fieldName('page') . ' = ' . $db->quote($page) . ' WHERE ' . $db->fieldName('ip') . ' = ' . $db->quote($ip));
}
private function addVisitor($ip, $page)
{
if($this->cacheEnabled) {
$this->data[$ip] = array('page' => $page, 'lastvisit' => time());
return;
}
global $db;
$db->query('INSERT INTO ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' (' . $db->fieldName('ip') . ' ,' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ') VALUE (' . $db->quote($ip) . ', ' . time() . ', ' . $db->quote($page) . ')');
}
public function getVisitors()
{
if($this->cacheEnabled) {
foreach($this->data as $ip => &$details)
$details['ip'] = $ip;
return $this->data;
}
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();
}
public function getAmountVisitors()
{
if($this->cacheEnabled) {
return count($this->data);
}
global $db;
$users = $db->query('SELECT COUNT(`ip`) as count FROM `' . TABLE_PREFIX . 'visitors`')->fetch();
return $users['count'];
}
public function show() {
echo $this->getAmountVisitors();
}
}
?>