2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use BadMethodCallException;
|
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2018-02-02 00:51:00 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* 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>
|
2016-11-29 00:31:16 +01:00
|
|
|
* Session::set('MyValue', 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
|
|
|
* 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>
|
2016-11-29 00:31:16 +01:00
|
|
|
* // save a variable
|
|
|
|
* $var = 1;
|
|
|
|
* Session::set('MyVar', $var);
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* // saves an array
|
|
|
|
* Session::set('MyArrayOfValues', array('1', '2', '3'));
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* // saves an object (you'll have to unserialize it back)
|
|
|
|
* $object = new Object();
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* Session::set('MyObject', serialize($object));
|
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>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.
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
* Note that session data isn't persisted in PHP's own session store (via $_SESSION)
|
|
|
|
* until {@link Session::save()} is called, which happens automatically at the end of a standard request
|
|
|
|
* through {@link SilverStripe\Control\Middleware\SessionMiddleware}.
|
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() {
|
2016-11-29 00:31:16 +01:00
|
|
|
* $value = Session::get('MyValue'); // $value = 6
|
|
|
|
* $var = Session::get('MyVar'); // $var = 1
|
|
|
|
* $array = Session::get('MyArrayOfValues'); // $array = array(1,2,3)
|
|
|
|
* $object = Session::get('MyObject', unserialize($object)); // $object = Object()
|
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
|
|
|
* 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>
|
2016-11-29 00:31:16 +01: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
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class Session
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
use Configurable;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set session timeout in seconds.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $timeout = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $session_ips = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cookie_domain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cookie_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $session_store_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $cookie_secure = false;
|
|
|
|
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cookie_name_secure = 'SECSESSID';
|
|
|
|
|
2018-06-14 05:54:31 +02:00
|
|
|
/**
|
|
|
|
* Name of session cache limiter to use.
|
|
|
|
* Defaults to '' to disable cache limiter entirely.
|
|
|
|
*
|
|
|
|
* @see https://secure.php.net/manual/en/function.session-cache-limiter.php
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
private static $sessionCacheLimiter = '';
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Session data.
|
|
|
|
* Will be null if session has not been started
|
|
|
|
*
|
|
|
|
* @var array|null
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
protected $data = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $started = false;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
2018-02-02 00:51:00 +01:00
|
|
|
* List of keys changed. This is a nested array which represents the
|
|
|
|
* keys modified in $this->data. The value of each item is either "true"
|
|
|
|
* or a nested array.
|
|
|
|
*
|
|
|
|
* If a value is in changedData but not in data, it must be removed
|
|
|
|
* from the destination during save().
|
|
|
|
*
|
|
|
|
* Only highest level changes are stored. E.g. changes to `Base.Sub`
|
|
|
|
* and then `Base` only records `Base` as the change.
|
|
|
|
*
|
|
|
|
* E.g.
|
|
|
|
* [
|
|
|
|
* 'Base' => true,
|
|
|
|
* 'Key' => [
|
|
|
|
* 'Nested' => true,
|
|
|
|
* ],
|
|
|
|
* ]
|
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
protected $changedData = array();
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Get user agent for this request
|
|
|
|
*
|
2017-06-25 05:12:29 +02:00
|
|
|
* @param HTTPRequest $request
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-06-25 05:12:29 +02:00
|
|
|
protected function userAgent(HTTPRequest $request)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-26 01:24:50 +02:00
|
|
|
return $request->getHeader('User-Agent');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start PHP session, then create a new Session object with the given start data.
|
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @param array|null|Session $data Can be an array of data (such as $_SESSION) or another Session object to clone.
|
|
|
|
* If null, this session is treated as unstarted.
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
if ($data instanceof Session) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$data = $data->getAll();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->data = $data;
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
$this->started = isset($data);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
* Init this session instance before usage,
|
|
|
|
* if a session identifier is part of the passed in request.
|
|
|
|
* Otherwise, a session might be started in {@link save()}
|
|
|
|
* if session data needs to be written with a new session identifier.
|
2017-06-25 05:12:29 +02:00
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-25 04:13:36 +02:00
|
|
|
public function init(HTTPRequest $request)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
|
|
|
|
if (!$this->isStarted() && $this->requestContainsSessionId($request)) {
|
2017-06-25 04:13:36 +02:00
|
|
|
$this->start($request);
|
2017-06-22 12:50:45 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Funny business detected!
|
|
|
|
if (isset($this->data['HTTP_USER_AGENT'])) {
|
2017-06-26 01:24:50 +02:00
|
|
|
if ($this->data['HTTP_USER_AGENT'] !== $this->userAgent($request)) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->clearAll();
|
|
|
|
$this->destroy();
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
$this->started = false;
|
2017-06-25 04:13:36 +02:00
|
|
|
$this->start($request);
|
2017-06-22 12:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Destroy existing session and restart
|
2017-06-25 05:12:29 +02:00
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-25 04:13:36 +02:00
|
|
|
public function restart(HTTPRequest $request)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->destroy();
|
2017-06-25 04:13:36 +02:00
|
|
|
$this->init($request);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Determine if this session has started
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return bool
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public function isStarted()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
return $this->started;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function requestContainsSessionId(HTTPRequest $request)
|
|
|
|
{
|
|
|
|
$secure = Director::is_https($request) && $this->config()->get('cookie_secure');
|
2018-07-18 11:15:56 +02:00
|
|
|
$name = $secure ? $this->config()->get('cookie_name_secure') : session_name();
|
|
|
|
return (bool)Cookie::get($name);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
* Begin session, regardless if a session identifier is present in the request,
|
|
|
|
* or whether any session data needs to be written.
|
|
|
|
* See {@link init()} if you want to "lazy start" a session.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-06-25 05:12:29 +02:00
|
|
|
* @param HTTPRequest $request The request for which to start a session
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-25 04:13:36 +02:00
|
|
|
public function start(HTTPRequest $request)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
if ($this->isStarted()) {
|
|
|
|
throw new BadMethodCallException("Session has already started");
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$path = $this->config()->get('cookie_path');
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!$path) {
|
|
|
|
$path = Director::baseURL();
|
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
$domain = $this->config()->get('cookie_domain');
|
2017-06-25 04:13:36 +02:00
|
|
|
$secure = Director::is_https($request) && $this->config()->get('cookie_secure');
|
2017-06-22 12:50:45 +02:00
|
|
|
$session_path = $this->config()->get('session_store_path');
|
|
|
|
$timeout = $this->config()->get('timeout');
|
2016-11-29 00:31:16 +01: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'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 04:24:40 +02:00
|
|
|
// If the session cookie is already set, then the session can be read even if headers_sent() = true
|
|
|
|
// This helps with edge-case such as debugging.
|
2018-11-27 04:40:15 +01:00
|
|
|
$data = [];
|
2018-09-19 04:24:40 +02:00
|
|
|
if (!session_id() && (!headers_sent() || !empty($_COOKIE[ini_get('session.name')]))) {
|
|
|
|
if (!headers_sent()) {
|
|
|
|
session_set_cookie_params($timeout, $path, $domain ?: null, $secure, true);
|
|
|
|
|
|
|
|
$limiter = $this->config()->get('sessionCacheLimiter');
|
|
|
|
if (isset($limiter)) {
|
|
|
|
session_cache_limiter($limiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If headers are sent then we can't have a session_cache_limiter otherwise we'll get a warning
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2018-09-19 04:24:40 +02:00
|
|
|
session_cache_limiter(null);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow storing the session in a non standard location
|
|
|
|
if ($session_path) {
|
|
|
|
session_save_path($session_path);
|
|
|
|
}
|
|
|
|
|
2018-11-27 04:40:15 +01:00
|
|
|
// If we want a secure cookie for HTTPS, use a separate session name. This lets us have a
|
|
|
|
// separate (less secure) session for non-HTTPS requests
|
2018-09-19 04:24:40 +02:00
|
|
|
// if headers_sent() is true then it's best to throw the resulting error rather than risk
|
|
|
|
// a security hole.
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($secure) {
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
session_name($this->config()->get('cookie_name_secure'));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 04:40:15 +01:00
|
|
|
$sessionParameters = [
|
|
|
|
"cookie_path" => $path,
|
|
|
|
"cookie_domain" => $domain ?: "",
|
|
|
|
"cookie_secure" => $secure,
|
|
|
|
"cookie_httponly" => true
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($timeout) {
|
|
|
|
$sessionParameters["cookie_lifetime"] = $timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
session_start($sessionParameters);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
if (isset($_SESSION)) {
|
|
|
|
// Initialise data from session store if present
|
|
|
|
$data = $_SESSION;
|
2018-11-27 04:40:15 +01:00
|
|
|
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
// Merge in existing in-memory data, taking priority over session store data
|
|
|
|
$this->recursivelyApply((array)$this->data, $data);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 04:40:15 +01:00
|
|
|
// Save any modified session data back to the session store if present, otherwise initialise it to an array.
|
|
|
|
$this->data = $data;
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
|
|
|
|
$this->started = true;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Destroy this session
|
|
|
|
*
|
|
|
|
* @param bool $removeCookie
|
|
|
|
*/
|
|
|
|
public function destroy($removeCookie = true)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if (session_id()) {
|
|
|
|
if ($removeCookie) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$path = $this->config()->get('cookie_path') ?: Director::baseURL();
|
|
|
|
$domain = $this->config()->get('cookie_domain');
|
|
|
|
$secure = $this->config()->get('cookie_secure');
|
2016-11-29 00:31:16 +01:00
|
|
|
Cookie::force_expiry(session_name(), $path, $domain, $secure, true);
|
|
|
|
}
|
|
|
|
session_destroy();
|
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
// Clean up the superglobal - session_destroy does not do it.
|
|
|
|
// http://nz1.php.net/manual/en/function.session-destroy.php
|
|
|
|
unset($_SESSION);
|
|
|
|
$this->data = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Set session value
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function set($name, $val)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-02 00:51:00 +01:00
|
|
|
$var = &$this->nestedValueRef($name, $this->data);
|
2017-06-22 12:50:45 +02:00
|
|
|
|
2018-02-02 00:51:00 +01:00
|
|
|
// Mark changed
|
|
|
|
if ($var !== $val) {
|
|
|
|
$var = $val;
|
|
|
|
$this->markChanged($name);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2018-02-02 00:51:00 +01:00
|
|
|
/**
|
|
|
|
* Mark key as changed
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
protected function markChanged($name)
|
|
|
|
{
|
|
|
|
$diffVar = &$this->changedData;
|
|
|
|
foreach (explode('.', $name) as $namePart) {
|
|
|
|
if (!isset($diffVar[$namePart])) {
|
|
|
|
$diffVar[$namePart] = [];
|
|
|
|
}
|
|
|
|
$diffVar = &$diffVar[$namePart];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2018-02-02 00:51:00 +01:00
|
|
|
// Already diffed
|
|
|
|
if ($diffVar === true) {
|
|
|
|
return;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 00:51:00 +01:00
|
|
|
// Mark changed
|
|
|
|
$diffVar = true;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Merge value with array
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $val
|
|
|
|
*/
|
|
|
|
public function addToArray($name, $val)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
$names = explode('.', $name);
|
|
|
|
|
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
|
|
|
$var = &$this->data;
|
|
|
|
$diffVar = &$this->changedData;
|
|
|
|
|
|
|
|
foreach ($names as $n) {
|
|
|
|
$var = &$var[$n];
|
|
|
|
$diffVar = &$diffVar[$n];
|
|
|
|
}
|
|
|
|
|
|
|
|
$var[] = $val;
|
|
|
|
$diffVar[sizeof($var)-1] = $val;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Get session value
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($name)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-02 00:51:00 +01:00
|
|
|
return $this->nestedValue($name, $this->data);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Clear session value
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function clear($name)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-02 00:51:00 +01:00
|
|
|
// Get var by path
|
|
|
|
$var = $this->nestedValue($name, $this->data);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2018-02-02 00:51:00 +01:00
|
|
|
// Unset var
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($var !== null) {
|
2018-02-02 00:51:00 +01:00
|
|
|
// Unset parent key
|
|
|
|
$parentParts = explode('.', $name);
|
|
|
|
$basePart = array_pop($parentParts);
|
|
|
|
if ($parentParts) {
|
|
|
|
$parent = &$this->nestedValueRef(implode('.', $parentParts), $this->data);
|
|
|
|
unset($parent[$basePart]);
|
|
|
|
} else {
|
|
|
|
unset($this->data[$name]);
|
|
|
|
}
|
|
|
|
$this->markChanged($name);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
return $this;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Clear all values
|
|
|
|
*/
|
|
|
|
public function clearAll()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if ($this->data && is_array($this->data)) {
|
|
|
|
foreach (array_keys($this->data) as $key) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->clear($key);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Get all values
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getAll()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Set user agent key
|
2017-06-25 05:12:29 +02:00
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
2017-06-22 12:50:45 +02:00
|
|
|
*/
|
2017-06-26 01:24:50 +02:00
|
|
|
public function finalize(HTTPRequest $request)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-26 01:24:50 +02:00
|
|
|
$this->set('HTTP_USER_AGENT', $this->userAgent($request));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save data to session
|
|
|
|
* Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
|
2017-06-25 05:12:29 +02:00
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-25 04:13:36 +02:00
|
|
|
public function save(HTTPRequest $request)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if ($this->changedData) {
|
2017-06-26 01:24:50 +02:00
|
|
|
$this->finalize($request);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
if (!$this->isStarted()) {
|
2017-06-25 04:13:36 +02:00
|
|
|
$this->start($request);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
BUG Lazy session state (fixes #8267)
Fixes regression from 3.x, where sessions where lazy started as required:
Either because an existing session identifier was sent through with the request,
or because new session data needed to be persisted as part of the request execution.
Without this lazy starting, *every* request will get a session,
which makes all those responses uncacheable by HTTP layers.
Note that 4.x also changed the $data vs. $changedData payloads:
In 3.x, they both contained key/value pairs.
In 4.x, $data contains key/value, while $changedData contains key/boolean to declare isChanged.
While this reduces duplication in the class, it also surfaced a bug which was latent in 3.x:
When an existing session is lazily resumed via start(), $data is set back to an empty array.
In 3.x, any changed data before this point was *also* retained in $changedData,
ensuring it gets merged into existing $_SESSION data.
In 4.x, this clears out data - hence the need for a more complex merge logic.
Since isset($this->data) is no longer an accurate indicator of a started session,
we introduce a separate $this->started flag.
Note that I've chosen not to make lazy an opt-in (e.g. via start($request, $lazy=false)).
We already have a distinction between lazy starting via init(), and force starting via start().
2018-07-18 04:24:27 +02:00
|
|
|
// Apply all changes recursively, implicitly writing them to the actual PHP session store.
|
2018-02-02 00:51:00 +01:00
|
|
|
$this->recursivelyApplyChanges($this->changedData, $this->data, $_SESSION);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively apply the changes represented in $data to $dest.
|
|
|
|
* Used to update $_SESSION
|
|
|
|
*
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.1.0:5.0.0 Use recursivelyApplyChanges() instead
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param array $data
|
|
|
|
* @param array $dest
|
|
|
|
*/
|
|
|
|
protected function recursivelyApply($data, &$dest)
|
|
|
|
{
|
2018-02-02 00:51:00 +01:00
|
|
|
Deprecation::notice('5.0', 'Use recursivelyApplyChanges() instead');
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
if (is_array($v)) {
|
|
|
|
if (!isset($dest[$k]) || !is_array($dest[$k])) {
|
|
|
|
$dest[$k] = array();
|
|
|
|
}
|
|
|
|
$this->recursivelyApply($v, $dest[$k]);
|
|
|
|
} else {
|
|
|
|
$dest[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-02 00:51:00 +01:00
|
|
|
* Returns the list of changed keys
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public function changedData()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
return $this->changedData;
|
|
|
|
}
|
2018-02-02 00:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to nested value in source array by name,
|
|
|
|
* creating a null placeholder if it doesn't exist.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param string $name
|
|
|
|
* @param array $source
|
|
|
|
* @return mixed Reference to value in $source
|
|
|
|
*/
|
|
|
|
protected function &nestedValueRef($name, &$source)
|
|
|
|
{
|
|
|
|
// Find var to change
|
|
|
|
$var = &$source;
|
|
|
|
foreach (explode('.', $name) as $namePart) {
|
|
|
|
if (!isset($var)) {
|
|
|
|
$var = [];
|
|
|
|
}
|
|
|
|
if (!isset($var[$namePart])) {
|
|
|
|
$var[$namePart] = null;
|
|
|
|
}
|
|
|
|
$var = &$var[$namePart];
|
|
|
|
}
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to nested value in source array by name,
|
|
|
|
* returning null if it doesn't exist.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param string $name
|
|
|
|
* @param array $source
|
|
|
|
* @return mixed Value in array in $source
|
|
|
|
*/
|
|
|
|
protected function nestedValue($name, $source)
|
|
|
|
{
|
|
|
|
// Find var to change
|
|
|
|
$var = $source;
|
|
|
|
foreach (explode('.', $name) as $namePart) {
|
|
|
|
if (!isset($var[$namePart])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$var = $var[$namePart];
|
|
|
|
}
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply all changes using separate keys and data sources and a destination
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param array $changes
|
|
|
|
* @param array $source
|
|
|
|
* @param array $destination
|
|
|
|
*/
|
|
|
|
protected function recursivelyApplyChanges($changes, $source, &$destination)
|
|
|
|
{
|
2018-07-18 11:15:56 +02:00
|
|
|
$source = $source ?: [];
|
2018-02-02 00:51:00 +01:00
|
|
|
foreach ($changes as $key => $changed) {
|
|
|
|
if ($changed === true) {
|
|
|
|
// Determine if replacement or removal
|
|
|
|
if (array_key_exists($key, $source)) {
|
|
|
|
$destination[$key] = $source[$key];
|
|
|
|
} else {
|
|
|
|
unset($destination[$key]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Recursively apply
|
|
|
|
$destVal = &$this->nestedValueRef($key, $destination);
|
|
|
|
$sourceVal = $this->nestedValue($key, $source);
|
|
|
|
$this->recursivelyApplyChanges($changed, $sourceVal, $destVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-22 22:47:22 +01:00
|
|
|
}
|