Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sessionconfigurationresolver.php
1<?php
2
3namespace Bitrix\Main\Session;
4
10
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));
125 return new Handlers\DatabaseSessionHandler($options);
126 case self::TYPE_REDIS:
127 return new Handlers\RedisSessionHandler($options);
129 return new Handlers\MemcacheSessionHandler($options);
130 case self::TYPE_ARRAY:
131 case self::TYPE_NULL:
132 return new Handlers\NullSessionHandler();
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
167 {
168 return $this->kernelSession;
169 }
170
172 {
173 return $this->configuration;
174 }
175}