[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/adodb/session/ -> adodb-session2.php (source)

   1  <?php
   2  
   3  
   4  /*
   5  V5.11 5 May 2010   (c) 2000-2010 John Lim (jlim#natsoft.com). All rights reserved.
   6           Contributed by Ross Smith (adodb@netebb.com). 
   7    Released under both BSD license and Lesser GPL library license.
   8    Whenever there is any discrepancy between the two licenses,
   9    the BSD license will take precedence.
  10        Set tabs to 4 for best viewing.
  11        
  12  
  13  */
  14  
  15  /*
  16  
  17  CREATE Table SCripts
  18  
  19  Oracle
  20  ======
  21  
  22  CREATE TABLE SESSIONS2
  23  (
  24    SESSKEY    VARCHAR2(48 BYTE)                  NOT NULL,
  25    EXPIRY     DATE                               NOT NULL,
  26    EXPIREREF  VARCHAR2(200 BYTE),
  27    CREATED    DATE                               NOT NULL,
  28    MODIFIED   DATE                               NOT NULL,
  29    SESSDATA   CLOB,
  30    PRIMARY KEY(SESSKEY)
  31  );
  32  
  33  
  34  CREATE INDEX SESS2_EXPIRY ON SESSIONS2(EXPIRY);
  35  CREATE UNIQUE INDEX SESS2_PK ON SESSIONS2(SESSKEY);
  36  CREATE INDEX SESS2_EXP_REF ON SESSIONS2(EXPIREREF);
  37  
  38  
  39   
  40   MySQL
  41   =====
  42   
  43  CREATE TABLE sessions2(
  44      sesskey VARCHAR( 64 ) NOT NULL DEFAULT '',
  45      expiry TIMESTAMP NOT NULL ,
  46      expireref VARCHAR( 250 ) DEFAULT '',
  47      created TIMESTAMP NOT NULL ,
  48      modified TIMESTAMP NOT NULL ,
  49      sessdata LONGTEXT DEFAULT '',
  50      PRIMARY KEY ( sesskey ) ,
  51      INDEX sess2_expiry( expiry ),
  52      INDEX sess2_expireref( expireref )
  53  )
  54  
  55  
  56  */
  57  
  58  if (!defined('_ADODB_LAYER')) {
  59      require realpath(dirname(__FILE__) . '/../adodb.inc.php');
  60  }
  61  
  62  if (defined('ADODB_SESSION')) return 1;
  63  
  64  define('ADODB_SESSION', dirname(__FILE__));
  65  define('ADODB_SESSION2', ADODB_SESSION);
  66  
  67  /* 
  68      Unserialize session data manually. See http://phplens.com/lens/lensforum/msgs.php?id=9821 
  69      
  70      From Kerr Schere, to unserialize session data stored via ADOdb. 
  71      1. Pull the session data from the db and loop through it. 
  72      2. Inside the loop, you will need to urldecode the data column. 
  73      3. After urldecode, run the serialized string through this function:
  74  
  75  */
  76  function adodb_unserialize( $serialized_string ) 
  77  {
  78      $variables = array( );
  79      $a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
  80      for( $i = 0; $i < count( $a ); $i = $i+2 ) {
  81          $variables[$a[$i]] = unserialize( $a[$i+1] );
  82      }
  83      return( $variables );
  84  }
  85  
  86  /*
  87      Thanks Joe Li. See http://phplens.com/lens/lensforum/msgs.php?id=11487&x=1
  88      Since adodb 4.61.
  89  */
  90  function adodb_session_regenerate_id() 
  91  {
  92      $conn = ADODB_Session::_conn();
  93      if (!$conn) return false;
  94  
  95      $old_id = session_id();
  96      if (function_exists('session_regenerate_id')) {
  97          session_regenerate_id();
  98      } else {
  99          session_id(md5(uniqid(rand(), true)));
 100          $ck = session_get_cookie_params();
 101          setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
 102          //@session_start();
 103      }
 104      $new_id = session_id();
 105      $ok = $conn->Execute('UPDATE '. ADODB_Session::table(). ' SET sesskey='. $conn->qstr($new_id). ' WHERE sesskey='.$conn->qstr($old_id));
 106      
 107      /* it is possible that the update statement fails due to a collision */
 108      if (!$ok) {
 109          session_id($old_id);
 110          if (empty($ck)) $ck = session_get_cookie_params();
 111          setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
 112          return false;
 113      }
 114      
 115      return true;
 116  }
 117  
 118  /*
 119      Generate database table for session data
 120      @see http://phplens.com/lens/lensforum/msgs.php?id=12280
 121      @return 0 if failure, 1 if errors, 2 if successful.
 122      @author Markus Staab http://www.public-4u.de
 123  */
 124  function adodb_session_create_table($schemaFile=null,$conn = null)
 125  {
 126      // set default values
 127      if ($schemaFile===null) $schemaFile = ADODB_SESSION . '/session_schema2.xml';
 128      if ($conn===null) $conn = ADODB_Session::_conn();
 129  
 130      if (!$conn) return 0;
 131  
 132      $schema = new adoSchema($conn);
 133      $schema->ParseSchema($schemaFile);
 134      return $schema->ExecuteSchema();
 135  }
 136  
 137  /*!
 138      \static
 139  */
 140  class ADODB_Session {
 141      /////////////////////
 142      // getter/setter methods
 143      /////////////////////
 144      
 145      /*
 146      
 147      function Lock($lock=null)
 148      {
 149      static $_lock = false;
 150      
 151          if (!is_null($lock)) $_lock = $lock;
 152          return $lock;
 153      }
 154      */
 155      /*!
 156      */
 157  	static function driver($driver = null) 
 158      {
 159          static $_driver = 'mysql';
 160          static $set = false;
 161  
 162          if (!is_null($driver)) {
 163              $_driver = trim($driver);
 164              $set = true;
 165          } elseif (!$set) {
 166              // backwards compatibility
 167              if (isset($GLOBALS['ADODB_SESSION_DRIVER'])) {
 168                  return $GLOBALS['ADODB_SESSION_DRIVER'];
 169              }
 170          }
 171  
 172          return $_driver;
 173      }
 174  
 175      /*!
 176      */
 177  	static function host($host = null) {
 178          static $_host = 'localhost';
 179          static $set = false;
 180  
 181          if (!is_null($host)) {
 182              $_host = trim($host);
 183              $set = true;
 184          } elseif (!$set) {
 185              // backwards compatibility
 186              if (isset($GLOBALS['ADODB_SESSION_CONNECT'])) {
 187                  return $GLOBALS['ADODB_SESSION_CONNECT'];
 188              }
 189          }
 190  
 191          return $_host;
 192      }
 193  
 194      /*!
 195      */
 196  	static function user($user = null) 
 197      {
 198          static $_user = 'root';
 199          static $set = false;
 200  
 201          if (!is_null($user)) {
 202              $_user = trim($user);
 203              $set = true;
 204          } elseif (!$set) {
 205              // backwards compatibility
 206              if (isset($GLOBALS['ADODB_SESSION_USER'])) {
 207                  return $GLOBALS['ADODB_SESSION_USER'];
 208              }
 209          }
 210  
 211          return $_user;
 212      }
 213  
 214      /*!
 215      */
 216  	static function password($password = null) 
 217      {
 218          static $_password = '';
 219          static $set = false;
 220  
 221          if (!is_null($password)) {
 222              $_password = $password;
 223              $set = true;
 224          } elseif (!$set) {
 225              // backwards compatibility
 226              if (isset($GLOBALS['ADODB_SESSION_PWD'])) {
 227                  return $GLOBALS['ADODB_SESSION_PWD'];
 228              }
 229          }
 230  
 231          return $_password;
 232      }
 233  
 234      /*!
 235      */
 236  	static function database($database = null) 
 237      {
 238          static $_database = '';
 239          static $set = false;
 240          
 241          if (!is_null($database)) {
 242              $_database = trim($database);
 243              $set = true;
 244          } elseif (!$set) {
 245              // backwards compatibility
 246              if (isset($GLOBALS['ADODB_SESSION_DB'])) {
 247                  return $GLOBALS['ADODB_SESSION_DB'];
 248              }
 249          }
 250          return $_database;
 251      }
 252  
 253      /*!
 254      */
 255  	static function persist($persist = null) 
 256      {
 257          static $_persist = true;
 258  
 259          if (!is_null($persist)) {
 260              $_persist = trim($persist);
 261          }
 262  
 263          return $_persist;
 264      }
 265  
 266      /*!
 267      */
 268  	static function lifetime($lifetime = null) 
 269      {
 270          static $_lifetime;
 271          static $set = false;
 272  
 273          if (!is_null($lifetime)) {
 274              $_lifetime = (int) $lifetime;
 275              $set = true;
 276          } elseif (!$set) {
 277              // backwards compatibility
 278              if (isset($GLOBALS['ADODB_SESS_LIFE'])) {
 279                  return $GLOBALS['ADODB_SESS_LIFE'];
 280              }
 281          }
 282          if (!$_lifetime) {
 283              $_lifetime = ini_get('session.gc_maxlifetime');
 284              if ($_lifetime <= 1) {
 285                  // bug in PHP 4.0.3 pl 1  -- how about other versions?
 286                  //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $lifetime</h3>";
 287                  $_lifetime = 1440;
 288              }
 289          }
 290  
 291          return $_lifetime;
 292      }
 293  
 294      /*!
 295      */
 296  	static function debug($debug = null) 
 297      {
 298          static $_debug = false;
 299          static $set = false;
 300  
 301          if (!is_null($debug)) {
 302              $_debug = (bool) $debug;
 303  
 304              $conn = ADODB_Session::_conn();
 305              if ($conn) {
 306                  #$conn->debug = $_debug;
 307              }
 308              $set = true;
 309          } elseif (!$set) {
 310              // backwards compatibility
 311              if (isset($GLOBALS['ADODB_SESS_DEBUG'])) {
 312                  return $GLOBALS['ADODB_SESS_DEBUG'];
 313              }
 314          }
 315  
 316          return $_debug;
 317      }
 318  
 319      /*!
 320      */
 321  	static function expireNotify($expire_notify = null) 
 322      {
 323          static $_expire_notify;
 324          static $set = false;
 325  
 326          if (!is_null($expire_notify)) {
 327              $_expire_notify = $expire_notify;
 328              $set = true;
 329          } elseif (!$set) {
 330              // backwards compatibility
 331              if (isset($GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'])) {
 332                  return $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
 333              }
 334          }
 335  
 336          return $_expire_notify;
 337      }
 338  
 339      /*!
 340      */
 341  	static function table($table = null) 
 342      {
 343          static $_table = 'sessions2';
 344          static $set = false;
 345  
 346          if (!is_null($table)) {
 347              $_table = trim($table);
 348              $set = true;
 349          } elseif (!$set) {
 350              // backwards compatibility
 351              if (isset($GLOBALS['ADODB_SESSION_TBL'])) {
 352                  return $GLOBALS['ADODB_SESSION_TBL'];
 353              }
 354          }
 355  
 356          return $_table;
 357      }
 358  
 359      /*!
 360      */
 361  	static function optimize($optimize = null) 
 362      {
 363          static $_optimize = false;
 364          static $set = false;
 365  
 366          if (!is_null($optimize)) {
 367              $_optimize = (bool) $optimize;
 368              $set = true;
 369          } elseif (!$set) {
 370              // backwards compatibility
 371              if (defined('ADODB_SESSION_OPTIMIZE')) {
 372                  return true;
 373              }
 374          }
 375  
 376          return $_optimize;
 377      }
 378  
 379      /*!
 380      */
 381  	static function syncSeconds($sync_seconds = null) {
 382          //echo ("<p>WARNING: ADODB_SESSION::syncSeconds is longer used, please remove this function for your code</p>");
 383          
 384          return 0;
 385      }
 386  
 387      /*!
 388      */
 389  	static function clob($clob = null) {
 390          static $_clob = false;
 391          static $set = false;
 392  
 393          if (!is_null($clob)) {
 394              $_clob = strtolower(trim($clob));
 395              $set = true;
 396          } elseif (!$set) {
 397              // backwards compatibility
 398              if (isset($GLOBALS['ADODB_SESSION_USE_LOBS'])) {
 399                  return $GLOBALS['ADODB_SESSION_USE_LOBS'];
 400              }
 401          }
 402  
 403          return $_clob;
 404      }
 405  
 406      /*!
 407      */
 408  	static function dataFieldName($data_field_name = null) {
 409          //echo ("<p>WARNING: ADODB_SESSION::dataFieldName() is longer used, please remove this function for your code</p>");
 410          return '';
 411      }
 412  
 413      /*!
 414      */
 415  	static function filter($filter = null) {
 416          static $_filter = array();
 417  
 418          if (!is_null($filter)) {
 419              if (!is_array($filter)) {
 420                  $filter = array($filter);
 421              }
 422              $_filter = $filter;
 423          }
 424  
 425          return $_filter;
 426      }
 427  
 428      /*!
 429      */
 430  	static function encryptionKey($encryption_key = null) {
 431          static $_encryption_key = 'CRYPTED ADODB SESSIONS ROCK!';
 432  
 433          if (!is_null($encryption_key)) {
 434              $_encryption_key = $encryption_key;
 435          }
 436  
 437          return $_encryption_key;
 438      }
 439  
 440      /////////////////////
 441      // private methods
 442      /////////////////////
 443  
 444      /*!
 445      */
 446  	static function _conn($conn=null) {
 447          return isset($GLOBALS['ADODB_SESS_CONN']) ? $GLOBALS['ADODB_SESS_CONN'] : false;
 448      }
 449  
 450      /*!
 451      */
 452  	static function _crc($crc = null) {
 453          static $_crc = false;
 454  
 455          if (!is_null($crc)) {
 456              $_crc = $crc;
 457          }
 458  
 459          return $_crc;
 460      }
 461  
 462      /*!
 463      */
 464  	static function _init() {
 465          session_module_name('user');
 466          session_set_save_handler(
 467              array('ADODB_Session', 'open'),
 468              array('ADODB_Session', 'close'),
 469              array('ADODB_Session', 'read'),
 470              array('ADODB_Session', 'write'),
 471              array('ADODB_Session', 'destroy'),
 472              array('ADODB_Session', 'gc')
 473          );
 474      }
 475  
 476  
 477      /*!
 478      */
 479  	static function _sessionKey() {
 480          // use this function to create the encryption key for crypted sessions
 481          // crypt the used key, ADODB_Session::encryptionKey() as key and session_id() as salt
 482          return crypt(ADODB_Session::encryptionKey(), session_id());
 483      }
 484  
 485      /*!
 486      */
 487  	static function _dumprs(&$rs) {
 488          $conn    = ADODB_Session::_conn();
 489          $debug    = ADODB_Session::debug();
 490  
 491          if (!$conn) {
 492              return;
 493          }
 494  
 495          if (!$debug) {
 496              return;
 497          }
 498  
 499          if (!$rs) {
 500              echo "<br />\$rs is null or false<br />\n";
 501              return;
 502          }
 503  
 504          //echo "<br />\nAffected_Rows=",$conn->Affected_Rows(),"<br />\n";
 505  
 506          if (!is_object($rs)) {
 507              return;
 508          }
 509          $rs = $conn->_rs2rs($rs);
 510          
 511          require_once  ADODB_SESSION.'/../tohtml.inc.php';
 512          rs2html($rs);
 513          $rs->MoveFirst();
 514      }
 515  
 516      /////////////////////
 517      // public methods
 518      /////////////////////
 519      
 520  	static function config($driver, $host, $user, $password, $database=false,$options=false)
 521      {
 522          ADODB_Session::driver($driver);
 523          ADODB_Session::host($host);
 524          ADODB_Session::user($user);
 525          ADODB_Session::password($password);
 526          ADODB_Session::database($database);
 527          
 528          if ($driver == 'oci8' || $driver == 'oci8po') $options['lob'] = 'CLOB';
 529          
 530          if (isset($options['table'])) ADODB_Session::table($options['table']);
 531          if (isset($options['lob'])) ADODB_Session::clob($options['lob']);
 532          if (isset($options['debug'])) ADODB_Session::debug($options['debug']);
 533      }
 534  
 535      /*!
 536          Create the connection to the database.
 537  
 538          If $conn already exists, reuse that connection
 539      */
 540  	static function open($save_path, $session_name, $persist = null) 
 541      {
 542          $conn = ADODB_Session::_conn();
 543  
 544          if ($conn) {
 545              return true;
 546          }
 547  
 548          $database    = ADODB_Session::database();
 549          $debug        = ADODB_Session::debug();
 550          $driver        = ADODB_Session::driver();
 551          $host        = ADODB_Session::host();
 552          $password    = ADODB_Session::password();
 553          $user        = ADODB_Session::user();
 554  
 555          if (!is_null($persist)) {
 556              ADODB_Session::persist($persist);
 557          } else {
 558              $persist = ADODB_Session::persist();
 559          }
 560  
 561  # these can all be defaulted to in php.ini
 562  #        assert('$database');
 563  #        assert('$driver');
 564  #        assert('$host');
 565  
 566          $conn = ADONewConnection($driver);
 567  
 568          if ($debug) {
 569              $conn->debug = true;        
 570              ADOConnection::outp( " driver=$driver user=$user db=$database ");
 571          }
 572          
 573          if (empty($conn->_connectionID)) { // not dsn
 574              if ($persist) {
 575                  switch($persist) {
 576                  default:
 577                  case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break;
 578                  case 'C': $ok = $conn->Connect($host, $user, $password, $database); break;
 579                  case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break;
 580                  }
 581              } else {
 582                  $ok = $conn->Connect($host, $user, $password, $database);
 583              }
 584          }
 585  
 586          if ($ok) $GLOBALS['ADODB_SESS_CONN'] = $conn;
 587          else
 588              ADOConnection::outp('<p>Session: connection failed</p>', false);
 589          
 590  
 591          return $ok;
 592      }
 593  
 594      /*!
 595          Close the connection
 596      */
 597  	static function close() 
 598      {
 599  /*
 600          $conn = ADODB_Session::_conn();
 601          if ($conn) $conn->Close();
 602  */
 603          return true;
 604      }
 605  
 606      /*
 607          Slurp in the session variables and return the serialized string
 608      */
 609  	static function read($key) 
 610      {
 611          $conn    = ADODB_Session::_conn();
 612          $filter    = ADODB_Session::filter();
 613          $table    = ADODB_Session::table();
 614  
 615          if (!$conn) {
 616              return '';
 617          }
 618  
 619          //assert('$table');
 620  
 621          $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
 622      
 623          $sql = "SELECT sessdata FROM $table WHERE sesskey = $binary ".$conn->Param(0)." AND expiry >= " . $conn->sysTimeStamp;
 624          /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if 
 625            developer has commited elsewhere... :(
 626           */
 627          #if (ADODB_Session::Lock())
 628          #    $rs = $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), sessdata);
 629          #else
 630              $rs = $conn->Execute($sql, array($key));
 631          //ADODB_Session::_dumprs($rs);
 632          if ($rs) {
 633              if ($rs->EOF) {
 634                  $v = '';
 635              } else {
 636                  $v = reset($rs->fields);
 637                  $filter = array_reverse($filter);
 638                  foreach ($filter as $f) {
 639                      if (is_object($f)) {
 640                          $v = $f->read($v, ADODB_Session::_sessionKey());
 641                      }
 642                  }
 643                  $v = rawurldecode($v);
 644              }
 645  
 646              $rs->Close();
 647  
 648              ADODB_Session::_crc(strlen($v) . crc32($v));
 649              return $v;
 650          }
 651  
 652          return '';
 653      }
 654  
 655      /*!
 656          Write the serialized data to a database.
 657  
 658          If the data has not been modified since the last read(), we do not write.
 659      */
 660  	static function write($key, $oval) 
 661      {
 662      global $ADODB_SESSION_READONLY;
 663      
 664          if (!empty($ADODB_SESSION_READONLY)) return;
 665          
 666          $clob            = ADODB_Session::clob();
 667          $conn            = ADODB_Session::_conn();
 668          $crc            = ADODB_Session::_crc();
 669          $debug            = ADODB_Session::debug();
 670          $driver            = ADODB_Session::driver();
 671          $expire_notify    = ADODB_Session::expireNotify();
 672          $filter            = ADODB_Session::filter();
 673          $lifetime        = ADODB_Session::lifetime();
 674          $table            = ADODB_Session::table();
 675      
 676          if (!$conn) {
 677              return false;
 678          }
 679          if ($debug) $conn->debug = 1;
 680          $sysTimeStamp = $conn->sysTimeStamp;
 681          
 682          //assert('$table');
 683  
 684          $expiry = $conn->OffsetDate($lifetime/(24*3600),$sysTimeStamp);
 685  
 686          $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
 687  
 688          // crc32 optimization since adodb 2.1
 689          // now we only update expiry date, thx to sebastian thom in adodb 2.32
 690          if ($crc !== false && $crc == (strlen($oval) . crc32($oval))) {
 691              if ($debug) {
 692                  echo '<p>Session: Only updating date - crc32 not changed</p>';
 693              }
 694              
 695              $expirevar = '';
 696              if ($expire_notify) {
 697                  $var = reset($expire_notify);
 698                  global $$var;
 699                  if (isset($$var)) {
 700                      $expirevar = $$var;
 701                  }
 702              }
 703              
 704              
 705              $sql = "UPDATE $table SET expiry = $expiry ,expireref=".$conn->Param('0').", modified = $sysTimeStamp WHERE $binary sesskey = ".$conn->Param('1')." AND expiry >= $sysTimeStamp";
 706              $rs = $conn->Execute($sql,array($expirevar,$key));
 707              return true;
 708          }
 709          $val = rawurlencode($oval);
 710          foreach ($filter as $f) {
 711              if (is_object($f)) {
 712                  $val = $f->write($val, ADODB_Session::_sessionKey());
 713              }
 714          }
 715  
 716          $expireref = '';
 717          if ($expire_notify) {
 718              $var = reset($expire_notify);
 719              global $$var;
 720              if (isset($$var)) {
 721                  $expireref = $$var;
 722              }
 723          } 
 724  
 725          if (!$clob) {    // no lobs, simply use replace()
 726              $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
 727              if ($rs) $rs->Close();
 728                      
 729              if ($rs && reset($rs->fields) > 0) {
 730                  $sql = "UPDATE $table SET expiry=$expiry, sessdata=".$conn->Param(0).", expireref= ".$conn->Param(1).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('2');
 731                  
 732              } else {
 733                  $sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified) 
 734                      VALUES ($expiry,".$conn->Param('0').", ". $conn->Param('1').", ".$conn->Param('2').", $sysTimeStamp, $sysTimeStamp)";
 735              }
 736              
 737      
 738              $rs = $conn->Execute($sql,array($val,$expireref,$key));
 739              
 740          } else {
 741              // what value shall we insert/update for lob row?
 742              switch ($driver) {
 743                  // empty_clob or empty_lob for oracle dbs
 744                  case 'oracle':
 745                  case 'oci8':
 746                  case 'oci8po':
 747                  case 'oci805':
 748                      $lob_value = sprintf('empty_%s()', strtolower($clob));
 749                      break;
 750  
 751                  // null for all other
 752                  default:
 753                      $lob_value = 'null';
 754                      break;
 755              }
 756              
 757              $conn->StartTrans();
 758              
 759              $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
 760                      
 761              if ($rs && reset($rs->fields) > 0) {
 762                  $sql = "UPDATE $table SET expiry=$expiry, sessdata=$lob_value, expireref= ".$conn->Param(0).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('1');
 763                  
 764              } else {
 765                  $sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified) 
 766                      VALUES ($expiry,$lob_value, ". $conn->Param('0').", ".$conn->Param('1').", $sysTimeStamp, $sysTimeStamp)";
 767              }
 768              
 769              $rs = $conn->Execute($sql,array($expireref,$key));
 770              
 771              $qkey = $conn->qstr($key);
 772              $rs2 = $conn->UpdateBlob($table, 'sessdata', $val, " sesskey=$qkey", strtoupper($clob));
 773              if ($debug) echo "<hr>",htmlspecialchars($oval), "<hr>";
 774              $rs = @$conn->CompleteTrans();
 775              
 776              
 777          }
 778  
 779          if (!$rs) {
 780              ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
 781              return false;
 782          }  else {
 783              // bug in access driver (could be odbc?) means that info is not committed
 784              // properly unless select statement executed in Win2000
 785              if ($conn->databaseType == 'access') {
 786                  $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
 787                  $rs = $conn->Execute($sql);
 788                  ADODB_Session::_dumprs($rs);
 789                  if ($rs) {
 790                      $rs->Close();
 791                  }
 792              }
 793          }/*
 794          if (ADODB_Session::Lock()) {
 795              $conn->CommitTrans();
 796          }*/
 797          return $rs ? true : false;
 798      }
 799  
 800      /*!
 801      */
 802  	static function destroy($key) {
 803          $conn            = ADODB_Session::_conn();
 804          $table            = ADODB_Session::table();
 805          $expire_notify    = ADODB_Session::expireNotify();
 806  
 807          if (!$conn) {
 808              return false;
 809          }
 810          $debug            = ADODB_Session::debug();
 811          if ($debug) $conn->debug = 1;
 812          //assert('$table');
 813  
 814          $qkey = $conn->quote($key);
 815          $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
 816  
 817          if ($expire_notify) {
 818              reset($expire_notify);
 819              $fn = next($expire_notify);
 820              $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
 821              $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
 822              $rs = $conn->Execute($sql);
 823              ADODB_Session::_dumprs($rs);
 824              $conn->SetFetchMode($savem);
 825              if (!$rs) {
 826                  return false;
 827              }
 828              if (!$rs->EOF) {
 829                  $ref = $rs->fields[0];
 830                  $key = $rs->fields[1];
 831                  //assert('$ref');
 832                  //assert('$key');
 833                  $fn($ref, $key);
 834              }
 835              $rs->Close();
 836          }
 837  
 838          $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
 839          $rs = $conn->Execute($sql);
 840          if ($rs) {
 841              $rs->Close();
 842          }
 843  
 844          return $rs ? true : false;
 845      }
 846  
 847      /*!
 848      */
 849      static function gc($maxlifetime) 
 850      {
 851          $conn            = ADODB_Session::_conn();
 852          $debug            = ADODB_Session::debug();
 853          $expire_notify    = ADODB_Session::expireNotify();
 854          $optimize        = ADODB_Session::optimize();
 855          $table            = ADODB_Session::table();
 856  
 857          if (!$conn) {
 858              return false;
 859          }
 860  
 861  
 862          $debug            = ADODB_Session::debug();
 863          if ($debug) {
 864              $conn->debug = 1;
 865              $COMMITNUM = 2;
 866          } else {
 867              $COMMITNUM = 20;
 868          }
 869          
 870          //assert('$table');
 871  
 872          $time = $conn->OffsetDate(-$maxlifetime/24/3600,$conn->sysTimeStamp);
 873          $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
 874  
 875          if ($expire_notify) {
 876              reset($expire_notify);
 877              $fn = next($expire_notify);
 878          } else {
 879              $fn = false;
 880          }
 881          
 882          $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
 883          $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time ORDER BY 2"; # add order by to prevent deadlock
 884          $rs = $conn->SelectLimit($sql,1000);
 885          ADODB_Session::_dumprs($rs);
 886          if ($debug) $conn->SetFetchMode($savem);
 887          if ($rs) {
 888              $tr = $conn->hasTransactions;
 889              if ($tr) $conn->BeginTrans();
 890              $keys = array();
 891              $ccnt = 0;
 892              while (!$rs->EOF) {
 893                  $ref = $rs->fields[0];
 894                  $key = $rs->fields[1];
 895                  if ($fn) $fn($ref, $key);
 896                  $del = $conn->Execute("DELETE FROM $table WHERE sesskey=".$conn->Param('0'),array($key));
 897                  $rs->MoveNext();
 898                  $ccnt += 1;
 899                  if ($tr && $ccnt % $COMMITNUM == 0) {
 900                      if ($debug) echo "Commit<br>\n";
 901                      $conn->CommitTrans();
 902                      $conn->BeginTrans();
 903                  }
 904              }
 905              $rs->Close();
 906              
 907              if ($tr) $conn->CommitTrans();
 908          }
 909          
 910  
 911          // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
 912          if ($optimize) {
 913              $driver = ADODB_Session::driver();
 914  
 915              if (preg_match('/mysql/i', $driver)) {
 916                  $sql = "OPTIMIZE TABLE $table";
 917              }
 918              if (preg_match('/postgres/i', $driver)) {
 919                  $sql = "VACUUM $table";
 920              }
 921              if (!empty($sql)) {
 922                  $conn->Execute($sql);
 923              }
 924          }
 925  
 926          
 927          return true;
 928      }
 929  }
 930  
 931  ADODB_Session::_init();
 932  if (empty($ADODB_SESSION_READONLY))
 933      register_shutdown_function('session_write_close');
 934  
 935  // for backwards compatability only
 936  function adodb_sess_open($save_path, $session_name, $persist = true) {
 937      return ADODB_Session::open($save_path, $session_name, $persist);
 938  }
 939  
 940  // for backwards compatability only
 941  function adodb_sess_gc($t)
 942  {    
 943      return ADODB_Session::gc($t);
 944  }
 945  
 946  ?>


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7