1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
sessionconfigurationresolver.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Main\Session;
4
5
use Bitrix\Main\ArgumentException;
6
use Bitrix\Main\Config\Configuration;
7
use Bitrix\Main\NotSupportedException;
8
use Bitrix\Main\Session\Handlers\AbstractSessionHandler;
9
use Bitrix\Main\Session\Handlers\StrictSessionHandler;
10
11
final
class
SessionConfigurationResolver
12
{
13
private
const
MODE_DEFAULT =
'default'
;
14
private
const
MODE_SEPARATED =
'separated'
;
15
16
public
const
TYPE_FILE
=
'file'
;
17
public
const
TYPE_DATABASE
=
'database'
;
18
public
const
TYPE_REDIS
=
'redis'
;
19
public
const
TYPE_MEMCACHE
=
'memcache'
;
20
public
const
TYPE_ARRAY
=
'array'
;
21
public
const
TYPE_NULL
=
'null'
;
22
public
const
TYPE_CUSTOM_INI
=
'save_handler.php.ini'
;
23
25
private
$configuration;
27
private
$session;
29
private
$kernelSession;
30
31
public
function
__construct
(
Configuration
$configuration)
32
{
33
$this->configuration = $configuration;
34
}
35
36
public
function
resolve
(): void
37
{
38
$sessionConfig = $this->
getSessionConfig
();
39
$generalOptions = $sessionConfig[
'handlers'
][
'general'
];
40
$generalHandler = $this->buildSessionHandlerByOptions($generalOptions[
'type'
], $generalOptions);
41
42
if
($sessionConfig[
'mode'
] === self::MODE_DEFAULT)
43
{
44
$this->session =
new
Session
($generalHandler);
45
$this->kernelSession =
new
KernelSessionProxy
($this->session);
46
}
47
elseif
($sessionConfig[
'mode'
] === self::MODE_SEPARATED)
48
{
49
if
(empty($sessionConfig[
'handlers'
][
'kernel'
]))
50
{
51
throw
new
NotSupportedException
(
'There is no handler for kernel session'
);
52
}
53
if
(empty($sessionConfig[
'handlers'
][
'general'
]))
54
{
55
throw
new
NotSupportedException
(
'There is no handler for general session'
);
56
}
57
if
($sessionConfig[
'handlers'
][
'kernel'
] !==
'encrypted_cookies'
)
58
{
59
throw
new
NotSupportedException
(
'Invalid settings. Kernel session works only with "encrypted_cookies"'
);
60
}
61
62
$this->session =
new
Session
($generalHandler);
63
$this->kernelSession =
new
KernelSession
($sessionConfig[
'lifetime'
] ?? 0);
64
Legacy\LazySessionStart::register
();
65
$this->session->enableLazyStart();
66
if
(!empty($sessionConfig[
'debug'
]))
67
{
68
$this->session->enableDebug();
69
70
$debugger = $this->session->getDebugger();
71
$debugger->setMode($sessionConfig[
'debug'
]);
72
$debugger->storeConfig($sessionConfig);
73
}
74
}
75
76
if
(isset($sessionConfig[
'ignoreSessionStartErrors'
]) && $sessionConfig[
'ignoreSessionStartErrors'
] ===
true
)
77
{
78
$this->session->enableIgnoringSessionStartErrors();
79
}
80
}
81
82
public
function
getSessionConfig
():
array
83
{
84
if
(defined(
"BX_SECURITY_SESSION_VIRTUAL"
) &&
BX_SECURITY_SESSION_VIRTUAL
===
true
)
85
{
86
return
[
87
'mode'
=> self::MODE_DEFAULT,
88
'handlers'
=> [
89
'general'
=> [
'type'
=> self::TYPE_ARRAY]
90
]
91
];
92
}
93
94
$sessionConfig = $this->configuration::getValue(
"session"
) ?: [];
95
$saveHandlerFromIni = ini_get(
'session.save_handler'
);
96
if
((!$sessionConfig || $sessionConfig === [
'mode'
=> self::MODE_DEFAULT]) && $saveHandlerFromIni !==
'files'
)
97
{
98
//when some specific save_handler was installed and we can't change behavior.
99
return
[
100
'mode'
=> self::MODE_DEFAULT,
101
'handlers'
=> [
102
'general'
=> [
'type'
=> self::TYPE_CUSTOM_INI]
103
]
104
];
105
}
106
107
$sessionConfig[
'mode'
] = $sessionConfig[
'mode'
] ?? self::MODE_DEFAULT;
108
$sessionConfig[
'handlers'
][
'general'
] = $this->normalizeSessionOptions($sessionConfig[
'handlers'
][
'general'
] ?? [
'type'
=> self::TYPE_FILE]);
109
110
if
(defined(
"BX_FORCE_DISABLE_SEPARATED_SESSION_MODE"
) &&
BX_FORCE_DISABLE_SEPARATED_SESSION_MODE
===
true
)
111
{
112
$sessionConfig[
'mode'
] = self::MODE_DEFAULT;
113
}
114
115
return
$sessionConfig;
116
}
117
118
private
function
buildSessionHandlerByOptions(
string
$type
,
array
$options
= []): ?
AbstractSessionHandler
119
{
120
switch
(
$type
)
121
{
122
case
self::TYPE_FILE:
123
return
new
StrictSessionHandler
(
new
Handlers
\NativeFileSessionHandler(
$options
));
124
case
self::TYPE_DATABASE:
125
return
new
Handlers\DatabaseSessionHandler
(
$options
);
126
case
self::TYPE_REDIS:
127
return
new
Handlers\RedisSessionHandler
(
$options
);
128
case
self::TYPE_MEMCACHE:
129
return
new
Handlers\MemcacheSessionHandler
(
$options
);
130
case
self::TYPE_ARRAY:
131
case
self::TYPE_NULL:
132
return
new
Handlers\NullSessionHandler
();
133
case
self::TYPE_CUSTOM_INI:
134
return
null
;
135
}
136
137
throw
new
NotSupportedException
(
"Unknown session handler {{$type}}"
);
138
}
139
140
private
function
normalizeSessionOptions(
$options
):
array
141
{
142
if
(!is_array(
$options
) && is_string(
$options
))
143
{
144
$options
= [
145
'type'
=>
$options
,
146
];
147
}
148
if
(is_array(
$options
))
149
{
150
if
(defined(
'BX_SECURITY_SESSION_READONLY'
) &&
BX_SECURITY_SESSION_READONLY
)
151
{
152
$options
[
'readOnly'
] =
true
;
153
}
154
155
return
$options
;
156
}
157
158
throw
new
ArgumentException(
'Session handler has to have options as array or string.'
);
159
}
160
161
public
function
getSession
():
SessionInterface
162
{
163
return
$this->session;
164
}
165
166
public
function
getKernelSession
():
SessionInterface
167
{
168
return
$this->kernelSession
;
169
}
170
171
public
function
getConfiguration
():
Configuration
172
{
173
return
$this->configuration;
174
}
175
}
$type
$type
Определения
options.php:106
BX_SECURITY_SESSION_VIRTUAL
const BX_SECURITY_SESSION_VIRTUAL
Определения
index.php:2
Bitrix\Main\Config\Configuration
Определения
configuration.php:9
Bitrix\Main\NotSupportedException
Определения
NotSupportedException.php:9
Bitrix\Main\Session\Handlers\AbstractSessionHandler
Определения
abstractsessionhandler.php:10
Bitrix\Main\Session\Handlers\DatabaseSessionHandler
Определения
databasesessionhandler.php:8
Bitrix\Main\Session\Handlers\MemcacheSessionHandler
Определения
memcachesessionhandler.php:10
Bitrix\Main\Session\Handlers\NullSessionHandler
Определения
nullsessionhandler.php:7
Bitrix\Main\Session\Handlers\RedisSessionHandler
Определения
redissessionhandler.php:10
Bitrix\Main\Session\Handlers\StrictSessionHandler
Определения
strictsessionhandler.php:6
Bitrix\Main\Session\KernelSession
Определения
kernelsession.php:10
Bitrix\Main\Session\KernelSessionProxy
Определения
kernelsessionproxy.php:6
Bitrix\Main\Session\Legacy\LazySessionStart\register
static register()
Определения
lazysessionstart.php:12
Bitrix\Main\Session\SessionConfigurationResolver
Определения
sessionconfigurationresolver.php:12
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_MEMCACHE
const TYPE_MEMCACHE
Определения
sessionconfigurationresolver.php:19
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_ARRAY
const TYPE_ARRAY
Определения
sessionconfigurationresolver.php:20
Bitrix\Main\Session\SessionConfigurationResolver\resolve
resolve()
Определения
sessionconfigurationresolver.php:36
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_CUSTOM_INI
const TYPE_CUSTOM_INI
Определения
sessionconfigurationresolver.php:22
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_DATABASE
const TYPE_DATABASE
Определения
sessionconfigurationresolver.php:17
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_NULL
const TYPE_NULL
Определения
sessionconfigurationresolver.php:21
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_FILE
const TYPE_FILE
Определения
sessionconfigurationresolver.php:16
Bitrix\Main\Session\SessionConfigurationResolver\__construct
__construct(Configuration $configuration)
Определения
sessionconfigurationresolver.php:31
Bitrix\Main\Session\SessionConfigurationResolver\TYPE_REDIS
const TYPE_REDIS
Определения
sessionconfigurationresolver.php:18
Bitrix\Main\Session\SessionConfigurationResolver\getKernelSession
getKernelSession()
Определения
sessionconfigurationresolver.php:166
Bitrix\Main\Session\SessionConfigurationResolver\getConfiguration
getConfiguration()
Определения
sessionconfigurationresolver.php:171
Bitrix\Main\Session\SessionConfigurationResolver\getSessionConfig
getSessionConfig()
Определения
sessionconfigurationresolver.php:82
Bitrix\Main\Session\SessionConfigurationResolver\getSession
getSession()
Определения
sessionconfigurationresolver.php:161
$options
$options
Определения
commerceml2.php:49
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
BX_SECURITY_SESSION_READONLY
const BX_SECURITY_SESSION_READONLY
Определения
user_options.php:11
Bitrix\Main\Session\SessionInterface
Определения
sessioninterface.php:6
$kernelSession
$kernelSession
Определения
include.php:181
BX_FORCE_DISABLE_SEPARATED_SESSION_MODE
const BX_FORCE_DISABLE_SEPARATED_SESSION_MODE
Определения
backup.php:16
Bitrix\Main\Session\Handlers
Определения
abstractsessionhandler.php:3
Bitrix\Main\Session
Определения
arrayaccesswithreferences.php:3
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
bitrix
modules
main
lib
session
sessionconfigurationresolver.php
Создано системой
1.14.0