lib/ezsession/classes/ezsession.php

Show: inherited
Table of Contents

File containing session interface

Copyright
Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.  
License
eZ Business Use License Agreement Version 2.0  
Package
lib  
Version
4.6.0  

\eZSession

Package: lib\ezsession

eZ Publish Session interface class

Session wrapper for session management, with support for handlers. Handler is defined by site.ini[Session]\Handler setting.

The session system has a hook system which allows external code to perform extra tasks before and after a certain action is executed. For instance to cleanup a table when sessions are removed. This can be used by adding a callback with the eZSession::addCallback function, first param is type and second is callback (called with call_user_func_array)

\code function cleanupStuff( $db, $key, $escKey ) { // Do cleanup here }

eZSession::addCallback( 'destroy_pre', 'cleanupstuff'); // Or if it was a class function: // eZSession::addCallback( 'destroy_pre', array('myClass', 'myCleanupStuff') ); \endcode

When a specific session is destroyed in the database it will call the \c destroy_pre and \c destroy_post hooks. The signature of the function is function destroy( $db, $key, $escapedKey )

When a specific session is regenerated (login/logout) and kept it will call \c regenerate_pre and \c regenerate_post hooks. The signature of the function is function regenerate( $db, $escapedNewKey, $escapedOldKey, $escUserID )

When multiple sessions are expired (garbage collector) in the database it will call the \c gc_pre and \c gc_post hooks. The signature of the function is function gcollect( $db, $expiredTime )

When all sessions are removed from the database it will call the \c cleanup_pre and \c cleanup_post hooks. The signature of the function is function cleanup( $db )

\param $db The database object used by the session manager. \param $key The session key which are being worked on, see also \a $escapedKey \param $escapedKey The same key as \a $key but is escaped correctly for the database. Use this to save a call to eZDBInterface::escapeString() \param $expirationTime An integer specifying the timestamp of when the session will expire. \param $expiredTime Similar to \a $expirationTime but is the time used to figure if a session is expired in the database. ie. all sessions with lower expiration times will be removed.

Properties

Propertyprotectedarray  $callbackFunctions= 'array()'
static

List of callback actions, see {@link eZSession::addCallback()}.

Default valuearray()Details
Type
array
Propertyprotected\ezpSessionHandler|null  $handlerInstance= 'null'
static

Current session handler or false, see {@link eZSession::getHandlerInstance()}.

Default valuenullDetails
Type
\ezpSessionHandler | null
Propertyprotectedbool|null  $hasSessionCookie= 'null'
static

Flag request contains session cookie, set in {@link eZSession::registerFunctions()}.

Default valuenullDetails
Type
bool | null
Propertyprotectedbool  $hasStarted= 'false'
static

Flag session has started, see {@link eZSession::start()}.

Default valuefalseDetails
Type
bool
Propertyprotectedint  $userID= '0'
static

User id, see {@link eZSession::userID()}.

Default value0Details
Type
int

Methods

methodprotected__construct( ) : void

Constructor (not used, this is an all static class)

methodpublicaddCallback( string $type, \handler $callback ) : void
static

Adds a callback function, to be triggered by {@link eZSession::triggerCallback()} when a certain session event occurs.

Use: eZSession::addCallback('gc_pre', myCustomGarabageFunction );

Parameters
Name Type Description
$type string

cleanup, gc, destroy, insert and update, pre and post types.

$callback \handler

a function to call.

Details
Deprecated
since 4.5, use {@link ezpEvent::getInstance()->attach()} with new events  
Since
4.1  
methodpubliccleanup( ) : bool
static

Truncates all session data in the database, this function is not supported by session handlers that don't have a session backend on their own.

Returns
Type Description
bool
Details
Since
4.1  
methodpubliccountActive( ) : string
static

Counts the number of active session and returns it, this function is not supported by session handlers that don't have a session backend on their own.

Returns
Type Description
string Number of sessions.
Details
Since
4.1  
methodprotectedforceStart( ) : true
static

See {@link eZSession::start()}

Returns
Type Description
true
Details
Since
4.4  
methodpublicgarbageCollector( ) : bool
static

Deletes all expired session data in the database, this function is not supported by session handlers that don't have a session backend on their own.

Returns
Type Description
bool
Details
Since
4.1  
methodpublicget( string | null $key = null, null | mixed $defaultValue = null ) : mixed | null
static

Get session value (wrapper)

Parameters
Name Type Description
$key string | null

Return the whole session array if null otherwise the value of $key

$defaultValue null | mixed

Return this if not null and session has not started

Returns
Type Description
mixed | null $defaultValue if key does not exist, otherwise session value depending on $key
Details
Since
4.4  
methodpublicgetHandlerInstance( ) : \ezpSessionHandler
static

Get curren session handler

Returns
Type Description
\ezpSessionHandler
Details
Since
4.4  
methodpublicgetUserSessionHash( ) : string
static

Gets/generates the user hash for use in validating the session based on [Session] SessionValidation* site.ini settings. The default hash is result of md5('empty').

Returns
Type Description
string MD5 hash based on parts of the user ip and agent string.
Details
Deprecated
as of 4.4, only returns default md5('empty') hash now for BC.  
Since
4.1  
methodpublichasStarted( ) : bool
static

Return value to indicate if session has started or not

Returns
Type Description
bool
Details
Since
4.4  
methodpublicissetkey( string $key, bool $forceStart = true ) : bool | null
static

Isset session value (wrapper)

Parameters
Name Type Description
$key string
$forceStart bool

Force session start if true

Returns
Type Description
bool | null Null if session has not started and $forceStart is false
Details
Since
4.4  
methodpubliclazyStart( bool $startIfUserHasCookie = true ) : bool | null
static

Inits eZSession and starts it if user has cookie and $startIfUserHasCookie is true.

Parameters
Name Type Description
$startIfUserHasCookie bool
Returns
Type Description
bool | null
Details
Since
4.4  
methodpublicregenerate( bool $updateBackendData = true ) : bool
static

Will make sure the user gets a new session ID while keepin the session data.

This is useful to call on logins, to avoid sessions theft from users. NOTE: make sure you set new user id first using {@link eZSession::setUserID()}

Parameters
Name Type Description
$updateBackendData bool

set to false to not update session backend with new session id and user id.

Returns
Type Description
bool Depending on if session was regenerated.
Details
Since
4.1  
methodprotectedregisterFunctions( ) : bool
static

Register the needed session functions, this is called automatically by {@link eZSession::start()}, so only call this if you don't start the session.

Returns
Type Description
bool Depending on if eZSession is registrated as session handler.
Details
Since
4.1  
methodpublicremove( ) : bool
static

Removes the current session and resets session variables.

Note: implicit stops session as well!

Returns
Type Description
bool Depending on if session was removed.
Details
Since
4.1  
methodpublicset( string $key,  $value ) : bool
static

Set session value (wrapper)

Parameters
Name Type Description
$key string
$value
Returns
Type Description
bool
Details
Since
4.4  
methodprotectedsetCookieParams( int | false $lifetime = false ) : void
static

Set default cookie parameters based on site.ini settings (fallback to php.ini settings) Used by {@link eZSession::registerFunctions()} Note: this will only have affect when session is created / re-created

Parameters
Name Type Description
$lifetime int | false

Cookie timeout of session cookie, will read from ini if not set

Details
Since
4.4  
methodpublicsetUserID( int $userID ) : void
static

Sets the current userID used by ezpSessionHandlerDB::write() on shutdown.

Parameters
Name Type Description
$userID int

to use in {@link ezpSessionHandlerDB::write()}

Details
Since
4.1  
methodpublicstart( int | false $cookieTimeout = false ) : bool
static

Starts the session and sets the timeout of the session cookie.

Multiple calls will be ignored unless you call {@link eZSession::stop()} first.

Parameters
Name Type Description
$cookieTimeout int | false

Use this to set custom cookie timeout.

Returns
Type Description
bool Depending on if session was started.
Details
Since
4.1  
methodpublicstop( ) : bool
static

Writes session data and stops the session, if not already stopped.

Returns
Type Description
bool Depending on if session was stopped.
Details
Since
4.1  
methodpublictriggerCallback( string $type, array $params ) : bool
static

Triggers callback functions by type, registrated by {@link eZSession::addCallback()} Use: eZSession::triggerCallback('gc_pre', array( $db, $time ) );

Parameters
Name Type Description
$type string

cleanup, gc, destroy, insert and update, pre and post types.

$params array

list of parameters to pass to the callback function.

Returns
Type Description
bool
Details
Deprecated
since 4.5, use {@link ezpEvent::getInstance()->notify()} with new events  
Since
4.1  
methodpublicunsetkey( string $key, bool $forceStart = true ) : bool | null
static

unset session value (wrapper)

Parameters
Name Type Description
$key string
$forceStart bool

Force session start if true

Returns
Type Description
bool | null True if value was removed, false if it did not exist and null if session is not started and $forceStart is false
Details
Since
4.4  
methodpublicuserHasSessionCookie( ) : bool | null
static

Returns if user had session cookie at start of request or not.

Returns
Type Description
bool | null Null if session is not started yet.
Details
Since
4.1  
methodpublicuserID( ) : int
static

Gets the current user id.

Returns
Type Description
int User id stored by {@link eZSession::setUserID()}
Details
Since
4.1  
methodpublicuserSessionIsValid( ) : bool | null
static

Returns if user session validated against stored data in db or if it was invalidated during the current request.

Returns
Type Description
bool | null Null if user is not validated yet (for instance a new session).
Details
Deprecated
as of 4.4, only returns true for bc  
Since
4.1  
Documentation was generated by DocBlox 0.18.1.