<?php
//
// +--------------------------------------------------------------------------+
// | |
// | XS PHP Library Generic Classes Library |
// | |
// | Copyright (c) 2001-2002 XSPHPLib Group. |
// | |
// +--------------------------------------------------------------------------+
// | |
// | Distributed under the terms of the GNU Lesser General Public License as |
// | published by the Free Software Foundation version 2.1 |
// | See the GNU Lesser General Public License for more details. You should |
// | have received a copy of the GNU Lesser General Public License along with |
// | this package; if not, write to the Free Software Foundation, Inc., |
// | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +--------------------------------------------------------------------------+
// | |
// | Authors: Robert Bala <hide@address.com> |
// | |
// +--------------------------------------------------------------------------+
//
// $Id: sgfiles.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $
/**
* File session storage cointainer class.
*
* Used for store session data within a file system.
*
* @author Robert Bala <hide@address.com>
* @access public
* @package sess
* @version $Id: sgfiles.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $
*/
class Storage extends Object {
/**
* Storage class constructor.
*
* Creates the new instance of Storage class.
* Note - this storage container is slower then that which comes from the
* PHP distribution. Note - only one storage container is allowed to handle
* session data.
*
* @access public
* @return void
*/
function Storage() {
Object::Object();
}
/**
* Opens the session storage container.
*
* Always returns true. Sets storage path and session name. Called when
* the session is initialized.
*
* @author Robert Bala <hide@address.com>
* @access public
* @param string $path the path to which data is saved.
* @param string $name the current session name.
* @return boolean
*/
function open($path, $name) {
global $storage_path, $storage_name;
$storage_path = $path;
$storage_name = $name;
return true;
}
/**
* Closes the session storage container.
*
* Always returns true. Called when the session is going down. It is used to
* release any resources allocated by the session.
*
* @author Robert Bala <hide@address.com>
* @access public
* @return boolean
*/
function close() {
global $storage_path, $storage_name;
$storage_path = '';
$storage_name = '';
return true;
}
/**
* Reads data from the session storage container.
*
* Returns current session data on success or empty string on any kind of
* failure. Called just after the session is opened. Reads data from file.
*
* @author Robert Bala <hide@address.com>
* @access public
* @param string $id the current session id.
* @return mixed
*/
function read($id) {
global $storage_path, $storage_name;
$storage = new File($storage_path . '/sess_' . $id, 'r');
if (is_object($storage) && $storage->exists()) {
if ($storage->open() == true) {
$result = $storage->read();
$storage->close();
if (!object_isError($result)) {
return $result;
}
}
}
return '';
}
/**
* Writes data to the session storage container.
*
* Returns true on success or false on any kind of failure. Called when
* there is the need to save session data. Writes data to file.
*
* @author Robert Bala <hide@address.com>
* @access public
* @param string $id the current session id.
* @param string $data the current session data.
* @return boolean
*/
function write($id, $data) {
global $storage_path, $storage_name;
$storage = new File($storage_path . '/sess_' . $id, 'w+');
if (is_object($storage)) {
if ($storage->open() == true) {
$result = $storage->write($data);
$storage->close();
if (!object_isError($result)) {
return $result;
}
}
}
return false;
}
/**
* Destroys all data registered to a session from storage container.
*
* Returns true on success or false on any kind of failure.
*
* @author Robert Bala <hide@address.com>
* @access public
* @param string $id the current session id.
* @return boolean
*/
function destroy($id) {
global $storage_path, $storage_name;
$storage = new File($storage_path . '/sess_' . $id, 'r');
if (is_object($storage)) {
return ($storage->delete() == true);
}
return false;
}
/**
* Garbage collection for storage container.
*
* Returns true on success or false on any kind of failure. Removes inactive
* session from storage container. Called just after the session is opened.
*
* @author Robert Bala <hide@address.com>
* @access public
* @param string $maxlifetime the maximum life time for inactive sessions.
* @return boolean
*/
function gc($maxlifetime) {
global $storage_path, $storage_name;
$storage = new Dir($storage_path);
if (is_object($storage)) {
if ($storage->open() == true) {
$modules = $storage->readFiles();
if (!object_isError($modules)) {
$expire = time() - $maxlifetime;
for ($i = 0; $i < count($modules); $i++) {
if (!strstr($modules[$i]->getName(), 'sess_')) {
continue;
}
if ($expire < $modules[$i]->getMTime()) {
continue;
}
$result = $modules[$i]->delete();
if (object_isError($result)) {
break;
}
}
}
$storage->close();
return (object_isError($result) == false);
}
}
return false;
}
}
?>