Location: PHPKode > scripts > XS PHP Library > xs-php-library/inc/session.inc.php
<?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: session.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $

/**
 * Session handling class.
 *
 * Used for managing sessions - opening, closing, clearing reading/writing
 * parameters advanced functions include choosing storage containers. It is a
 * wrapper for standard PHP session management functions.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @package sess
 * @version $Id: session.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $
 */
class Session extends Object {

	/**
	 * Indicates whether data is stored in storage or not.
     * @access private
	 * @var boolean
	 */
    var $_persistent;

    /**
     * Session class constructor.
     *
     * Creates the new instance of Session class and sets up basic properties.
     * Note - only the one instance of this class can be created.
     *
     * @access public
     * @param boolean $persistent indicates whether the session data should be
     * stored in storage, defaults to true.
     * @param string $cache the current cache limiter, defaults to "nocache".
     * @return void
     */
    function Session($persistent=true, $cache='nocache') {
        global $session_storage;
        Object::Object();
        $this->_persistent = $persistent;
        if (defined('SESSION_CREATED')) {
            trigger_error('Could not create second session object.', E_USER_ERROR);
        }
        if (class_exists('Storage')) {
            $session_storage = new Storage;
            if (!is_object($session_storage)) {
                trigger_error('Could not create session storage object.', E_USER_ERROR);
            }
        } else {
            trigger_error('Storage class definition has not been included.', E_USER_ERROR);
        }
        define('SESSION_CREATED', 1);
        @session_cache_limiter($cache);
    }

    /**
     * Gets the current session id.
     *
     * Returns the session id for the current session.
     *
     * @access public
     * @return string
     */
    function getId() {
        return @session_id();
    }
    
    /**
     * Gets the current session name.
     *
     * Returns the name of the current session.
     *
     * @access public
     * @return string
     */
    function getName() {
        return @session_name();
    }
    
    /**
     * Gets the current session save path.
     *
     * Returns the path of the current directory used to save session data.
     *
     * @access public
     * @return string
     */
    function getPath() {
        return @session_save_path();
    }
    
    /**
     * Gets the current cache limiter.
     *
     * Returns the name of the current cache limiter.
     *
     * @access public
     * @return string
     */
    function getCache() {
        return @session_cache_limiter();
    }

    /**
     * Gets the  current session variable.
     *
     * If the the variable is registered returns its value or null otherwise.
     *
     * @access public
     * @param string $param the variable name.
     * @return mixed
     */
    function getParam($param) {
        global $HTTP_SESSION_VARS;
        if (ini_get('register_globals')) {
            if (session_is_registered($param)) {
                if (isset($GLOBALS[$param])) {
                    return $GLOBALS[$param];
                }
            }
        } else {
            if (isset($HTTP_SESSION_VARS[$param])) {
                return $HTTP_SESSION_VARS[$param];
            }
        }
        return null;
    }
    
    /**
     * Gets the current session storage module.
     *
     * Returns the current session storage module.
     *
     * @access public
     * @return string
     */
    function getModule() {
        return @session_module_name();
    }

    /**
     * Sets the current session id.
     *
     * If called when the session is already opened, it returns error object
     * otherwise it returns true.
     *
     * @access public
     * @param string $id the new session id.
     * @return mixed
     */
    function setId($id) {
        global $HTTP_SESSION_VARS;
        if (!isset($HTTP_SESSION_VARS)) {
            @session_id($id);
        } else {
            return new Error('setid(): Session is already opened.');
        }
        return true;
    }

    /**
     * Sets the current session name.
     *
     * If called when the session is already opened, it returns error object
     * otherwise it returns true.
     *
     * @access public
     * @param string $name the new session name.
     * @return mixed
     */
    function setName($name) {
        global $HTTP_SESSION_VARS;
        if (!isset($HTTP_SESSION_VARS)) {
            @session_name($name);
        } else {
            return new Error('setname(): Session is already opened.');
        }
        return true;
    }

    /**
     * Sets the current session save path.
     *
     * If called when the session is already opened, it returns error object
     * otherwise it returns true.
     *
     * @access public
     * @param string $path the path to which data is saved.
     * @return mixed
     */
    function setPath($path) {
        global $HTTP_SESSION_VARS;
        if (!isset($HTTP_SESSION_VARS)) {
            @session_save_path($path);
        } else {
            return new Error('setpath(): Session is already opened.');
        }
        return true;
    }

    /**
     * Sets the current cache limiter.
     *
     * If called when the session is already opened, it returns error object
     * otherwise it returns true.
     *
     * @access public
     * @param string $cache the cache limiter.
     * @return mixed
     */
    function setCache($cache) {
        global $HTTP_SESSION_VARS;
        if (!isset($HTTP_SESSION_VARS)) {
            @session_cache_limiter($cache);
        } else {
            return new Error('setcache(): Session is already opened.');
        }
        return true;
    }

    /**
     * Sets the current session variable.
     *
     * If called when the session is already opened, it returns error object
     * otherwise it returns true on success or a error object with an  error
     * message on any kind of failure.
     *
     * @access public
     * @param string $param the variable name.
     * @param string $value the variable value.
     * @return mixed
     */
    function setParam($param, $value) {
        global $HTTP_SESSION_VARS;
        if (isset($HTTP_SESSION_VARS)) {
            if (strlen($param)) {
                if (ini_get('register_globals')) {
                    if (@session_is_registered($param)) {
                        if (isset($GLOBALS[$param])) {
                           unset($GLOBALS[$param]);
                        }
                        @session_unregister($param);
                    }
                    @session_register($param);
                    $GLOBALS[$param] = $value;
                } else {
                    if (isset($HTTP_SESSION_VARS[$param])) {
                        unset($HTTP_SESSION_VARS[$param]);
                    }
                    $HTTP_SESSION_VARS[$param] = $value;
                }
            } else {
                return new Error('setparam(): Session param name is empty.');
            }
        } else {
            return new Error('setparam(): Session has not been opened.');
        }
        return true;
    }
    
    /**
     * Sets the current session storage module.
     *
     * If called when the session is already opened, it returns error object
     * otherwise it returns true.
     *
     * @access public
     * @param string $module the module which will be used to store session data.
     * @return mixed
     */
    function setModule($module) {
        global $HTTP_SESSION_VARS;
        if (!isset($HTTP_SESSION_VARS)) {
            @session_module_name($module);
        } else {
            return new Error('setmodule(): Session is already opened.');
        }
        return true;
    }

    /**
     * Attempt to open session.
     *
     * If called when the session is already opened, it returns error
     * object otherwise it returns true on success or a error object with an
     * error message on any kind of failure.
     *
     * @access public
     * @return mixed
     */
    function open() {
        global $HTTP_SESSION_VARS;
        if (!isset($HTTP_SESSION_VARS)) {
            @session_start();
            if (!isset($GLOBALS['HTTP_SESSION_VARS'])) {
                return new Error('open(): Session could not be opened.');
            }
        } else {
            return new Error('open(): Session is already opened.');
        }
        return true;
    }

    /**
     * Attempt to close session.
     *
     * If called when the session is closed, it returns error object otherwise
     * it returns true on success or a error object with an error message on
     * any kind of failure. If session is in persistent mode all current
     * session variables are saved in storage module for the next call.
     *
     * @access public
     * @return mixed
     */
    function close() {
        global $HTTP_SESSION_VARS;
        if (isset($HTTP_SESSION_VARS)) {
            if (!$this->_persistent) {
                @session_destroy();
            } else {
                @session_write_close();
            }
            if (ini_get('register_globals')) {
               @session_unset();
            }
            unset($GLOBALS['HTTP_SESSION_VARS']);
        } else {
            return new Error('close(): Session has not been opened.');
        }
        return true;
    }
    
    /**
     * Free all session variables.
     *
     * Frees all session variables currently registered. If called when the
     * session is closed, it returns error object otherwise it returns true.
     *
     * @access public
     * @return mixed
     */
    function clear() {
        global $HTTP_SESSION_VARS;
        if (isset($HTTP_SESSION_VARS)) {
            if (ini_get('register_globals')) {
               @session_unset();
            }
            unset($GLOBALS['HTTP_SESSION_VARS']);
        } else {
            return new Error('clear(): Session has not been opened.');
        }
        return true;
    }
    
    /**
     * Encodes the current session data to a string.
     *
     * If called when the session is closed, it returns error object otherwise
     * it returns the contents of the current session encoded.
     *
     * @access public
     * @return mixed
     */
    function encode() {
        global $HTTP_SESSION_VARS;
        if (isset($HTTP_SESSION_VARS)) {
            $result = @session_encode();
        } else {
            return new Error('encode(): Session has not been opened.');
        }
        return $result;
    }
    
    /**
     * Decodes session data from a string.
     *
     * Decodes the session data, setting variables stored in the session.
     * If called when the session is closed, it returns error object otherwise
     * it returns true.
     *
     * @access public
     * @param string $data the module which will be used to store session data.
     * @return mixed
     */
    function decode($data) {
        global $HTTP_SESSION_VARS;
        if (isset($HTTP_SESSION_VARS)) {
            @session_decode($data);
        } else {
            return new Error('decode(): Session has not been opened.');
        }
        return true;
    }
    
    /**
     * Checks whether the specified session variable exists.
     *
     * Returns true if the session variable exists, false otherwise.
     *
     * @access public
     * @param string $param the variable name.
     * @return boolean
     */
    function exists($param) {
        global $HTTP_SESSION_VARS;
        if (isset($HTTP_SESSION_VARS)) {
            if (ini_get('register_globals')) {
                return @session_is_registered($param);
            } else {
                return isset($HTTP_SESSION_VARS[$param]);
            }
        }
        return false;
    }
    
    /**
     * Generates a storable session variable.
     *
     * If called when the session is closed, it returns error object otherwise
     * it returns true. Saves global variable as serialized session variable.
     * This is useful when working with objects stored within session variables.
     *
     * @access public
     * @param string $param the variable name.
     * @return mixed
     */
    function serialize($param) {
        if (isset($GLOBALS[$param])) {
            $string = @serialize($GLOBALS[$param]);
            if ($this->setParam($param, $string) != true) {
                return new Error('serialize(): Could not set session param.');
            }
        } else {
            return new Error('serialize(): Session could not serialize param.');
        }
        return true;
    }

    /**
     * Restores global variable serialized in session variable.
     *
     * If called when the session is closed, it returns error object otherwise
     * it returns true. Restores global variable serialized in session variable.
     * This is useful when working with objects stored within session variables.
     *
     * @access public
     * @param string $param the variable name.
     * @return mixed
     */
    function unserialize($param) {
        $string = $this->getParam($param);
        if (isset($string) && $this->exists($param)) {
            $GLOBALS[$param] = @unserialize($string);
        } else {
            return new Error('unserialize(): Session param has not been set.');
        }
        return true;
    }
    
    /**
     * Finds whether the session is opened.
     *
     * Returns true if the session is opened, false otherwise.
     *
     * @access public
     * @return boolean
     */
    function isOpened() {
        return isset($GLOBALS['HTTP_SESSION_VARS']);
    }
    
    /**
     * Finds whether the session data is stored in storage.
     *
     * Returns true if the session data is stored in storage, false otherwise.
     *
     * @access public
     * @return boolean
     */
    function isPersistent() {
        return $this->_persistent;
    }
}

/**
 * Opens the session storage container.
 *
 * Returns true on success or false if storage container is not aviable or on
 * any kind of failure. Called at session initialization.
 *
 * @author Robert Bala <hide@address.com>
 * @access private
 * @param string $path the path to which data is saved.
 * @param string $name the current session name.
 * @return boolean
 */
function _session_open($path, $name) {
    global $session_storage;
    if (isset($session_storage)) {
        return $session_storage->open($path, $name);
    } else {
        return false;
    }
}

/**
 * Closes the session storage container.
 *
 * Returns true on success or false if storage container is not aviable or on
 * any kind of failure. 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 private
 * @return boolean
 */
function _session_close() {
    global $session_storage;
    if (isset($session_storage)) {
        return $session_storage->close();
    } else {
        return false;
    }
}

/**
 * Reads data from the session storage container.
 *
 * Returns current session data on success or false if storage container
 * is not aviable or on any kind of failure. If data for the session are not
 * present returns empty string. Called just after the session is opened.
 *
 * @author Robert Bala <hide@address.com>
 * @access private
 * @param string $id the current session id.
 * @return mixed
 */
function _session_read($id) {
    global $session_storage;
    if (isset($session_storage)) {
        return $session_storage->read($id);
    } else {
        return '';
    }
}

/**
 * Writes data to the session storage container.
 *
 * Returns true on success or false if storage container is not aviable or on
 * any kind of failure. Called when there is the need to save session data.
 *
 * @author Robert Bala <hide@address.com>
 * @access private
 * @param string $id the current session id.
 * @param string $data the current session data.
 * @return boolean
 */
function _session_write($id, $data) {
    global $session_storage;
    if (isset($session_storage)) {
       return $session_storage->write($id, $data);
    } else {
        return false;
    }
}

/**
 * Destroys all data registered to a session from storage container.
 *
 * Returns true on success or false if storage container is not aviable or on
 * any kind of failure.
 *
 * @author Robert Bala <hide@address.com>
 * @access private
 * @param string $id the current session id.
 * @return boolean
 */
function _session_destroy($id) {
    global $session_storage;
    if (isset($session_storage)) {
        return $session_storage->destroy($id);
    } else {
        return false;
    }
}

/**
 * Garbage collection for storage container.
 *
 * Returns true on success or false if storage container is not aviable or 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 private
 * @param string $maxlifetime the maximum life time for inactive sessions.
 * @return boolean
 */
function _session_gc($maxlifetime) {
    global $session_storage;
    if (isset($session_storage)) {
        return $session_storage->gc($maxlifetime);
    } else {
        return false;
    }
}

session_set_save_handler("_session_open", "_session_close", "_session_read", "_session_write", "_session_destroy", "_session_gc");

?>
Return current item: XS PHP Library