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

255 lines
8.4 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
2017-11-21 11:41:06 +01:00
protected $_iDeleteThreshold = 30; // Zeitdifferenz zwischen 2 L<>schauftr<74>gen
protected $_iExpTime = 600; // Ablaufzeit f<>r inaktive Benutzer
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();
2015-01-23 10:22:46 +01:00
$this->init('d3usersonline');
2011-10-19 22:05:36 +02:00
}
2013-09-03 12:33:05 +02:00
/**
* @param $iExpTime
*/
2017-11-21 11:41:06 +01:00
public function clearOldItems()
2011-10-19 22:05:36 +02:00
{
2013-09-03 12:33:05 +02:00
startProfile(__METHOD__);
$iTime = time();
$iLastDeleteTime = oxRegistry::getConfig()->getShopConfVar('iLastDeleteTime', null, 'd3usersonline');
if ($iTime > $iLastDeleteTime + $this->_iDeleteThreshold) {
2017-11-21 11:41:06 +01:00
$iExptime = $iTime - $this->_iExpTime;
oxDb::getDb()->Execute("delete from " . $this->getViewName() . " where timevisit < $iExptime");
oxRegistry::getConfig()->saveShopConfVar('int', 'iLastDeleteTime', $iTime, null, 'd3usersonline');
}
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
*/
2017-11-21 11:32:40 +01:00
public function getUserCount($blGroupByClass = false)
2011-10-19 22:05:36 +02:00
{
2013-09-03 12:33:05 +02:00
startProfile(__METHOD__);
2011-10-20 22:03:48 +02:00
2017-11-21 11:32:40 +01:00
if ($blGroupByClass) {
$sSelect = "SELECT count(oxid) AS counter, oxclass FROM " .
$this->getViewName() . " GROUP BY oxclass ORDER BY counter DESC";
} else {
$sSelect = "select count(oxid) AS counter, oxclass, oxpage from ".
$this->getViewName()." GROUP BY oxclass, oxpage ORDER BY counter desc";
}
$aRecords = oxDb::getDb(oxDb::FETCH_MODE_ASSOC)->getAll($sSelect);
2011-10-20 22:03:48 +02:00
$iAllCounter = 0;
$aUserClasses = array();
2015-01-23 10:22:46 +01:00
if ($aRecords && is_array($aRecords) && count($aRecords)) {
foreach ($aRecords as $aRecord) {
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'];
2017-11-21 11:02:32 +01:00
$oTmp->page = $aRecord['OXPAGE'];
2013-09-03 12:33:05 +02:00
$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(),
2017-11-21 11:02:32 +01:00
'oxclass' => oxRegistry::getConfig()->getActiveView()->getClassName(),
'oxpage' => $this->getPageIdent(),
2013-09-03 12:33:05 +02:00
);
$this->assign($aValues);
$this->save();
stopProfile(__METHOD__);
2011-10-19 22:05:36 +02:00
}
2017-11-21 11:02:32 +01:00
/**
* @return null|string
*/
public function getPageIdent()
{
switch (strtolower(oxRegistry::getConfig()->getActiveView()->getClassName()))
{
case 'details':
case 'oxwarticledetails':
if (($oView = oxRegistry::getConfig()->getActiveView())
&& method_exists($oView, 'getProduct')
) {
return $oView->getProduct()->getFieldData('oxtitle');
}
return null;
case 'alist':
case 'manufacturerlist':
case 'vendorlist':
if (($oView = oxRegistry::getConfig()->getActiveView())
&& method_exists($oView, 'getTitle')
) {
return $oView->getTitle();
}
return null;
case 'search':
return oxRegistry::getConfig()->getRequestParameter('searchparam');
case 'content':
if (($oView = oxRegistry::getConfig()->getActiveView())
&& method_exists($oView, 'getContent')
) {
return $oView->getContent()->getFieldData('oxtitle');
}
return null;
case 'tag':
if (($oView = oxRegistry::getConfig()->getActiveView())
&& method_exists($oView, 'getTag')
) {
return $oView->getTag();
}
return null;
}
return null;
}
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
2015-01-23 10:22:46 +01:00
if (!empty($this->_remoteAddr)) {
2013-09-03 12:33:05 +02:00
$sDirectIp = $this->_remoteAddr;
2015-01-23 10:22:46 +01:00
}
2013-09-03 12:33:05 +02:00
// Gets the proxy ip sent by the user
2015-01-23 10:22:46 +01:00
if (!empty($this->_httpXForwardedFor)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpXForwardedFor;
2015-01-23 10:22:46 +01:00
} elseif (!empty($this->_httpXForwarded)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpXForwarded;
2015-01-23 10:22:46 +01:00
} elseif (!empty($this->_httpForwardedFor)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpForwardedFor;
2015-01-23 10:22:46 +01:00
} elseif (!empty($this->_httpForwarded)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpForwarded;
2015-01-23 10:22:46 +01:00
} elseif (!empty($this->_httpVia)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpVia;
2015-01-23 10:22:46 +01:00
} elseif (!empty($this->_httpXComingFrom)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpXComingFrom;
2015-01-23 10:22:46 +01:00
} elseif (!empty($this->_httpComingFrom)) {
2013-09-03 12:33:05 +02:00
$sProxyIp = $this->_httpComingFrom;
2015-01-23 10:22:46 +01:00
}
2013-09-03 12:33:05 +02:00
// Returns the true IP if it has been found, else ...
2015-01-23 10:22:46 +01:00
if (empty($sProxyIp)) {
2013-09-03 12:33:05 +02:00
// True IP without proxy
return $sDirectIp;
2015-01-23 10:22:46 +01:00
} else {
2013-09-03 12:33:05 +02:00
$blIsIp = preg_match('@^([0-9]{1,3}.){3,3}[0-9]{1,3}@', $sProxyIp, $aMatches);
2015-01-23 10:22:46 +01:00
if ($blIsIp && (count($aMatches) > 0)) {
2013-09-03 12:33:05 +02:00
// True IP behind a proxy
return $aMatches[0];
2015-01-23 10:22:46 +01:00
} else {
if (empty($this->_httpClientIp)) {
2013-09-03 12:33:05 +02:00
// Can't define IP: there is a proxy but we don't have
// information about the true IP
return "(unbekannt) " . $sProxyIp;
2015-01-23 10:22:46 +01:00
} else {
2013-09-03 12:33:05 +02:00
// better than nothing
return $this->_httpClientIp;
}
}
}
}
/**
* @param $sTargetVarName
* @param $sDataName
*/
protected function _getIpData($sTargetVarName, $sDataName)
{
2015-01-23 10:22:46 +01:00
if (empty($this->{$sTargetVarName})) {
if (!empty($_SERVER) && isset($_SERVER[$sDataName])) {
2013-09-03 12:33:05 +02:00
$this->{$sTargetVarName} = $_SERVER[$sDataName];
2015-01-23 10:22:46 +01:00
} elseif (!empty($_ENV) && isset($_ENV[$sDataName])) {
2013-09-03 12:33:05 +02:00
$this->{$sTargetVarName} = $_ENV[$sDataName];
2015-01-23 10:22:46 +01:00
} elseif (@getenv($sDataName)) {
2013-09-03 12:33:05 +02:00
$this->{$sTargetVarName} = getenv($sDataName);
2015-01-23 10:22:46 +01:00
}
2013-09-03 12:33:05 +02:00
}
}
2011-10-19 22:05:36 +02:00
}