Skip to content

Commit

Permalink
Session: $started replaced with checking session_status() for better …
Browse files Browse the repository at this point in the history
…cooperation with the session started outside nette [Closes nette/forms#214]
  • Loading branch information
dg committed Mar 11, 2019
1 parent fa9b839 commit af6c8f8
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class Session
/** @var bool has been session ID regenerated? */
private $regenerated = false;

/** @var bool has been session started? */
private static $started = false;

/** @var array default configuration */
private $options = [
// security
Expand Down Expand Up @@ -59,7 +56,6 @@ public function __construct(IRequest $request, IResponse $response)
{
$this->request = $request;
$this->response = $response;
self::$started = self::$started && session_status() === PHP_SESSION_ACTIVE;
$this->options['cookie_path'] = &$this->response->cookiePath;
$this->options['cookie_domain'] = &$this->response->cookieDomain;
$this->options['cookie_secure'] = &$this->response->cookieSecure;
Expand All @@ -72,7 +68,8 @@ public function __construct(IRequest $request, IResponse $response)
*/
public function start(): void
{
if (self::$started) {
if ($this->isStarted()) {
$this->initialize();
return;
}

Expand All @@ -99,8 +96,13 @@ public function start(): void
throw $e;
}

self::$started = true;
$this->initialize();
register_shutdown_function([$this, 'clean']);
}


private function initialize(): void
{
/* structure:
__NF: Data, Meta, Time
DATA: section->variable = data
Expand Down Expand Up @@ -136,8 +138,6 @@ public function start(): void
}
}
}

register_shutdown_function([$this, 'clean']);
}


Expand All @@ -146,7 +146,7 @@ public function start(): void
*/
public function isStarted(): bool
{
return self::$started;
return session_status() === PHP_SESSION_ACTIVE;
}


Expand All @@ -155,10 +155,9 @@ public function isStarted(): bool
*/
public function close(): void
{
if (self::$started) {
if ($this->isStarted()) {
$this->clean();
session_write_close();
self::$started = false;
}
}

Expand All @@ -168,13 +167,12 @@ public function close(): void
*/
public function destroy(): void
{
if (!self::$started) {
if (!$this->isStarted()) {
throw new Nette\InvalidStateException('Session is not started.');
}

session_destroy();
$_SESSION = null;
self::$started = false;
if (!$this->response->isSent()) {
$params = session_get_cookie_params();
$this->response->deleteCookie(session_name(), $params['path'], $params['domain'], $params['secure']);
Expand All @@ -187,7 +185,7 @@ public function destroy(): void
*/
public function exists(): bool
{
return self::$started || $this->request->getCookie($this->getName()) !== null;
return $this->isStarted() || $this->request->getCookie($this->getName()) !== null;
}


Expand All @@ -200,7 +198,7 @@ public function regenerateId(): void
if ($this->regenerated) {
return;
}
if (self::$started) {
if ($this->isStarted()) {
if (headers_sent($file, $line)) {
throw new Nette\InvalidStateException('Cannot regenerate session ID after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
}
Expand Down Expand Up @@ -271,7 +269,7 @@ public function getSection(string $section, string $class = SessionSection::clas
*/
public function hasSection(string $section): bool
{
if ($this->exists() && !self::$started) {
if ($this->exists() && !$this->isStarted()) {
$this->start();
}

Expand All @@ -284,7 +282,7 @@ public function hasSection(string $section): bool
*/
public function getIterator(): \Iterator
{
if ($this->exists() && !self::$started) {
if ($this->exists() && !$this->isStarted()) {
$this->start();
}

Expand All @@ -303,7 +301,7 @@ public function getIterator(): \Iterator
*/
public function clean(): void
{
if (!self::$started || empty($_SESSION)) {
if (!$this->isStarted() || empty($_SESSION)) {
return;
}

Expand Down Expand Up @@ -345,7 +343,7 @@ public function setOptions(array $options)
$key = strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $key)); // camelCase -> snake_case
$normalized[$key] = $value;
}
if (self::$started) {
if ($this->isStarted()) {
$this->configure($normalized);
}
$this->options = $normalized + $this->options;
Expand Down Expand Up @@ -390,7 +388,7 @@ private function configure(array $config): void

} else {
if (session_status() === PHP_SESSION_ACTIVE) {
throw new Nette\InvalidStateException("Unable to set 'session.$key' to value '$value' when session has been started" . (self::$started ? '.' : ' by session.auto_start or session_start().'));
throw new Nette\InvalidStateException("Unable to set 'session.$key' to value '$value' when session has been started" . ($this->isStarted() ? '.' : ' by session.auto_start or session_start().'));
}
if (isset($special[$key])) {
$key = "session_$key";
Expand All @@ -417,7 +415,7 @@ private function configure(array $config): void
$cookie['httponly']
);
}
if (self::$started) {
if ($this->isStarted()) {
$this->sendCookie();
}
}
Expand Down Expand Up @@ -493,7 +491,7 @@ public function setSavePath(string $path)
*/
public function setHandler(\SessionHandlerInterface $handler)
{
if (self::$started) {
if ($this->isStarted()) {
throw new Nette\InvalidStateException('Unable to set handler when session has been started.');
}
$this->handler = $handler;
Expand Down

0 comments on commit af6c8f8

Please sign in to comment.