2 declare(strict_types = 1);
18 use Doctrine\DBAL\DBALException;
54 $this->hasAnonymousSessions = (bool)(
$configuration[
'has_anonymous'] ??
false);
67 if (empty($this->configuration[
'table'])) {
68 throw new \InvalidArgumentException(
69 'The session backend "' . static::class .
'" needs a "table" configuration.',
76 public function hash(
string $sessionId): string
79 $key = sha1(
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey'] .
'core-session-backend');
81 return hash_hmac(
'md5', $sessionId, $key);
91 public function get(
string $sessionId): array
96 ->from($this->configuration[
'table'])
97 ->where($query->expr()->eq(
'ses_id', $query->createNamedParameter($this->hash($sessionId), \PDO::PARAM_STR)));
99 $result = $query->execute()->fetch();
101 if (!is_array($result)) {
105 $result = $query->select(
'*')
106 ->from($this->configuration[
'table'])
107 ->where($query->expr()->eq(
'ses_id', $query->createNamedParameter($sessionId, \PDO::PARAM_STR)))
110 if (!is_array($result)) {
111 throw new SessionNotFoundException(
112 'The session with identifier ' . $sessionId .
' was not found ',
126 public function remove(
string $sessionId):
bool
129 $query->delete($this->configuration[
'table'])
132 $query->expr()->eq(
'ses_id', $query->createNamedParameter($this->hash($sessionId), \PDO::PARAM_STR)),
133 $query->expr()->eq(
'ses_id', $query->createNamedParameter($sessionId, \PDO::PARAM_STR))
137 return (
bool)$query->execute();
150 public function set(
string $sessionId, array $sessionData): array
152 $sessionId = $this->
hash($sessionId);
153 $sessionData[
'ses_id'] = $sessionId;
154 $sessionData[
'ses_tstamp'] =
$GLOBALS[
'EXEC_TIME'] ?? time();
158 $this->configuration[
'table'],
160 [
'ses_data' => \PDO::PARAM_LOB]
162 }
catch (DBALException $e) {
163 throw new SessionNotCreatedException(
164 'Session could not be written to database: ' . $e->getMessage(),
183 public function update(
string $sessionId, array $sessionData): array
185 $hashedSessionId = $this->
hash($sessionId);
186 $sessionData[
'ses_id'] = $hashedSessionId;
187 $sessionData[
'ses_tstamp'] =
$GLOBALS[
'EXEC_TIME'] ?? time();
192 $this->configuration[
'table'],
194 [
'ses_id' => $hashedSessionId],
195 [
'ses_data' => \PDO::PARAM_LOB]
199 $this->configuration[
'table'],
201 [
'ses_id' => $sessionId],
202 [
'ses_data' => \PDO::PARAM_LOB]
204 }
catch (DBALException $e) {
205 throw new SessionNotUpdatedException(
206 'Session with id ' . $sessionId .
' could not be updated: ' . $e->getMessage(),
220 public function collectGarbage(
int $maximumLifetime,
int $maximumAnonymousLifetime = 0)
224 $query->delete($this->configuration[
'table'])
225 ->where($query->expr()->lt(
'ses_tstamp', (
int)(
$GLOBALS[
'EXEC_TIME'] - (
int)$maximumLifetime)))
226 ->andWhere($this->hasAnonymousSessions ? $query->expr()->eq(
'ses_anonymous', 0) :
' 1 = 1');
229 if ($maximumAnonymousLifetime > 0 && $this->hasAnonymousSessions) {
231 $query->delete($this->configuration[
'table'])
232 ->where($query->expr()->lt(
'ses_tstamp', (
int)(
$GLOBALS[
'EXEC_TIME'] - (
int)$maximumAnonymousLifetime)))
233 ->andWhere($query->expr()->eq(
'ses_anonymous', 1));
243 public function getAll(): array
246 $query->select(
'*')->from($this->configuration[
'table']);
247 return $query->execute()->fetchAll();
263 return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->configuration[
'table']);