Files
@ f4e0e3085bf0
Branch filter:
Location: hot67beta/libraries/joomla/session/session.php
f4e0e3085bf0
17.0 KiB
text/x-php
morepad
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 | <?php
/**
* @version $Id: session.php 11409 2009-01-10 02:27:08Z willebil $
* @package Joomla.Framework
* @subpackage Session
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
//Register the session storage class with the loader
JLoader::register('JSessionStorage', dirname(__FILE__).DS.'storage.php');
/**
* Class for managing HTTP sessions
*
* Provides access to session-state values as well as session-level
* settings and lifetime management methods.
* Based on the standart PHP session handling mechanism it provides
* for you more advanced features such as expire timeouts.
*
* @package Joomla.Framework
* @subpackage Session
* @since 1.5
*/
class JSession extends JObject
{
/**
* internal state
*
* @access protected
* @var string $_state one of 'active'|'expired'|'destroyed|'error'
* @see getState()
*/
var $_state = 'active';
/**
* Maximum age of unused session
*
* @access protected
* @var string $_expire minutes
*/
var $_expire = 15;
/**
* The session store object
*
* @access protected
* @var object A JSessionStorage object
*/
var $_store = null;
/**
* security policy
*
* Default values:
* - fix_browser
* - fix_adress
*
* @access protected
* @var array $_security list of checks that will be done.
*/
var $_security = array( 'fix_browser' );
/**
* Force cookies to be SSL only
*
* @access protected
* @default false
* @var bool $force_ssl
*/
var $_force_ssl = false;
/**
* Constructor
*
* @access protected
* @param string $storage
* @param array $options optional parameters
*/
function __construct( $store = 'none', $options = array() )
{
// Register faked "destructor" in PHP4, this needs to happen before creating the session store
if (version_compare(PHP_VERSION, '5') == -1) {
register_shutdown_function((array(&$this, '__destruct')));
}
//Need to destroy any existing sessions started with session.auto_start
if (session_id()) {
session_unset();
session_destroy();
}
//set default sessios save handler
ini_set('session.save_handler', 'files');
//disable transparent sid support
ini_set('session.use_trans_sid', '0');
//create handler
$this->_store =& JSessionStorage::getInstance($store, $options);
//set options
$this->_setOptions( $options );
$this->_setCookieParams();
//load the session
$this->_start();
//initialise the session
$this->_setCounter();
$this->_setTimers();
$this->_state = 'active';
// perform security checks
$this->_validate();
}
/**
* Session object destructor
*
* @access private
* @since 1.5
*/
function __destruct() {
$this->close();
}
/**
* Returns a reference to the global Session object, only creating it
* if it doesn't already exist.
*
* This method must be invoked as:
* <pre> $session = &JSession::getInstance();</pre>
*
* @access public
* @return JSession The Session object.
* @since 1.5
*/
function & getInstance($handler, $options)
{
static $instance;
if (!is_object($instance)) {
$instance = new JSession($handler, $options);
}
return $instance;
}
/**
* Get current state of session
*
* @access public
* @return string The session state
*/
function getState() {
return $this->_state;
}
/**
* Get expiration time in minutes
*
* @access public
* @return integer The session expiration time in minutes
*/
function getExpire() {
return $this->_expire;
}
/**
* Get a session token, if a token isn't set yet one will be generated.
*
* Tokens are used to secure forms from spamming attacks. Once a token
* has been generated the system will check the post request to see if
* it is present, if not it will invalidate the session.
*
* @param boolean $forceNew If true, force a new token to be created
* @access public
* @return string The session token
*/
function getToken($forceNew = false)
{
$token = $this->get( 'session.token' );
//create a token
if( $token === null || $forceNew ) {
$token = $this->_createToken( 12 );
$this->set( 'session.token', $token );
}
return $token;
}
/**
* Method to determine if a token exists in the session. If not the
* session will be set to expired
*
* @param string Hashed token to be verified
* @param boolean If true, expires the session
* @since 1.5
* @static
*/
function hasToken($tCheck, $forceExpire = true)
{
// check if a token exists in the session
$tStored = $this->get( 'session.token' );
//check token
if(($tStored !== $tCheck))
{
if($forceExpire) {
$this->_state = 'expired';
}
return false;
}
return true;
}
/**
* Get session name
*
* @access public
* @return string The session name
*/
function getName()
{
if( $this->_state === 'destroyed' ) {
// @TODO : raise error
return null;
}
return session_name();
}
/**
* Get session id
*
* @access public
* @return string The session name
*/
function getId()
{
if( $this->_state === 'destroyed' ) {
// @TODO : raise error
return null;
}
return session_id();
}
/**
* Get the session handlers
*
* @access public
* @return array An array of available session handlers
*/
function getStores()
{
jimport('joomla.filesystem.folder');
$handlers = JFolder::files(dirname(__FILE__).DS.'storage', '.php$');
$names = array();
foreach($handlers as $handler)
{
$name = substr($handler, 0, strrpos($handler, '.'));
$class = 'JSessionStorage'.ucfirst($name);
//Load the class only if needed
if(!class_exists($class)) {
require_once(dirname(__FILE__).DS.'storage'.DS.$name.'.php');
}
if(call_user_func_array( array( trim($class), 'test' ), null)) {
$names[] = $name;
}
}
return $names;
}
/**
* Check whether this session is currently created
*
* @access public
* @return boolean $result true on success
*/
function isNew()
{
$counter = $this->get( 'session.counter' );
if( $counter === 1 ) {
return true;
}
return false;
}
/**
* Get data from the session store
*
* @static
* @access public
* @param string $name Name of a variable
* @param mixed $default Default value of a variable if not set
* @param string $namespace Namespace to use, default to 'default'
* @return mixed Value of a variable
*/
function &get($name, $default = null, $namespace = 'default')
{
$namespace = '__'.$namespace; //add prefix to namespace to avoid collisions
if($this->_state !== 'active' && $this->_state !== 'expired') {
// @TODO :: generated error here
$error = null;
return $error;
}
if (isset($_SESSION[$namespace][$name])) {
return $_SESSION[$namespace][$name];
}
return $default;
}
/**
* Set data into the session store
*
* @access public
* @param string $name Name of a variable
* @param mixed $value Value of a variable
* @param string $namespace Namespace to use, default to 'default'
* @return mixed Old value of a variable
*/
function set($name, $value, $namespace = 'default')
{
$namespace = '__'.$namespace; //add prefix to namespace to avoid collisions
if($this->_state !== 'active') {
// @TODO :: generated error here
return null;
}
$old = isset($_SESSION[$namespace][$name]) ? $_SESSION[$namespace][$name] : null;
if (null === $value) {
unset($_SESSION[$namespace][$name]);
} else {
$_SESSION[$namespace][$name] = $value;
}
return $old;
}
/**
* Check wheter data exists in the session store
*
* @access public
* @param string $name Name of variable
* @param string $namespace Namespace to use, default to 'default'
* @return boolean $result true if the variable exists
*/
function has( $name, $namespace = 'default' )
{
$namespace = '__'.$namespace; //add prefix to namespace to avoid collisions
if( $this->_state !== 'active' ) {
// @TODO :: generated error here
return null;
}
return isset( $_SESSION[$namespace][$name] );
}
/**
* Unset data from the session store
*
* @access public
* @param string $name Name of variable
* @param string $namespace Namespace to use, default to 'default'
* @return mixed $value the value from session or NULL if not set
*/
function clear( $name, $namespace = 'default' )
{
$namespace = '__'.$namespace; //add prefix to namespace to avoid collisions
if( $this->_state !== 'active' ) {
// @TODO :: generated error here
return null;
}
$value = null;
if( isset( $_SESSION[$namespace][$name] ) ) {
$value = $_SESSION[$namespace][$name];
unset( $_SESSION[$namespace][$name] );
}
return $value;
}
/**
* Start a session
*
* Creates a session (or resumes the current one based on the state of the session)
*
* @access private
* @return boolean $result true on success
*/
function _start()
{
// start session if not startet
if( $this->_state == 'restart' ) {
session_id( $this->_createId() );
}
session_cache_limiter('none');
session_start();
// Send modified header for IE 6.0 Security Policy
header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
return true;
}
/**
* Frees all session variables and destroys all data registered to a session
*
* This method resets the $_SESSION variable and destroys all of the data associated
* with the current session in its storage (file or DB). It forces new session to be
* started after this method is called. It does not unset the session cookie.
*
* @static
* @access public
* @return void
* @see session_unset()
* @see session_destroy()
*/
function destroy()
{
// session was already destroyed
if( $this->_state === 'destroyed' ) {
return true;
}
// In order to kill the session altogether, like to log the user out, the session id
// must also be unset. If a cookie is used to propagate the session id (default behavior),
// then the session cookie must be deleted.
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
session_unset();
session_destroy();
$this->_state = 'destroyed';
return true;
}
/**
* restart an expired or locked session
*
* @access public
* @return boolean $result true on success
* @see destroy
*/
function restart()
{
$this->destroy();
if( $this->_state !== 'destroyed' ) {
// @TODO :: generated error here
return false;
}
// Re-register the session handler after a session has been destroyed, to avoid PHP bug
$this->_store->register();
$this->_state = 'restart';
//regenerate session id
$id = $this->_createId( strlen( $this->getId() ) );
session_id($id);
$this->_start();
$this->_state = 'active';
$this->_validate();
$this->_setCounter();
return true;
}
/**
* Create a new session and copy variables from the old one
*
* @abstract
* @access public
* @return boolean $result true on success
*/
function fork()
{
if( $this->_state !== 'active' ) {
// @TODO :: generated error here
return false;
}
// save values
$values = $_SESSION;
// keep session config
$trans = ini_get( 'session.use_trans_sid' );
if( $trans ) {
ini_set( 'session.use_trans_sid', 0 );
}
$cookie = session_get_cookie_params();
// create new session id
$id = $this->_createId( strlen( $this->getId() ) );
// kill session
session_destroy();
// re-register the session store after a session has been destroyed, to avoid PHP bug
$this->_store->register();
// restore config
ini_set( 'session.use_trans_sid', $trans );
session_set_cookie_params( $cookie['lifetime'], $cookie['path'], $cookie['domain'], $cookie['secure'] );
// restart session with new id
session_id( $id );
session_start();
return true;
}
/**
* Writes session data and ends session
*
* Session data is usually stored after your script terminated without the need
* to call JSession::close(),but as session data is locked to prevent concurrent
* writes only one script may operate on a session at any time. When using
* framesets together with sessions you will experience the frames loading one
* by one due to this locking. You can reduce the time needed to load all the
* frames by ending the session as soon as all changes to session variables are
* done.
*
* @access public
* @see session_write_close()
*/
function close() {
session_write_close();
}
/**
* Create a session id
*
* @static
* @access private
* @return string Session ID
*/
function _createId( )
{
$id = 0;
while (strlen($id) < 32) {
$id .= mt_rand(0, mt_getrandmax());
}
$id = md5( uniqid($id, true));
return $id;
}
/**
* Set session cookie parameters
*
* @access private
*/
function _setCookieParams() {
$cookie = session_get_cookie_params();
if($this->_force_ssl) {
$cookie['secure'] = true;
}
session_set_cookie_params( $cookie['lifetime'], $cookie['path'], $cookie['domain'], $cookie['secure'] );
}
/**
* Create a token-string
*
* @access protected
* @param int $length lenght of string
* @return string $id generated token
*/
function _createToken( $length = 32 )
{
static $chars = '0123456789abcdef';
$max = strlen( $chars ) - 1;
$token = '';
$name = session_name();
for( $i = 0; $i < $length; ++$i ) {
$token .= $chars[ (rand( 0, $max )) ];
}
return md5($token.$name);
}
/**
* Set counter of session usage
*
* @access protected
* @return boolean $result true on success
*/
function _setCounter()
{
$counter = $this->get( 'session.counter', 0 );
++$counter;
$this->set( 'session.counter', $counter );
return true;
}
/**
* Set the session timers
*
* @access protected
* @return boolean $result true on success
*/
function _setTimers()
{
if( !$this->has( 'session.timer.start' ) )
{
$start = time();
$this->set( 'session.timer.start' , $start );
$this->set( 'session.timer.last' , $start );
$this->set( 'session.timer.now' , $start );
}
$this->set( 'session.timer.last', $this->get( 'session.timer.now' ) );
$this->set( 'session.timer.now', time() );
return true;
}
/**
* set additional session options
*
* @access protected
* @param array $options list of parameter
* @return boolean $result true on success
*/
function _setOptions( &$options )
{
// set name
if( isset( $options['name'] ) ) {
session_name( md5($options['name']) );
}
// set id
if( isset( $options['id'] ) ) {
session_id( $options['id'] );
}
// set expire time
if( isset( $options['expire'] ) ) {
$this->_expire = $options['expire'];
}
// get security options
if( isset( $options['security'] ) ) {
$this->_security = explode( ',', $options['security'] );
}
if( isset( $options['force_ssl'] ) ) {
$this->_force_ssl = (bool) $options['force_ssl'];
}
//sync the session maxlifetime
ini_set('session.gc_maxlifetime', $this->_expire);
return true;
}
/**
* Do some checks for security reason
*
* - timeout check (expire)
* - ip-fixiation
* - browser-fixiation
*
* If one check failed, session data has to be cleaned.
*
* @access protected
* @param boolean $restart reactivate session
* @return boolean $result true on success
* @see http://shiflett.org/articles/the-truth-about-sessions
*/
function _validate( $restart = false )
{
// allow to restart a session
if( $restart )
{
$this->_state = 'active';
$this->set( 'session.client.address' , null );
$this->set( 'session.client.forwarded' , null );
$this->set( 'session.client.browser' , null );
$this->set( 'session.token' , null );
}
// check if session has expired
if( $this->_expire )
{
$curTime = $this->get( 'session.timer.now' , 0 );
$maxTime = $this->get( 'session.timer.last', 0 ) + $this->_expire;
// empty session variables
if( $maxTime < $curTime ) {
$this->_state = 'expired';
return false;
}
}
// record proxy forwarded for in the session in case we need it later
if( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$this->set( 'session.client.forwarded', $_SERVER['HTTP_X_FORWARDED_FOR']);
}
// check for client adress
if( in_array( 'fix_adress', $this->_security ) && isset( $_SERVER['REMOTE_ADDR'] ) )
{
$ip = $this->get( 'session.client.address' );
if( $ip === null ) {
$this->set( 'session.client.address', $_SERVER['REMOTE_ADDR'] );
}
else if( $_SERVER['REMOTE_ADDR'] !== $ip )
{
$this->_state = 'error';
return false;
}
}
// check for clients browser
if( in_array( 'fix_browser', $this->_security ) && isset( $_SERVER['HTTP_USER_AGENT'] ) )
{
$browser = $this->get( 'session.client.browser' );
if( $browser === null ) {
$this->set( 'session.client.browser', $_SERVER['HTTP_USER_AGENT']);
}
else if( $_SERVER['HTTP_USER_AGENT'] !== $browser )
{
// $this->_state = 'error';
// return false;
}
}
return true;
}
}
|