Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 889 lines (769 sloc) 27.742 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
<?php
/**
* Wraps the session control functions and the `$_SESSION` superglobal for a more consistent and safer API
*
* A `Cannot send session cache limiter` warning will be triggered if ::open(),
* ::add(), ::clear(), ::delete(), ::get() or ::set() is called after output has
* been sent to the browser. To prevent such a warning, explicitly call ::open()
* before generating any output.
*
* @copyright Copyright (c) 2007-2012 Will Bond, others
* @author Will Bond [wb] <will@flourishlib.com>
* @author Alex Leeds [al] <alex@kingleeds.com>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fSession
*
* @version 1.0.0b22
* @changes 1.0.0b22 Fixed ::destroy() to no longer call ::regenerateID() since it fails after a session is destroyed [wb, 2012-09-20]
* @changes 1.0.0b21 Changed ::regenerateID() to not fail silently if the session has not been opened yet [wb, 2012-09-15]
* @changes 1.0.0b20 Fixed bugs with ::reset() introduced in 1.0.0b19 [wb, 2011-08-23]
* @changes 1.0.0b19 Fixed some session warning messages for PHP 5.1.6 [wb, 2011-07-29]
* @changes 1.0.0b18 Added support for storing session data in memcache, redis and databases using fCache and ::setBackend() [wb, 2011-06-21]
* @changes 1.0.0b17 Updated ::ignoreSubdomain() to use `$_SERVER['HTTP_HOST']` when `$_SERVER['SERVER_NAME']` is not set [wb, 2011-02-01]
* @changes 1.0.0b16 Changed ::delete() to return the value of the key being deleted [wb, 2010-09-19]
* @changes 1.0.0b15 Added documentation about `[sub-key]` syntax [wb, 2010-09-12]
* @changes 1.0.0b14 Backwards Compatibility Break - ::add(), ::delete(), ::get() and ::set() now interpret `[` and `]` as array shorthand and thus they can not be used in keys - added `$beginning` parameter to ::add(), added ::remove() method [wb, 2010-09-12]
* @changes 1.0.0b13 Fixed a bug that prevented working with existing sessions since they did not have the `fSession::expires` key [wb, 2010-08-24]
* @changes 1.0.0b12 Changed ::enablePersistence() to always regenerate the session ID, which ensures the function works even if the ID has already been regenerated by fAuthorizaton [wb, 2010-08-21]
* @changes 1.0.0b11 Updated the class to make sure ::enablePersistence() is called after ::ignoreSubdomain(), ::setLength() and ::setPath() [wb, 2010-05-29]
* @changes 1.0.0b10 Fixed some documentation bugs [wb, 2010-03-03]
* @changes 1.0.0b9 Fixed a bug in ::destroy() where sessions weren't always being properly destroyed [wb, 2009-12-08]
* @changes 1.0.0b8 Fixed a bug that made the unit tests fail on PHP 5.1 [wb, 2009-10-27]
* @changes 1.0.0b7 Backwards Compatibility Break - Removed the `$prefix` parameter from the methods ::delete(), ::get() and ::set() - added the methods ::add(), ::enablePersistence(), ::regenerateID() [wb+al, 2009-10-23]
* @changes 1.0.0b6 Backwards Compatibility Break - the first parameter of ::clear() was removed, use ::delete() instead [wb, 2009-05-08]
* @changes 1.0.0b5 Added documentation about session cache limiter warnings [wb, 2009-05-04]
* @changes 1.0.0b4 The class now works with existing sessions [wb, 2009-05-04]
* @changes 1.0.0b3 Fixed ::clear() to properly handle when `$key` is `NULL` [wb, 2009-02-05]
* @changes 1.0.0b2 Made ::open() public, fixed some consistency issues with setting session options through the class [wb, 2009-01-06]
* @changes 1.0.0b The initial implementation [wb, 2007-06-14]
*/
class fSession
{
// The following constants allow for nice looking callbacks to static methods
const add = 'fSession::add';
const clear = 'fSession::clear';
const close = 'fSession::close';
const closeCache = 'fSession::closeCache';
const delete = 'fSession::delete';
const destroy = 'fSession::destroy';
const destroyCache = 'fSession::destroyCache';
const enablePersistence = 'fSession::enablePersistence';
const gcCache = 'fSession::gcCache';
const get = 'fSession::get';
const ignoreSubdomain = 'fSession::ignoreSubdomain';
const open = 'fSession::open';
const openCache = 'fSession::openCache';
const readCache = 'fSession::readCache';
const regenerateID = 'fSession::regenerateID';
const reset = 'fSession::reset';
const set = 'fSession::set';
const setBackend = 'fSession::setBackend';
const setLength = 'fSession::setLength';
const setPath = 'fSession::setPath';
const writeCache = 'fSession::writeCache';


/**
* The fCache backend to use for the session
*
* @var fCache
*/
static private $backend = NULL;

/**
* The key prefix to use when saving the session to an fCache
*
* @var string
*/
static private $key_prefix = '';

/**
* The length for a normal session
*
* @var integer
*/
static private $normal_timespan = NULL;

/**
* The name of the old session module to revent to when fSession is closed
*
* @var string
*/
static private $old_session_module_name = NULL;

/**
* If the session is open
*
* @var boolean
*/
static private $open = FALSE;

/**
* The length for a persistent session cookie - one that survives browser restarts
*
* @var integer
*/
static private $persistent_timespan = NULL;

/**
* If the session ID was regenerated during this script
*
* @var boolean
*/
static private $regenerated = FALSE;


/**
* Adds a value to an already-existing array value, or to a new array value
*
* @param string $key The name to access the array under - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names
* @param mixed $value The value to add to the array
* @param boolean $beginning If the value should be added to the beginning
* @return void
*/
static public function add($key, $value, $beginning=FALSE)
{
self::open();
$tip =& $_SESSION;

if ($bracket_pos = strpos($key, '[')) {
$original_key = $key;
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);

preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
array_unshift($array_keys, $key);

foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key])) {
$tip[$array_key] = array();
} elseif (!is_array($tip[$array_key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::add()',
$original_key
);
}
$tip =& $tip[$array_key];
}
$key = end($array_keys);
}


if (!isset($tip[$key])) {
$tip[$key] = array();
} elseif (!is_array($tip[$key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::add()',
$key
);
}

if ($beginning) {
array_unshift($tip[$key], $value);
} else {
$tip[$key][] = $value;
}
}


/**
* Removes all session values with the provided prefix
*
* This method will not remove session variables used by this class, which
* are prefixed with `fSession::`.
*
* @param string $prefix The prefix to clear all session values for
* @return void
*/
static public function clear($prefix=NULL)
{
self::open();

$session_type = $_SESSION['fSession::type'];
$session_expires = $_SESSION['fSession::expires'];

if ($prefix) {
foreach ($_SESSION as $key => $value) {
if (strpos($key, $prefix) === 0) {
unset($_SESSION[$key]);
}
}
} else {
$_SESSION = array();
}

$_SESSION['fSession::type'] = $session_type;
$_SESSION['fSession::expires'] = $session_expires;
}


/**
* Closes the session for writing, allowing other pages to open the session
*
* @return void
*/
static public function close()
{
if (!self::$open) { return; }

session_write_close();
unset($_SESSION);
self::$open = FALSE;
if (self::$old_session_module_name) {
session_module_name(self::$old_session_module_name);
}
}


/**
* Callback to close the session
*
* @internal
*
* @return boolean If the operation succeeded
*/
static public function closeCache()
{
return TRUE;
}


/**
* Deletes a value from the session
*
* @param string $key The key of the value to delete - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names
* @param mixed $default_value The value to return if the `$key` is not set
* @return mixed The value of the `$key` that was deleted
*/
static public function delete($key, $default_value=NULL)
{
self::open();

$value = $default_value;

if ($bracket_pos = strpos($key, '[')) {
$original_key = $key;
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);

if (!isset($_SESSION[$key])) {
return $value;
}

preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);

$tip =& $_SESSION[$key];

foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key])) {
return $value;
} elseif (!is_array($tip[$array_key])) {
throw new fProgrammerException(
'%1$s was called for an element, %2$s, which is not an array',
__CLASS__ . '::delete()',
$original_key
);
}
$tip =& $tip[$array_key];
}

$key = end($array_keys);

} else {
$tip =& $_SESSION;
}

if (isset($tip[$key])) {
$value = $tip[$key];
unset($tip[$key]);
}

return $value;
}


/**
* Destroys the session, removing all values
*
* @return void
*/
static public function destroy()
{
self::open();
$_SESSION = array();
unset($_SESSION);
if (isset($_COOKIE[session_name()])) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time()-43200, $params['path'], $params['domain'], $params['secure']);
}
session_destroy();
}


/**
* Callback to destroy a session
*
* @internal
*
* @param string $id The session to destroy
* @return boolean If the operation succeeded
*/
static public function destroyCache($id)
{
return self::$backend->delete(self::$key_prefix . $id);
}


/**
* Changed the session to use a time-based cookie instead of a session-based cookie
*
* The length of the time-based cookie is controlled by ::setLength(). When
* this method is called, a time-based cookie is used to store the session
* ID. This means the session can persist browser restarts. Normally, a
* session-based cookie is used, which is wiped when a browser restart
* occurs.
*
* This method should be called during the login process and will normally
* be controlled by a checkbox or similar where the user can indicate if
* they want to stay logged in for an extended period of time.
*
* @return void
*/
static public function enablePersistence()
{
if (self::$persistent_timespan === NULL) {
throw new fProgrammerException(
'The method %1$s must be called with the %2$s parameter before calling %3$s',
__CLASS__ . '::setLength()',
'$persistent_timespan',
__CLASS__ . '::enablePersistence()'
);
}

$current_params = session_get_cookie_params();

$params = array(
self::$persistent_timespan,
$current_params['path'],
$current_params['domain'],
$current_params['secure']
);

call_user_func_array('session_set_cookie_params', $params);

self::open();

$_SESSION['fSession::type'] = 'persistent';

session_regenerate_id();
self::$regenerated = TRUE;
}


/**
* Callback to garbage-collect the session cache
*
* @internal
*
* @return boolean If the operation succeeded
*/
static public function gcCache()
{
self::$backend->clean();
return TRUE;
}


/**
* Gets data from the `$_SESSION` superglobal
*
* @param string $key The name to get the value for - array elements can be accessed via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names
* @param mixed $default_value The default value to use if the requested key is not set
* @return mixed The data element requested
*/
static public function get($key, $default_value=NULL)
{
self::open();

$array_dereference = NULL;
if ($bracket_pos = strpos($key, '[')) {
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
}

if (!isset($_SESSION[$key])) {
return $default_value;
}
$value = $_SESSION[$key];

if ($array_dereference) {
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
foreach ($array_keys as $array_key) {
if (!is_array($value) || !isset($value[$array_key])) {
$value = $default_value;
break;
}
$value = $value[$array_key];
}
}

return $value;
}


/**
* Sets the session to run on the main domain, not just the specific subdomain currently being accessed
*
* This method should be called after any calls to
* [http://php.net/session_set_cookie_params `session_set_cookie_params()`].
*
* @return void
*/
static public function ignoreSubdomain()
{
if (self::$open || isset($_SESSION)) {
throw new fProgrammerException(
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s',
__CLASS__ . '::ignoreSubdomain()',
__CLASS__ . '::add()',
__CLASS__ . '::clear()',
__CLASS__ . '::enablePersistence()',
__CLASS__ . '::get()',
__CLASS__ . '::open()',
__CLASS__ . '::set()',
'session_start()'
);
}

$current_params = session_get_cookie_params();

if (isset($_SERVER['SERVER_NAME'])) {
$domain = $_SERVER['SERVER_NAME'];
} elseif (isset($_SERVER['HTTP_HOST'])) {
$domain = $_SERVER['HTTP_HOST'];
} else {
throw new fEnvironmentException(
'The domain name could not be found in %1$s or %2$s. Please set one of these keys to use %3$s.',
'$_SERVER[\'SERVER_NAME\']',
'$_SERVER[\'HTTP_HOST\']',
__CLASS__ . '::ignoreSubdomain()'
);
}

$params = array(
$current_params['lifetime'],
$current_params['path'],
preg_replace('#.*?([a-z0-9\\-]+\.[a-z]+)$#iD', '.\1', $domain),
$current_params['secure']
);

call_user_func_array('session_set_cookie_params', $params);
}


/**
* Opens the session for writing, is automatically called by ::clear(), ::get() and ::set()
*
* A `Cannot send session cache limiter` warning will be triggered if this,
* ::add(), ::clear(), ::delete(), ::get() or ::set() is called after output
* has been sent to the browser. To prevent such a warning, explicitly call
* this method before generating any output.
*
* @param boolean $cookie_only_session_id If the session id should only be allowed via cookie - this is a security issue and should only be set to `FALSE` when absolutely necessary
* @return void
*/
static public function open($cookie_only_session_id=TRUE)
{
if (self::$open) { return; }

self::$open = TRUE;

if (self::$normal_timespan === NULL) {
self::$normal_timespan = ini_get('session.gc_maxlifetime');
}

if (self::$backend && isset($_SESSION) && session_module_name() != 'user') {
throw new fProgrammerException(
'A custom backend was provided by %1$s, however the session has already been started, so it can not be used',
__CLASS__ . '::setBackend()'
);
}

// If the session is already open, we just piggy-back without setting options
if (!isset($_SESSION)) {
if ($cookie_only_session_id) {
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
}
// If we are using a custom backend we have to set the session handler
if (self::$backend && session_module_name() != 'user') {
session_set_save_handler(
array('fSession', 'openCache'),
array('fSession', 'closeCache'),
array('fSession', 'readCache'),
array('fSession', 'writeCache'),
array('fSession', 'destroyCache'),
array('fSession', 'gcCache')
);
}
session_start();
}

// If the session has existed for too long, reset it
if (isset($_SESSION['fSession::expires']) && $_SESSION['fSession::expires'] < $_SERVER['REQUEST_TIME']) {
$_SESSION = array();
self::regenerateID();
}

if (!isset($_SESSION['fSession::type'])) {
$_SESSION['fSession::type'] = 'normal';
}

// We store the expiration time for a session to allow for both normal and persistent sessions
if ($_SESSION['fSession::type'] == 'persistent' && self::$persistent_timespan) {
$_SESSION['fSession::expires'] = $_SERVER['REQUEST_TIME'] + self::$persistent_timespan;

} else {
$_SESSION['fSession::expires'] = $_SERVER['REQUEST_TIME'] + self::$normal_timespan;
}
}


/**
* Callback to open the session
*
* @internal
*
* @return boolean If the operation succeeded
*/
static public function openCache()
{
return TRUE;
}


/**
* Callback to read a session's values
*
* @internal
*
* @param string $id The session to read
* @return string The session's serialized data
*/
static public function readCache($id)
{
return self::$backend->get(self::$key_prefix . $id, '');
}


/**
* Regenerates the session ID, but only once per script execution
*
* @internal
*
* @return void
*/
static public function regenerateID()
{
if (!self::$regenerated){
self::open();
if (!session_regenerate_id(TRUE)) {
throw new fUnexpectedException('There was an error regenerating the session id');
}
self::$regenerated = TRUE;
}
}


/**
* Removes and returns the value from the end of an array value
*
* @param string $key The name of the element to remove the value from - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names
* @param boolean $beginning If the value should be removed to the beginning
* @return mixed The value that was removed
*/
static public function remove($key, $beginning=FALSE)
{
self::open();
$tip =& $_SESSION;

if ($bracket_pos = strpos($key, '[')) {
$original_key = $key;
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);

preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
array_unshift($array_keys, $key);

foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key])) {
return NULL;
} elseif (!is_array($tip[$array_key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::remove()',
$original_key
);
}
$tip =& $tip[$array_key];
}
$key = end($array_keys);
}


if (!isset($tip[$key])) {
return NULL;
} elseif (!is_array($tip[$key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::remove()',
$key
);
}

if ($beginning) {
return array_shift($tip[$key]);
}

return array_pop($tip[$key]);
}


/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$normal_timespan = NULL;
self::$persistent_timespan = NULL;
self::$regenerated = FALSE;
self::destroy();
self::close();
self::$backend = NULL;
self::$key_prefix = '';
}


/**
* Sets data to the `$_SESSION` superglobal
*
* @param string $key The name to save the value under - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names
* @param mixed $value The value to store
* @return void
*/
static public function set($key, $value)
{
self::open();
$tip =& $_SESSION;

if ($bracket_pos = strpos($key, '[')) {
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);

preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
array_unshift($array_keys, $key);

foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key]) || !is_array($tip[$array_key])) {
$tip[$array_key] = array();
}
$tip =& $tip[$array_key];
}
$tip[end($array_keys)] = $value;

} else {
$tip[$key] = $value;
}
}


/**
* Sets an fCache object to store sessions in
*
* While any type of fCache backend should technically work, it would be
* unwise to use the `file` and `directory` types. The `file` caching
* backend stores all values in a single file, which would quickly become a
* performance bottleneck and could cause data loss with many concurrent
* users. The `directory` caching backend would not make sense since it is
* the same general functionality as the default session handler, but it
* would be slightly slower since it is written in PHP and not C.
*
* It is recommended to set the `serializer` and `unserializer` `$config`
* settings on the fCache object to `string` for the best performance and
* minimal storage space.
*
* For better performance, check out using the built-in session handlers
* that are bundled with the following extensions:
*
* - [http://php.net/memcached.sessions memcached]
* - [http://php.net/memcache.examples-overview#example-3596 memcache]
* - [https://github.com/nicolasff/phpredis redis]
*
* The [http://pecl.php.net/package/igbinary igbinary] extension can
* provide even more of a performance boost by storing serialized data in
* binary format instead of as text.
*
* @param fCache $backend An fCache object to store session values in
* @param string $key_prefix A prefix to add to all session IDs before storing them in the cache
* @return void
*/
static public function setBackend($backend, $key_prefix='')
{
if (self::$open || isset($_SESSION)) {
throw new fProgrammerException(
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s',
__CLASS__ . '::setLength()',
__CLASS__ . '::add()',
__CLASS__ . '::clear()',
__CLASS__ . '::enablePersistence()',
__CLASS__ . '::get()',
__CLASS__ . '::open()',
__CLASS__ . '::set()',
'session_start()'
);
}

self::$old_session_module_name = session_module_name();

self::$backend = $backend;
self::$key_prefix = $key_prefix;

session_set_save_handler(
array('fSession', 'openCache'),
array('fSession', 'closeCache'),
array('fSession', 'readCache'),
array('fSession', 'writeCache'),
array('fSession', 'destroyCache'),
array('fSession', 'gcCache')
);

// This ensures the session is closed before the fCache object is destructed
register_shutdown_function(array('fSession', 'close'));
}


/**
* Sets the minimum length of a session - PHP might not clean up the session data right away once this timespan has elapsed
*
* Please be sure to set a custom session path via ::setPath() to ensure
* another site on the server does not garbage collect the session files
* from this site!
*
* Both of the timespan can accept either a integer timespan in seconds,
* or an english description of a timespan (e.g. `'30 minutes'`, `'1 hour'`,
* `'1 day 2 hours'`).
*
* @param string|integer $normal_timespan The normal, session-based cookie, length for the session
* @param string|integer $persistent_timespan The persistent, timed-based cookie, length for the session - this is enabled by calling ::enabledPersistence() during login
* @return void
*/
static public function setLength($normal_timespan, $persistent_timespan=NULL)
{
if (self::$open || isset($_SESSION)) {
throw new fProgrammerException(
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s',
__CLASS__ . '::setLength()',
__CLASS__ . '::add()',
__CLASS__ . '::clear()',
__CLASS__ . '::enablePersistence()',
__CLASS__ . '::get()',
__CLASS__ . '::open()',
__CLASS__ . '::set()',
'session_start()'
);
}

$seconds = (!is_numeric($normal_timespan)) ? strtotime($normal_timespan) - time() : $normal_timespan;
self::$normal_timespan = $seconds;

if ($persistent_timespan) {
$seconds = (!is_numeric($persistent_timespan)) ? strtotime($persistent_timespan) - time() : $persistent_timespan;
self::$persistent_timespan = $seconds;
}

ini_set('session.gc_maxlifetime', $seconds);
}


/**
* Sets the path to store session files in
*
* This method should always be called with a non-standard directory
* whenever ::setLength() is called to ensure that another site on the
* server does not garbage collect the session files for this site.
*
* Standard session directories usually include `/tmp` and `/var/tmp`.
*
* @param string|fDirectory $directory The directory to store session files in
* @return void
*/
static public function setPath($directory)
{
if (self::$open || isset($_SESSION)) {
throw new fProgrammerException(
'%1$s must be called before any of %2$s, %3$s, %4$s, %5$s, %6$s, %7$s or %8$s',
__CLASS__ . '::setPath()',
__CLASS__ . '::add()',
__CLASS__ . '::clear()',
__CLASS__ . '::enablePersistence()',
__CLASS__ . '::get()',
__CLASS__ . '::open()',
__CLASS__ . '::set()',
'session_start()'
);
}

if (!$directory instanceof fDirectory) {
$directory = new fDirectory($directory);
}

if (!$directory->isWritable()) {
throw new fEnvironmentException(
'The directory specified, %s, is not writable',
$directory->getPath()
);
}

session_save_path($directory->getPath());
}


/**
* Callback to write a session's values
*
* @internal
*
* @param string $id The session to write
* @param string $values The serialized values
* @return string The session's serialized data
*/
static public function writeCache($id, $values)
{
return self::$backend->set(self::$key_prefix . $id, $values);
}


/**
* Forces use as a static class
*
* @return fSession
*/
private function __construct() { }
}



/**
* Copyright (c) 2007-2012 Will Bond <will@flourishlib.com>, others
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Something went wrong with that request. Please try again.