2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles all manipulation of the session.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-08-17 07:45:15 +02:00
|
|
|
* The static methods are used to manipulate the currently active controller's session.
|
|
|
|
* The instance methods are used to manipulate a particular session. There can be more than one of these created.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* In order to support things like testing, the session is associated with a particular Controller. In normal usage,
|
|
|
|
* this is loaded from and saved to the regular PHP session, but for things like static-page-generation and
|
|
|
|
* unit-testing, you can create multiple Controllers, each with their own session.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session
|
|
|
|
* data.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Saving Data</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* You can write a value to a users session from your PHP code using the static function {@link Session::set()}. You
|
|
|
|
* can add this line in any function or file you wish to save the value.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* Session::set('MyValue', 6);
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* Saves the value of "6" to the MyValue session data. You can also save arrays or serialized objects in session (but
|
|
|
|
* note there may be size restrictions as to how much you can save)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* // save a variable
|
|
|
|
* $var = 1;
|
|
|
|
* Session::set('MyVar', $var);
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* // saves an array
|
|
|
|
* Session::set('MyArrayOfValues', array('1','2','3'));
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* // saves an object (you'll have to unserialize it back)
|
|
|
|
* $object = new Object();
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* Session::set('MyObject', serialize($object));
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Accessing Data</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Once you have saved a value to the Session you can access it by using the {@link Session::get()} function.
|
2010-10-15 05:55:22 +02:00
|
|
|
* Like the {@link Session::set()} function you can use this anywhere in your PHP files.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* The values in the comments are the values stored from the previous example.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2012-09-19 12:07:39 +02:00
|
|
|
* public function bar() {
|
2010-10-15 05:55:22 +02:00
|
|
|
* $value = Session::get('MyValue'); // $value = 6
|
2014-08-15 08:53:05 +02:00
|
|
|
* $var = Session::get('MyVar'); // $var = 1
|
2010-10-15 05:55:22 +02:00
|
|
|
* $array = Session::get('MyArrayOfValues'); // $array = array(1,2,3)
|
|
|
|
* $object = Session::get('MyObject', unserialize($object)); // $object = Object()
|
|
|
|
* }
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* You can also get all the values in the session at once. This is useful for debugging.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2011-04-28 13:06:29 +02:00
|
|
|
* Session::get_all(); // returns an array of all the session values.
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Clearing Data</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* Once you have accessed a value from the Session it doesn't automatically wipe the value from the Session, you have
|
|
|
|
* to specifically remove it. To clear a value you can either delete 1 session value by the name that you saved it
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2014-10-24 09:59:28 +02:00
|
|
|
* Session::clear('MyValue'); // MyValue is no longer 6.
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* Or you can clear every single value in the session at once. Note SilverStripe stores some of its own session data
|
|
|
|
* including form and page comment information. None of this is vital but clear_all will clear everything.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2011-04-28 13:06:29 +02:00
|
|
|
* Session::clear_all();
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* @see Cookie
|
|
|
|
* @todo This class is currently really basic and could do with a more well-thought-out implementation.
|
2010-04-12 05:32:28 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-04-12 05:32:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
class Session {
|
2010-10-19 02:55:02 +02:00
|
|
|
|
2008-08-12 06:46:04 +02:00
|
|
|
/**
|
2013-06-21 00:56:00 +02:00
|
|
|
* @var $timeout Set session timeout in seconds.
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-12 06:46:04 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $timeout = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $session_ips = array();
|
2010-10-19 02:55:02 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cookie_domain;
|
2010-10-19 02:55:02 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cookie_path;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $session_store_path;
|
2010-10-19 02:55:02 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $cookie_secure = false;
|
2010-12-13 23:30:49 +01:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Session data
|
|
|
|
*/
|
|
|
|
protected $data = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:55:02 +02:00
|
|
|
protected $changedData = array();
|
|
|
|
|
2013-07-06 05:15:49 +02:00
|
|
|
protected function userAgent() {
|
|
|
|
if (isset($_SERVER['HTTP_USER_AGENT'])) {
|
|
|
|
return $_SERVER['HTTP_USER_AGENT'];
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-26 06:43:58 +02:00
|
|
|
/**
|
|
|
|
* Start PHP session, then create a new Session object with the given start data.
|
|
|
|
*
|
2014-06-19 00:38:26 +02:00
|
|
|
* @param $data array|Session Can be an array of data (such as $_SESSION) or another Session object to clone.
|
2012-04-26 06:43:58 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($data) {
|
2012-04-26 06:43:58 +02:00
|
|
|
if($data instanceof Session) $data = $data->inst_getAll();
|
|
|
|
|
|
|
|
$this->data = $data;
|
2017-11-30 02:48:36 +01:00
|
|
|
$this->expireIfInvalid();
|
2012-04-26 06:43:58 +02:00
|
|
|
}
|
|
|
|
|
2010-10-19 02:55:02 +02:00
|
|
|
/**
|
|
|
|
* Cookie domain, for example 'www.php.net'.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:55:02 +02:00
|
|
|
* To make cookies visible on all subdomains then the domain
|
|
|
|
* must be prefixed with a dot like '.php.net'.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.cookie_domain" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:55:02 +02:00
|
|
|
* @param string $domain The domain to set
|
|
|
|
*/
|
|
|
|
public static function set_cookie_domain($domain) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.cookie_domain" config setting instead');
|
2014-01-17 15:27:12 +01:00
|
|
|
Config::inst()->update('Session', 'cookie_domain', $domain);
|
2010-10-19 02:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cookie domain.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.cookie_domain" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:55:02 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_cookie_domain() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.cookie_domain" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
return Config::inst()->get('Session', 'cookie_domain');
|
2010-10-19 02:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to set on the domain where the session cookie will work.
|
|
|
|
* Use a single slash ('/') for all paths on the domain.
|
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.cookie_path" config setting instead
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2010-10-19 02:55:02 +02:00
|
|
|
* @param string $path The path to set
|
|
|
|
*/
|
|
|
|
public static function set_cookie_path($path) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.cookie_path" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Session', 'cookie_path', $path);
|
2010-10-19 02:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path on the domain where the session cookie will work.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.cookie_path" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:55:02 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_cookie_path() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.cookie_path" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
if(Config::inst()->get('Session', 'cookie_path')) {
|
|
|
|
return Config::inst()->get('Session', 'cookie_path');
|
2010-10-19 02:55:02 +02:00
|
|
|
} else {
|
|
|
|
return Director::baseURL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-13 23:30:49 +01:00
|
|
|
/**
|
|
|
|
* Secure cookie, tells the browser to only send it over SSL.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.cookie_secure" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-13 23:30:49 +01:00
|
|
|
* @param boolean $secure
|
|
|
|
*/
|
|
|
|
public static function set_cookie_secure($secure) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.cookie_secure" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Session', 'cookie_secure', (bool)$secure);
|
2010-12-13 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get if the cookie is secure
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.cookie_secure" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-13 23:30:49 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function get_cookie_secure() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.cookie_secure" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
return Config::inst()->get('Session', 'cookie_secure');
|
2010-12-13 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 22:47:22 +01:00
|
|
|
/**
|
|
|
|
* Set the session store path
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.session_store_path" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-22 22:47:22 +01:00
|
|
|
* @param string $path Filesystem path to the session store
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2011-03-22 22:47:22 +01:00
|
|
|
public static function set_session_store_path($path) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.session_store_path" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Session', 'session_store_path', $path);
|
2011-03-22 22:47:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-22 22:47:22 +01:00
|
|
|
/**
|
|
|
|
* Get the session store path
|
|
|
|
* @return string
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated since version 4.0
|
2011-03-22 22:47:22 +01:00
|
|
|
*/
|
|
|
|
public static function get_session_store_path() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.session_store_path" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
return Config::inst()->get('Session', 'session_store_path');
|
2011-03-22 22:47:22 +01:00
|
|
|
}
|
|
|
|
|
2008-08-12 06:46:04 +02:00
|
|
|
/**
|
|
|
|
* Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or
|
|
|
|
* individual IPv4 addresses. The key is an IP address or range and the value is the time
|
|
|
|
* until the session expires in seconds. For example:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-12 06:46:04 +02:00
|
|
|
* Session::set_timeout_ips(array(
|
2014-08-15 08:53:05 +02:00
|
|
|
* '127.0.0.1' => 36000
|
2008-08-12 06:46:04 +02:00
|
|
|
* ));
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-12 06:46:04 +02:00
|
|
|
* Any user connecting from 127.0.0.1 (localhost) will have their session expired after 10 hours.
|
|
|
|
*
|
|
|
|
* Session::set_timeout is used to set the timeout value for any users whose address is not in the given IP range.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.timeout_ips" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-12 06:46:04 +02:00
|
|
|
* @param array $session_ips Array of IPv4 rules.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
public static function set_timeout_ips($ips) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.timeout_ips" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Session', 'timeout_ips', $ips);
|
2008-08-12 06:46:04 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:50:43 +02:00
|
|
|
/**
|
|
|
|
* Add a value to a specific key in the session array
|
|
|
|
*/
|
|
|
|
public static function add_to_array($name, $val) {
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::current_session()->inst_addToArray($name, $val);
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Set a key/value pair in the session
|
|
|
|
*
|
|
|
|
* @param string $name Key
|
|
|
|
* @param string $val Value
|
|
|
|
*/
|
|
|
|
public static function set($name, $val) {
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::current_session()->inst_set($name, $val);
|
2010-04-12 05:32:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Return a specific value by session key
|
|
|
|
*
|
|
|
|
* @param string $name Key to lookup
|
|
|
|
*/
|
2007-08-17 07:45:15 +02:00
|
|
|
public static function get($name) {
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::current_session()->inst_get($name);
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Return all the values in session
|
2010-10-15 04:50:43 +02:00
|
|
|
*
|
|
|
|
* @return Array
|
2010-04-12 05:32:28 +02:00
|
|
|
*/
|
2010-10-15 04:50:43 +02:00
|
|
|
public static function get_all() {
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::current_session()->inst_getAll();
|
2007-11-12 02:47:48 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Clear a given session key, value pair.
|
|
|
|
*
|
|
|
|
* @param string $name Key to lookup
|
|
|
|
*/
|
|
|
|
public static function clear($name) {
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::current_session()->inst_clear($name);
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
/**
|
2010-04-12 05:32:28 +02:00
|
|
|
* Clear all the values
|
2012-04-20 18:36:11 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2007-08-17 07:45:15 +02:00
|
|
|
*/
|
2010-10-15 04:50:43 +02:00
|
|
|
public static function clear_all() {
|
2012-04-20 18:36:11 +02:00
|
|
|
self::current_session()->inst_clearAll();
|
2014-08-15 08:53:05 +02:00
|
|
|
self::$default_session = null;
|
2010-04-12 05:32:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
/**
|
2010-04-12 05:32:28 +02:00
|
|
|
* Save all the values in our session to $_SESSION
|
2007-08-17 07:45:15 +02:00
|
|
|
*/
|
2010-04-12 05:32:28 +02:00
|
|
|
public static function save() {
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::current_session()->inst_save();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:28 +02:00
|
|
|
protected static $default_session = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:28 +02:00
|
|
|
protected static function current_session() {
|
|
|
|
if(Controller::has_curr()) {
|
|
|
|
return Controller::curr()->getSession();
|
|
|
|
} else {
|
2014-06-19 00:38:26 +02:00
|
|
|
if(!self::$default_session) {
|
|
|
|
self::$default_session = Injector::inst()->create('Session', isset($_SESSION) ? $_SESSION : array());
|
|
|
|
}
|
|
|
|
|
2010-10-19 05:35:28 +02:00
|
|
|
return self::$default_session;
|
|
|
|
}
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
|
|
|
|
2014-06-19 00:38:26 +02:00
|
|
|
public function inst_start($sid = null) {
|
|
|
|
$path = Config::inst()->get('Session', 'cookie_path');
|
|
|
|
if(!$path) $path = Director::baseURL();
|
|
|
|
$domain = Config::inst()->get('Session', 'cookie_domain');
|
2016-10-04 15:21:32 +02:00
|
|
|
// Director::baseURL can return absolute domain names - this extracts the relevant parts
|
|
|
|
// for the session otherwise we can get broken session cookies
|
|
|
|
if (Director::is_absolute_url($path)) {
|
|
|
|
$urlParts = parse_url($path);
|
|
|
|
$path = $urlParts['path'];
|
|
|
|
if (!$domain) {
|
|
|
|
$domain = $urlParts['host'];
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 00:38:26 +02:00
|
|
|
$secure = Director::is_https() && Config::inst()->get('Session', 'cookie_secure');
|
|
|
|
$session_path = Config::inst()->get('Session', 'session_store_path');
|
|
|
|
$timeout = Config::inst()->get('Session', 'timeout');
|
|
|
|
|
|
|
|
if(!session_id() && !headers_sent()) {
|
|
|
|
if($domain) {
|
|
|
|
session_set_cookie_params($timeout, $path, $domain, $secure, true);
|
|
|
|
} else {
|
|
|
|
session_set_cookie_params($timeout, $path, null, $secure, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow storing the session in a non standard location
|
|
|
|
if($session_path) session_save_path($session_path);
|
|
|
|
|
|
|
|
// If we want a secure cookie for HTTPS, use a seperate session name. This lets us have a
|
|
|
|
// seperate (less secure) session for non-HTTPS requests
|
|
|
|
if($secure) session_name('SECSESSID');
|
|
|
|
|
|
|
|
if($sid) session_id($sid);
|
2014-08-21 07:30:03 +02:00
|
|
|
session_start();
|
2014-06-19 00:38:26 +02:00
|
|
|
|
|
|
|
$this->data = isset($_SESSION) ? $_SESSION : array();
|
|
|
|
}
|
|
|
|
|
2017-11-30 02:48:36 +01:00
|
|
|
// Ensure session is validated on start
|
|
|
|
$this->expireIfInvalid();
|
|
|
|
|
2014-06-19 00:38:26 +02:00
|
|
|
// Modify the timeout behaviour so it's the *inactive* time before the session expires.
|
|
|
|
// By default it's the total session lifetime
|
|
|
|
if($timeout && !headers_sent()) {
|
|
|
|
Cookie::set(session_name(), session_id(), $timeout/86400, $path, $domain ? $domain
|
|
|
|
: null, $secure, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function inst_destroy($removeCookie = true) {
|
|
|
|
if(session_id()) {
|
|
|
|
if($removeCookie) {
|
2014-09-26 06:01:23 +02:00
|
|
|
$path = Config::inst()->get('Session', 'cookie_path') ?: Director::baseURL();
|
2014-06-19 00:38:26 +02:00
|
|
|
$domain = Config::inst()->get('Session', 'cookie_domain');
|
|
|
|
$secure = Config::inst()->get('Session', 'cookie_secure');
|
|
|
|
|
2014-09-26 06:01:23 +02:00
|
|
|
Cookie::force_expiry(session_name(), $path, $domain, $secure, true);
|
2014-06-19 00:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session_destroy();
|
|
|
|
|
|
|
|
// Clean up the superglobal - session_destroy does not do it.
|
|
|
|
// http://nz1.php.net/manual/en/function.session-destroy.php
|
|
|
|
unset($_SESSION);
|
|
|
|
$this->data = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_set($name, $val) {
|
2009-07-31 07:38:50 +02:00
|
|
|
// Quicker execution path for "."-free names
|
|
|
|
if(strpos($name,'.') === false) {
|
|
|
|
$this->data[$name] = $val;
|
|
|
|
$this->changedData[$name] = $val;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$names = explode('.', $name);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-31 07:38:50 +02:00
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
|
|
|
$var = &$this->data;
|
|
|
|
$diffVar = &$this->changedData;
|
2012-06-17 23:46:52 +02:00
|
|
|
|
|
|
|
// Iterate twice over the names - once to see if the value needs to be changed,
|
|
|
|
// and secondly to get the changed data value. This is done to solve a problem
|
|
|
|
// where iterating over the diff var would create empty arrays, and the value
|
|
|
|
// would then not be set, inadvertently clearing session values.
|
2009-07-31 07:38:50 +02:00
|
|
|
foreach($names as $n) {
|
|
|
|
$var = &$var[$n];
|
|
|
|
}
|
2012-04-26 06:43:58 +02:00
|
|
|
|
|
|
|
if($var !== $val) {
|
2012-06-17 23:46:52 +02:00
|
|
|
foreach($names as $n) {
|
|
|
|
$diffVar = &$diffVar[$n];
|
|
|
|
}
|
|
|
|
|
2012-04-26 06:43:58 +02:00
|
|
|
$var = $val;
|
|
|
|
$diffVar = $val;
|
|
|
|
}
|
2009-07-31 07:38:50 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-04-26 06:43:58 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_addToArray($name, $val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$names = explode('.', $name);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
2007-08-17 07:45:15 +02:00
|
|
|
$var = &$this->data;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$this->changedData;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($names as $n) {
|
|
|
|
$var = &$var[$n];
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$diffVar[$n];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$var[] = $val;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar[sizeof($var)-1] = $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_get($name) {
|
2009-07-31 07:38:50 +02:00
|
|
|
// Quicker execution path for "."-free names
|
|
|
|
if(strpos($name,'.') === false) {
|
|
|
|
if(isset($this->data[$name])) return $this->data[$name];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-31 07:38:50 +02:00
|
|
|
} else {
|
|
|
|
$names = explode('.', $name);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-31 07:38:50 +02:00
|
|
|
if(!isset($this->data)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-31 07:38:50 +02:00
|
|
|
$var = $this->data;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-07-31 07:38:50 +02:00
|
|
|
foreach($names as $n) {
|
|
|
|
if(!isset($var[$n])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$var = $var[$n];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 07:38:50 +02:00
|
|
|
return $var;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_clear($name) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$names = explode('.', $name);
|
|
|
|
|
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
2007-08-17 07:45:15 +02:00
|
|
|
$var = &$this->data;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$this->changedData;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($names as $n) {
|
2012-04-26 06:43:58 +02:00
|
|
|
// don't clear a record that doesn't exist
|
|
|
|
if(!isset($var[$n])) return;
|
2007-07-19 12:40:28 +02:00
|
|
|
$var = &$var[$n];
|
2012-04-26 06:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// only loop to find data within diffVar if var is proven to exist in the above loop
|
|
|
|
foreach($names as $n) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$diffVar[$n];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-04-12 05:32:28 +02:00
|
|
|
|
2012-04-26 06:43:58 +02:00
|
|
|
if($var !== null) {
|
|
|
|
$var = null;
|
|
|
|
$diffVar = null;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-04-26 06:43:58 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
public function inst_clearAll() {
|
|
|
|
if($this->data && is_array($this->data)) {
|
|
|
|
foreach(array_keys($this->data) as $key) {
|
|
|
|
$this->inst_clear($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-26 06:43:58 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_getAll() {
|
|
|
|
return $this->data;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-07-06 05:15:49 +02:00
|
|
|
|
|
|
|
public function inst_finalize() {
|
|
|
|
$this->inst_set('HTTP_USER_AGENT', $this->userAgent());
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Save data to session
|
|
|
|
* Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2007-11-12 02:47:48 +01:00
|
|
|
public function inst_save() {
|
2012-04-26 06:43:58 +02:00
|
|
|
if($this->changedData) {
|
2013-07-06 05:15:49 +02:00
|
|
|
$this->inst_finalize();
|
2014-06-19 00:38:26 +02:00
|
|
|
|
|
|
|
if(!isset($_SESSION)) {
|
|
|
|
$this->inst_start();
|
|
|
|
}
|
|
|
|
|
2012-04-26 06:43:58 +02:00
|
|
|
$this->recursivelyApply($this->changedData, $_SESSION);
|
|
|
|
}
|
2007-11-19 04:12:29 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Recursively apply the changes represented in $data to $dest.
|
|
|
|
* Used to update $_SESSION
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2008-02-25 03:10:37 +01:00
|
|
|
protected function recursivelyApply($data, &$dest) {
|
|
|
|
foreach($data as $k => $v) {
|
|
|
|
if(is_array($v)) {
|
2011-04-18 06:44:11 +02:00
|
|
|
if(!isset($dest[$k]) || !is_array($dest[$k])) $dest[$k] = array();
|
2008-02-25 03:10:37 +01:00
|
|
|
$this->recursivelyApply($v, $dest[$k]);
|
|
|
|
} else {
|
|
|
|
$dest[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-26 06:43:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the changed data, for debugging purposes.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function inst_changedData() {
|
|
|
|
return $this->changedData;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Sets the appropriate form message in session, with type. This will be shown once,
|
|
|
|
* for the form specified.
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $formname the form name you wish to use ( usually $form->FormName() )
|
|
|
|
* @param string $message the message you wish to add to it
|
|
|
|
* @param string $type the type of message
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function setFormMessage($formname, $message, $type){
|
2013-02-11 22:57:55 +01:00
|
|
|
Session::set("FormInfo.$formname.formError.message", $message);
|
|
|
|
Session::set("FormInfo.$formname.formError.type", $type);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:10:43 +02:00
|
|
|
/**
|
|
|
|
* Is there a session ID in the request?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function request_contains_session_id() {
|
|
|
|
$secure = Director::is_https() && Config::inst()->get('Session', 'cookie_secure');
|
|
|
|
$name = $secure ? 'SECSESSID' : session_name();
|
|
|
|
return isset($_COOKIE[$name]) || isset($_REQUEST[$name]);
|
|
|
|
}
|
|
|
|
|
2009-11-21 02:40:31 +01:00
|
|
|
/**
|
|
|
|
* Initialize session.
|
2010-04-12 05:32:28 +02:00
|
|
|
*
|
2009-11-21 02:40:31 +01:00
|
|
|
* @param string $sid Start the session with a specific ID
|
|
|
|
*/
|
|
|
|
public static function start($sid = null) {
|
2014-06-19 00:38:26 +02:00
|
|
|
self::current_session()->inst_start($sid);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-21 02:40:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the active session.
|
2010-04-12 05:32:28 +02:00
|
|
|
*
|
2009-11-21 02:40:31 +01:00
|
|
|
* @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove
|
|
|
|
*/
|
|
|
|
public static function destroy($removeCookie = true) {
|
2014-06-19 00:38:26 +02:00
|
|
|
self::current_session()->inst_destroy($removeCookie);
|
2009-11-21 02:40:31 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 06:46:04 +02:00
|
|
|
/**
|
2010-04-12 05:32:28 +02:00
|
|
|
* Set the timeout of a Session value
|
2008-08-12 06:46:04 +02:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.timeout" config setting instead
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-12 06:46:04 +02:00
|
|
|
* @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed.
|
|
|
|
*/
|
|
|
|
public static function set_timeout($timeout) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.timeout" config setting instead');
|
2015-09-28 11:21:02 +02:00
|
|
|
Config::inst()->update('Session', 'timeout', (int) $timeout);
|
2008-08-12 06:46:04 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Session.timeout" config setting instead
|
2013-03-21 19:48:54 +01:00
|
|
|
*/
|
2008-08-12 23:05:29 +02:00
|
|
|
public static function get_timeout() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Session.timeout" config setting instead');
|
2013-10-22 14:20:18 +02:00
|
|
|
return Config::inst()->get('Session', 'timeout');
|
2008-08-12 23:05:29 +02:00
|
|
|
}
|
2017-11-30 02:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the user agent against the current data, resetting the
|
|
|
|
* current session if a mismatch is detected.
|
|
|
|
*
|
|
|
|
* @deprecated 3.0..4.0 Removed in 4.0
|
|
|
|
* @return bool If user agent has been set against this session, returns
|
|
|
|
* the valid state of this session as either true or false. If the agent
|
|
|
|
* isn't set it is assumed valid and returns true.
|
|
|
|
*/
|
|
|
|
private function expireIfInvalid() {
|
|
|
|
// If not set, indeterminable; Assume true as safe default
|
|
|
|
if (!isset($this->data['HTTP_USER_AGENT'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Agents match, deterministically true
|
|
|
|
if ($this->data['HTTP_USER_AGENT'] === $this->userAgent()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funny business detected!
|
|
|
|
$this->inst_clearAll();
|
|
|
|
$this->inst_destroy();
|
|
|
|
$this->inst_start();
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-22 22:47:22 +01:00
|
|
|
}
|