Permalink
Browse files

Convert the database session handler class to a SessionHandler

Remove base64_encode of session data
Fix file session handler filenames (prepended with sess_)
Disable session.use_strict_mode as a check is already performed manually
Rename session class destroy() function to kill() to not conflict with the database session handler destroy() function
  • Loading branch information...
1 parent d71c72d commit 85b25463e6c0be8e8fb2a4d6b916ec3863fa98e8 @haraldpdl committed Feb 26, 2014
@@ -1,8 +1,8 @@
<?php
/**
* osCommerce Online Merchant
- *
- * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
+ *
+ * @copyright Copyright (c) 2014 osCommerce; http://www.oscommerce.com
* @license BSD License; http://www.oscommerce.com/bsdlicense.txt
*/
@@ -26,13 +26,22 @@ class Session {
public static function load($name = null) {
$class_name = 'osCommerce\\OM\\Core\\Session\\' . OSCOM::getConfig('store_sessions');
- if ( class_exists($class_name) ) {
- return new $class_name($name);
+ if ( !class_exists($class_name) ) {
+ trigger_error('Session Handler \'' . $class_name . '\' does not exist, using default \'osCommerce\\OM\\Core\\Session\\File\'', E_USER_ERROR);
+
+ $class_name = 'osCommerce\\OM\\Core\\Session\\File';
+ }
+
+ $obj = new $class_name();
+
+ if ( !isset($name) ) {
+ $name = 'sid';
}
- trigger_error('Session Handler \'' . $class_name . '\' does not exist, using default \'osCommerce\\OM\\Core\\Session\\File\'', E_USER_ERROR);
+ $obj->setName($name);
+ $obj->setLifeTime(ini_get('session.gc_maxlifetime'));
- return new Session\File($name);
+ return $obj;
}
}
?>
@@ -16,24 +16,16 @@
* @since v3.0.0
*/
- class Database extends \osCommerce\OM\Core\SessionAbstract {
+ class Database extends \osCommerce\OM\Core\SessionAbstract implements \SessionHandlerInterface {
/**
- * Initialize database based session storage handler
+ * Initialize database storage handler
*
- * @param string $name The name of the session
* @since v3.0.0
*/
- public function __construct($name) {
- $this->setName($name);
-
- session_set_save_handler(array($this, 'handlerOpen'),
- array($this, 'handlerClose'),
- array($this, 'handlerRead'),
- array($this, 'handlerWrite'),
- array($this, 'handlerDestroy'),
- array($this, 'handlerClean'));
+ public function __construct() {
+ session_set_save_handler($this, true);
}
/**
@@ -50,99 +42,84 @@ public function exists($id) {
}
/**
- * Opens the database based session storage handler
+ * Opens the database storage handler
*
- * @since v3.0.0
+ * @since v3.0.3
*/
- public function handlerOpen() {
+ public function open($save_path, $id) {
return true;
}
/**
- * Closes the database based session storage handler
+ * Closes the database storage handler
*
- * @since v3.0.0
+ * @since v3.0.3
*/
- public function handlerClose() {
+ public function close() {
return true;
}
/**
- * Read session data from the database based session storage handler
+ * Read session data from the database storage handler
*
* @param string $id The ID of the session
- * @since v3.0.0
+ * @since v3.0.3
*/
- public function handlerRead($id) {
+ public function read($id) {
$data = array('id' => $id);
$result = OSCOM::callDB('Session\Database\Get', $data, 'Core');
if ( $result !== false ) {
- return base64_decode($result['value']);
+ return $result['value'];
}
return false;
}
/**
- * Writes session data to the database based session storage handler
+ * Writes session data to the database storage handler
*
* @param string $id The ID of the session
* @param string $value The session data to store
- * @since v3.0.0
+ * @since v3.0.3
*/
- public function handlerWrite($id, $value) {
+ public function write($id, $value) {
$data = array('id' => $id,
'expiry' => time(),
- 'value' => base64_encode($value));
+ 'value' => $value);
return OSCOM::callDB('Session\Database\Save', $data, 'Core');
}
/**
- * Destroys the session data from the database based session storage handler
+ * Deletes the session data from the database storage handler
*
* @param string $id The ID of the session
- * @since v3.0.0
+ * @since v3.0.3
*/
- public function handlerDestroy($id) {
- return $this->delete($id);
+ public function destroy($id) {
+ $data = array('id' => $id);
+
+ return OSCOM::callDB('Session\Database\Delete', $data, 'Core');
}
/**
- * Garbage collector for the database based session storage handler
+ * Garbage collector for the database storage handler
*
* @param string $max_life_time The maxmimum time a session should exist
- * @since v3.0.0
+ * @since v3.0.3
*/
- public function handlerClean($max_life_time) {
+ public function gc($max_life_time) {
$data = array('expiry' => $max_life_time);
return OSCOM::callDB('Session\Database\DeleteExpired', $data, 'Core');
}
-
-/**
- * Deletes the session data from the database based session storage handler
- *
- * @param string $id The ID of the session
- * @since v3.0.0
- */
-
- public function delete($id = null) {
- if ( empty($id) ) {
- $id = $this->_id;
- }
-
- $data = array('id' => $id);
-
- return OSCOM::callDB('Session\Database\Delete', $data, 'Core');
- }
}
?>
@@ -1,8 +1,8 @@
<?php
/**
* osCommerce Online Merchant
- *
- * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
+ *
+ * @copyright Copyright (c) 2014 osCommerce; http://www.oscommerce.com
* @license BSD License; http://www.oscommerce.com/bsdlicense.txt
*/
@@ -19,7 +19,7 @@
class File extends \osCommerce\OM\Core\SessionAbstract {
/**
- * Holds the file system path where sessions are saved.
+ * Holds the file system path where sessions are saved
*
* @var string
* @since v3.0.0
@@ -28,15 +28,15 @@ class File extends \osCommerce\OM\Core\SessionAbstract {
protected $_save_path;
/**
- * Initialize file based session storage handler
+ * Initialize file storage handler
*
- * @param string $name The name of the session
* @since v3.0.0
*/
- public function __construct($name) {
- $this->setName($name);
+ public function __construct() {
$this->setSavePath(OSCOM::BASE_DIRECTORY . 'Work/Session');
+
+ register_shutdown_function('session_write_close');
}
/**
@@ -47,40 +47,30 @@ public function __construct($name) {
*/
public function exists($id) {
- return file_exists($this->_save_path . '/' . $id);
- }
-
-/**
- * Deletes an existing session
- *
- * @since v3.0.0
- */
+ $id = basename($id);
- public function destroy() {
- $this->delete();
-
- parent::destroy();
+ return file_exists($this->_save_path . '/sess_' . $id);
}
/**
- * Deletes an existing session from the storage handler
+ * Deletes the session data from the file storage handler
*
* @param string $id The ID of the session
* @since v3.0.0
*/
- public function delete($id = null) {
- if ( empty($id) ) {
- $id = $this->_id;
- }
+ public function destroy($id) {
+ $id = basename($id);
if ( $this->exists($id) ) {
- unlink($this->_save_path . '/' . $id);
+ return unlink($this->_save_path . '/sess_' . $id);
}
+
+ return false;
}
/**
- * Return the session file based storage location
+ * Return the session file storage location
*
* @return string
* @since v3.0.0
@@ -91,7 +81,7 @@ public function getSavePath() {
}
/**
- * Sets the storage location for the file based storage handler
+ * Sets the storage location for the file storage handler
*
* @param string $path The file path to store the session data in
* @since v3.0.0
Oops, something went wrong.

0 comments on commit 85b2546

Please sign in to comment.