From 5a0f464e63dda79c3b90ada833504566995cbe38 Mon Sep 17 00:00:00 2001 From: Baldur Rensch Date: Mon, 10 Sep 2012 13:04:44 -0700 Subject: [PATCH 1/3] [HttpFoundation] Added possibility to disable base_64 encoding of session data in PdoSessionHandler This may be necessary when sharing sessions with legacy applications. Added a setting called 'base64_encode' to the configuration array. By default, the base_64 encoding stays enabled, so it does not break BC. Unit tests included. --- .../Storage/Handler/PdoSessionHandler.php | 30 ++++++++++++++----- .../Storage/Handler/PdoSessionHandlerTest.php | 15 ++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php index 9c4c5aa3d1ff3..1c95387053951 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php @@ -51,9 +51,10 @@ public function __construct(\PDO $pdo, array $dbOptions = array()) $this->pdo = $pdo; $this->dbOptions = array_merge(array( - 'db_id_col' => 'sess_id', - 'db_data_col' => 'sess_data', - 'db_time_col' => 'sess_time', + 'db_id_col' => 'sess_id', + 'db_data_col' => 'sess_data', + 'db_time_col' => 'sess_time', + 'base64_encode' => true, ), $dbOptions); } @@ -141,7 +142,11 @@ public function read($id) $sessionRows = $stmt->fetchAll(\PDO::FETCH_NUM); if (count($sessionRows) == 1) { - return base64_decode($sessionRows[0][0]); + if ($this->dbOptions['base64_encode']) { + return base64_decode($sessionRows[0][0]); + } else { + return $sessionRows[0][0]; + } } // session does not exist, create it @@ -164,8 +169,12 @@ public function write($id, $data) $dbIdCol = $this->dbOptions['db_id_col']; $dbTimeCol = $this->dbOptions['db_time_col']; - //session data can contain non binary safe characters so we need to encode it - $encoded = base64_encode($data); + if ($this->dbOptions['base64_encode']) { + //session data can contain non binary safe characters so we need to encode it + $encoded = base64_encode($data); + } else { + $encoded = $data; + } try { if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) { @@ -218,8 +227,13 @@ private function createNewSession($id, $data = '') $sql = "INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time)"; - //session data can contain non binary safe characters so we need to encode it - $encoded = base64_encode($data); + if ($this->dbOptions['base64_encode']) { + //session data can contain non binary safe characters so we need to encode it + $encoded = base64_encode($data); + } else { + $encoded = $data; + } + $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':id', $id, \PDO::PARAM_STR); $stmt->bindParam(':data', $encoded, \PDO::PARAM_STR); diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php index ff1565a183a94..7305766ba7834 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php @@ -60,4 +60,19 @@ public function testSessionGC() $storage->gc(-1); $this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll())); } + + public function testEncoding() + { + $storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array()); + + $storage->write('foo', 'bar'); + $result = $this->pdo->query('SELECT * FROM sessions')->fetchAll(); + $this->assertEquals(base64_encode('bar'), $result[0]['sess_data']); + + $storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions', 'base64_encode' => false), array()); + + $storage->write('foo', 'bar'); + $result = $this->pdo->query('SELECT * FROM sessions')->fetchAll(); + $this->assertEquals('bar', $result[0]['sess_data']); + } } From 718b3e2148a14d5f0c3b2e1d8a8cdb1160d5a1e0 Mon Sep 17 00:00:00 2001 From: Baldur Rensch Date: Mon, 10 Sep 2012 13:44:00 -0700 Subject: [PATCH 2/3] Added description --- .../Tests/Session/Storage/Handler/PdoSessionHandlerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php index 7305766ba7834..82b8446fd460c 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php @@ -61,6 +61,9 @@ public function testSessionGC() $this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll())); } + /** + * This tests disabling the base64_encoding option. + */ public function testEncoding() { $storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array()); From 78d697ec6f08de6ac8eb20ced52f4bb9b873b25d Mon Sep 17 00:00:00 2001 From: Baldur Rensch Date: Mon, 10 Sep 2012 13:50:12 -0700 Subject: [PATCH 3/3] Removed unnecessary else --- .../Session/Storage/Handler/PdoSessionHandler.php | 6 +++--- .../Tests/Session/Storage/Handler/PdoSessionHandlerTest.php | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php index 1c95387053951..531d04bf407a1 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php @@ -144,9 +144,9 @@ public function read($id) if (count($sessionRows) == 1) { if ($this->dbOptions['base64_encode']) { return base64_decode($sessionRows[0][0]); - } else { - return $sessionRows[0][0]; - } + } + + return $sessionRows[0][0]; } // session does not exist, create it diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php index 82b8446fd460c..7305766ba7834 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php @@ -61,9 +61,6 @@ public function testSessionGC() $this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll())); } - /** - * This tests disabling the base64_encoding option. - */ public function testEncoding() { $storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());