‪TYPO3CMS  11.5
UserSession.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
39 {
40  protected const ‪SESSION_UPDATE_GRACE_PERIOD = 61;
41  protected string ‪$identifier;
42  protected ?int ‪$userId;
43  protected int ‪$lastUpdated;
44  protected array ‪$data;
45  protected bool ‪$wasUpdated = false;
46  protected string ‪$ipLock = '';
47  protected bool ‪$isNew = true;
48  protected bool ‪$isPermanent = false;
49 
50  protected function ‪__construct(string ‪$identifier, int ‪$userId, int ‪$lastUpdated, array ‪$data = [])
51  {
52  $this->identifier = ‪$identifier;
53  $this->userId = ‪$userId > 0 ? ‪$userId : null;
54  $this->lastUpdated = ‪$lastUpdated;
55  $this->data = ‪$data;
56  }
57 
63  public function ‪getIdentifier(): string
64  {
65  return ‪$this->identifier;
66  }
67 
73  public function ‪getUserId(): ?int
74  {
75  return ‪$this->userId;
76  }
77 
83  public function ‪getLastUpdated(): int
84  {
85  return ‪$this->lastUpdated;
86  }
87 
95  public function set(string $key, $value): void
96  {
97  if ($key === '') {
98  throw new \InvalidArgumentException('Argument key must not be empty', 1484312516);
99  }
100  if ($value === null) {
101  unset($this->data[$key]);
102  } else {
103  $this->data[$key] = $value;
104  }
105  $this->wasUpdated = true;
106  }
107 
113  public function ‪hasData(): bool
114  {
115  return $this->data !== [];
116  }
117 
124  public function get(string $key)
125  {
126  return $this->data[$key] ?? null;
127  }
128 
134  public function ‪getData(): array
135  {
136  return ‪$this->data;
137  }
138 
145  public function ‪overrideData(array ‪$data): void
146  {
147  if ($this->data !== ‪$data) {
148  // Only set update flag if there is change in the $data array
149  $this->wasUpdated = true;
150  }
151 
152  $this->data = ‪$data;
153  }
154 
160  public function ‪dataWasUpdated(): bool
161  {
162  return ‪$this->wasUpdated;
163  }
164 
171  public function ‪isAnonymous(): bool
172  {
173  return $this->userId === 0 || $this->userId === null;
174  }
175 
181  public function ‪getIpLock(): string
182  {
183  return ‪$this->ipLock;
184  }
185 
191  public function ‪isNew(): bool
192  {
193  return ‪$this->isNew;
194  }
195 
201  public function ‪isPermanent(): bool
202  {
203  return ‪$this->isPermanent;
204  }
205 
211  public function ‪needsUpdate(): bool
212  {
213  return ‪$GLOBALS['EXEC_TIME'] > ($this->lastUpdated + ‪self::SESSION_UPDATE_GRACE_PERIOD);
214  }
215 
224  public static function ‪createFromRecord(string $id, array $record, bool $markAsNew = false): self
225  {
226  $userSession = new self(
227  $id,
228  (int)($record['ses_userid'] ?? 0),
229  (int)($record['ses_tstamp'] ?? 0),
230  unserialize($record['ses_data'] ?? '', ['allowed_classes' => false]) ?: []
231  );
232  $userSession->ipLock = $record['ses_iplock'] ?? '';
233  $userSession->isNew = $markAsNew;
234  if (isset($record['ses_permanent'])) {
235  $userSession->isPermanent = (bool)$record['ses_permanent'];
236  }
237  return $userSession;
238  }
239 
247  public static function ‪createNonFixated(string ‪$identifier): self
248  {
249  $userSession = new self(‪$identifier, 0, ‪$GLOBALS['EXEC_TIME'], []);
250  $userSession->isPermanent = false;
251  $userSession->isNew = true;
252  return $userSession;
253  }
254 
260  public function ‪toArray(): array
261  {
262  ‪$data = [
263  'ses_id' => ‪$this->identifier,
264  'ses_data' => serialize($this->data),
265  'ses_userid' => (int)$this->userId,
266  'ses_iplock' => $this->ipLock,
267  'ses_tstamp' => $this->lastUpdated,
268  ];
269  if ($this->‪isPermanent) {
270  $data['ses_permanent'] = 1;
271  }
272  return ‪$data;
273  }
274 }
‪TYPO3\CMS\Core\Session\UserSession\getIdentifier
‪string getIdentifier()
Definition: UserSession.php:63
‪TYPO3\CMS\Core\Session\UserSession\needsUpdate
‪bool needsUpdate()
Definition: UserSession.php:211
‪TYPO3\CMS\Core\Session\UserSession\SESSION_UPDATE_GRACE_PERIOD
‪const SESSION_UPDATE_GRACE_PERIOD
Definition: UserSession.php:40
‪TYPO3\CMS\Core\Session\UserSession\toArray
‪array toArray()
Definition: UserSession.php:260
‪TYPO3\CMS\Core\Session\UserSession\hasData
‪bool hasData()
Definition: UserSession.php:113
‪TYPO3\CMS\Core\Session\UserSession\$isNew
‪bool $isNew
Definition: UserSession.php:47
‪TYPO3\CMS\Core\Session\UserSession\createFromRecord
‪static UserSession createFromRecord(string $id, array $record, bool $markAsNew=false)
Definition: UserSession.php:224
‪TYPO3\CMS\Core\Session\UserSession\$wasUpdated
‪bool $wasUpdated
Definition: UserSession.php:45
‪TYPO3\CMS\Core\Session\UserSession
Definition: UserSession.php:39
‪TYPO3\CMS\Core\Session\UserSession\$isPermanent
‪bool $isPermanent
Definition: UserSession.php:48
‪TYPO3\CMS\Core\Session\UserSession\isNew
‪bool isNew()
Definition: UserSession.php:191
‪TYPO3\CMS\Core\Session
‪TYPO3\CMS\Core\Session\UserSession\$identifier
‪string $identifier
Definition: UserSession.php:41
‪TYPO3\CMS\Core\Session\UserSession\createNonFixated
‪static UserSession createNonFixated(string $identifier)
Definition: UserSession.php:247
‪TYPO3\CMS\Core\Session\UserSession\isPermanent
‪bool isPermanent()
Definition: UserSession.php:201
‪TYPO3\CMS\Core\Session\UserSession\$userId
‪int $userId
Definition: UserSession.php:42
‪TYPO3\CMS\Core\Session\UserSession\getIpLock
‪string getIpLock()
Definition: UserSession.php:181
‪TYPO3\CMS\Core\Session\UserSession\__construct
‪__construct(string $identifier, int $userId, int $lastUpdated, array $data=[])
Definition: UserSession.php:50
‪TYPO3\CMS\Core\Session\UserSession\getData
‪array getData()
Definition: UserSession.php:134
‪TYPO3\CMS\Core\Session\UserSession\$lastUpdated
‪int $lastUpdated
Definition: UserSession.php:43
‪TYPO3\CMS\Core\Session\UserSession\isAnonymous
‪bool isAnonymous()
Definition: UserSession.php:171
‪TYPO3\CMS\Core\Session\UserSession\$ipLock
‪string $ipLock
Definition: UserSession.php:46
‪TYPO3\CMS\Core\Session\UserSession\overrideData
‪overrideData(array $data)
Definition: UserSession.php:145
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Session\UserSession\dataWasUpdated
‪bool dataWasUpdated()
Definition: UserSession.php:160
‪TYPO3\CMS\Core\Session\UserSession\getLastUpdated
‪int getLastUpdated()
Definition: UserSession.php:83
‪TYPO3\CMS\Core\Session\UserSession\getUserId
‪int getUserId()
Definition: UserSession.php:73
‪TYPO3\CMS\Core\Session\UserSession\$data
‪array $data
Definition: UserSession.php:44