TYPO3 CMS  TYPO3_8-7
DatabaseSessionBackend.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
26 
34 {
38  protected $configuration = [];
39 
43  protected $hasAnonymousSessions = false;
44 
52  public function initialize(string $identifier, array $configuration)
53  {
54  $this->hasAnonymousSessions = (bool)($configuration['has_anonymous'] ?? false);
55  $this->configuration = $configuration;
56  }
57 
65  public function validateConfiguration(): bool
66  {
67  if (empty($this->configuration['table'])) {
68  throw new \InvalidArgumentException(
69  'The session backend "' . get_class($this) . '" needs a "table" configuration.',
70  1442996707
71  );
72  }
73  return true;
74  }
75 
83  public function get(string $sessionId): array
84  {
85  $query = $this->getQueryBuilder();
86 
87  $query->select('*')
88  ->from($this->configuration['table'])
89  ->where($query->expr()->eq('ses_id', $query->createNamedParameter($sessionId, \PDO::PARAM_STR)));
90 
91  $result = $query->execute()->fetch();
92 
93  if (!is_array($result)) {
94  throw new SessionNotFoundException(
95  'The session with identifier ' . $sessionId . ' was not found ',
96  1481885483
97  );
98  }
99  return $result;
100  }
101 
108  public function remove(string $sessionId): bool
109  {
110  $query = $this->getQueryBuilder();
111  $query->delete($this->configuration['table'])
112  ->where($query->expr()->eq('ses_id', $query->createNamedParameter($sessionId, \PDO::PARAM_STR)));
113 
114  return (bool)$query->execute();
115  }
116 
127  public function set(string $sessionId, array $sessionData): array
128  {
129  $sessionData['ses_id'] = $sessionId;
130  $sessionData['ses_tstamp'] = $GLOBALS['EXEC_TIME'] ?? time();
131 
132  try {
133  $this->getConnection()->insert(
134  $this->configuration['table'],
135  $sessionData,
136  ['ses_data' => \PDO::PARAM_LOB]
137  );
138  } catch (DBALException $e) {
139  throw new SessionNotCreatedException(
140  'Session could not be written to database: ' . $e->getMessage(),
141  1481895005,
142  $e
143  );
144  }
145 
146  return $sessionData;
147  }
148 
159  public function update(string $sessionId, array $sessionData): array
160  {
161  $sessionData['ses_id'] = $sessionId;
162  $sessionData['ses_tstamp'] = $GLOBALS['EXEC_TIME'] ?? time();
163 
164  try {
165  // allow 0 records to be affected, happens when no columns where changed
166  $this->getConnection()->update(
167  $this->configuration['table'],
168  $sessionData,
169  ['ses_id' => $sessionId],
170  ['ses_data' => \PDO::PARAM_LOB]
171  );
172  } catch (DBALException $e) {
173  throw new SessionNotUpdatedException(
174  'Session with id ' . $sessionId . ' could not be updated: ' . $e->getMessage(),
175  1481889220,
176  $e
177  );
178  }
179  return $sessionData;
180  }
181 
188  public function collectGarbage(int $maximumLifetime, int $maximumAnonymousLifetime = 0)
189  {
190  $query = $this->getQueryBuilder();
191 
192  $query->delete($this->configuration['table'])
193  ->where($query->expr()->lt('ses_tstamp', (int)($GLOBALS['EXEC_TIME'] - (int)$maximumLifetime)))
194  ->andWhere($this->hasAnonymousSessions ? $query->expr()->eq('ses_anonymous', 0) :' 1 = 1');
195  $query->execute();
196 
197  if ($maximumAnonymousLifetime > 0 && $this->hasAnonymousSessions) {
198  $query = $this->getQueryBuilder();
199  $query->delete($this->configuration['table'])
200  ->where($query->expr()->lt('ses_tstamp', (int)($GLOBALS['EXEC_TIME'] - (int)$maximumAnonymousLifetime)))
201  ->andWhere($query->expr()->eq('ses_anonymous', 1));
202  $query->execute();
203  }
204  }
205 
211  public function getAll(): array
212  {
213  $query = $this->getQueryBuilder();
214  $query->select('*')->from($this->configuration['table']);
215  return $query->execute()->fetchAll();
216  }
217 
221  protected function getQueryBuilder(): QueryBuilder
222  {
223  return $this->getConnection()->createQueryBuilder();
224  }
225 
229  protected function getConnection(): Connection
230  {
231  return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->configuration['table']);
232  }
233 }
static makeInstance($className,... $constructorArguments)
initialize(string $identifier, array $configuration)
collectGarbage(int $maximumLifetime, int $maximumAnonymousLifetime=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']