Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions src/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public function start()
self::$started = TRUE;

/* structure:
__NF: BrowserKey, Data, Meta, Time
__NF: Data, Meta, Time
DATA: section->variable = data
META: section->variable = Timestamp, Browser
META: section->variable = Timestamp
*/
$nf = & $_SESSION['__NF'];

Expand All @@ -114,14 +114,6 @@ public function start()
$this->regenerated = TRUE;
}

// browser closing detection
$browserKey = $this->request->getCookie('nette-browser');
if (!is_string($browserKey) || !preg_match('#^[0-9a-z]{10}\z#', $browserKey)) {
$browserKey = Nette\Utils\Random::generate();
}
$browserClosed = !isset($nf['B']) || $nf['B'] !== $browserKey;
$nf['B'] = $browserKey;

// resend cookie
$this->sendCookie();

Expand All @@ -132,7 +124,7 @@ public function start()
foreach ($nf['META'] as $section => $metadata) {
if (is_array($metadata)) {
foreach ($metadata as $variable => $value) {
if ((!empty($value['B']) && $browserClosed) || (!empty($value['T']) && $now > $value['T'])) { // whenBrowserIsClosed || Time
if (!empty($value['T']) && $now > $value['T']) {
if ($variable === '') { // expire whole section
unset($nf['META'][$section], $nf['DATA'][$section]);
continue 2;
Expand Down Expand Up @@ -543,10 +535,6 @@ private function sendCookie()
$cookie['lifetime'] ? $cookie['lifetime'] + time() : 0,
$cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']
);
$this->response->setCookie(
'nette-browser', $_SESSION['__NF']['B'],
Response::BROWSER, $cookie['path'], $cookie['domain']
);
}

}
13 changes: 4 additions & 9 deletions src/Http/SessionSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,23 @@ public function offsetUnset($name)

/**
* Sets the expiration of the section or specific variables.
* @param string|int|\DateTimeInterface time, value 0 means "until the browser is closed"
* @param string|int|\DateTimeInterface time
* @param mixed optional list of variables / single variable to expire
* @return self
*/
public function setExpiration($time, $variables = NULL)
{
$this->start();
if (empty($time)) {
$time = NULL;
$whenBrowserIsClosed = TRUE;
} else {
if ($time) {
$time = Nette\Utils\DateTime::from($time)->format('U');
$max = (int) ini_get('session.gc_maxlifetime');
if ($max !== 0 && ($time - time() > $max + 3)) { // 0 - unlimited in memcache handler, 3 - bulgarian constant
trigger_error("The expiration time is greater than the session expiration $max seconds");
}
$whenBrowserIsClosed = FALSE;
}

foreach (is_array($variables) ? $variables : [$variables] as $variable) {
$this->meta[$variable]['T'] = $time;
$this->meta[$variable]['B'] = $whenBrowserIsClosed;
$this->meta[$variable]['T'] = $time ?: NULL;
}
return $this;
}
Expand All @@ -213,7 +208,7 @@ public function removeExpiration($variables = NULL)
{
$this->start();
foreach (is_array($variables) ? $variables : [$variables] as $variable) {
unset($this->meta['']['T'], $this->meta['']['B']);
unset($this->meta['']['T']);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Session.id.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ require __DIR__ . '/../bootstrap.php';
$_COOKIE['PHPSESSID'] = $leet = md5('1337');

// create fake session
$cookies = ['PHPSESSID' => $sessionId = md5('1'), 'nette-browser' => $B = substr(md5('2'), 0, 10)];
file_put_contents(TEMP_DIR . '/sess_' . $sessionId, sprintf('__NF|a:3:{s:4:"Time";i:%s;s:1:"B";s:10:"%s";s:4:"DATA";a:1:{s:4:"temp";a:1:{s:5:"value";s:3:"yes";}}}', time() - 1000, $B));
$cookies = ['PHPSESSID' => $sessionId = md5('1')];
file_put_contents(TEMP_DIR . '/sess_' . $sessionId, sprintf('__NF|a:2:{s:4:"Time";i:%s;s:4:"DATA";a:1:{s:4:"temp";a:1:{s:5:"value";s:3:"yes";}}}', time() - 1000));

$session = new Session(new Http\Request(new Http\UrlScript('http://nette.org'), NULL, [], [], $cookies), new Http\Response());

Expand Down
29 changes: 0 additions & 29 deletions tests/Http/Session.netteCookie.phpt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Http/Session.regenerate-empty-session.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require __DIR__ . '/../bootstrap.php';


// create fake session
$cookies = ['PHPSESSID' => $sessionId = md5('3'), 'nette-browser' => $B = substr(md5('4'), 0, 10)];
$cookies = ['PHPSESSID' => $sessionId = md5('3')];
file_put_contents(TEMP_DIR . '/sess_' . $sessionId, '__NF|a:1:{s:4:"DATA";a:1:{s:4:"temp";a:1:{s:5:"value";s:3:"yes";}}}');

$session = new Session(new Http\Request(new Http\UrlScript('http://nette.org'), NULL, [], [], $cookies), new Http\Response());
Expand Down