Skip to content

Commit

Permalink
Fix restart method causing fatal error
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonge committed Mar 23, 2016
1 parent 3932899 commit 025d8c2
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions libraries/joomla/session/handler/native.php
Expand Up @@ -38,7 +38,6 @@ class JSessionHandlerNative implements JSessionHandlerInterface
* @return boolean True if started
*
* @since 3.5
* @throws RuntimeException If something goes wrong starting the session.
*/
public function start()
{
Expand All @@ -47,42 +46,7 @@ public function start()
return true;
}

// Register our function as shutdown method, so we can manipulate it
register_shutdown_function(array($this, 'save'));

// Disable the cache limiter
session_cache_limiter('none');

/*
* Extended checks to determine if the session has already been started
*/

// If running PHP 5.4, try to use the native API
if (version_compare(PHP_VERSION, '5.4', 'ge') && PHP_SESSION_ACTIVE === session_status())
{
throw new RuntimeException('Failed to start the session: already started by PHP.');
}

// Fallback check for PHP 5.3
if (version_compare(PHP_VERSION, '5.4', 'lt') && !$this->closed && isset($_SESSION) && $this->getId())
{
throw new RuntimeException('Failed to start the session: already started by PHP ($_SESSION is set).');
}

// If we are using cookies (default true) and headers have already been started (early output),
if (ini_get('session.use_cookies') && headers_sent($file, $line))
{
throw new RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line));
}

// Ok to try and start the session
if (!session_start())
{
throw new RuntimeException('Failed to start the session');
}

// Mark ourselves as started
$this->started = true;
$this->doSessionStart();

return true;
}
Expand Down Expand Up @@ -191,12 +155,12 @@ public function regenerate($destroy = false, $lifetime = null)
if (isset($_SESSION))
{
$backup = $_SESSION;
session_start();
$this->doSessionStart();
$_SESSION = $backup;
}
else
{
session_start();
$this->doSessionStart();
}

return $return;
Expand Down Expand Up @@ -251,4 +215,52 @@ public function clear()
$this->closed = true;
$this->started = false;
}

/**
* Performs the session start mechanism
*
* @return void
*
* @since 3.5.1
* @throws RuntimeException If something goes wrong starting the session.
*/
private function doSessionStart()
{
// Register our function as shutdown method, so we can manipulate it
register_shutdown_function(array($this, 'save'));

// Disable the cache limiter
session_cache_limiter('none');

/*
* Extended checks to determine if the session has already been started
*/

// If running PHP 5.4, try to use the native API
if (version_compare(PHP_VERSION, '5.4', 'ge') && PHP_SESSION_ACTIVE === session_status())
{
throw new RuntimeException('Failed to start the session: already started by PHP.');
}

// Fallback check for PHP 5.3
if (version_compare(PHP_VERSION, '5.4', 'lt') && !$this->closed && isset($_SESSION) && $this->getId())
{
throw new RuntimeException('Failed to start the session: already started by PHP ($_SESSION is set).');
}

// If we are using cookies (default true) and headers have already been started (early output),
if (ini_get('session.use_cookies') && headers_sent($file, $line))
{
throw new RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line));
}

// Ok to try and start the session
if (!session_start())
{
throw new RuntimeException('Failed to start the session');
}

// Mark ourselves as started
$this->started = true;
}
}

0 comments on commit 025d8c2

Please sign in to comment.