ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML\SessionHandlerPHP Class Reference
+ Inheritance diagram for SimpleSAML\SessionHandlerPHP:
+ Collaboration diagram for SimpleSAML\SessionHandlerPHP:

Public Member Functions

 restorePrevious ()
 Restore a previously-existing session. More...
 
 newSessionId ()
 Create a new session id. More...
 
 getCookieSessionId ()
 Retrieve the session ID saved in the session cookie, if there's one. More...
 
 getSessionCookieName ()
 Retrieve the session cookie name. More...
 
 saveSession (\SimpleSAML_Session $session)
 Save the current session to the PHP session array. More...
 
 loadSession ($sessionId=null)
 Load the session from the PHP session array. More...
 
 hasSessionCookie ()
 Check whether the session cookie is set. More...
 
 getCookieParams ()
 Get the cookie parameters that should be used for session cookies. More...
 
 setCookie ($sessionName, $sessionID, array $cookieParams=null)
 Set a session cookie. More...
 
- Public Member Functions inherited from SimpleSAML\SessionHandler
 newSessionId ()
 Create a new session id. More...
 
 getCookieSessionId ()
 Retrieve the session ID saved in the session cookie, if there's one. More...
 
 getSessionCookieName ()
 Retrieve the session cookie name. More...
 
 saveSession (\SimpleSAML_Session $session)
 Save the session. More...
 
 loadSession ($sessionId=null)
 Load the session. More...
 
 setCookie ($sessionName, $sessionID, array $cookieParams=null)
 Set a session cookie. More...
 
 hasSessionCookie ()
 Check whether the session cookie is set. More...
 
 getCookieParams ()
 Get the cookie parameters that should be used for session cookies. More...
 

Protected Member Functions

 __construct ()
 Initialize the PHP session handling. More...
 
- Protected Member Functions inherited from SimpleSAML\SessionHandler
 __construct ()
 This constructor is included in case it is needed in the future. More...
 

Protected Attributes

 $cookie_name
 

Private Member Functions

 sessionStart ()
 This method starts a session, making sure no warnings are generated due to headers being already sent. More...
 

Private Attributes

 $previous_session = array()
 

Additional Inherited Members

- Static Public Member Functions inherited from SimpleSAML\SessionHandler
static getSessionHandler ()
 This function retrieves the current instance of the session handler. More...
 
- Static Protected Attributes inherited from SimpleSAML\SessionHandler
static $sessionHandler = null
 

Detailed Description

Definition at line 17 of file SessionHandlerPHP.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\SessionHandlerPHP::__construct ( )
protected

Initialize the PHP session handling.

This constructor is protected because it should only be called from ::createSessionHandler(...).

Definition at line 44 of file SessionHandlerPHP.php.

References $config, $params, defined, and SimpleSAML_Configuration\getInstance().

45  {
46  // call the parent constructor in case it should become necessary in the future
47  parent::__construct();
48 
50  $this->cookie_name = $config->getString('session.phpsession.cookiename', null);
51 
52  if (function_exists('session_status') && defined('PHP_SESSION_ACTIVE')) { // PHP >= 5.4
53  $previous_session = session_status() === PHP_SESSION_ACTIVE;
54  } else {
55  $previous_session = (session_id() !== '') && (session_name() !== $this->cookie_name);
56  }
57 
58  if ($previous_session) {
59  if (session_name() === $this->cookie_name || $this->cookie_name === null) {
61  'There is already a PHP session with the same name as SimpleSAMLphp\'s session, or the '.
62  "'session.phpsession.cookiename' configuration option is not set. Make sure to set ".
63  "SimpleSAMLphp's cookie name with a value not used by any other applications."
64  );
65  }
66 
67  /*
68  * We shouldn't have a session at this point, so it might be an application session. Save the details to
69  * retrieve it later and commit.
70  */
71  $this->previous_session['cookie_params'] = session_get_cookie_params();
72  $this->previous_session['id'] = session_id();
73  $this->previous_session['name'] = session_name();
74  session_write_close();
75  }
76 
77  if (!empty($this->cookie_name)) {
78  session_name($this->cookie_name);
79  } else {
80  $this->cookie_name = session_name();
81  }
82 
83  $params = $this->getCookieParams();
84 
85  session_set_cookie_params(
86  $params['lifetime'],
87  $params['path'],
88  $params['domain'],
89  $params['secure'],
90  $params['httponly']
91  );
92 
93  $savepath = $config->getString('session.phpsession.savepath', null);
94  if (!empty($savepath)) {
95  session_save_path($savepath);
96  }
97  }
$params
Definition: disable.php:11
static warning($string)
Definition: Logger.php:179
getCookieParams()
Get the cookie parameters that should be used for session cookies.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
+ Here is the call graph for this function:

Member Function Documentation

◆ getCookieParams()

SimpleSAML\SessionHandlerPHP::getCookieParams ( )

Get the cookie parameters that should be used for session cookies.

This function contains some adjustments from the default to provide backwards-compatibility.

Returns
array The cookie parameters for our sessions. If both 'session.phpsession.limitedpath' and 'session.cookie.path' options are set at the same time in the configuration.

Definition at line 298 of file SessionHandlerPHP.php.

References $config, $ret, and SimpleSAML_Configuration\getInstance().

299  {
301 
302  $ret = parent::getCookieParams();
303 
304  if ($config->hasValue('session.phpsession.limitedpath') && $config->hasValue('session.cookie.path')) {
305  throw new \SimpleSAML_Error_Exception(
306  'You cannot set both the session.phpsession.limitedpath and session.cookie.path options.'
307  );
308  } elseif ($config->hasValue('session.phpsession.limitedpath')) {
309  $ret['path'] = $config->getBoolean(
310  'session.phpsession.limitedpath',
311  false
312  ) ? $config->getBasePath() : '/';
313  }
314 
315  $ret['httponly'] = $config->getBoolean('session.phpsession.httponly', true);
316 
317  return $ret;
318  }
$ret
Definition: parser.php:6
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
+ Here is the call graph for this function:

◆ getCookieSessionId()

SimpleSAML\SessionHandlerPHP::getCookieSessionId ( )

Retrieve the session ID saved in the session cookie, if there's one.

Returns
string|null The session id saved in the cookie or null if no session cookie was set.
Exceptions

Definition at line 188 of file SessionHandlerPHP.php.

References $_COOKIE.

189  {
190  if (!self::hasSessionCookie()) {
191  return null; // there's no session cookie, can't return ID
192  }
193 
194  // do not rely on session_id() as it can return the ID of a previous session. Get it from the cookie instead.
195  session_id($_COOKIE[$this->cookie_name]);
196 
197  $session_cookie_params = session_get_cookie_params();
198 
199  if ($session_cookie_params['secure'] && !HTTP::isHTTPS()) {
200  throw new \SimpleSAML_Error_Exception('Session start with secure cookie not allowed on http.');
201  }
202 
203  $this->sessionStart();
204  return session_id();
205  }
$_COOKIE['client_id']
Definition: server.php:9
static isHTTPS()
This function checks if we are using HTTPS as protocol.
Definition: HTTP.php:865
sessionStart()
This method starts a session, making sure no warnings are generated due to headers being already sent...

◆ getSessionCookieName()

SimpleSAML\SessionHandlerPHP::getSessionCookieName ( )

Retrieve the session cookie name.

Returns
string The session cookie name.

Definition at line 213 of file SessionHandlerPHP.php.

214  {
215  return $this->cookie_name;
216  }

◆ hasSessionCookie()

SimpleSAML\SessionHandlerPHP::hasSessionCookie ( )

Check whether the session cookie is set.

This function will only return false if is is certain that the cookie isn't set.

Returns
boolean True if it was set, false otherwise.

Definition at line 281 of file SessionHandlerPHP.php.

References $_COOKIE.

282  {
283  return array_key_exists($this->cookie_name, $_COOKIE);
284  }
$_COOKIE['client_id']
Definition: server.php:9

◆ loadSession()

SimpleSAML\SessionHandlerPHP::loadSession (   $sessionId = null)

Load the session from the PHP session array.

Parameters
string | null$sessionIdThe ID of the session we should load, or null to use the default.
Returns
|null The session object, or null if it doesn't exist.
Exceptions

Definition at line 240 of file SessionHandlerPHP.php.

References $_SESSION, $ret, and $session.

241  {
242  assert('is_string($sessionId) || is_null($sessionId)');
243 
244  if ($sessionId !== null) {
245  if (session_id() === '') {
246  // session not initiated with getCookieSessionId(), start session without setting cookie
247  $ret = ini_set('session.use_cookies', '0');
248  if ($ret === false) {
249  throw new \SimpleSAML_Error_Exception('Disabling PHP option session.use_cookies failed.');
250  }
251 
252  session_id($sessionId);
253  $this->sessionStart();
254  } elseif ($sessionId !== session_id()) {
255  throw new \SimpleSAML_Error_Exception('Cannot load PHP session with a specific ID.');
256  }
257  } elseif (session_id() === '') {
258  self::getCookieSessionId();
259  }
260 
261  if (!isset($_SESSION['SimpleSAMLphp_SESSION'])) {
262  return null;
263  }
264 
265  $session = $_SESSION['SimpleSAMLphp_SESSION'];
266  assert('is_string($session)');
267 
268  $session = unserialize($session);
269 
270  return ($session !== false) ? $session : null;
271  }
$_SESSION["AccountId"]
$session
sessionStart()
This method starts a session, making sure no warnings are generated due to headers being already sent...
$ret
Definition: parser.php:6

◆ newSessionId()

SimpleSAML\SessionHandlerPHP::newSessionId ( )

Create a new session id.

Returns
string The new session id.

Definition at line 171 of file SessionHandlerPHP.php.

References SimpleSAML_Session\createSession().

172  {
173  // generate new (secure) session id
174  $sessionId = bin2hex(openssl_random_pseudo_bytes(16));
176 
177  return $sessionId;
178  }
static createSession($sessionId)
Create a new session and cache it.
Definition: Session.php:418
+ Here is the call graph for this function:

◆ restorePrevious()

SimpleSAML\SessionHandlerPHP::restorePrevious ( )

Restore a previously-existing session.

Use this method to restore a previous PHP session existing before SimpleSAMLphp initialized its own session.

WARNING: do not use this method directly, unless you know what you are doing. Calling this method directly, outside of SimpleSAML_Session, could cause SimpleSAMLphp's session to be lost or mess the application's one. The session must always be saved properly before calling this method. If you don't understand what this is about, don't use this method.

Definition at line 136 of file SessionHandlerPHP.php.

References array.

137  {
138  if (empty($this->previous_session)) {
139  return; // nothing to do here
140  }
141 
142  // close our own session
143  session_write_close();
144 
145  session_name($this->previous_session['name']);
146  session_set_cookie_params(
147  $this->previous_session['cookie_params']['lifetime'],
148  $this->previous_session['cookie_params']['path'],
149  $this->previous_session['cookie_params']['domain'],
150  $this->previous_session['cookie_params']['secure'],
151  $this->previous_session['cookie_params']['httponly']
152  );
153  session_id($this->previous_session['id']);
154  $this->previous_session = array();
155  $this->sessionStart();
156 
157  /*
158  * At this point, we have restored a previously-existing session, so we can't continue to use our session here.
159  * Therefore, we need to load our session again in case we need it. We remove this handler from the parent
160  * class so that the handler is initialized again if we ever need to do something with the session.
161  */
162  parent::$sessionHandler = null;
163  }
sessionStart()
This method starts a session, making sure no warnings are generated due to headers being already sent...
Create styles array
The data for the language used.

◆ saveSession()

SimpleSAML\SessionHandlerPHP::saveSession ( \SimpleSAML_Session  $session)

Save the current session to the PHP session array.

Parameters
\SimpleSAML_Session$sessionThe session object we should save.

Definition at line 224 of file SessionHandlerPHP.php.

References $_SESSION.

225  {
226  $_SESSION['SimpleSAMLphp_SESSION'] = serialize($session);
227  }
$_SESSION["AccountId"]
$session

◆ sessionStart()

SimpleSAML\SessionHandlerPHP::sessionStart ( )
private

This method starts a session, making sure no warnings are generated due to headers being already sent.

Definition at line 103 of file SessionHandlerPHP.php.

104  {
105  $cacheLimiter = session_cache_limiter();
106  if (headers_sent()) {
107  /*
108  * session_start() tries to send HTTP headers depending on the configuration, according to the
109  * documentation:
110  *
111  * http://php.net/manual/en/function.session-start.php
112  *
113  * If headers have been already sent, it will then trigger an error since no more headers can be sent.
114  * Being unable to send headers does not mean we cannot recover the session by calling session_start(),
115  * so we still want to call it. In this case, though, we want to avoid session_start() to send any
116  * headers at all so that no error is generated, so we clear the cache limiter temporarily (no headers
117  * sent then) and restore it after successfully starting the session.
118  */
119  session_cache_limiter('');
120  }
121  session_cache_limiter($cacheLimiter);
122  @session_start();
123  }

◆ setCookie()

SimpleSAML\SessionHandlerPHP::setCookie (   $sessionName,
  $sessionID,
array  $cookieParams = null 
)

Set a session cookie.

Parameters
string$sessionNameThe name of the session.
string | null$sessionIDThe session ID to use. Set to null to delete the cookie.
array | null$cookieParamsAdditional parameters to use for the session cookie.
Exceptions

Definition at line 330 of file SessionHandlerPHP.php.

References $sessionID.

331  {
332  if ($cookieParams === null) {
333  $cookieParams = session_get_cookie_params();
334  }
335 
336  if ($cookieParams['secure'] && !HTTP::isHTTPS()) {
337  throw new CannotSetCookie(
338  'Setting secure cookie on plain HTTP is not allowed.',
340  );
341  }
342 
343  if (headers_sent()) {
344  throw new CannotSetCookie(
345  'Headers already sent.',
347  );
348  }
349 
350  session_set_cookie_params(
351  $cookieParams['lifetime'],
352  $cookieParams['path'],
353  $cookieParams['domain'],
354  $cookieParams['secure'],
355  $cookieParams['httponly']
356  );
357 
358  if (session_id() !== '') {
359  // session already started, close it
360  session_write_close();
361  }
362 
363  session_id($sessionID);
364  $this->sessionStart();
365  }
$sessionID
static isHTTPS()
This function checks if we are using HTTPS as protocol.
Definition: HTTP.php:865
sessionStart()
This method starts a session, making sure no warnings are generated due to headers being already sent...

Field Documentation

◆ $cookie_name

SimpleSAML\SessionHandlerPHP::$cookie_name
protected

Definition at line 25 of file SessionHandlerPHP.php.

◆ $previous_session

SimpleSAML\SessionHandlerPHP::$previous_session = array()
private

Definition at line 37 of file SessionHandlerPHP.php.


The documentation for this class was generated from the following file: