diff options
author | robocoder <anthon.pang@gmail.com> | 2011-07-03 05:36:31 +0400 |
---|---|---|
committer | robocoder <anthon.pang@gmail.com> | 2011-07-03 05:36:31 +0400 |
commit | 340a868616c5cb5db173623cd4fa4983e1fe6875 (patch) | |
tree | 89b5db6c3d0fca7d66b7dc1a161974db94d8d340 | |
parent | 8d2efe9ad3fff6b850860aae572c508f6787bde3 (diff) |
refs #2491 - replace Zend_Session_SaveHandler_DbTable with a leaner and cleaner implementation
git-svn-id: http://dev.piwik.org/svn/trunk@4969 59fd770c-687e-43c8-a1e3-f5a4ff64c105
-rw-r--r-- | core/Session.php | 17 | ||||
-rw-r--r-- | core/Session/SaveHandler/DbTable.php | 141 |
2 files changed, 143 insertions, 15 deletions
diff --git a/core/Session.php b/core/Session.php index 5dd32a4a0e..e887bf6040 100644 --- a/core/Session.php +++ b/core/Session.php @@ -56,7 +56,6 @@ class Piwik_Session extends Zend_Session if(in_array($currentSaveHandler, array('user', 'mm', 'files'))) { $db = Zend_Registry::get('db'); - $db->getConnection(); $config = array( 'name' => Piwik_Common::prefixTable('session'), @@ -67,7 +66,7 @@ class Piwik_Session extends Zend_Session 'db' => $db, ); - $saveHandler = new Zend_Session_SaveHandler_DbTable($config); + $saveHandler = new Piwik_Session_SaveHandler_DbTable($config); if($saveHandler) { self::setSaveHandler($saveHandler); @@ -82,22 +81,10 @@ class Piwik_Session extends Zend_Session try { Zend_Session::start(); - register_shutdown_function(array('Piwik_Session', 'writeClose'), true); + register_shutdown_function(array('Zend_Session', 'writeClose'), true); } catch(Exception $e) { Piwik::log('Unable to start session: ' . $e->getMessage()); Piwik_ExitWithMessage(Piwik_Translate('General_ExceptionUnableToStartSession')); } } - - /** - * writeClose() - Shutdown the sesssion, close writing and detach $_SESSION from the back-end storage mechanism. - * This will complete the internal data transformation on this request. - * - * @param bool $readonly - remove write access - * @return void - */ - public static function writeClose($readOnly = true) - { - Zend_Session::writeClose($readOnly); - } } diff --git a/core/Session/SaveHandler/DbTable.php b/core/Session/SaveHandler/DbTable.php new file mode 100644 index 0000000000..b5cf0c8ba0 --- /dev/null +++ b/core/Session/SaveHandler/DbTable.php @@ -0,0 +1,141 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id$ + * + * @category Piwik + * @package Piwik + */ + +/** + * Database-backed session save handler + * + * @package Piwik + * @subpackage Piwik_Session + */ +class Piwik_Session_SaveHandler_DbTable implements Zend_Session_SaveHandler_Interface +{ + protected $config; + protected $maxLifetime; + + function __construct($config) + { + $this->config = $config; + $this->maxLifetime = ini_get('session.gc_maxlifetime'); + } + + /** + * Destructor + * + * @return void + */ + public function __destruct() + { + Zend_Session::writeClose(); + } + + /** + * Open Session - retrieve resources + * + * @param string $save_path + * @param string $name + * @return boolean + */ + public function open($save_path, $name) + { + $this->config['db']->getConnection(); + + return true; + } + + /** + * Close Session - free resources + * + * @return boolean + */ + public function close() + { + return true; + } + + /** + * Read session data + * + * @param string $id + * @return string + */ + public function read($id) + { + $sql = 'SELECT '.$this->config['dataColumn'].' FROM '.$this->config['name'] + .' WHERE '.$this->config['primary'].' = ?' + .' AND '.$this->config['modifiedColumn'].' + '.$this->config['lifetimeColumn'].' < ?'; + + $result = $this->config['db']->fetchOne($sql, array($id, time())); + if(!$result) + $result = ''; + + return $result; + } + + /** + * Write Session - commit data to resource + * + * @param string $id + * @param mixed $data + * @return boolean + */ + public function write($id, $data) + { + $sql = 'INSERT INTO '.$this->config['name'] + .' ('.$this->config['primary'].',' + .$this->config['modifiedColumn'].',' + .$this->config['lifetimeColumn'].',' + .$this->config['dataColumn'].')' + .' VALUES (?,?,?,?)' + .' ON DUPLICATE KEY UPDATE ' + .$this->config['modifiedColumn'].' = ?,' + .$this->config['lifetimeColumn'].' = ?,' + .$this->config['dataColumn'].' = ?'; + + $this->config['db']->query($sql, array($id, time(), $this->maxLifetime, $data, time(), $this->maxLifetime, $data)); + + return true; + } + + /** + * Destroy Session - remove data from resource for + * given session id + * + * @param string $id + * @return boolean + */ + public function destroy($id) + { + $sql = 'DELETE FROM '.$this->config['name'] + .' WHERE '.$this->config['primary'].' = ?'; + + $this->config['db']->query($sql, array($id)); + + return true; + } + + /** + * Garbage Collection - remove old session data older + * than $maxlifetime (in seconds) + * + * @param int $maxlifetime + * @return true + */ + public function gc($maxlifetime) + { + $sql = 'DELETE FROM '.$this->config['name'] + .' WHERE '.$this->config['modifiedColumn'].' + '.$this->config['lifetimeColumn'].' < ?'; + + $this->config['db']->query($sql, array(time())); + + return true; + } +} |