1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
abstractsessionhandler.php
См. документацию.
1<?php
2
4
8
9abstract class AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface, \SessionIdInterface
10{
11 public const LOCK_ERROR_MESSAGE = 'Unable to get session lock within 60 seconds.';
12
14 protected $readOnly = false;
16 protected $sessionId;
18 private $prefetchId;
20 private $prefetchData;
22 private $lastCreatedId;
24 private $listValidatedIds = [];
26 private $releaseLockAfterClose = true;
27
28 #[\ReturnTypeWillChange]
29 public function read($sessionId)
30 {
31 if (!$this->validateSessionId($sessionId))
32 {
33 return '';
34 }
35
36 $this->sessionId = $sessionId;
37 if ($this->prefetchId !== null)
38 {
39 $prefetchId = $this->prefetchId;
40 $prefetchData = $this->prefetchData;
41
42 $this->prefetchId = null;
43 $this->prefetchData = null;
44
45 if ($prefetchId === $this->sessionId)
46 {
47 return $prefetchData;
48 }
49 }
50
51 if (!$this->readOnly && !$this->lock($this->sessionId))
52 {
53 $this->triggerLockFatalError();
54 }
55
56 return $this->processRead($sessionId);
57 }
58
59 abstract protected function processRead($sessionId): string;
60
61 protected function triggerLockFatalError(string $additionalText = ''): void
62 {
63 $text = self::LOCK_ERROR_MESSAGE;
64 if ($additionalText)
65 {
66 $text .= $additionalText;
67 }
68
69 $httpResponse = new HttpResponse();
70 $httpResponse->setStatus('500 Internal Server Error');
71 trigger_error($text, E_USER_ERROR);
72 Application::getInstance()->end(0, $httpResponse);
73 }
74
75 public function write($sessionId, $sessionData): bool
76 {
77 if (!$this->validateSessionId($sessionId))
78 {
79 return false;
80 }
81
82 if ($this->readOnly)
83 {
84 return true;
85 }
86
87 return $this->processWrite($sessionId, $sessionData);
88 }
89
90 abstract protected function processWrite($sessionId, $sessionData): bool;
91
92 abstract protected function lock($sessionId): bool;
93
94 abstract protected function unlock($sessionId): bool;
95
96 private function releaseLocksAfterValidate(): void
97 {
98 unset($this->listValidatedIds[$this->sessionId]);
99 foreach ($this->listValidatedIds as $mustBeUnlockedId => $true)
100 {
101 $this->unlock($mustBeUnlockedId);
102 unset($this->listValidatedIds[$this->sessionId]);
103 }
104 }
105
106 public function close(): bool
107 {
108 if (!$this->readOnly && $this->validateSessionId($this->sessionId))
109 {
110 if (isSessionExpired())
111 {
112 $this->destroy($this->sessionId);
113 }
114
115 if ($this->releaseLockAfterClose)
116 {
117 $this->unlock($this->sessionId);
118 }
119
120 $this->releaseLocksAfterValidate();
121 }
122
123 $this->sessionId = null;
124 $this->lastCreatedId = null;
125
126 return true;
127 }
128
129 public function destroy($sessionId): bool
130 {
131 if ($this->readOnly)
132 {
133 return true;
134 }
135
136 if (!$this->validateSessionId($sessionId))
137 {
138 return false;
139 }
140
142 $this->lastCreatedId = null;
143
144 return $result;
145 }
146
147 abstract protected function processDestroy($sessionId): bool;
148
149 public function validateId($sessionId): bool
150 {
151 $this->listValidatedIds[$sessionId] = true;
152
153 $this->prefetchData = $this->read($sessionId);
154 $this->prefetchId = $sessionId;
155
156 return $this->prefetchData !== '';
157 }
158
159 public function create_sid(): string
160 {
161 $this->lastCreatedId = Random::getString(32, true);
162
163 return $this->lastCreatedId;
164 }
165
166 protected function validateSessionId($sessionId): bool
167 {
168 return
169 $sessionId &&
170 is_string($sessionId) &&
171 preg_match('/^[\da-z\-,]{6,}$/iD', $sessionId)
172 ;
173 }
174
176 {
177 $this->releaseLockAfterClose = false;
178 }
179
181 {
182 $this->releaseLockAfterClose = true;
183 }
184}
static getInstance()
Определения application.php:98
triggerLockFatalError(string $additionalText='')
Определения abstractsessionhandler.php:61
$result
Определения get_property_values.php:14
isSessionExpired()
Определения tools.php:5138
$text
Определения template_pdf.php:79