8000 [HttpFoundation][Sessions] Removed native save handlers · Pull Request #4454 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation][Sessions] Removed native save handlers #4454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from May 30, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[HttpFoundation][Sessions] Refactored tests
  • Loading branch information
Drak committed May 28, 2012
commit 3c8cc0a1a091d6adeab64e271b1b7cdb234c153b
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,71 @@ class FileSessionHandler implements \SessionHandlerInterface
*/
private $savePath;

/**
* @var string
*/
private $prefix;

/**
* Constructor.
*
* @param string $savePath Path of directory to save session files.
*/
public function __construct($savePath)
public function __construct($savePath = null, $prefix = 'sess_')
{
if (null === $savePath) {
$savePath = sys_get_temp_dir();
}

$this->savePath = $savePath;
if (false === is_dir($this->savePath)) {
mkdir($this->savePath, 0777, true);
}

$this->prefix = $prefix;
}

/**
* {@inheritdoc]
*/
function open($savePath, $sessionName)
public function open($savePath, $sessionName)
{
return true;
}

/**
* {@inheritdoc]
*/
function close()
public function close()
{
return true;
}

/**
* {@inheritdoc]
*/
function read($id)
public function read($id)
{
$file = $this->savePath.'/sess_'.$id;
if (file_exists($file)) {
return file_get_contents($file);
}
$file = $this->getPath().$id;

return '';
return is_readable($file) ? file_get_contents($file) : '';
}

/**
* {@inheritdoc]
*/
function write($id, $data)
public function write($id, $data)
{
return false === file_put_contents($this->savePath.'/sess_'.$id, $data) ? false : true;
return false === file_put_contents($this->getPath().$id, $data) ? false : true;
}

/**
* {@inheritdoc]
*/
function destroy($id)
public function destroy($id)
{
$file = $this->savePath.'/sess_'.$id;
if (file_exists($file)) {
$file = $this->getPath().$id;
if (is_file($file)) {
unlink($file);
}

Expand All @@ -89,14 +97,19 @@ function destroy($id)
/**
* {@inheritdoc]
*/
function gc($maxlifetime)
public function gc($maxlifetime)
{
foreach (glob($this->savePath.'/sess_*') as $file) {
foreach (glob($this->getPath().'*') as $file) {
if ((filemtime($file) + $maxlifetime) < time()) {
unlink($file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if someone is using the global temp folder and that contains sessions from other system users that are under different uid/gid?
Should be silenced?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would postulate they should not be using a shared folder for this. The code only scans for sess_* and the other alternative is to change the session file prefix. I wouldn't silence here as you it would simply hide stuff that should not be happening (due to bad configuration).

}
}

return true;
}

private function getPath()
{
return $this->savePath.'/'.$this->prefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* NullSessionHandler.
*
* Can be used in unit testing or in a sitation where persisted sessions are not desired.
* Can be used in unit testing or in a situations where persisted sessions are not desired.
*
* @author Drak <drak@zikula.org>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\HttpFoundation\Session\Storage;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;

/**
* MockFileSessionStorage is used to mock sessions for
* functional testing when done in a single PHP process.
Expand All @@ -25,31 +28,32 @@
class MockFileSessionStorage extends MockArraySessionStorage
{
/**
* @var string
* @var array
*/
private $savePath;

private $sessionData;

/**
* @var FileSessionHandler
*/
private $handler;

/**
* Constructor.
*
* @param string $savePath Path of directory to save session files.
* @param string $name Session name.
* @param string $savePath Path of directory to save session files.
* @param string $name Session name.
* @param FileSessionHandler $handler Save handler
* @param MetadataBag $metaData Metadatabag
*/
public function __construct($savePath = null, $name = 'MOCKSESSID')
public function __construct($savePath = null, $name = 'MOCKSESSID', FileSessionHandler $handler = null, MetadataBag $metaData = null)
{
if (null === $savePath) {
$savePath = sys_get_temp_dir();
}

if (!is_dir($savePath)) {
mkdir($savePath, 0777, true);
if (null == $handler) {
$handler = new FileSessionHandler($savePath, 'mocksess_');
}

$this->savePath = $savePath;
$this->handler = $handler;

parent::__construct($name);
parent::__construct($name, $metaData);
}

/**
Expand Down Expand Up @@ -93,7 +97,7 @@ public function regenerate($destroy = false, $lifetime = null)
*/
public function save()
{
file_put_contents($this->getFilePath(), serialize($this->data));
$this->handler->write($this->id, serialize($this->data));
}

/**
Expand All @@ -102,28 +106,16 @@ public function save()
*/
private function destroy()
{
if (is_file($this->getFilePath())) {
unlink($this->getFilePath());
}
}

/**
* Calculate path to file.
*
* @return string File path
*/
private function getFilePath()
{
return $this->savePath.'/'.$this->id.'.mocksess';
$this->handler->destroy($this->id);
}

/**
* Reads session from storage and loads session.
*/
private function read()
{
$filePath = $this->getFilePath();
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
$data = $this->handler->read($this->id);
$this->data = $data ? unserialize($data) : array();

$this->loadSession();
}
Expand Down
F438
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase
*/
private $handler;

/**
* @var string
*/
private $path;

public function setUp()
{
$this->path = sys_get_temp_dir().'/filesessionhandler/';
$this->handler = new FileSessionHandler($this->path);
$this->path = sys_get_temp_dir().'/filesessionhandler';
$this->handler = new FileSessionHandler($this->path, 'mocksess_');

parent::setUp();
}

public function tearDown()
{
foreach (glob($this->path.'*') as $file) {
foreach (glob($this->path.'/*') as $file) {
unlink($file);
}

Expand Down Expand Up @@ -79,7 +82,7 @@ public function testDestroy()

public function testGc()
{
$prefix = $this->path.'sess_';
$prefix = $this->path.'/mocksess_';
$this->handler->write('1', 'data');
touch($prefix.'1', time()-86400);

Expand All @@ -92,13 +95,12 @@ public function testGc()
$this->handler->write('4', 'data');

$this->handler->gc(90000);
$this->assertEquals(4, count(glob($this->path.'*')));
$this->assertEquals(4, count(glob($this->path.'/*')));

$this->handler->gc(4000);
$this->assertEquals(3, count(glob($this->path.'*')));
$this->assertEquals(3, count(glob($this->path.'/*')));

$this->handler->gc(200);
$this->assertEquals(1, count(glob($this->path.'*')));
$this->assertEquals(1, count(glob($this->path.'/*')));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
private $sessionDir;

/**
* @var FileMockSessionStorage
* @var MockFileSessionStorage
*/
protected $storage;

Expand All @@ -40,12 +40,14 @@ protected function setUp()

protected function tearDown()
{
foreach (glob($this->sessionDir.'/mocksess_*') as $file) {
unlink($file);
}

rmdir($this->sessionDir);

$this->sessionDir = null;
$this->storage = null;
array_map('unlink', glob($this->sessionDir.'/*.session'));
if (is_dir($this->sessionDir)) {
rmdir($this->sessionDir);
}
}

public function testStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;

use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
Expand Down Expand Up @@ -137,8 +136,8 @@ public function testSetSaveHandlerPHP53()
$this->markTestSkipped('Test skipped, for PHP 5.3 only.');
}

ini_set('session.save_handler', 'files');
$storage = $this->getStorage();
$storage->setSaveHandler(new NativeFileSessionHandler());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
}

Expand Down
0