- <?php
- class Session extends BaseObject {
- var $magic_string;
- var $remote_addr;
- var $http_user_agent;
- var $expire_time;
- var $grant_time;
- var $session_id;
- var $session_elems;
- var $session_elem_id;
- var $db_id;
- var $session_db;
- var $debug;
- Function Session( $session_db = '' ) {
- global $REMOTE_ADDR;
- $this->BaseObject( 'Session' );
- $this->session_db = new Dummy_Session_Db();
- if ( is_object( $session_db ) ) {
- $this->session_db = $session_db;
- }
- $this->session_id = '';
- $this->remote_addr = '127.0.0.1';
- if ( isset( $REMOTE_ADDR ) && $REMOTE_ADDR != '' ) {
- $this->remote_addr = $REMOTE_ADDR;
- }
- $this->http_user_agent = 'NONE';
- $this->grant_time = new TimeObject();
- $this->expire_time = new TimeObject();
- $this->session_elems = Array();
- $this->session_elem_id = 0;
- $this->db_id = -1;
- $this->debug = new Debug();
- $this->debug->prefix = 'Session';
- $this->debug->Off();
- }
- Function Copy( $target_obj ) {
- $this->magic_string = $target_obj->magic_string;
- $this->remote_addr = $target_obj->remote_addr;
- $this->http_user_agent = $target_obj->http_user_agent;
- $this->expire_time = $target_obj->expire_time;
- $this->grant_time = $target_obj->grant_time;
- $this->session_id = $target_obj->session_id;
- $this->session_elems = $target_obj->session_elems;
- $this->db_id = $target_obj->db_id;
- }
- Function GenerateId() {
- return $this->CreateSessionId();
- }
- Function CreateSessionId() {
- global $REMOTE_ADDR;
- global $HTTP_USER_AGENT;
- /* Blank out the session id */
- $this->magic_string = '';
- /* seed with microseconds since last "whole" second */
- mt_srand((double)microtime()*1000000);
- for( $i = 0; $i < 50; $i++ ) {
- $randval = mt_rand( 33, 128);
- $this->magic_string .= sprintf( '%c', $randval );
- }
- /*
- $this->session_id = rawurlencode( $this->session_id );
- */
- $this->http_user_agent = $HTTP_USER_AGENT;
- $this->session_id =
- md5(
- $this->remote_addr .
- $this->http_user_agent .
- $this->magic_string
- );
- return $this->session_id;
- }
- Function ListAll() {}
- Function Add( $A_SessionElem ) {
- if ( ! is_object( $A_SessionElem ) ) {
- return Array( false, 'Add - Non object pass.' );
- }
- /* Jic this is out of whack */
- $this->session_elem_id = count( $this->session_elems );
- $this->session_elems[ $this->session_elem_id ] = $A_SessionElem;
- $this->session_elem_id++;
- return Array( true, $this->session_elem_id );
- }
- Function Save() {
- global $default;
- $session_db = $this->session_db;
- $session_db->debug->debug = $this->debug->debug;
- $ret_vals = $session_db->Get( $this->session_id, 0 );
- if ( $ret_vals[ 0 ] == true ) {
- $t_obj = $ret_vals[ 1 ];
- $this->db_id = $t_obj->db_id;
- $session_db->Modify( $this );
- } else {
- if ( $this->session_id == '' ) {
- $this->session_id = $this->CreateSessionId();
- }
- $session_db->Add( $this );
- }
- }
- Function Get( $session_id ) {
- global $REMOTE_ADDR;
- global $HTTP_USER_AGENT;
- global $default;
- $session_db = $this->session_db;
- $session_db->debug->debug = $this->debug->debug;
- $this->debug->Message( 'Retrieve : ' . $session_id );
- $ret_vals = $session_db->Get( $session_id );
- $this->session_id = $session_id;
- if ( $ret_vals[ 0 ] == true ) {
- // Trust the database don't trust userspace muckery of
- // the timestamp
- $this->Copy( $ret_vals[ 1 ] );
- return true;
- } else {
- /*
- We shouldn't autocreate a session on a get...
- $this->session_id = $session_id;
- return $session_db->Add( $this );
- */
- }
- }
- Function Expire( $session_id = '') {
- if ( $session_id == '' ) {
- $session_id = $this->db_id;
- }
- $session_db = $this->session_db;
- $session_db->debug->debug = $this->debug->debug;
- $session_db->Delete( $session_id );
- }
- }
- ?>