-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f33b77c
[HttpFoundation] Added a custom file save handler
b2cc580
[HttpFoundation] Removed Native*Handler session save handler classes
13a2c82
[FrameworkBundle] Refactor session file handler service name and upda…
3c8cc0a
[HttpFoundation][Sessions] Refactored tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[HttpFoundation] Added a custom file save handler
- Loading branch information
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; | ||
|
||
/** | ||
* FileSessionHandler. | ||
* | ||
* @author Drak <drak@zikula.org> | ||
*/ | ||
class FileSessionHandler implements \SessionHandlerInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $savePath; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $savePath Path of directory to save session files. | ||
*/ | ||
public function __construct($savePath) | ||
{ | ||
$this->savePath = $savePath; | ||
if (false === is_dir($this->savePath)) { | ||
mkdir($this->savePath, 0777, true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc] | ||
*/ | ||
function open($savePath, $sessionName) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc] | ||
*/ | ||
function close() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc] | ||
*/ | ||
function read($id) | ||
{ | ||
$file = $this->savePath.'/sess_'.$id; | ||
if (file_exists($file)) { | ||
return file_get_contents($file); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* {@inheritdoc] | ||
*/ | ||
function write($id, $data) | ||
{ | ||
return false === file_put_contents($this->savePath.'/sess_'.$id, $data) ? false : true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc] | ||
*/ | ||
function destroy($id) | ||
{ | ||
$file = $this->savePath.'/sess_'.$id; | ||
if (file_exists($file)) { | ||
unlink($file); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc] | ||
*/ | ||
function gc($maxlifetime) | ||
{ | ||
foreach (glob($this->savePath.'/sess_*') as $file) { | ||
if ((filemtime($file) + $maxlifetime) < time()) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...ymfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; | ||
|
||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler; | ||
|
||
/** | ||
* Test class for FileSessionHandler. | ||
* | ||
* @author Drak <drak@zikula.org> | ||
*/ | ||
class FileSessionStorageTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var FileSessionHandler | ||
*/ | ||
private $handler; | ||
|
||
private $path; | ||
|
||
public function setUp() | ||
{ | ||
$this->path = sys_get_temp_dir().'/filesessionhandler/'; | ||
$this->handler = new FileSessionHandler($this->path); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
foreach (glob($this->path.'*') as $file) { | ||
unlink($file); | ||
} | ||
|
||
rmdir($this->path); | ||
|
||
$this->handler = null; | ||
} | ||
|
||
public function test__construct() | ||
{ | ||
$this->assertTrue(is_dir($this->path)); | ||
} | ||
|
||
public function testOpen() | ||
{ | ||
$this->assertTrue($this->handler->open('a', 'b')); | ||
} | ||
|
||
public function testClose() | ||
{ | ||
$this->assertTrue($this->handler->close()); | ||
} | ||
|
||
public function testReadWrite() | ||
{ | ||
$this->assertEmpty($this->handler->read('123')); | ||
$this->assertTrue($this->handler->write('123', 'data')); | ||
$this->assertEquals('data', $this->handler->read('123')); | ||
} | ||
|
||
public function testDestroy() | ||
{ | ||
$this->handler->write('456', 'data'); | ||
$this->handler->destroy('123'); | ||
$this->assertEquals('data', $this->handler->read('456')); | ||
$this->handler->destroy('456'); | ||
$this->assertEmpty($this->handler->read('456')); | ||
} | ||
|
||
public function testGc() | ||
{ | ||
$prefix = $this->path.'sess_'; | ||
$this->handler->write('1', 'data'); | ||
touch($prefix.'1', time()-86400); | ||
|
||
$this->handler->write('2', 'data'); | ||
touch($prefix.'2', time()-3600); | ||
|
||
$this->handler->write('3', 'data'); | ||
touch($prefix.'3', time()-300); | ||
|
||
$this->handler->write('4', 'data'); | ||
|
||
$this->handler->gc(90000); | ||
$this->assertEquals(4, count(glob($this->path.'*'))); | ||
|
||
$this->handler->gc(4000); | ||
$this->assertEquals(3, count(glob($this->path.'*'))); | ||
|
||
$this->handler->gc(200); | ||
$this->assertEquals(1, count(glob($this->path.'*'))); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).