ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SessionHandlerCookie.php
Go to the documentation of this file.
1 <?php
2 
3 
15 namespace SimpleSAML;
16 
18 
19 abstract class SessionHandlerCookie extends SessionHandler
20 {
21 
27  private $session_id = null;
28 
29 
35  protected $cookie_name;
36 
37 
42  protected function __construct()
43  {
44  // call the constructor in the base class in case it should become necessary in the future
45  parent::__construct();
46 
48  $this->cookie_name = $config->getString('session.cookie.name', 'SimpleSAMLSessionID');
49  }
50 
51 
57  public function newSessionId()
58  {
59  $this->session_id = self::createSessionID();
60  \SimpleSAML_Session::createSession($this->session_id);
61 
62  return $this->session_id;
63  }
64 
65 
71  public function getCookieSessionId()
72  {
73  if ($this->session_id === null) {
74  if (self::hasSessionCookie()) {
75  // attempt to retrieve the session id from the cookie
76  $this->session_id = $_COOKIE[$this->cookie_name];
77  }
78 
79  // check if we have a valid session id
80  if (!self::isValidSessionID($this->session_id)) {
81  // invalid, disregard this session
82  return null;
83  }
84  }
85 
86  return $this->session_id;
87  }
88 
89 
95  public function getSessionCookieName()
96  {
97  return $this->cookie_name;
98  }
99 
100 
106  private static function createSessionID()
107  {
108  return bin2hex(openssl_random_pseudo_bytes(16));
109  }
110 
111 
120  private static function isValidSessionID($session_id)
121  {
122  if (!is_string($session_id)) {
123  return false;
124  }
125 
126  if (strlen($session_id) != 32) {
127  return false;
128  }
129 
130  if (preg_match('/[^0-9a-f]/', $session_id)) {
131  return false;
132  }
133 
134  return true;
135  }
136 
137 
145  public function hasSessionCookie()
146  {
147  return array_key_exists($this->cookie_name, $_COOKIE);
148  }
149 
150 
160  public function setCookie($sessionName, $sessionID, array $cookieParams = null)
161  {
162  assert(is_string($sessionName));
163  assert(is_string($sessionID) || $sessionID === null);
164 
165  if ($cookieParams !== null) {
166  $params = array_merge($this->getCookieParams(), $cookieParams);
167  } else {
168  $params = $this->getCookieParams();
169  }
170 
171  HTTP::setCookie($sessionName, $sessionID, $params, true);
172  }
173 }
$_COOKIE['client_id']
Definition: server.php:9
static isValidSessionID($session_id)
This static function validates a session id.
getCookieSessionId()
Retrieve the session ID saved in the session cookie, if there&#39;s one.
__construct()
This constructor initializes the session id based on what we receive in a cookie. ...
$config
Definition: bootstrap.php:15
static createSessionID()
This static function creates a session id.
getSessionCookieName()
Retrieve the session cookie name.
hasSessionCookie()
Check whether the session cookie is set.
static createSession($sessionId)
Create a new session and cache it.
Definition: Session.php:416
$sessionID
newSessionId()
Create a new session id.
Attribute-related utility methods.
setCookie($sessionName, $sessionID, array $cookieParams=null)
Set a session cookie.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.