UserOnline/copy_this/modules/d3/d3usersonline/models/d3usersonline.php

201 lines
6.1 KiB
PHP
Raw Normal View History

2011-10-19 22:05:36 +02:00
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
2013-09-03 12:33:05 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
2011-10-19 22:05:36 +02:00
*/
2013-09-03 12:33:05 +02:00
class d3usersonline extends oxbase
2011-10-19 22:05:36 +02:00
{
2011-10-19 22:53:23 +02:00
protected $_sCoreTbl = 'd3usersonline';
2013-09-03 12:33:05 +02:00
protected $_sClassName = 'd3usersonline';
protected $_remoteAddr = null;
protected $_httpClientIp = null;
protected $_httpXForwardedFor = null;
protected $_httpXForwarded = null;
protected $_httpForwardedFor = null;
protected $_httpForwarded = null;
protected $_httpVia = null;
protected $_httpXComingFrom = null;
protected $_httpComingFrom = null;
2011-10-19 22:05:36 +02:00
/**
2013-09-03 12:33:05 +02:00
* constructor
2011-10-19 22:05:36 +02:00
*/
2013-09-03 12:33:05 +02:00
public function __construct()
2011-10-19 22:05:36 +02:00
{
parent::__construct();
2011-10-19 22:53:23 +02:00
$this->init( 'd3usersonline' );
2011-10-19 22:05:36 +02:00
}
2013-09-03 12:33:05 +02:00
/**
* @param $iExpTime
*/
2011-10-19 22:05:36 +02:00
public function clearOldItems($iExpTime)
{
2013-09-03 12:33:05 +02:00
startProfile(__METHOD__);
2011-10-19 22:05:36 +02:00
$exptime = time() - $iExpTime;
2011-10-19 22:53:23 +02:00
oxDb::getDb()->Execute("delete from ".$this->getViewName()." where timevisit < $exptime");
2011-10-19 22:05:36 +02:00
2013-09-03 12:33:05 +02:00
stopProfile(__METHOD__);
2011-10-19 22:05:36 +02:00
}
2013-09-03 12:33:05 +02:00
/**
* @return array
*/
2011-10-19 22:05:36 +02:00
public function getUserCount()
{
2013-09-03 12:33:05 +02:00
startProfile(__METHOD__);
2011-10-20 22:03:48 +02:00
2013-09-03 12:33:05 +02:00
$sSelect = "select count(oxid) AS counter, oxclass from ".$this->getViewName()." GROUP BY oxclass ORDER BY counter desc";
$aRecords = oxDb::getDb(oxDb::FETCH_MODE_ASSOC)->getArray($sSelect);
2011-10-20 22:03:48 +02:00
$iAllCounter = 0;
$aUserClasses = array();
2013-09-03 12:33:05 +02:00
if ($aRecords && is_array($aRecords) && count($aRecords))
2011-10-20 22:03:48 +02:00
{
2013-09-03 12:33:05 +02:00
foreach ($aRecords as $aRecord)
2011-10-20 22:03:48 +02:00
{
2013-09-03 12:33:05 +02:00
$aRecord = array_change_key_case($aRecord, CASE_UPPER);
2011-10-20 22:03:48 +02:00
$oTmp = new stdClass;
2013-09-03 12:33:05 +02:00
$oTmp->classname = $aRecord['OXCLASS'];
$oTmp->counter = $aRecord['COUNTER'];
$iAllCounter += $aRecord['COUNTER'];
2011-10-20 22:03:48 +02:00
$aUserClasses['classes'][] = $oTmp;
}
}
$aUserClasses['all'] = $iAllCounter;
2013-09-03 12:33:05 +02:00
stopProfile(__METHOD__);
2011-10-20 22:03:48 +02:00
return $aUserClasses;
2011-10-19 22:05:36 +02:00
}
2013-09-03 12:33:05 +02:00
public function setActTimeVisit()
2011-10-19 22:05:36 +02:00
{
2013-09-03 12:33:05 +02:00
startProfile(__METHOD__);
$this->setId($this->_getIPHash());
$aValues = array(
'timevisit' => time(),
'oxclass' => oxRegistry::getConfig()->getActiveView()->getClassName()
);
$this->assign($aValues);
$this->save();
stopProfile(__METHOD__);
2011-10-19 22:05:36 +02:00
}
2013-09-03 12:33:05 +02:00
/**
* @return string
*/
protected function _getIPHash()
2011-10-19 22:05:36 +02:00
{
2013-09-03 12:33:05 +02:00
return md5($this->_getTrueIP());
2011-10-19 22:05:36 +02:00
}
2013-09-03 12:33:05 +02:00
/**
* @return null|string
*/
protected function _getTrueIP()
{
$sDirectIp = '';
$this->_getIpData('_remoteAddr', 'REMOTE_ADDR');
$this->_getIpData('_httpClientIp', 'HTTP_CLIENT_IP');
$this->_getIpData('_httpXForwardedFor', 'HTTP_X_FORWARDED_FOR');
$this->_getIpData('_httpXForwarded', 'HTTP_X_FORWARDED');
$this->_getIpData('_httpForwardedFor', 'HTTP_FORWARDED_FOR');
$this->_getIpData('_httpForwarded', 'HTTP_FORWARDED');
$this->_getIpData('_httpVia', 'HTTP_VIA');
$this->_getIpData('_httpXComingFrom', 'HTTP_X_COMING_FROM');
$this->_getIpData('_httpComingFrom', 'HTTP_COMING_FROM');
// Gets the default ip sent by the user
if (!empty($this->_remoteAddr))
$sDirectIp = $this->_remoteAddr;
// Gets the proxy ip sent by the user
if (!empty($this->_httpXForwardedFor))
$sProxyIp = $this->_httpXForwardedFor;
else if (!empty($this->_httpXForwarded))
$sProxyIp = $this->_httpXForwarded;
else if (!empty($this->_httpForwardedFor))
$sProxyIp = $this->_httpForwardedFor;
else if (!empty($this->_httpForwarded))
$sProxyIp = $this->_httpForwarded;
else if (!empty($this->_httpVia))
$sProxyIp = $this->_httpVia;
else if (!empty($this->_httpXComingFrom))
$sProxyIp = $this->_httpXComingFrom;
else if (!empty($this->_httpComingFrom))
$sProxyIp = $this->_httpComingFrom;
// Returns the true IP if it has been found, else ...
if (empty($sProxyIp))
{
// True IP without proxy
return $sDirectIp;
}
else
{
$blIsIp = preg_match('@^([0-9]{1,3}.){3,3}[0-9]{1,3}@', $sProxyIp, $aMatches);
if ($blIsIp && (count($aMatches) > 0))
{
// True IP behind a proxy
return $aMatches[0];
}
else
{
if (empty($this->_httpClientIp))
{
// Can't define IP: there is a proxy but we don't have
// information about the true IP
return "(unbekannt) " . $sProxyIp;
}
else
{
// better than nothing
return $this->_httpClientIp;
}
}
}
}
/**
* @param $sTargetVarName
* @param $sDataName
*/
protected function _getIpData($sTargetVarName, $sDataName)
{
if (empty($this->{$sTargetVarName}))
{
if (!empty($_SERVER) && isset($_SERVER[$sDataName]))
$this->{$sTargetVarName} = $_SERVER[$sDataName];
else if (!empty($_ENV) && isset($_ENV[$sDataName]))
$this->{$sTargetVarName} = $_ENV[$sDataName];
else if (@getenv($sDataName))
$this->{$sTargetVarName} = getenv($sDataName);
}
}
2011-10-19 22:05:36 +02:00
}