Mysql_Session_Db.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:10k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. class Mysql_Session_Db extends BaseObject {
  3.    var $debug;
  4.    var $session_db;
  5.    Function Mysql_Session_Db( $db_config = '' ) {
  6.       $this->BaseObject( 'Mysql_Session_Db' );
  7.       $this->session_db             = new Mysql_Db();
  8.       if ( is_object( $db_config ) ) {
  9.          $this->session_db->db = $db_config;
  10.       }
  11.       $this->debug                  = new Debug;
  12.       $this->debug->prefix          = 'Mysql_Db::Session_Db';
  13.       $this->debug->Off();
  14.       $this->session_db->debug->Off();
  15.       } /* END Mysql_User_Db() */
  16.    Function InitDbConnection() {
  17.       /*
  18.         Make sure the database handle is available 
  19.         ( if not try to open it ) 
  20.       */
  21.       $returns = Array();
  22.       $this->session_db->debug->debug = $this->debug->debug;
  23.       if ( $this->session_db->connection_init == false) {
  24.          $returns = $this->session_db->CreateConnection();
  25.       }
  26.       /* Oh no we failed to open the connection */
  27.       if ( $this->session_db->connection_init == false ) {
  28.          return array( false, 'Database not connected', $returns );
  29.       }
  30.       return array( true );
  31.    }
  32.    Function ListAll() {
  33.       /* Init the database connection and bubble up errors */
  34.       $this->debug->Message( 'Init connection' );
  35.       list( $ret_val, $reason ) = $this->InitDbConnection();
  36.       if ( $ret_val == false ) {
  37.       $this->debug->Message( 'connect failed' );
  38.          return array( $ret_val, $reason );
  39.       }
  40.       /* There is no need for this right now - JEO */
  41.       return array( false);
  42.    } /* END List All */
  43.    Function Add( $ThisSession ) {
  44.       /* Sanity check on input */
  45.       /* Make sure the database handle is available */
  46.       /* Init the database connection and bubble up errors */
  47.       $this->debug->Message( 'Init connection' );
  48.       list( $ret_val, $reason ) = $this->InitDbConnection();
  49.       if ( $ret_val == false ) {
  50.       $this->debug->Message( 'connect failed' );
  51.          return array( $ret_val, $reason );
  52.       }
  53.       if ( $ThisSession->grant_time->time == '' ) {
  54.          $ThisSession->grant_time->Modify();
  55.          if ( $ThisSession->grant_time->time == '' ) {
  56.             $ThisSession->grant_time->GetLocalTime();
  57.          }
  58.       }
  59.       if ( $ThisSession->expire_time->time == '' ) {
  60.          $ThisSession->expire_time->Modify();
  61.          if ( $ThisSession->expire_time->time == '' ) {
  62.             $ThisSession->expire_time->GetLocalTime();
  63.          }
  64.       }
  65.       $sql_util = new SqlUtil();
  66.       $insert_session = $this->session_db->PrepareSql( '
  67. INSERT INTO
  68.    sessions_table ( md5, ip, magic_string, grant_time, expire_time )
  69. VALUES ( ' .
  70. $sql_util->Quote( $ThisSession->session_id ) . ', ' .
  71. $sql_util->Quote( $ThisSession->remote_addr ) . ', ' .
  72. $sql_util->Quote( 
  73.    AddSlashes( $ThisSession->magic_string )
  74. ) . ', ' .
  75. $sql_util->Quote(
  76.    AddSlashes( serialize( $ThisSession->grant_time ) )
  77. ) . ', ' .
  78. $sql_util->Quote( 
  79.    AddSlashes( serialize( $ThisSession->expire_time ) )
  80. ) .
  81. ' )'
  82. );
  83.       list( $ret_val, $reason ) = $insert_session->Exec();
  84.       if ( ! $ret_val ) {
  85.          return array( false, 'Insert to table failed.');
  86.       } else {
  87.          $sess_id = $insert_session->GetInsertId();
  88.          $ThisSession->db_id = $sess_id;
  89.          for( 
  90.             $i = 0; 
  91.             $i < count( $ThisSession->session_elems );
  92.             $i++ ) 
  93.          {
  94.             $obj_addr = $ThisSession->session_elems[ $i ];
  95.             $var_name  = $obj_addr->variable_name;
  96.             $var_value = AddSlashes( $obj_addr->StoreValue() ) ;
  97.             $var_type  = $obj_addr->variable_type;
  98.             $insert_sess_data = $this->session_db->PrepareSql( '
  99. INSERT INTO
  100.    sessions_data_table ( session_id, variable_name, variable_type, variable_data )
  101. VALUES ( ' .
  102. $sql_util->Quote( $sess_id ) . ', ' . 
  103. $sql_util->Quote( $var_name ) . ', ' .
  104. $sql_util->Quote( $var_type ) . ', ' .
  105. $sql_util->Quote( $var_value ) .
  106. ' )' 
  107. );
  108.          $insert_sess_data->Exec();
  109.          }
  110.          return array( true, $ThisSession );
  111.       }
  112.       } /* END Add() */
  113.    Function Delete( $session_db_id ) {
  114.       /* Make sure the database handle is available */
  115.       /* Init the database connection and bubble up errors */
  116.       $this->debug->Message( 'Init connection' );
  117.       list( $ret_val, $reason ) = $this->InitDbConnection();
  118.       if ( $ret_val == false ) {
  119.       $this->debug->Message( 'connect failed' );
  120.          return array( $ret_val, $reason );
  121.       }
  122.       $delete_data =
  123.          $this->session_db->PrepareSql(
  124.  'DELETE FROM sessions_data_table WHERE session_id = ' . $session_db_id
  125.       );
  126.       list( $ret_val, $reason ) = $delete_data->Exec();
  127.       if ( ! $ret_val ) {
  128.          return array( false, 'Failed to delete session_data : ' . $session_db_id );
  129.       } else {
  130.          $delete_session =
  131.             $this->session_db->PrepareSql(
  132.  'DELETE FROM sessions_table WHERE session_id = ' . $session_db_id
  133.          );
  134.          list( $ret_val, $reason ) = $delete_session->Exec();
  135.          if ( ! $ret_val ) {
  136.          return array( false, 'Failed to delete session : ' . $session_db_id );
  137.          } else {
  138.          return array( true, $session_db_id );
  139.          }
  140.       }
  141.       } /* END Delete */
  142.    Function Get( $TargetSessionId, $ThawOnGet = 1 ) {
  143.       /* Make sure the database handle is available */
  144.       /* Init the database connection and bubble up errors */
  145.       $this->debug->Message( 'Init connection' );
  146.       list( $ret_val, $reason ) = $this->InitDbConnection();
  147.       if ( $ret_val == false ) {
  148.       $this->debug->Message( 'connect failed' );
  149.          return array( $ret_val, $reason );
  150.       }
  151.       /* Build up the query string */
  152.       $query_string = '';
  153.       $query_string .= '
  154. SELECT
  155.    session_id, ip, magic_string, grant_time, expire_time
  156. FROM
  157.    sessions_table
  158. WHERE
  159.    md5 = "' . $TargetSessionId . '";';
  160.       $get_session_info = $this->session_db->PrepareSql( $query_string );
  161.       if ( ! ( 
  162.          list( $session_id, $ip, $magic_string, $grant_time, $expire_time ) 
  163.                = $get_session_info->FetchRow() 
  164.       ) ) {
  165.          return array( false, 'NO Match found.' );
  166.       }
  167.       $query_string = '';
  168.       $query_string .= '
  169. SELECT
  170.    variable_name, variable_type, variable_data
  171. FROM
  172.    sessions_data_table
  173. WHERE
  174.    session_id = ' . $session_id . ';' ;
  175.       $get_session_info_2 = $this->session_db->PrepareSql( $query_string );
  176.       $ResultObject                    = new Session();
  177.       $ResultObject->db_id             = $session_id;
  178.       $ResultObject->session_id        = $TargetSessionId;
  179.       $ResultObject->magic_string      = $magic_string;
  180.       $ResultObject->grant_time->Copy(  unserialize( $grant_time ) );
  181.       $ResultObject->expire_time->Copy( unserialize( $expire_time ) );
  182.       /* We have the expire and grant is the session still good? */
  183.       $ResultObject->grant_time->Modify();
  184.       $ResultObject->expire_time->Modify();
  185.       if (
  186.          $ResultObject->grant_time->time == 
  187.             $ResultObject->expire_time->time ) {
  188.          /* NO expiration */
  189.          $this->debug->Message( 'Expire and grant are the same' );
  190.       } else {
  191.       $ResultObject->grant_time->GetLocalTime();
  192.       $ResultObject->grant_time->FormatTime();
  193.       }
  194.       if ( 
  195.          $ResultObject->grant_time->time 
  196.             > $ResultObject->expire_time->time ) {
  197.             /* Session Has Expired */
  198.          $this->debug->Message(
  199.             'SESSION EXPIRED : ' . "n" .
  200.             'grant time      : ' . 
  201.                $ResultObject->grant_time->time . "n" . 
  202.             'expire time     : ' .
  203.                $ResultObject->expire_time->time . "n"
  204.          );
  205.          $this->Delete( $ResultObject->db_id );
  206.          return array(false, 'Session Expired' );
  207.       } else {
  208.          /*
  209.          echo( 'SESSION NOT : ' . $ResultObject->grant_time->time . ' - ' . $ResultObject->expire_time->time . '<br>' );
  210.          */
  211.       }
  212.       if ( $ThawOnGet == 1 ) {
  213.       while( 
  214.          ( list( $var_name, $var_type, $var_data ) = $get_session_info_2->FetchRow() ) 
  215.       ) {
  216.          $session_elem = new SessionElement( $var_name, $var_type);
  217.          $session_elem->Thaw( $var_data );
  218.          $ResultObject->Add( $session_elem );
  219.       }
  220.       }
  221.       return array( true, $ResultObject );
  222.    } /* END Get() */
  223.    Function Modify( $ThisSession ) {
  224.       /* Make sure the database handle is available */
  225.       /* Init the database connection and bubble up errors */
  226.       $this->debug->Message( 'Init connection' );
  227.       list( $ret_val, $reason ) = $this->InitDbConnection();
  228.       $sql_util = new SqlUtil();
  229.       if ( $ret_val == false ) {
  230.       $this->debug->Message( 'connect failed' );
  231.          return array( $ret_val, $reason );
  232.       }
  233.       $query_string = '';
  234.       $query_string = 
  235. '
  236. UPDATE
  237.    sessions_table
  238. SET 
  239.    ip                = "' . $ThisSession->remote_addr .'", ' . 
  240. '  magic_string      = "' . AddSlashes( $ThisSession->magic_string ) . '", ' .
  241. '  grant_time        = "' . AddSlashes( Serialize( $ThisSession->grant_time ) ) . '", ' .
  242. '  expire_time       = "' . AddSlashes( Serialize( $ThisSession->expire_time ) ) . '" ' .
  243. '
  244. WHERE
  245.    md5 = "' . $ThisSession->session_id . '"'
  246. ;
  247.       $mod_session_info = $this->session_db->PrepareSql( $query_string );
  248.       $mod_session_info->Exec();
  249.       /* Why do we do a delete? */
  250.       /* This is so that a user can add / remove fields from a session at will */
  251.       $query_string = '';
  252.       $query_string = 
  253. 'DELETE FROM sessions_data_table WHERE session_id = ' . $sql_util->Quote( $ThisSession->db_id ) 
  254.       ;
  255.       $remove_session_data = $this->session_db->PrepareSql( $query_string );
  256.       $remove_session_data->Exec();
  257.          for( 
  258.             $i = 0; 
  259.             $i < count( $ThisSession->session_elems );
  260.             $i++ ) 
  261.          {
  262.             $obj_addr = $ThisSession->session_elems[ $i ];
  263.             $var_name  = $obj_addr->variable_name;
  264.             $var_value = AddSlashes( $obj_addr->StoreValue() ) ;
  265.             $var_type  = $obj_addr->variable_type;
  266.             $sql_util = new SqlUtil();
  267.             $update_sess_data = $this->session_db->PrepareSql( '
  268. INSERT INTO
  269.    sessions_data_table ( session_id, variable_name, variable_type, variable_data )
  270. VALUES ( ' .
  271. $sql_util->Quote( $ThisSession->db_id ) . ', ' . 
  272. $sql_util->Quote( $var_name ) . ', ' .
  273. $sql_util->Quote( $var_type ) . ', ' .
  274. $sql_util->Quote( $var_value ) .
  275. ' )' 
  276. );
  277.          $update_sess_data->Exec();
  278.          }
  279.       return array( true );
  280.       } /* END Modify */
  281.    }
  282. ?>