1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
session_redis.php
См. документацию.
1<?
2
9{
11 protected static $connection = null;
12 protected static $sessionId = null;
13 protected static $isReadOnly = false;
14 protected static $isSessionReady = false;
15 protected static $hasFailedRead = false;
19 public static function Init()
20 {
21 if(self::isConnected())
22 {
23 return true;
24 }
25
26 self::$isReadOnly = defined('BX_SECURITY_SESSION_READONLY');
27
28 return self::newConnection();
29 }
30
36 public static function open($savePath, $sessionName)
37 {
39 }
40
44 public static function close()
45 {
46 if(!self::isConnected() || !self::isValidId(self::$sessionId))
47 return false;
48
49 if (!self::$isReadOnly && self::$isSessionReady)
50 {
52 {
53 self::destroy(self::$sessionId);
54 }
55
56 self::$connection->delete(self::getPrefix().self::$sessionId.".lock");
57 }
58
59 self::$sessionId = null;
61 return true;
62 }
63
68 public static function read($id)
69 {
70 if(!self::isConnected() || !self::isValidId($id))
71 {
72 return "";
73 }
74
75 $sid = self::getPrefix();
76
77 if (!self::$isReadOnly)
78 {
79 $lockTimeout = 55;//TODO: add setting
80 $lockWait = 59000000;//micro seconds = 60 seconds TODO: add setting
81 $waitStep = 100;
82
83 if (defined('BX_SECURITY_SESSION_REDIS_EXLOCK') && BX_SECURITY_SESSION_REDIS_EXLOCK)
84 {
85 $lock = Bitrix\Main\Context::getCurrent()->getRequest()->getRequestedPage();
86 }
87 else
88 {
89 $lock = 1;
90 }
91
92 while(!self::$connection->setnx($sid.$id.'.lock', $lock))
93 {
94 usleep($waitStep);
95 $lockWait -= $waitStep;
96 if($lockWait < 0)
97 {
98 $errorText = 'Unable to get session lock within 60 seconds.';
99 if ($lock !== 1)
100 {
101 $lockedUri = self::$connection->get($sid.$id.".lock");
102 if ($lockedUri && $lockedUri != 1)
103 $errorText .= sprintf(' Locked by "%s".', $lockedUri);
104 }
105
107 }
108
109 if($waitStep < 1000000)
110 $waitStep *= 2;
111 }
112 self::$connection->expire($sid.$id.".lock", $lockTimeout);
113 }
114
115 self::$sessionId = $id;
116 self::$isSessionReady = true;
117 $res = self::$connection->get($sid.$id);
118
119 if($res === false)
120 {
121 if (!self::$hasFailedRead)
122 {
123 AddEventHandler("main", "OnPageStart", array("CSecuritySession", "UpdateSessID"));
124 self::$hasFailedRead = true;
125 }
126 $res = "";
127 }
128
129 return $res;
130 }
131
137 public static function write($id, $sessionData)
138 {
139 if(!self::isConnected() || !self::isValidId($id))
140 return false;
141
142 if (!self::$isSessionReady)
143 return false;
144
145 if (self::$isReadOnly)
146 {
148 {
149 return true;
150 }
151 }
152
153 $sid = self::getPrefix();
154 $maxLifetime = intval(ini_get("session.gc_maxlifetime"));
155
157 {
158 $oldSessionId = CSecuritySession::getOldSessionId(true);
159 self::$connection->delete($sid.$oldSessionId);
160 }
161
162 self::$connection->setex($sid.$id, $maxLifetime, $sessionData);
163
164 return true;
165 }
166
171 public static function destroy($id)
172 {
173 if(!self::isValidId($id))
174 return false;
175
176 if (!self::$isSessionReady)
177 return false;
178
179 if (self::$isReadOnly)
180 return false;
181
182 $isConnectionRestored = false;
183 if(!self::isConnected())
184 $isConnectionRestored = self::newConnection();
185
186 if(!self::isConnected())
187 return false;
188
189 $sid = self::getPrefix();
190 self::$connection->delete($sid.$id);
191
193 self::$connection->delete($sid.CSecuritySession::getOldSessionId(true));
194
195 if($isConnectionRestored)
197
198 return true;
199 }
200
205 public static function gc($maxLifeTime)
206 {
207 return true;
208 }
209
213 public static function isStorageEnabled()
214 {
215 return defined("BX_SECURITY_SESSION_REDIS_HOST");
216 }
217
221 protected static function isConnected()
222 {
223 return self::$connection !== null;
224 }
225
230 protected static function isValidId($pId)
231 {
232 return (
233 $pId
234 && is_string($pId)
235 && preg_match('/^[\da-z\-,]{6,}$/iD', $pId)
236 );
237 }
238
242 protected static function getPrefix()
243 {
244 return defined("BX_CACHE_SID")? BX_CACHE_SID: "BX";
245 }
246
251 protected static function newConnection()
252 {
253 $result = false;
254 $exception = null;
255
256 if (!extension_loaded('redis'))
257 {
258 $result = false;
259 $exception = new \ErrorException("redis extention not loaded.", 0, E_USER_ERROR, __FILE__, __LINE__);
260 }
261
262 if (!$exception)
263 {
264 if (!self::isStorageEnabled())
265 {
266 $result = false;
267 $exception = new \ErrorException("BX_SECURITY_SESSION_REDIS_HOST constant is not defined.", 0, E_USER_ERROR, __FILE__, __LINE__);
268 }
269 }
270
271 if (!$exception)
272 {
273 $port = defined("BX_SECURITY_SESSION_REDIS_PORT")? intval(BX_SECURITY_SESSION_REDIS_PORT): 11211;
274 self::$connection = new \Redis();
275 $result = self::$connection->pconnect(BX_SECURITY_SESSION_REDIS_HOST, $port);
276
277 if ($result)
278 {
279 self::$connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY);
280 }
281 else
282 {
283 $error = error_get_last();
284 if ($error && $error["type"] == E_WARNING)
285 {
286 $exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
287 }
288 }
289 }
290
291 if ($exception)
292 {
294 $exceptionHandler = $application->getExceptionHandler();
295 $exceptionHandler->writeToLog($exception);
296 }
297
298 return $result;
299 }
300
301 protected static function closeConnection()
302 {
303 self::$connection->close();
304 self::$connection = null;
305 }
306
307}
static getInstance()
Определения application.php:98
static isOldSessionIdExist()
Определения session.php:72
static getOldSessionId($cleanUp=false)
Определения session.php:81
static triggerFatalError($pMessage="")
Определения session.php:36
static destroy($id)
Определения session_redis.php:171
static $connection
Определения session_redis.php:11
static closeConnection()
Определения session_redis.php:301
static close()
Определения session_redis.php:44
static $isReadOnly
Определения session_redis.php:13
static isValidId($pId)
Определения session_redis.php:230
static $sessionId
Определения session_redis.php:12
static getPrefix()
Определения session_redis.php:242
static read($id)
Определения session_redis.php:68
static gc($maxLifeTime)
Определения session_redis.php:205
static $hasFailedRead
Определения session_redis.php:15
static write($id, $sessionData)
Определения session_redis.php:137
static $isSessionReady
Определения session_redis.php:14
static Init()
Определения session_redis.php:19
static isConnected()
Определения session_redis.php:221
static newConnection()
Определения session_redis.php:251
static isStorageEnabled()
Определения session_redis.php:213
static open($savePath, $sessionName)
Определения session_redis.php:36
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$application
Определения bitrix.php:23
isSessionExpired()
Определения tools.php:5138
AddEventHandler($FROM_MODULE_ID, $MESSAGE_ID, $CALLBACK, $SORT=100, $FULL_PATH=false)
Определения tools.php:5165
$error
Определения subscription_card_product.php:20