1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
session_mc.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 return true;
23
24 self::$isReadOnly = defined('BX_SECURITY_SESSION_READONLY');
25
26 return self::newConnection();
27 }
28
34 public static function open($savePath, $sessionName)
35 {
37 }
38
42 public static function close()
43 {
44 if(!self::isConnected() || !self::isValidId(self::$sessionId))
45 return false;
46
47 if (!self::$isReadOnly && self::$isSessionReady)
48 {
50 self::destroy(self::$sessionId);
51
52 self::$connection->replace(self::getPrefix().self::$sessionId.".lock", 0, 0, 1);
53 }
54
55 self::$sessionId = null;
57 return true;
58 }
59
64 public static function read($id)
65 {
66 if(!self::isConnected() || !self::isValidId($id))
67 return "";
68
69 $sid = self::getPrefix();
70
71 if (!self::$isReadOnly)
72 {
73 $lockTimeout = 55;//TODO: add setting
74 $lockWait = 59000000;//micro seconds = 60 seconds TODO: add setting
75 $waitStep = 100;
76
77 if (defined('BX_SECURITY_SESSION_MEMCACHE_EXLOCK') && BX_SECURITY_SESSION_MEMCACHE_EXLOCK)
78 $lock = Bitrix\Main\Context::getCurrent()->getRequest()->getRequestedPage();
79 else
80 $lock = 1;
81
82 while(!self::$connection->add($sid.$id.".lock", $lock, 0, $lockTimeout))
83 {
84 if(self::$connection->increment($sid.$id.".lock", 1) === 1)
85 {
86 self::$connection->replace($sid.$id.".lock", $lock, 0, $lockTimeout);
87 break;
88 }
89 usleep($waitStep);
90 $lockWait -= $waitStep;
91 if($lockWait < 0)
92 {
93 $errorText = 'Unable to get session lock within 60 seconds.';
94 if ($lock !== 1)
95 {
96 $lockedUri = self::$connection->get($sid.$id.".lock");
97 if ($lockedUri && $lockedUri != 1)
98 $errorText .= sprintf(' Locked by "%s".', $lockedUri);
99 }
100
102 }
103
104 if($waitStep < 1000000)
105 $waitStep *= 2;
106 }
107 }
108
109 self::$sessionId = $id;
110 self::$isSessionReady = true;
111 $res = self::$connection->get($sid.$id);
112 if($res === false)
113 {
114 if (!self::$hasFailedRead)
115 {
116 AddEventHandler("main", "OnPageStart", array("CSecuritySession", "UpdateSessID"));
117 self::$hasFailedRead = true;
118 }
119 $res = "";
120 }
121
122 return $res;
123 }
124
130 public static function write($id, $sessionData)
131 {
132 if(!self::isConnected() || !self::isValidId($id))
133 return false;
134
135 if (!self::$isSessionReady)
136 return false;
137
138 if (self::$isReadOnly)
139 {
141 {
142 return true;
143 }
144 }
145
146 $sid = self::getPrefix();
147 $maxLifetime = intval(ini_get("session.gc_maxlifetime"));
148
150 {
151 $oldSessionId = CSecuritySession::getOldSessionId(true);
152 self::$connection->replace($sid.$oldSessionId, "", 0, 1);
153 }
154
155 self::$connection->set($sid.$id, $sessionData, 0, $maxLifetime);
156
157 return true;
158 }
159
164 public static function destroy($id)
165 {
166 if(!self::isValidId($id))
167 return false;
168
169 if (!self::$isSessionReady)
170 return false;
171
172 if (self::$isReadOnly)
173 return false;
174
175 $isConnectionRestored = false;
176 if(!self::isConnected())
177 $isConnectionRestored = self::newConnection();
178
179 if(!self::isConnected())
180 return false;
181
182 $sid = self::getPrefix();
183 self::$connection->replace($sid.$id, "", 0, 1);
184
186 self::$connection->replace($sid.CSecuritySession::getOldSessionId(true), "", 0, 1);
187
188 if($isConnectionRestored)
190
191 return true;
192 }
193
198 public static function gc($maxLifeTime)
199 {
200 return true;
201 }
202
206 public static function isStorageEnabled()
207 {
208 return defined("BX_SECURITY_SESSION_MEMCACHE_HOST");
209 }
210
214 protected static function isConnected()
215 {
216 return self::$connection !== null;
217 }
218
223 protected static function isValidId($pId)
224 {
225 return (
226 $pId
227 && is_string($pId)
228 && preg_match('/^[\da-z\-,]{6,}$/iD', $pId)
229 );
230 }
231
235 protected static function getPrefix()
236 {
237 return defined("BX_CACHE_SID")? BX_CACHE_SID: "BX";
238 }
239
243 protected static function newConnection()
244 {
245 $result = false;
246 $exception = null;
247
248 if (!extension_loaded('memcache'))
249 {
250 $result = false;
251 $exception = new \ErrorException("memcache extention not loaded.", 0, E_USER_ERROR, __FILE__, __LINE__);
252 }
253
254 if (!$exception)
255 {
256 if (!self::isStorageEnabled())
257 {
258 $result = false;
259 $exception = new \ErrorException("BX_SECURITY_SESSION_MEMCACHE_HOST constant is not defined.", 0, E_USER_ERROR, __FILE__, __LINE__);
260 }
261 }
262
263 if (!$exception)
264 {
265 $port = defined("BX_SECURITY_SESSION_MEMCACHE_PORT")? intval(BX_SECURITY_SESSION_MEMCACHE_PORT): 11211;
266 self::$connection = new Memcache;
267 $result = self::$connection->pconnect(BX_SECURITY_SESSION_MEMCACHE_HOST, $port);
268 if (!$result)
269 {
270 $error = error_get_last();
271 if ($error && $error["type"] == E_WARNING)
272 {
273 $exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
274 }
275 }
276 }
277
278 if ($exception)
279 {
281 $exceptionHandler = $application->getExceptionHandler();
282 $exceptionHandler->writeToLog($exception);
283 }
284
285 return $result;
286 }
287
288 protected static function closeConnection()
289 {
290 self::$connection->close();
291 self::$connection = null;
292 }
293
294}
static getInstance()
Определения application.php:98
static isOldSessionIdExist()
Определения session.php:72
static getOldSessionId($cleanUp=false)
Определения session.php:81
static triggerFatalError($pMessage="")
Определения session.php:36
Определения session_mc.php:9
static destroy($id)
Определения session_mc.php:164
static $connection
Определения session_mc.php:11
static closeConnection()
Определения session_mc.php:288
static close()
Определения session_mc.php:42
static $isReadOnly
Определения session_mc.php:13
static isValidId($pId)
Определения session_mc.php:223
static $sessionId
Определения session_mc.php:12
static getPrefix()
Определения session_mc.php:235
static read($id)
Определения session_mc.php:64
static gc($maxLifeTime)
Определения session_mc.php:198
static $hasFailedRead
Определения session_mc.php:15
static write($id, $sessionData)
Определения session_mc.php:130
static $isSessionReady
Определения session_mc.php:14
static Init()
Определения session_mc.php:19
static isConnected()
Определения session_mc.php:214
static newConnection()
Определения session_mc.php:243
static isStorageEnabled()
Определения session_mc.php:206
static open($savePath, $sessionName)
Определения session_mc.php:34
</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