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
|
|
|
*
|
2018-12-13 01:50:35 +01:00
|
|
|
* An instance of a `Session` object can be retrieved via an `HTTPRequest` by calling the `getSession()` method.
|
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
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Saving Data</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2018-12-12 23:02:24 +01:00
|
|
|
* Once you've retrieved a session instance, you can write a value to a users session using the function {@link Session::set()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2018-12-12 23:02:24 +01:00
|
|
|
* $request->getSession()->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>
|
2018-12-12 23:02:24 +01:00
|
|
|
*
|
|
|
|
* $session = $request->getSession();
|
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* // save a variable
|
|
|
|
* $var = 1;
|
2018-12-12 23:02:24 +01:00
|
|
|
* $session->set('MyVar', $var);
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* // saves an array
|
2018-12-12 23:02:24 +01:00
|
|
|
* $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
|
|
|
*
|
2018-12-12 23:02:24 +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.
|
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() {
|
2018-12-12 23:02:24 +01:00
|
|
|
* $session = $this->getRequest()->getSession();
|
|
|
|
* $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>
|
2018-12-14 01:01:22 +01:00
|
|
|
* $session->getAll(); // 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>
|
2018-12-12 23:02:24 +01: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
|
2018-12-12 23:02:24 +01:00
|
|
|
* including form and page comment information. None of this is vital but `clearAll()` will clear everything.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2018-12-12 23:02:24 +01:00
|
|
|
* $session->clearAll();
|
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
|
2018-12-12 23:02:24 +01:00
|
|
|
* @see HTTPRequest
|
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
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $session_ips = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 = '';
|
|
|
|
|
2019-08-06 12:00:01 +02:00
|
|
|
/**
|
|
|
|
* Invalidate the session if user agent header changes between request. Defaults to true. Disabling this checks is
|
|
|
|
* not recommended.
|
|
|
|
* @var bool
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $strict_user_agent_check = true;
|
|
|
|
|
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
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $changedData = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
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!
|
2019-08-06 12:00:01 +02:00
|
|
|
if (self::config()->get('strict_user_agent_check') && 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();
|
2019-11-20 15:21:30 +01:00
|
|
|
$this->restart($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
|
|
|
{
|
2021-08-06 02:55:05 +02:00
|
|
|
$this->destroy(true, $request);
|
2019-11-20 15:21:30 +01:00
|
|
|
$this->start($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)) {
|
2022-04-14 03:12:59 +02:00
|
|
|
$urlParts = parse_url($path ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
$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-12-03 00:28:21 +01:00
|
|
|
if (!session_id() && (!headers_sent() || $this->requestContainsSessionId($request))) {
|
2018-09-19 04:24:40 +02:00
|
|
|
if (!headers_sent()) {
|
2018-12-03 00:28:21 +01:00
|
|
|
session_set_cookie_params($timeout ?: 0, $path, $domain ?: null, $secure, true);
|
2018-09-19 04:24:40 +02:00
|
|
|
|
|
|
|
$limiter = $this->config()->get('sessionCacheLimiter');
|
|
|
|
if (isset($limiter)) {
|
|
|
|
session_cache_limiter($limiter);
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:28:21 +01:00
|
|
|
// Allow storing the session in a non standard location
|
|
|
|
if ($session_path) {
|
|
|
|
session_save_path($session_path);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2018-12-03 00:28:21 +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
|
|
|
|
// if headers_sent() is true then it's best to throw the resulting error rather than risk
|
|
|
|
// a security hole.
|
|
|
|
if ($secure) {
|
|
|
|
session_name($this->config()->get('cookie_name_secure'));
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2018-12-03 00:28:21 +01:00
|
|
|
session_start();
|
2019-01-14 02:35:58 +01:00
|
|
|
|
2019-01-17 22:07:53 +01:00
|
|
|
// Session start emits a cookie, but only if there's no existing session. If there is a session timeout
|
|
|
|
// tied to this request, make sure the session is held for the entire timeout by refreshing the cookie age.
|
|
|
|
if ($timeout && $this->requestContainsSessionId($request)) {
|
2019-01-14 02:35:58 +01:00
|
|
|
Cookie::set(session_name(), session_id(), $timeout / 86400, $path, $domain ?: null, $secure, true);
|
|
|
|
}
|
2018-12-03 00:28:21 +01:00
|
|
|
} else {
|
|
|
|
// If headers are sent then we can't have a session_cache_limiter otherwise we'll get a warning
|
|
|
|
session_cache_limiter(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
|
|
|
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
|
2021-08-06 02:55:05 +02:00
|
|
|
* @param HTTPRequest $request The request for which to destroy a session
|
2017-06-22 12:50:45 +02:00
|
|
|
*/
|
2021-08-06 02:55:05 +02:00
|
|
|
public function destroy($removeCookie = true, HTTPRequest $request = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if (session_id()) {
|
|
|
|
if ($removeCookie) {
|
2021-08-06 02:55:05 +02:00
|
|
|
if (!$request) {
|
|
|
|
$request = Controller::curr()->getRequest();
|
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
$path = $this->config()->get('cookie_path') ?: Director::baseURL();
|
|
|
|
$domain = $this->config()->get('cookie_domain');
|
2021-08-06 02:55:05 +02:00
|
|
|
$secure = Director::is_https($request) && $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;
|
2019-11-20 15:21:30 +01:00
|
|
|
$this->started = false;
|
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;
|
2022-04-14 03:12:59 +02:00
|
|
|
foreach (explode('.', $name ?? '') as $namePart) {
|
2018-02-02 00:51:00 +01:00
|
|
|
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
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
$names = explode('.', $name ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// 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;
|
2018-12-07 02:45:41 +01:00
|
|
|
$diffVar[sizeof($var) - 1] = $val;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
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
|
2022-04-14 03:12:59 +02:00
|
|
|
$parentParts = explode('.', $name ?? '');
|
2018-02-02 00:51:00 +01:00
|
|
|
$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)) {
|
2022-04-14 03:12:59 +02:00
|
|
|
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])) {
|
2020-04-20 19:58:09 +02:00
|
|
|
$dest[$k] = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
$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;
|
2022-04-14 03:12:59 +02:00
|
|
|
foreach (explode('.', $name ?? '') as $namePart) {
|
2018-02-02 00:51:00 +01:00
|
|
|
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;
|
2022-04-14 03:12:59 +02:00
|
|
|
foreach (explode('.', $name ?? '') as $namePart) {
|
2018-02-02 00:51:00 +01:00
|
|
|
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
|
2022-04-14 03:12:59 +02:00
|
|
|
if (array_key_exists($key, $source ?? [])) {
|
2018-02-02 00:51:00 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 06:29:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate session id
|
|
|
|
*
|
|
|
|
* @internal This is for internal use only. Isn't a part of public API.
|
|
|
|
*/
|
|
|
|
public function regenerateSessionId()
|
|
|
|
{
|
2020-04-28 19:21:52 +02:00
|
|
|
if (!headers_sent() && session_status() === PHP_SESSION_ACTIVE) {
|
2019-09-16 06:29:42 +02:00
|
|
|
session_regenerate_id(true);
|
|
|
|
}
|
|
|
|
}
|
2011-03-22 22:47:22 +01:00
|
|
|
}
|