Documentation TYPO3 par Ameos

adodb-cryptsession.php

00001 <?php
00002 /*
00003 V4.80 8 Mar 2006  (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved.
00004   Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence.
00007         Made table name configurable - by David Johnson djohnson@inpro.net
00008         Encryption by Ari Kuorikoski <ari.kuorikoski@finebyte.com>
00009         
00010   Set tabs to 4 for best viewing.
00011   
00012   Latest version of ADODB is available at http://php.weblogs.com/adodb
00013   ======================================================================
00014   
00015  This file provides PHP4 session management using the ADODB database
00016 wrapper library.
00017  
00018  Example
00019  =======
00020  
00021         include('adodb.inc.php');
00022         #---------------------------------#
00023         include('adodb-cryptsession.php'); 
00024         #---------------------------------#
00025         session_start();
00026         session_register('AVAR');
00027         $_SESSION['AVAR'] += 1;
00028         print "
00029 -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
00030 
00031  
00032  Installation
00033  ============
00034  1. Create a new database in MySQL or Access "sessions" like
00035 so:
00036  
00037   create table sessions (
00038            SESSKEY char(32) not null,
00039            EXPIRY int(11) unsigned not null,
00040            EXPIREREF varchar(64),
00041            DATA CLOB,
00042           primary key (sesskey)
00043   );
00044   
00045   2. Then define the following parameters. You can either modify
00046      this file, or define them before this file is included:
00047          
00048         $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
00049         $ADODB_SESSION_CONNECT='server to connect to';
00050         $ADODB_SESSION_USER ='user';
00051         $ADODB_SESSION_PWD ='password';
00052         $ADODB_SESSION_DB ='database';
00053         $ADODB_SESSION_TBL = 'sessions'
00054         
00055   3. Recommended is PHP 4.0.2 or later. There are documented
00056 session bugs in earlier versions of PHP.
00057 
00058 */
00059 
00060 
00061 include_once('crypt.inc.php');
00062 
00063 if (!defined('_ADODB_LAYER')) {
00064         include (dirname(__FILE__).'/adodb.inc.php');
00065 }
00066 
00067  /* if database time and system time is difference is greater than this, then give warning */
00068  define('ADODB_SESSION_SYNCH_SECS',60); 
00069 
00070 if (!defined('ADODB_SESSION')) {
00071 
00072  define('ADODB_SESSION',1);
00073  
00074 GLOBAL  $ADODB_SESSION_CONNECT, 
00075         $ADODB_SESSION_DRIVER,
00076         $ADODB_SESSION_USER,
00077         $ADODB_SESSION_PWD,
00078         $ADODB_SESSION_DB,
00079         $ADODB_SESS_CONN,
00080         $ADODB_SESS_LIFE,
00081         $ADODB_SESS_DEBUG,
00082         $ADODB_SESS_INSERT,
00083         $ADODB_SESSION_EXPIRE_NOTIFY,
00084         $ADODB_SESSION_TBL; 
00085 
00086         //$ADODB_SESS_DEBUG = true;
00087         
00088         /* SET THE FOLLOWING PARAMETERS */
00089 if (empty($ADODB_SESSION_DRIVER)) {
00090         $ADODB_SESSION_DRIVER='mysql';
00091         $ADODB_SESSION_CONNECT='localhost';
00092         $ADODB_SESSION_USER ='root';
00093         $ADODB_SESSION_PWD ='';
00094         $ADODB_SESSION_DB ='xphplens_2';
00095 }
00096 
00097 if (empty($ADODB_SESSION_TBL)){
00098         $ADODB_SESSION_TBL = 'sessions';
00099 }
00100 
00101 if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
00102         $ADODB_SESSION_EXPIRE_NOTIFY = false;
00103 }
00104 
00105 function ADODB_Session_Key() 
00106 {
00107 $ADODB_CRYPT_KEY = 'CRYPTED ADODB SESSIONS ROCK!';
00108 
00109         /* USE THIS FUNCTION TO CREATE THE ENCRYPTION KEY FOR CRYPTED SESSIONS  */
00110         /* Crypt the used key, $ADODB_CRYPT_KEY as key and session_ID as SALT   */
00111         return crypt($ADODB_CRYPT_KEY, session_ID());
00112 }
00113 
00114 $ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime');
00115 if ($ADODB_SESS_LIFE <= 1) {
00116         // bug in PHP 4.0.3 pl 1  -- how about other versions?
00117         //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $ADODB_SESS_LIFE</h3>";
00118         $ADODB_SESS_LIFE=1440;
00119 }
00120 
00121 function adodb_sess_open($save_path, $session_name) 
00122 {
00123 GLOBAL  $ADODB_SESSION_CONNECT, 
00124         $ADODB_SESSION_DRIVER,
00125         $ADODB_SESSION_USER,
00126         $ADODB_SESSION_PWD,
00127         $ADODB_SESSION_DB,
00128         $ADODB_SESS_CONN,
00129         $ADODB_SESS_DEBUG;
00130         
00131         $ADODB_SESS_INSERT = false;
00132         
00133         if (isset($ADODB_SESS_CONN)) return true;
00134         
00135         $ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
00136         if (!empty($ADODB_SESS_DEBUG)) {
00137                 $ADODB_SESS_CONN->debug = true;
00138                 print" conn=$ADODB_SESSION_CONNECT user=$ADODB_SESSION_USER pwd=$ADODB_SESSION_PWD db=$ADODB_SESSION_DB ";
00139         }
00140         return $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT,
00141                         $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
00142         
00143 }
00144 
00145 function adodb_sess_close() 
00146 {
00147 global $ADODB_SESS_CONN;
00148 
00149         if ($ADODB_SESS_CONN) $ADODB_SESS_CONN->Close();
00150         return true;
00151 }
00152 
00153 function adodb_sess_read($key) 
00154 {
00155 $Crypt = new MD5Crypt;
00156 global $ADODB_SESS_CONN,$ADODB_SESS_INSERT,$ADODB_SESSION_TBL;
00157         $rs = $ADODB_SESS_CONN->Execute("SELECT data FROM $ADODB_SESSION_TBL WHERE sesskey = '$key' AND expiry >= " . time());
00158         if ($rs) {
00159                 if ($rs->EOF) {
00160                         $ADODB_SESS_INSERT = true;
00161                         $v = '';
00162                 } else {
00163                         // Decrypt session data
00164                         $v = rawurldecode($Crypt->Decrypt(reset($rs->fields), ADODB_Session_Key()));
00165                 }
00166                 $rs->Close();
00167                 return $v;
00168         }
00169         else $ADODB_SESS_INSERT = true;
00170         
00171         return '';
00172 }
00173 
00174 function adodb_sess_write($key, $val) 
00175 {
00176 $Crypt = new MD5Crypt;
00177         global $ADODB_SESS_INSERT,$ADODB_SESS_CONN, $ADODB_SESS_LIFE, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
00178 
00179         $expiry = time() + $ADODB_SESS_LIFE;
00180 
00181         // encrypt session data..       
00182         $val = $Crypt->Encrypt(rawurlencode($val), ADODB_Session_Key());
00183         
00184         $arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
00185         if ($ADODB_SESSION_EXPIRE_NOTIFY) {
00186                 $var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
00187                 global $$var;
00188                 $arr['expireref'] = $$var;
00189         }
00190         $rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,
00191             $arr,
00192         'sesskey',$autoQuote = true);
00193 
00194         if (!$rs) {
00195                 ADOConnection::outp( '
00196 -- Session Replace: '.$ADODB_SESS_CONN->ErrorMsg().'</p>',false);
00197         } else {
00198                 // bug in access driver (could be odbc?) means that info is not commited
00199                 // properly unless select statement executed in Win2000
00200         
00201         if ($ADODB_SESS_CONN->databaseType == 'access') $rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
00202         }
00203         return isset($rs);
00204 }
00205 
00206 function adodb_sess_destroy($key) 
00207 {
00208         global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
00209         
00210         if ($ADODB_SESSION_EXPIRE_NOTIFY) {
00211                 reset($ADODB_SESSION_EXPIRE_NOTIFY);
00212                 $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
00213                 $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM);
00214                 $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
00215                 $ADODB_SESS_CONN->SetFetchMode($savem);
00216                 if ($rs) {
00217                         $ADODB_SESS_CONN->BeginTrans();
00218                         while (!$rs->EOF) {
00219                                 $ref = $rs->fields[0];
00220                                 $key = $rs->fields[1];
00221                                 $fn($ref,$key);
00222                                 $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
00223                                 $rs->MoveNext();
00224                         }
00225                         $ADODB_SESS_CONN->CommitTrans();
00226                 }
00227         } else {
00228                 $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE sesskey = '$key'";
00229                 $rs = $ADODB_SESS_CONN->Execute($qry);
00230         }
00231         return $rs ? true : false;
00232 }
00233 
00234 
00235 function adodb_sess_gc($maxlifetime) {
00236         global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY,$ADODB_SESS_DEBUG;
00237 
00238         if ($ADODB_SESSION_EXPIRE_NOTIFY) {
00239                 reset($ADODB_SESSION_EXPIRE_NOTIFY);
00240                 $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
00241                 $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM);
00242                 $t = time();
00243                 $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE expiry < $t");
00244                 $ADODB_SESS_CONN->SetFetchMode($savem);
00245                 if ($rs) {
00246                         $ADODB_SESS_CONN->BeginTrans();
00247                         while (!$rs->EOF) {
00248                                 $ref = $rs->fields[0];
00249                                 $key = $rs->fields[1];
00250                                 $fn($ref,$key);
00251                                 //$del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
00252                                 $rs->MoveNext();
00253                         }
00254                         $rs->Close();
00255                         
00256                         $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE expiry < $t");
00257                         $ADODB_SESS_CONN->CommitTrans();
00258                 }
00259         } else {
00260                 $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time();
00261                 $ADODB_SESS_CONN->Execute($qry);
00262         }
00263         
00264         // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
00265         if (defined('ADODB_SESSION_OPTIMIZE'))
00266         {
00267         global $ADODB_SESSION_DRIVER;
00268         
00269                 switch( $ADODB_SESSION_DRIVER ) {
00270                         case 'mysql':
00271                         case 'mysqlt':
00272                                 $opt_qry = 'OPTIMIZE TABLE '.$ADODB_SESSION_TBL;
00273                                 break;
00274                         case 'postgresql':
00275                         case 'postgresql7':
00276                                 $opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;        
00277                                 break;
00278                 }
00279         }
00280         
00281         if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select  TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL;
00282         else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL;
00283         
00284         $rs =& $ADODB_SESS_CONN->SelectLimit($sql,1);
00285         if ($rs && !$rs->EOF) {
00286         
00287                 $dbts = reset($rs->fields);
00288                 $rs->Close();
00289                 $dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts);
00290                 $t = time();
00291                 if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) {
00292                         $msg = 
00293                         __FILE__.": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)";
00294                         error_log($msg);
00295                         if ($ADODB_SESS_DEBUG) ADOConnection::outp("
00296 -- $msg</p>");
00297                 }
00298         }
00299         
00300         return true;
00301 }
00302 
00303 session_module_name('user'); 
00304 session_set_save_handler(
00305         "adodb_sess_open",
00306         "adodb_sess_close",
00307         "adodb_sess_read",
00308         "adodb_sess_write",
00309         "adodb_sess_destroy",
00310         "adodb_sess_gc");
00311 }
00312 
00313 /*  TEST SCRIPT -- UNCOMMENT */
00314 /*
00315 if (0) {
00316 
00317         session_start();
00318         session_register('AVAR');
00319         $_SESSION['AVAR'] += 1;
00320         print "
00321 -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
00322 }
00323 */
00324 ?>


Généré par Les experts TYPO3 avec  doxygen 1.4.6