ct_sql.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:4k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. ##
  3. ## Copyright (c) 1998,1999 SH Online Dienst GmbH
  4. ##                    Boris Erdmann, Kristian Koehntopp
  5. ##
  6. ## Copyright (c) 1998,1999 Sascha Schumann <sascha@schumann.cx>
  7. ## 
  8. ## $Id: ct_sql.inc,v 1.2 2000/04/11 14:46:24 prenagha Exp $
  9. ##
  10. ## PHPLIB Data Storage Container using a SQL database
  11. ##
  12. class CT_Sql {
  13.   ##
  14.   ## Define these parameters by overwriting or by
  15.   ## deriving your own class from it (recommened)
  16.   ##
  17.     
  18.   var $database_table = "active_sessions";
  19.   var $database_class = "DB_Sql";
  20.   var $database_lock_semaphore = "";
  21.   var $encoding_mode = "base64";
  22.   ## end of configuration
  23.   var $db;
  24.   function ac_start() {
  25.     $name = $this->database_class;
  26.     $this->db = new $name;
  27.   }
  28.   function ac_get_lock() {
  29.     if ( "" != $this->database_lock_semaphore ) {
  30.       $query = sprintf("SELECT get_lock('%s')", $this->database_lock_semaphore);
  31.       while ( ! $this->db->query($query)) {
  32.         $t = 1 + time(); while ( $t > time() ) { ; }
  33.       }
  34.     }
  35.   }
  36.   function ac_release_lock() {
  37.     if ( "" != $this->database_lock_semaphore ) {
  38.       $query = sprintf("SELECT release_lock('%s')", $this->database_lock_semaphore);
  39.       $this->db->query($query);
  40.     }
  41.   }
  42.   function ac_gc($gc_time, $name) {
  43.     $timeout = time();
  44.     $sqldate = date("YmdHis", $timeout - ($gc_time * 60));
  45.     $this->db->query(sprintf("DELETE FROM %s WHERE changed < '%s' AND name = '%s'",
  46.                     $this->database_table, 
  47.                     $sqldate,
  48.                     addslashes($name)));
  49.     }
  50.   function ac_store($id, $name, $str) {
  51.     $ret = true;
  52.     switch ( $this->encoding_mode ) {
  53.       case "slashes":
  54.         $str = addslashes($name . ":" . $str);
  55.       break;
  56.       case "base64":
  57.       default:
  58.         $str = base64_encode($name . ":" . $str);
  59.     };
  60.     $name = addslashes($name);
  61.     ## update duration of visit
  62.     global $HTTP_REFERER, $HTTP_USER_AGENT, $REMOTE_ADDR;
  63.     $now = date("YmdHis", time());
  64.     $uquery = sprintf("update %s set val='%s', changed='%s' where sid='%s' and name='%s'",
  65.       $this->database_table,
  66.       $str,
  67.       $now,
  68.       $id,
  69.       $name);
  70.     $iquery = sprintf("insert into %s ( sid, name, val, changed ) values ('%s', '%s', '%s', '%s')",
  71.       $this->database_table,
  72.       $id,
  73.       $name,
  74.       $str,
  75.       $now);
  76.     $this->db->query($uquery);
  77.     if (  $this->db->affected_rows() == 0 
  78.       && !$this->db->query($iquery)) {
  79.         $ret = false;
  80.     }
  81.     return $ret;
  82.   }
  83.   function ac_delete($id, $name) {
  84.     $this->db->query(sprintf("delete from %s where name = '%s' and sid = '%s'",
  85.       $this->database_table,
  86.       addslashes($name),
  87.       $id));
  88.   }
  89.   function ac_get_value($id, $name) {
  90.     $this->db->query(sprintf("select val from %s where sid  = '%s' and name = '%s'",
  91.       $this->database_table,
  92.       $id,
  93.       addslashes($name)));
  94.     if ($this->db->next_record()) {
  95.       $str  = $this->db->f("val");
  96.       $str2 = base64_decode( $str );
  97.       if ( ereg("^".$name.":.*", $str2) ) {
  98.          $str = ereg_replace("^".$name.":", "", $str2 );
  99.       } else {
  100.         $str3 = stripslashes( $str );
  101.         if ( ereg("^".$name.":.*", $str3) ) {
  102.           $str = ereg_replace("^".$name.":", "", $str3 );
  103.         } else {
  104.           switch ( $this->encoding_mode ) {
  105.             case "slashes":
  106.               $str = stripslashes($str);
  107.             break;
  108.             case "base64":
  109.             default:
  110.               $str = base64_decode($str);
  111.           }
  112.         }
  113.       };
  114.       return $str;
  115.     };
  116.     return "";
  117.   }
  118.   function ac_newid($str, $name) {
  119.     return $str;
  120.   }
  121.   function ac_halt($s) {
  122.     $this->db->halt($s);
  123.   }
  124. }
  125. ?>