- md5
- 5bc0c8d8232dcb045c9f89ad261e278a
- sha1
- bc7f9d1ff27a10016ad7f42dd8043ad91a827e77
- sha256
- 517d148db78145c0aba34ee6cc5972d97a8b230ddeb67e8c7b94449a0dd77533
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Session;
defined('JPATH_PLATFORM') or die;
use Joomla\Application\AbstractApplication;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\User\User;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Database\ParameterType;
/**
* Manager for optional session metadata.
*
* @since 3.8.6
* @internal
*/
final class MetadataManager
{
/**
* Application object.
*
* @var AbstractApplication
* @since 3.8.6
*/
private $app;
/**
* Database driver.
*
* @var DatabaseInterface
* @since 3.8.6
*/
private $db;
/**
* MetadataManager constructor.
*
* @param AbstractApplication $app Application object.
* @param DatabaseInterface $db Database driver.
*
* @since 3.8.6
*/
public function __construct(AbstractApplication $app, DatabaseInterface $db)
{
$this->app = $app;
$this->db = $db;
}
/**
* Create the metadata record if it does not exist.
*
* @param Session $session The session to create the metadata record for.
* @param User $user The user to associate with the record.
*
* @return void
*
* @since 3.8.6
* @throws \RuntimeException
*/
public function createRecordIfNonExisting(Session $session, User $user)
{
$sessionId = $session->getId();
$query = $this->db->getQuery(true)
->select($this->db->quoteName('session_id'))
->from($this->db->quoteName('#__session'))
->where($this->db->quoteName('session_id') . ' = :session_id')
->bind(':session_id', $sessionId)
->setLimit(1);
$this->db->setQuery($query);
try
{
$exists = $this->db->loadResult();
}
catch (ExecutionFailureException $e)
{
return;
}
// If the session record doesn't exist initialise it.
if ($exists)
{
return;
}
$query->clear();
$time = $session->isNew() ? time() : $session->get('session.timer.start');
$columns = [
$this->db->quoteName('session_id'),
$this->db->quoteName('guest'),
$this->db->quoteName('time'),
$this->db->quoteName('userid'),
$this->db->quoteName('username'),
];
// Add query placeholders
$values = [
':session_id',
':guest',
':time',
':user_id',
':username',
];
// Bind query values
$userIsGuest = $user->guest;
$userId = $user->id;
$username = $user->username;
$query->bind(':session_id', $sessionId)
->bind(':guest', $userIsGuest, ParameterType::BOOLEAN)
->bind(':time', $time)
->bind(':user_id', $userId, ParameterType::INTEGER)
->bind(':username', $username);
if ($this->app instanceof CMSApplication && !$this->app->get('shared_session', false))
{
$clientId = $this->app->getClientId();
$columns[] = $this->db->quoteName('client_id');
$values[] = ':client_id';
$query->bind(':client_id', $clientId, ParameterType::INTEGER);
}
$query->insert($this->db->quoteName('#__session'))
->columns($columns)
->values(implode(', ', $values));
$this->db->setQuery($query);
try
{
$this->db->execute();
}
catch (ExecutionFailureException $e)
{
// This failure isn't critical, we can go on without the metadata
}
}
/**
* Delete records with a timestamp prior to the given time.
*
* @param integer $time The time records should be deleted if expired before.
*
* @return void
*
* @since 3.8.6
*/
public function deletePriorTo($time)
{
$query = $this->db->getQuery(true)
->delete($this->db->quoteName('#__session'))
->where($this->db->quoteName('time') . ' < :time')
->bind(':time', $time, ParameterType::INTEGER);
$this->db->setQuery($query);
try
{
$this->db->execute();
}
catch (ExecutionFailureException $exception)
{
// Since garbage collection does not result in a fatal error when run in the session API, we don't allow it here either.
}
}
}
The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.