Mysql_Session_Db.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:10k
- <?php
- class Mysql_Session_Db extends BaseObject {
- var $debug;
- var $session_db;
- Function Mysql_Session_Db( $db_config = '' ) {
- $this->BaseObject( 'Mysql_Session_Db' );
- $this->session_db = new Mysql_Db();
- if ( is_object( $db_config ) ) {
- $this->session_db->db = $db_config;
- }
- $this->debug = new Debug;
- $this->debug->prefix = 'Mysql_Db::Session_Db';
- $this->debug->Off();
- $this->session_db->debug->Off();
- } /* END Mysql_User_Db() */
- Function InitDbConnection() {
- /*
- Make sure the database handle is available
- ( if not try to open it )
- */
- $returns = Array();
- $this->session_db->debug->debug = $this->debug->debug;
- if ( $this->session_db->connection_init == false) {
- $returns = $this->session_db->CreateConnection();
- }
- /* Oh no we failed to open the connection */
- if ( $this->session_db->connection_init == false ) {
- return array( false, 'Database not connected', $returns );
- }
- return array( true );
- }
- Function ListAll() {
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- /* There is no need for this right now - JEO */
- return array( false);
- } /* END List All */
- Function Add( $ThisSession ) {
- /* Sanity check on input */
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- if ( $ThisSession->grant_time->time == '' ) {
- $ThisSession->grant_time->Modify();
- if ( $ThisSession->grant_time->time == '' ) {
- $ThisSession->grant_time->GetLocalTime();
- }
- }
- if ( $ThisSession->expire_time->time == '' ) {
- $ThisSession->expire_time->Modify();
- if ( $ThisSession->expire_time->time == '' ) {
- $ThisSession->expire_time->GetLocalTime();
- }
- }
- $sql_util = new SqlUtil();
- $insert_session = $this->session_db->PrepareSql( '
- INSERT INTO
- sessions_table ( md5, ip, magic_string, grant_time, expire_time )
- VALUES ( ' .
- $sql_util->Quote( $ThisSession->session_id ) . ', ' .
- $sql_util->Quote( $ThisSession->remote_addr ) . ', ' .
- $sql_util->Quote(
- AddSlashes( $ThisSession->magic_string )
- ) . ', ' .
- $sql_util->Quote(
- AddSlashes( serialize( $ThisSession->grant_time ) )
- ) . ', ' .
- $sql_util->Quote(
- AddSlashes( serialize( $ThisSession->expire_time ) )
- ) .
- ' )'
- );
- list( $ret_val, $reason ) = $insert_session->Exec();
- if ( ! $ret_val ) {
- return array( false, 'Insert to table failed.');
- } else {
- $sess_id = $insert_session->GetInsertId();
- $ThisSession->db_id = $sess_id;
- for(
- $i = 0;
- $i < count( $ThisSession->session_elems );
- $i++ )
- {
- $obj_addr = $ThisSession->session_elems[ $i ];
- $var_name = $obj_addr->variable_name;
- $var_value = AddSlashes( $obj_addr->StoreValue() ) ;
- $var_type = $obj_addr->variable_type;
- $insert_sess_data = $this->session_db->PrepareSql( '
- INSERT INTO
- sessions_data_table ( session_id, variable_name, variable_type, variable_data )
- VALUES ( ' .
- $sql_util->Quote( $sess_id ) . ', ' .
- $sql_util->Quote( $var_name ) . ', ' .
- $sql_util->Quote( $var_type ) . ', ' .
- $sql_util->Quote( $var_value ) .
- ' )'
- );
- $insert_sess_data->Exec();
- }
- return array( true, $ThisSession );
- }
- } /* END Add() */
- Function Delete( $session_db_id ) {
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- $delete_data =
- $this->session_db->PrepareSql(
- 'DELETE FROM sessions_data_table WHERE session_id = ' . $session_db_id
- );
- list( $ret_val, $reason ) = $delete_data->Exec();
- if ( ! $ret_val ) {
- return array( false, 'Failed to delete session_data : ' . $session_db_id );
- } else {
- $delete_session =
- $this->session_db->PrepareSql(
- 'DELETE FROM sessions_table WHERE session_id = ' . $session_db_id
- );
- list( $ret_val, $reason ) = $delete_session->Exec();
- if ( ! $ret_val ) {
- return array( false, 'Failed to delete session : ' . $session_db_id );
- } else {
- return array( true, $session_db_id );
- }
- }
- } /* END Delete */
- Function Get( $TargetSessionId, $ThawOnGet = 1 ) {
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- /* Build up the query string */
- $query_string = '';
- $query_string .= '
- SELECT
- session_id, ip, magic_string, grant_time, expire_time
- FROM
- sessions_table
- WHERE
- md5 = "' . $TargetSessionId . '";';
- $get_session_info = $this->session_db->PrepareSql( $query_string );
- if ( ! (
- list( $session_id, $ip, $magic_string, $grant_time, $expire_time )
- = $get_session_info->FetchRow()
- ) ) {
- return array( false, 'NO Match found.' );
- }
- $query_string = '';
- $query_string .= '
- SELECT
- variable_name, variable_type, variable_data
- FROM
- sessions_data_table
- WHERE
- session_id = ' . $session_id . ';' ;
- $get_session_info_2 = $this->session_db->PrepareSql( $query_string );
- $ResultObject = new Session();
- $ResultObject->db_id = $session_id;
- $ResultObject->session_id = $TargetSessionId;
- $ResultObject->magic_string = $magic_string;
- $ResultObject->grant_time->Copy( unserialize( $grant_time ) );
- $ResultObject->expire_time->Copy( unserialize( $expire_time ) );
- /* We have the expire and grant is the session still good? */
- $ResultObject->grant_time->Modify();
- $ResultObject->expire_time->Modify();
- if (
- $ResultObject->grant_time->time ==
- $ResultObject->expire_time->time ) {
- /* NO expiration */
- $this->debug->Message( 'Expire and grant are the same' );
- } else {
- $ResultObject->grant_time->GetLocalTime();
- $ResultObject->grant_time->FormatTime();
- }
- if (
- $ResultObject->grant_time->time
- > $ResultObject->expire_time->time ) {
- /* Session Has Expired */
- $this->debug->Message(
- 'SESSION EXPIRED : ' . "n" .
- 'grant time : ' .
- $ResultObject->grant_time->time . "n" .
- 'expire time : ' .
- $ResultObject->expire_time->time . "n"
- );
- $this->Delete( $ResultObject->db_id );
- return array(false, 'Session Expired' );
- } else {
- /*
- echo( 'SESSION NOT : ' . $ResultObject->grant_time->time . ' - ' . $ResultObject->expire_time->time . '<br>' );
- */
- }
- if ( $ThawOnGet == 1 ) {
- while(
- ( list( $var_name, $var_type, $var_data ) = $get_session_info_2->FetchRow() )
- ) {
- $session_elem = new SessionElement( $var_name, $var_type);
- $session_elem->Thaw( $var_data );
- $ResultObject->Add( $session_elem );
- }
- }
- return array( true, $ResultObject );
- } /* END Get() */
- Function Modify( $ThisSession ) {
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- $sql_util = new SqlUtil();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- $query_string = '';
- $query_string =
- '
- UPDATE
- sessions_table
- SET
- ip = "' . $ThisSession->remote_addr .'", ' .
- ' magic_string = "' . AddSlashes( $ThisSession->magic_string ) . '", ' .
- ' grant_time = "' . AddSlashes( Serialize( $ThisSession->grant_time ) ) . '", ' .
- ' expire_time = "' . AddSlashes( Serialize( $ThisSession->expire_time ) ) . '" ' .
- '
- WHERE
- md5 = "' . $ThisSession->session_id . '"'
- ;
- $mod_session_info = $this->session_db->PrepareSql( $query_string );
- $mod_session_info->Exec();
- /* Why do we do a delete? */
- /* This is so that a user can add / remove fields from a session at will */
- $query_string = '';
- $query_string =
- 'DELETE FROM sessions_data_table WHERE session_id = ' . $sql_util->Quote( $ThisSession->db_id )
- ;
- $remove_session_data = $this->session_db->PrepareSql( $query_string );
- $remove_session_data->Exec();
- for(
- $i = 0;
- $i < count( $ThisSession->session_elems );
- $i++ )
- {
- $obj_addr = $ThisSession->session_elems[ $i ];
- $var_name = $obj_addr->variable_name;
- $var_value = AddSlashes( $obj_addr->StoreValue() ) ;
- $var_type = $obj_addr->variable_type;
- $sql_util = new SqlUtil();
- $update_sess_data = $this->session_db->PrepareSql( '
- INSERT INTO
- sessions_data_table ( session_id, variable_name, variable_type, variable_data )
- VALUES ( ' .
- $sql_util->Quote( $ThisSession->db_id ) . ', ' .
- $sql_util->Quote( $var_name ) . ', ' .
- $sql_util->Quote( $var_type ) . ', ' .
- $sql_util->Quote( $var_value ) .
- ' )'
- );
- $update_sess_data->Exec();
- }
- return array( true );
- } /* END Modify */
- }
- ?>