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