<?php
class DBTech_Security_XenForo_Session extends XFCP_DBTech_Security_XenForo_Session
{
/**
* Constructor.
*
* @param array $config Config elements to override default.
* @param Zend_Cache_Core|null $cache
* @param Zend_Db_Adapter_Abstract|null $db
*/
public function __construct(array $config = array(), Zend_Cache_Core $cache = null, Zend_Db_Adapter_Abstract $db = null)
{
// Bubble up the tree
$previous = parent::__construct($config, $cache, $db);
// Shorthand
$xenOptions = XenForo_Application::getOptions();
if (
$xenOptions->dbtech_security_active
AND $xenOptions->dbtech_security_spiders
)
{
$spiders = include 'src/addons/DBTech/Security/3rdParty/spiders.php';
foreach ($spiders as $spider)
{
// Normalisation
$spider['ident'] = strtolower($spider['ident']);
if (isset($this->_knownRobots[$spider['ident']]))
{
// Skip this spider
continue;
}
// Set the "known robots"
$this->_knownRobots[$spider['ident']] = $spider['ident'];
// Set the extended info
$this->_robotMap[$spider['ident']] = [
'title' => $spider['name'],
'link' => isset($spider['info']) ? $spider['info'] : ''
];
}
}
return $previous;
}
/**
* Sets up the session.
*
* @param string $sessionId Session ID to look up, if one exists
* @param string|false $ipAddress IP address in binary format or false, for access limiting.
* @param array|null $defaultSession If no session can be found, uses this as the default session value
*/
protected function _setup($sessionId = '', $ipAddress = false, array $defaultSession = null)
{
$sessionId = strval($sessionId);
if ($sessionId)
{
/** @var DBTech_Security_XenForo_Model_Security $securityModel */
$securityModel = XenForo_Model::create('DBTech_Security_XenForo_Model_Security');
// Remove old sessions
$securityModel->deleteOldSessions();
// Get cookie
$_sessionId = $securityModel->getSessionCookie();
if ($_sessionId)
{
// Check to see if we can get our session
if (!$securityModel->getSessionBySessionId($_sessionId))
{
// Delete session cookie
$securityModel->deleteSessionCookie();
// Create a new session for us
return parent::_setup('', $ipAddress, $defaultSession);
}
}
}
return parent::_setup($sessionId, $ipAddress, $defaultSession);
}
/**
* Indicates a login as a user and sets up a password date in the session
* for an extra layer of security (invalidates the session when the password changes).
*
* @param integer $userId
* @param integer $passwordDate
*/
public function userLogin($userId, $passwordDate)
{
$previous = parent::userLogin($userId, $passwordDate);
/** @var DBTech_Security_XenForo_Model_Security $securityModel */
$securityModel = XenForo_Model::create('DBTech_Security_XenForo_Model_Security');
// Remove old sessions
$securityModel->deleteOldSessions();
// Get cookie
$sessionId = $securityModel->getSessionCookie();
if (!$sessionId)
{
// Create a new session
$sessionId = $securityModel->createSession($userId);
// Set our cookie
$securityModel->setSessionCookie($sessionId);
// And do what we did before
return $previous;
}
else
{
// Get the session if we have it
$session = $securityModel->getSessionBySessionId($sessionId);
if ($session)
{
// The session was real, this user should be logged in
$securityModel->updateSessionLastActive($sessionId);
// Set our cookie
$securityModel->setSessionCookie($sessionId);
// And do what we did before
return $previous;
}
else
{
// Create a new session
$sessionId = $securityModel->createSession($userId);
// Set our cookie
$securityModel->setSessionCookie($sessionId);
// And do what we did before
return $previous;
}
}
return $previous;
}
public function saveSessionToSource($sessionId, $isUpdate)
{
/** @var DBTech_Security_XenForo_Model_Security $securityModel */
$securityModel = XenForo_Model::create('DBTech_Security_XenForo_Model_Security');
// Remove old sessions
$securityModel->deleteOldSessions();
// Get cookie
$_sessionId = $securityModel->getSessionCookie();
if ($_sessionId)
{
// Check to see if we can get our session
$securityModel->updateSessionLastActive($_sessionId);
}
return parent::saveSessionToSource($sessionId, $isUpdate);
}
}