Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

lib/ezsession/classes/ezsession.php
Go to the documentation of this file.
1 <?php
64 class eZSession
65 {
71 static protected $userID = 0;
72
78 static protected $hasStarted = false;
79
85 static protected $hasSessionCookie = null;
86
92 static protected $callbackFunctions = array();
93
99 static protected $handlerInstance = null;
100
107 static protected $namespace = null;
108
112 protected function __construct()
113 {
114 }
115
123 static private function &sessionArray()
124 {
125 if ( self::$namespace !== null )
126 {
127 if ( !isset( $_SESSION[self::$namespace] ) )
128 $_SESSION[self::$namespace] = array();
129 return $_SESSION[self::$namespace];
130 }
131 return $_SESSION;
132 }
133
142 static public function &get( $key = null, $defaultValue = null )
143 {
144 if ( self::$hasStarted === false )
145 {
146 if ( $defaultValue !== null )
147 return $defaultValue;
148 self::start();
149 }
150 $session =& self::sessionArray();
151
152 if ( $key === null )
153 return $session;
154 else if ( isset( $session[$key] ) )
155 return $session[$key];
156 return $defaultValue;
157 }
158
166 static public function set( $key, $value )
167 {
168 if ( self::$hasStarted === false )
169 {
170 self::start();
171 }
172 $session =& self::sessionArray();
173 $session[$key] = $value;
174
175 return true;
176 }
177
186 static public function issetkey( $key, $forceStart = true )
187 {
188 if ( self::$hasStarted === false )
189 {
190 if ( !$forceStart )
191 return null;
192 self::start();
193 }
194 $session = self::sessionArray();
195 return isset( $session[$key] );
196 }
197
207 static public function unsetkey( $key, $forceStart = true )
208 {
209 if ( self::$hasStarted === false )
210 {
211 if ( !$forceStart )
212 return null;
213 self::start();
214 }
215 $session =& self::sessionArray();
216
217 if ( !isset( $session[$key] ) )
218 return false;
219
220 unset( $session[$key] );
221 return true;
222 }
223
231 static public function garbageCollector()
232 {
233 return self::getHandlerInstance()->gc( (int)$_SERVER['REQUEST_TIME'] );
234 }
235
243 static public function cleanup()
244 {
245 return self::getHandlerInstance()->cleanup();
246 }
247
255 static public function countActive()
256 {
257 return self::getHandlerInstance()->count();
258 }
259
267 static protected function registerFunctions( $sessionName = false, ezpSessionHandler|null $handler = null )
268 {
269 if ( self::$hasStarted || self::$handlerInstance !== null )
270 return false;
271
272 $ini = eZINI::instance();
273 if ( $sessionName !== false )
274 {
275 if ( $sessionName !== session_name() )
276 {
277 session_name( $sessionName );
278 }
279 }
280 else if ( $ini->variable( 'Session', 'SessionNameHandler' ) === 'custom' )
281 {
282 $sessionName = $ini->variable( 'Session', 'SessionNamePrefix' );
283 if ( $ini->variable( 'Session', 'SessionNamePerSiteAccess' ) === 'enabled' )
284 {
285 $access = $GLOBALS['eZCurrentAccess'];
286 // Use md5 to make sure name is only consistent of alphanumeric characters
287 $sessionName .= md5( $access['name'] );
288 }
289 if ( $sessionName !== session_name() ) {
290 session_name( $sessionName );
291 }
292 }
293 else
294 {
295 $sessionName = session_name();
296 }
297
298 // See if user has session, used to avoid reading from db if no session.
299 // Allow session bye post params for use by flash, but use $_POST directly
300 // to avoid session double start issues ( #014686 ) caused by eZHTTPTool
301 if ( isset( $_POST[ $sessionName ] ) )
302 {
303 // First use session id from post params (for use in flash upload)
304 session_id( $_POST[ $sessionName ] );
305 self::$hasSessionCookie = true;
306 }
307 else
308 {
309 // else check cookie as used by default
310 self::$hasSessionCookie = isset( $_COOKIE[ $sessionName ] );
311 }
312
313 return self::getHandlerInstance( $handler )->setSaveHandler();
314 }
315
322 static public function setCookieLifetime( $lifetime )
323 {
324 session_set_cookie_params( (int)$lifetime );
325 }
326
336 static public function setCookieParams( $lifetime = false )
337 {
338 $ini = eZINI::instance();
339 $params = session_get_cookie_params();
340 if ( $lifetime === false )
341 {
342 if ( $ini->hasVariable( 'Session', 'CookieTimeout' )
343 && $ini->variable( 'Session', 'CookieTimeout' ) )
344 $lifetime = (int) $ini->variable('Session', 'CookieTimeout');
345 else
346 $lifetime = $params['lifetime'];
347 }
348 $path = $ini->hasVariable( 'Session', 'CookiePath' ) ? $ini->variable( 'Session', 'CookiePath' ) : $params['path'];
349 $domain = $ini->hasVariable( 'Session', 'CookieDomain' ) ? $ini->variable( 'Session', 'CookieDomain' ) : $params['domain'];
350 if ( $ini->hasVariable( 'Session', 'CookieSecure' ) )
351 {
352 $secure = ( $ini->variable( 'Session', 'CookieSecure' ) == 'true' ) ? true : false ;
353 }
354 else
355 {
356 $secure = $params['secure'];
357 }
358 if ( isset( $params['httponly'] ) ) // only available on PHP 5.2 and up
359 {
360 if ( $ini->hasVariable( 'Session', 'CookieHttponly') )
361 {
362 $httponly = ( $ini->variable( 'Session', 'CookieHttponly' ) == 'true' ) ? true : false ;
363 }
364 else
365 {
366 $httponly = $params['httponly'];
367 }
368 session_set_cookie_params( $lifetime, $path, $domain, $secure, $httponly );
369 }
370 else
371 {
372 session_set_cookie_params( $lifetime, $path, $domain, $secure );
373 }
374 }
375
384 static public function start( $cookieTimeout = false )
385 {
386 if ( self::lazyStart( false ) === false )
387 {
388 return false;
389 }
390 self::setCookieParams( $cookieTimeout );
391 return self::forceStart();
392 }
393
401 static public function lazyStart( $startIfUserHasCookie = true )
402 {
403 if ( self::$hasStarted ||
404 ( isset( $GLOBALS['eZSiteBasics']['session-required'] ) &&
405 !$GLOBALS['eZSiteBasics']['session-required'] ) )
406 {
407 return false;
408 }
409 self::registerFunctions();
410 if ( $startIfUserHasCookie && self::$hasSessionCookie )
411 {
412 self::setCookieParams();
413 return self::forceStart();
414 }
415 return null;
416 }
417
428 static public function init( $name, $started, $ns, ezpSessionHandler $handler )
429 {
430 if ( self::$hasStarted )
431 {
432 return;
433 }
434 self::registerFunctions( $name, $handler );
435 self::$namespace = $ns;
436 if ( self::$hasSessionCookie && !$started )
437 {
438 self::forceStart();
439 }
440 else if ( $started )
441 {
442 self::$hasStarted = true;
443 }
444 }
445
453 static protected function forceStart()
454 {
455 return self::$hasStarted = self::getHandlerInstance()->sessionStart();
456 }
457
464 static public function stop()
465 {
466 if ( !self::$hasStarted )
467 {
468 return false;
469 }
470 session_write_close();
471 self::$hasStarted = false;
472 self::$handlerInstance = null;
473 return true;
474 }
475
485 static public function regenerate( $updateBackendData = true )
486 {
487 if ( !self::$hasStarted )
488 {
489 return false;
490 }
491 if ( !function_exists( 'session_regenerate_id' ) )
492 {
493 return false;
494 }
495 if ( headers_sent() )
496 {
497 if ( PHP_SAPI !== 'cli' )
498 eZDebug::writeWarning( 'Could not regenerate session id, HTTP headers already sent.', __METHOD__ );
499 return false;
500 }
501
502 return self::getHandlerInstance()->regenerate( ($updateBackendData && self::$hasSessionCookie) );
503 }
504
512 static public function remove()
513 {
514 // CLI scripts can use sessions and in that case session is forced to start.
515 // However, Symfony does not handle sessions in CLI (which seems to be normal), so session is never started.
516 // Hence check with session_id()
517 if ( !self::$hasStarted || session_id() === '' )
518 {
519 return false;
520 }
521
522 $result = self::getHandlerInstance()->destroy( session_id() );
523
524 self::$hasStarted = false;
525 self::$handlerInstance = null;
526
527 return $result;
528 }
529
536 static public function setUserID( $userID )
537 {
538 self::$userID = $userID;
539 }
540
547 static public function userID()
548 {
549 return self::$userID;
550 }
551
558 static public function userHasSessionCookie()
559 {
560 return self::$hasSessionCookie;
561 }
562
569 static public function hasStarted()
570 {
571 return self::$hasStarted;
572 }
573
580 static public function getHandlerInstance( ezpSessionHandler|null $handler = null )
581 {
582 if ( self::$handlerInstance === null )
583 {
584 if ( $handler === null )
585 {
586 $ini = eZINI::instance();
587 if ( $ini->variable( 'Session', 'Handler' ) !== '' )
588 {
589 $optionArray = array(
590 'iniFile' => 'site.ini',
591 'iniSection' => 'Session',
592 'iniVariable' => 'Handler',
593 'handlerParams' => array( self::$hasSessionCookie )
594 );
595
596 $options = new ezpExtensionOptions( $optionArray );
597 self::$handlerInstance = eZExtension::getHandlerClass( $options );
598 }
599 }
600 else
601 {
602 self::$handlerInstance = $handler;
603 }
604 if ( !self::$handlerInstance instanceof ezpSessionHandler )
605 {
606 self::$handlerInstance = new ezpSessionHandlerPHP( self::$hasSessionCookie );
607 }
608 }
609 return self::$handlerInstance;
610 }
611
622 static public function addCallback( $type, $callback )
623 {
624 if ( !isset( self::$callbackFunctions[$type] ) )
625 {
626 self::$callbackFunctions[$type] = array();
627 }
628 self::$callbackFunctions[$type][] = $callback;
629 }
630
641 static public function triggerCallback( $type, $params )
642 {
643 if ( isset( self::$callbackFunctions[$type] ) )
644 {
645 foreach( self::$callbackFunctions[$type] as $callback )
646 {
647 call_user_func_array( $callback, $params );
648 }
649 return true;
650 }
651 return false;
652 }
653 }
654
655 ?>
$session
$session
Definition: admin/articlelist.php:44
$userID
else $userID
Definition: appointmentedit.php:121
$access
if(! $user->isRegistered()) $access
Definition: authorize.php:88
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:503
eZExtension\getHandlerClass
static getHandlerClass(ezpExtensionOptions $options)
Definition: ezextension.php:480
eZSession
Definition: lib/ezsession/classes/ezsession.php:65
eZSession\hasStarted
static hasStarted()
Definition: lib/ezsession/classes/ezsession.php:569
eZSession\forceStart
static forceStart()
Definition: lib/ezsession/classes/ezsession.php:453
eZSession\$handlerInstance
static $handlerInstance
Definition: lib/ezsession/classes/ezsession.php:99
eZSession\__construct
__construct()
Definition: lib/ezsession/classes/ezsession.php:112
eZSession\unsetkey
static unsetkey( $key, $forceStart=true)
Definition: lib/ezsession/classes/ezsession.php:207
eZSession\setCookieLifetime
static setCookieLifetime( $lifetime)
Definition: lib/ezsession/classes/ezsession.php:322
eZSession\setUserID
static setUserID( $userID)
Definition: lib/ezsession/classes/ezsession.php:536
eZSession\cleanup
static cleanup()
Definition: lib/ezsession/classes/ezsession.php:243
eZSession\$callbackFunctions
static $callbackFunctions
Definition: lib/ezsession/classes/ezsession.php:92
eZSession\start
static start( $cookieTimeout=false)
Definition: lib/ezsession/classes/ezsession.php:384
eZSession\$userID
static $userID
Definition: lib/ezsession/classes/ezsession.php:71
eZSession\registerFunctions
static registerFunctions( $sessionName=false, ezpSessionHandler|null $handler=null)
Definition: lib/ezsession/classes/ezsession.php:267
eZSession\sessionArray
static & sessionArray()
Definition: lib/ezsession/classes/ezsession.php:123
eZSession\setCookieParams
static setCookieParams( $lifetime=false)
Definition: lib/ezsession/classes/ezsession.php:336
eZSession\garbageCollector
static garbageCollector()
Definition: lib/ezsession/classes/ezsession.php:231
eZSession\init
static init( $name, $started, $ns, ezpSessionHandler $handler)
Definition: lib/ezsession/classes/ezsession.php:428
eZSession\issetkey
static issetkey( $key, $forceStart=true)
Definition: lib/ezsession/classes/ezsession.php:186
eZSession\addCallback
static addCallback( $type, $callback)
Definition: lib/ezsession/classes/ezsession.php:622
eZSession\userID
static userID()
Definition: lib/ezsession/classes/ezsession.php:547
eZSession\countActive
static countActive()
Definition: lib/ezsession/classes/ezsession.php:255
eZSession\$hasSessionCookie
static $hasSessionCookie
Definition: lib/ezsession/classes/ezsession.php:85
eZSession\userHasSessionCookie
static userHasSessionCookie()
Definition: lib/ezsession/classes/ezsession.php:558
eZSession\lazyStart
static lazyStart( $startIfUserHasCookie=true)
Definition: lib/ezsession/classes/ezsession.php:401
eZSession\$namespace
static $namespace
Definition: lib/ezsession/classes/ezsession.php:107
eZSession\$hasStarted
static $hasStarted
Definition: lib/ezsession/classes/ezsession.php:78
eZSession\triggerCallback
static triggerCallback( $type, $params)
Definition: lib/ezsession/classes/ezsession.php:641
eZSession\stop
static stop()
Definition: lib/ezsession/classes/ezsession.php:464
eZSession\getHandlerInstance
static getHandlerInstance(ezpSessionHandler|null $handler=null)
Definition: lib/ezsession/classes/ezsession.php:580
eZSession\regenerate
static regenerate( $updateBackendData=true)
Definition: lib/ezsession/classes/ezsession.php:485
$ini
$ini
Definition: classes/admin/linklist.php:64
$type
$type
Definition: classes/admin/linkselect.php:220
ezpExtensionOptions
Definition: ezpextensionoptions.php:59
ezpSessionHandlerPHP
Definition: ezpsessionhandlerphp.php:20
ezpSessionHandler
Definition: ezpsessionhandler.php:26
$result
$result
Definition: companysearch.php:94
$name
$name
Definition: companytypelist.php:194
$options
$options
Definition: optionlist.php:56
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.