| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
@copyright |
|---|
| 11 |
@author will@flourishlib.com |
|---|
| 12 |
@author alex@kingleeds.com |
|---|
| 13 |
@license http://flourishlib.com/license |
|---|
| 14 |
|
|---|
| 15 |
@package |
|---|
| 16 |
@link http://flourishlib.com/fSession |
|---|
| 17 |
|
|---|
| 18 |
@version |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
@changes |
|---|
| 24 |
@changes |
|---|
| 25 |
@changes |
|---|
| 26 |
@changes |
|---|
| 27 |
@changes |
|---|
| 28 |
@changes |
|---|
| 29 |
@changes |
|---|
| 30 |
|
|---|
| 31 |
class fSession |
|---|
| 32 |
{ |
|---|
| 33 |
|
|---|
| 34 |
const add = 'fSession::add'; |
|---|
| 35 |
const clear = 'fSession::clear'; |
|---|
| 36 |
const close = 'fSession::close'; |
|---|
| 37 |
const delete = 'fSession::delete'; |
|---|
| 38 |
const destroy = 'fSession::destroy'; |
|---|
| 39 |
const enablePersistence = 'fSession::enablePersistence'; |
|---|
| 40 |
const get = 'fSession::get'; |
|---|
| 41 |
const ignoreSubdomain = 'fSession::ignoreSubdomain'; |
|---|
| 42 |
const open = 'fSession::open'; |
|---|
| 43 |
const regenerateID = 'fSession::regenerateID'; |
|---|
| 44 |
const reset = 'fSession::reset'; |
|---|
| 45 |
const set = 'fSession::set'; |
|---|
| 46 |
const setLength = 'fSession::setLength'; |
|---|
| 47 |
const setPath = 'fSession::setPath'; |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
@var |
|---|
| 54 |
|
|---|
| 55 |
static private $normal_timespan = NULL; |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
@var |
|---|
| 61 |
|
|---|
| 62 |
static private $open = FALSE; |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
@var |
|---|
| 68 |
|
|---|
| 69 |
static private $persistent_timespan = NULL; |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
@var |
|---|
| 75 |
|
|---|
| 76 |
static private $regenerated = FALSE; |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
@param |
|---|
| 83 |
@param |
|---|
| 84 |
@return |
|---|
| 85 |
|
|---|
| 86 |
static public function add($key, $value) |
|---|
| 87 |
{ |
|---|
| 88 |
self::open(); |
|---|
| 89 |
if (!isset($_SESSION[$key])) { |
|---|
| 90 |
$_SESSION[$key] = array(); |
|---|
| 91 |
} |
|---|
| 92 |
if (!is_array($_SESSION[$key])) { |
|---|
| 93 |
throw new fProgrammerException( |
|---|
| 94 |
'%1$s was called for the key, %2$s, which is not an array', |
|---|
| 95 |
__CLASS__ . '::add()', |
|---|
| 96 |
$key |
|---|
| 97 |
); |
|---|
| 98 |
} |
|---|
| 99 |
$_SESSION[$key][] = $value; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
@param |
|---|
| 110 |
@return |
|---|
| 111 |
|
|---|
| 112 |
static public function clear($prefix=NULL) |
|---|
| 113 |
{ |
|---|
| 114 |
self::open(); |
|---|
| 115 |
|
|---|
| 116 |
$session_type = $_SESSION['fSession::type']; |
|---|
| 117 |
$session_expires = $_SESSION['fSession::expires']; |
|---|
| 118 |
|
|---|
| 119 |
if ($prefix) { |
|---|
| 120 |
foreach ($_SESSION as $key => $value) { |
|---|
| 121 |
if (strpos($key, $prefix) === 0) { |
|---|
| 122 |
unset($_SESSION[$key]); |
|---|
| 123 |
} |
|---|
| 124 |
} |
|---|
| 125 |
} else { |
|---|
| 126 |
$_SESSION = array(); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
$_SESSION['fSession::type'] = $session_type; |
|---|
| 130 |
$_SESSION['fSession::expires'] = $session_expires; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
@return |
|---|
| 138 |
|
|---|
| 139 |
static public function close() |
|---|
| 140 |
{ |
|---|
| 141 |
if (!self::$open) { return; } |
|---|
| 142 |
|
|---|
| 143 |
session_write_close(); |
|---|
| 144 |
unset($_SESSION); |
|---|
| 145 |
self::$open = FALSE; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
@param |
|---|
| 153 |
@return |
|---|
| 154 |
|
|---|
| 155 |
static public function delete($key) |
|---|
| 156 |
{ |
|---|
| 157 |
self::open(); |
|---|
| 158 |
|
|---|
| 159 |
unset($_SESSION[$key]); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
@return |
|---|
| 167 |
|
|---|
| 168 |
static public function destroy() |
|---|
| 169 |
{ |
|---|
| 170 |
self::open(); |
|---|
| 171 |
$_SESSION = array(); |
|---|
| 172 |
if (isset($_COOKIE[session_name()])) { |
|---|
| 173 |
$params = session_get_cookie_params(); |
|---|
| 174 |
setcookie(session_name(), '', time()-43200, $params['path'], $params['domain'], $params['secure']); |
|---|
| 175 |
} |
|---|
| 176 |
session_destroy(); |
|---|
| 177 |
self::regenerateID(); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
@return |
|---|
| 195 |
|
|---|
| 196 |
static public function enablePersistence() |
|---|
| 197 |
{ |
|---|
| 198 |
if (self::$persistent_timespan === NULL) { |
|---|
| 199 |
throw new fProgrammerException( |
|---|
| 200 |
'The method %1$s must be called with the %2$s parameter before calling %3$s', |
|---|
| 201 |
__CLASS__ . '::setLength()', |
|---|
| 202 |
'$persistent_timespan', |
|---|
| 203 |
__CLASS__ . '::enablePersistence()' |
|---|
| 204 |
); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
$current_params = session_get_cookie_params(); |
|---|
| 208 |
|
|---|
| 209 |
$params = array( |
|---|
| 210 |
self::$persistent_timespan, |
|---|
| 211 |
$current_params['path'], |
|---|
| 212 |
$current_params['domain'], |
|---|
| 213 |
$current_params['secure'] |
|---|
| 214 |
); |
|---|
| 215 |
|
|---|
| 216 |
call_user_func_array('session_set_cookie_params', $params); |
|---|
| 217 |
|
|---|
| 218 |
self::open(); |
|---|
| 219 |
|
|---|
| 220 |
$_SESSION['fSession::type'] = 'persistent'; |
|---|
| 221 |
|
|---|
| 222 |
if (isset($_COOKIE[session_name()])) { |
|---|
| 223 |
self::regenerateID(); |
|---|
| 224 |
} |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
@param |
|---|
| 232 |
@param |
|---|
| 233 |
@return |
|---|
| 234 |
|
|---|
| 235 |
static public function get($key, $default_value=NULL) |
|---|
| 236 |
{ |
|---|
| 237 |
self::open(); |
|---|
| 238 |
return (isset($_SESSION[$key])) ? $_SESSION[$key] : $default_value; |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
http://php.net/session_set_cookie_params |
|---|
| 247 |
|
|---|
| 248 |
@return |
|---|
| 249 |
|
|---|
| 250 |
static public function ignoreSubdomain() |
|---|
| 251 |
{ |
|---|
| 252 |
if (self::$open || isset($_SESSION)) { |
|---|
| 253 |
throw new fProgrammerException( |
|---|
| 254 |
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s', |
|---|
| 255 |
__CLASS__ . '::ignoreSubdomain()', |
|---|
| 256 |
__CLASS__ . '::add()', |
|---|
| 257 |
__CLASS__ . '::clear()', |
|---|
| 258 |
__CLASS__ . '::enablePersistence()', |
|---|
| 259 |
__CLASS__ . '::get()', |
|---|
| 260 |
__CLASS__ . '::open()', |
|---|
| 261 |
__CLASS__ . '::set()', |
|---|
| 262 |
'session_start()' |
|---|
| 263 |
); |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
$current_params = session_get_cookie_params(); |
|---|
| 267 |
|
|---|
| 268 |
$params = array( |
|---|
| 269 |
$current_params['lifetime'], |
|---|
| 270 |
$current_params['path'], |
|---|
| 271 |
preg_replace('#.*?([a-z0-9\\-]+\.[a-z]+)$#iD', '.\1', $_SERVER['SERVER_NAME']), |
|---|
| 272 |
$current_params['secure'] |
|---|
| 273 |
); |
|---|
| 274 |
|
|---|
| 275 |
call_user_func_array('session_set_cookie_params', $params); |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
@param |
|---|
| 288 |
@return |
|---|
| 289 |
|
|---|
| 290 |
static public function open($cookie_only_session_id=TRUE) |
|---|
| 291 |
{ |
|---|
| 292 |
if (self::$open) { return; } |
|---|
| 293 |
|
|---|
| 294 |
self::$open = TRUE; |
|---|
| 295 |
|
|---|
| 296 |
if (self::$normal_timespan === NULL) { |
|---|
| 297 |
self::$normal_timespan = ini_get('session.gc_maxlifetime'); |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
if (!isset($_SESSION)) { |
|---|
| 302 |
if ($cookie_only_session_id) { |
|---|
| 303 |
ini_set('session.use_cookies', 1); |
|---|
| 304 |
ini_set('session.use_only_cookies', 1); |
|---|
| 305 |
} |
|---|
| 306 |
session_start(); |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
if (!isset($_SESSION['fSession::expires']) || $_SESSION['fSession::expires'] < $_SERVER['REQUEST_TIME']) { |
|---|
| 311 |
$_SESSION = array(); |
|---|
| 312 |
if (isset($_SESSION['fSession::expires'])) { |
|---|
| 313 |
self::regenerateID(); |
|---|
| 314 |
} |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
if (!isset($_SESSION['fSession::type'])) { |
|---|
| 318 |
$_SESSION['fSession::type'] = 'normal'; |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
if ($_SESSION['fSession::type'] == 'persistent' && self::$persistent_timespan) { |
|---|
| 323 |
$_SESSION['fSession::expires'] = $_SERVER['REQUEST_TIME'] + self::$persistent_timespan; |
|---|
| 324 |
|
|---|
| 325 |
} else { |
|---|
| 326 |
$_SESSION['fSession::expires'] = $_SERVER['REQUEST_TIME'] + self::$normal_timespan; |
|---|
| 327 |
} |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
@internal |
|---|
| 335 |
|
|---|
| 336 |
@return |
|---|
| 337 |
|
|---|
| 338 |
static public function regenerateID() |
|---|
| 339 |
{ |
|---|
| 340 |
if (!self::$regenerated){ |
|---|
| 341 |
session_regenerate_id(); |
|---|
| 342 |
self::$regenerated = TRUE; |
|---|
| 343 |
} |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
@internal |
|---|
| 351 |
|
|---|
| 352 |
@return |
|---|
| 353 |
|
|---|
| 354 |
static public function reset() |
|---|
| 355 |
{ |
|---|
| 356 |
self::$normal_timespan = NULL; |
|---|
| 357 |
self::$persistent_timespan = NULL; |
|---|
| 358 |
self::$regenerated = FALSE; |
|---|
| 359 |
self::destroy(); |
|---|
| 360 |
self::close(); |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
@param |
|---|
| 368 |
@param |
|---|
| 369 |
@return |
|---|
| 370 |
|
|---|
| 371 |
static public function set($key, $value) |
|---|
| 372 |
{ |
|---|
| 373 |
self::open(); |
|---|
| 374 |
$_SESSION[$key] = $value; |
|---|
| 375 |
} |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
@param |
|---|
| 390 |
@param |
|---|
| 391 |
@return |
|---|
| 392 |
|
|---|
| 393 |
static public function setLength($normal_timespan, $persistent_timespan=NULL) |
|---|
| 394 |
{ |
|---|
| 395 |
if (self::$open || isset($_SESSION)) { |
|---|
| 396 |
throw new fProgrammerException( |
|---|
| 397 |
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s', |
|---|
| 398 |
__CLASS__ . '::setLength()', |
|---|
| 399 |
__CLASS__ . '::add()', |
|---|
| 400 |
__CLASS__ . '::clear()', |
|---|
| 401 |
__CLASS__ . '::enablePersistence()', |
|---|
| 402 |
__CLASS__ . '::get()', |
|---|
| 403 |
__CLASS__ . '::open()', |
|---|
| 404 |
__CLASS__ . '::set()', |
|---|
| 405 |
'session_start()' |
|---|
| 406 |
); |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
$seconds = (!is_numeric($normal_timespan)) ? strtotime($normal_timespan) - time() : $normal_timespan; |
|---|
| 410 |
self::$normal_timespan = $seconds; |
|---|
| 411 |
|
|---|
| 412 |
if ($persistent_timespan) { |
|---|
| 413 |
$seconds = (!is_numeric($persistent_timespan)) ? strtotime($persistent_timespan) - time() : $persistent_timespan; |
|---|
| 414 |
self::$persistent_timespan = $seconds; |
|---|
| 415 |
} |
|---|
| 416 |
|
|---|
| 417 |
ini_set('session.gc_maxlifetime', $seconds); |
|---|
| 418 |
} |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
@param |
|---|
| 431 |
@return |
|---|
| 432 |
|
|---|
| 433 |
static public function setPath($directory) |
|---|
| 434 |
{ |
|---|
| 435 |
if (self::$open || isset($_SESSION)) { |
|---|
| 436 |
throw new fProgrammerException( |
|---|
| 437 |
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s', |
|---|
| 438 |
__CLASS__ . '::setPath()', |
|---|
| 439 |
__CLASS__ . '::add()', |
|---|
| 440 |
__CLASS__ . '::clear()', |
|---|
| 441 |
__CLASS__ . '::enablePersistence()', |
|---|
| 442 |
__CLASS__ . '::get()', |
|---|
| 443 |
__CLASS__ . '::open()', |
|---|
| 444 |
__CLASS__ . '::set()', |
|---|
| 445 |
'session_start()' |
|---|
| 446 |
); |
|---|
| 447 |
} |
|---|
| 448 |
|
|---|
| 449 |
if (!$directory instanceof fDirectory) { |
|---|
| 450 |
$directory = new fDirectory($directory); |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
if (!$directory->isWritable()) { |
|---|
| 454 |
throw new fEnvironmentException( |
|---|
| 455 |
'The directory specified, %s, is not writable', |
|---|
| 456 |
$directory->getPath() |
|---|
| 457 |
); |
|---|
| 458 |
} |
|---|
| 459 |
|
|---|
| 460 |
session_save_path($directory->getPath()); |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
@return |
|---|
| 468 |
|
|---|
| 469 |
private function __construct() { } |
|---|
| 470 |
} |
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
will@flourishlib.com |
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|