source: trunk/phpgwapi/inc/adodb/session/old/adodb-session.php @ 2

Revision 2, 12.5 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2/*
3V4.51 29 July 2004  (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
4  Released under both BSD license and Lesser GPL library license.
5  Whenever there is any discrepancy between the two licenses,
6  the BSD license will take precedence.
7          Set tabs to 4 for best viewing.
8 
9  Latest version of ADODB is available at http://php.weblogs.com/adodb
10  ======================================================================
11 
12 This file provides PHP4 session management using the ADODB database
13wrapper library.
14 
15 Example
16 =======
17 
18        GLOBAL $HTTP_SESSION_VARS;
19        include('adodb.inc.php');
20        include('adodb-session.php');
21        session_start();
22        session_register('AVAR');
23        $HTTP_SESSION_VARS['AVAR'] += 1;
24        print "<p>\$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>";
25       
26To force non-persistent connections, call adodb_session_open first before session_start():
27
28        GLOBAL $HTTP_SESSION_VARS;
29        include('adodb.inc.php');
30        include('adodb-session.php');
31        adodb_sess_open(false,false,false);
32        session_start();
33        session_register('AVAR');
34        $HTTP_SESSION_VARS['AVAR'] += 1;
35        print "<p>\$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>";
36
37 
38 Installation
39 ============
40 1. Create this table in your database (syntax might vary depending on your db):
41 
42  create table sessions (
43           SESSKEY char(32) not null,
44           EXPIRY int(11) unsigned not null,
45           EXPIREREF varchar(64),
46           DATA text not null,
47          primary key (sesskey)
48  );
49 
50  For oracle:
51    create table sessions (
52           SESSKEY char(32) not null,
53           EXPIRY DECIMAL(16)  not null,
54           EXPIREREF varchar(64),
55           DATA varchar(4000) not null,
56          primary key (sesskey)
57  );
58
59
60  2. Then define the following parameters. You can either modify
61     this file, or define them before this file is included:
62         
63        $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
64        $ADODB_SESSION_CONNECT='server to connect to';
65        $ADODB_SESSION_USER ='user';
66        $ADODB_SESSION_PWD ='password';
67        $ADODB_SESSION_DB ='database';
68        $ADODB_SESSION_TBL = 'sessions'
69       
70  3. Recommended is PHP 4.0.6 or later. There are documented
71         session bugs in earlier versions of PHP.
72
73  4. If you want to receive notifications when a session expires, then
74         you can tag a session with an EXPIREREF, and before the session
75         record is deleted, we can call a function that will pass the EXPIREREF
76         as the first parameter, and the session key as the second parameter.
77         
78         To do this, define a notification function, say NotifyFn:
79         
80                function NotifyFn($expireref, $sesskey)
81                {
82                }
83         
84         Then you need to define a global variable $ADODB_SESSION_EXPIRE_NOTIFY.
85         This is an array with 2 elements, the first being the name of the variable
86         you would like to store in the EXPIREREF field, and the 2nd is the
87         notification function's name.
88         
89         In this example, we want to be notified when a user's session
90         has expired, so we store the user id in the global variable $USERID,
91         store this value in the EXPIREREF field:
92         
93                $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
94               
95        Then when the NotifyFn is called, we are passed the $USERID as the first
96        parameter, eg. NotifyFn($userid, $sesskey).
97*/
98
99if (!defined('_ADODB_LAYER')) {
100        include (dirname(__FILE__).'/adodb.inc.php');
101}
102
103if (!defined('ADODB_SESSION')) {
104
105 define('ADODB_SESSION',1);
106 
107 /* if database time and system time is difference is greater than this, then give warning */
108 define('ADODB_SESSION_SYNCH_SECS',60);
109
110/****************************************************************************************\
111        Global definitions
112\****************************************************************************************/
113GLOBAL  $ADODB_SESSION_CONNECT,
114        $ADODB_SESSION_DRIVER,
115        $ADODB_SESSION_USER,
116        $ADODB_SESSION_PWD,
117        $ADODB_SESSION_DB,
118        $ADODB_SESS_CONN,
119        $ADODB_SESS_LIFE,
120        $ADODB_SESS_DEBUG,
121        $ADODB_SESSION_EXPIRE_NOTIFY,
122        $ADODB_SESSION_CRC,
123        $ADODB_SESSION_TBL;
124       
125       
126        $ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime');
127        if ($ADODB_SESS_LIFE <= 1) {
128         // bug in PHP 4.0.3 pl 1  -- how about other versions?
129         //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $ADODB_SESS_LIFE</h3>";
130                $ADODB_SESS_LIFE=1440;
131        }
132        $ADODB_SESSION_CRC = false;
133        //$ADODB_SESS_DEBUG = true;
134       
135        //////////////////////////////////
136        /* SET THE FOLLOWING PARAMETERS */
137        //////////////////////////////////
138       
139        if (empty($ADODB_SESSION_DRIVER)) {
140                $ADODB_SESSION_DRIVER='mysql';
141                $ADODB_SESSION_CONNECT='localhost';
142                $ADODB_SESSION_USER ='root';
143                $ADODB_SESSION_PWD ='';
144                $ADODB_SESSION_DB ='xphplens_2';
145        }
146       
147        if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
148                $ADODB_SESSION_EXPIRE_NOTIFY = false;
149        }
150        //  Made table name configurable - by David Johnson djohnson@inpro.net
151        if (empty($ADODB_SESSION_TBL)){
152                $ADODB_SESSION_TBL = 'sessions';
153        }
154       
155        /*
156        $ADODB_SESS['driver'] = $ADODB_SESSION_DRIVER;
157        $ADODB_SESS['connect'] = $ADODB_SESSION_CONNECT;
158        $ADODB_SESS['user'] = $ADODB_SESSION_USER;
159        $ADODB_SESS['pwd'] = $ADODB_SESSION_PWD;
160        $ADODB_SESS['db'] = $ADODB_SESSION_DB;
161        $ADODB_SESS['life'] = $ADODB_SESS_LIFE;
162        $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
163       
164        $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
165        $ADODB_SESS['table'] = $ADODB_SESS_TBL;
166        */
167       
168/****************************************************************************************\
169        Create the connection to the database.
170       
171        If $ADODB_SESS_CONN already exists, reuse that connection
172\****************************************************************************************/
173function adodb_sess_open($save_path, $session_name,$persist=true)
174{
175GLOBAL $ADODB_SESS_CONN;
176        if (isset($ADODB_SESS_CONN)) return true;
177       
178GLOBAL  $ADODB_SESSION_CONNECT,
179        $ADODB_SESSION_DRIVER,
180        $ADODB_SESSION_USER,
181        $ADODB_SESSION_PWD,
182        $ADODB_SESSION_DB,
183        $ADODB_SESS_DEBUG;
184       
185        // cannot use & below - do not know why...
186        $ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
187        if (!empty($ADODB_SESS_DEBUG)) {
188                $ADODB_SESS_CONN->debug = true;
189                ADOConnection::outp( " conn=$ADODB_SESSION_CONNECT user=$ADODB_SESSION_USER pwd=$ADODB_SESSION_PWD db=$ADODB_SESSION_DB ");
190        }
191        if ($persist) $ok = $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT,
192                        $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
193        else $ok = $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT,
194                        $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
195       
196        if (!$ok) ADOConnection::outp( "<p>Session: connection failed</p>",false);
197}
198
199/****************************************************************************************\
200        Close the connection
201\****************************************************************************************/
202function adodb_sess_close()
203{
204global $ADODB_SESS_CONN;
205
206        if ($ADODB_SESS_CONN) $ADODB_SESS_CONN->Close();
207        return true;
208}
209
210/****************************************************************************************\
211        Slurp in the session variables and return the serialized string
212\****************************************************************************************/
213function adodb_sess_read($key)
214{
215global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
216
217        $rs = $ADODB_SESS_CONN->Execute("SELECT data FROM $ADODB_SESSION_TBL WHERE sesskey = '$key' AND expiry >= " . time());
218        if ($rs) {
219                if ($rs->EOF) {
220                        $v = '';
221                } else
222                        $v = rawurldecode(reset($rs->fields));
223                       
224                $rs->Close();
225               
226                // new optimization adodb 2.1
227                $ADODB_SESSION_CRC = strlen($v).crc32($v);
228               
229                return $v;
230        }
231       
232        return ''; // thx to Jorma Tuomainen, webmaster#wizactive.com
233}
234
235/****************************************************************************************\
236        Write the serialized data to a database.
237       
238        If the data has not been modified since adodb_sess_read(), we do not write.
239\****************************************************************************************/
240function adodb_sess_write($key, $val)
241{
242        global
243                $ADODB_SESS_CONN,
244                $ADODB_SESS_LIFE,
245                $ADODB_SESSION_TBL,
246                $ADODB_SESS_DEBUG,
247                $ADODB_SESSION_CRC,
248                $ADODB_SESSION_EXPIRE_NOTIFY;
249
250        $expiry = time() + $ADODB_SESS_LIFE;
251       
252        // crc32 optimization since adodb 2.1
253        // now we only update expiry date, thx to sebastian thom in adodb 2.32
254        if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) {
255                if ($ADODB_SESS_DEBUG) echo "<p>Session: Only updating date - crc32 not changed</p>";
256                $qry = "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " . time();
257                $rs = $ADODB_SESS_CONN->Execute($qry); 
258                return true;
259        }
260        $val = rawurlencode($val);
261       
262        $arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
263        if ($ADODB_SESSION_EXPIRE_NOTIFY) {
264                $var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
265                global $$var;
266                $arr['expireref'] = $$var;
267        }
268        $rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,$arr,
269        'sesskey',$autoQuote = true);
270       
271        if (!$rs) {
272                ADOConnection::outp( '<p>Session Replace: '.$ADODB_SESS_CONN->ErrorMsg().'</p>',false);
273        }  else {
274                // bug in access driver (could be odbc?) means that info is not commited
275                // properly unless select statement executed in Win2000
276                if ($ADODB_SESS_CONN->databaseType == 'access')
277                        $rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
278        }
279        return !empty($rs);
280}
281
282function adodb_sess_destroy($key)
283{
284        global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
285       
286        if ($ADODB_SESSION_EXPIRE_NOTIFY) {
287                reset($ADODB_SESSION_EXPIRE_NOTIFY);
288                $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
289                $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM);
290                $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
291                $ADODB_SESS_CONN->SetFetchMode($savem);
292                if ($rs) {
293                        $ADODB_SESS_CONN->BeginTrans();
294                        while (!$rs->EOF) {
295                                $ref = $rs->fields[0];
296                                $key = $rs->fields[1];
297                                $fn($ref,$key);
298                                $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
299                                $rs->MoveNext();
300                        }
301                        $ADODB_SESS_CONN->CommitTrans();
302                }
303        } else {
304                $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE sesskey = '$key'";
305                $rs = $ADODB_SESS_CONN->Execute($qry);
306        }
307        return $rs ? true : false;
308}
309
310function adodb_sess_gc($maxlifetime)
311{
312        global $ADODB_SESS_DEBUG, $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
313       
314        if ($ADODB_SESSION_EXPIRE_NOTIFY) {
315                reset($ADODB_SESSION_EXPIRE_NOTIFY);
316                $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
317                $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM);
318                $t = time();
319                $rs =& $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE expiry < $t");
320                $ADODB_SESS_CONN->SetFetchMode($savem);
321                if ($rs) {
322                        $ADODB_SESS_CONN->BeginTrans();
323                        while (!$rs->EOF) {
324                                $ref = $rs->fields[0];
325                                $key = $rs->fields[1];
326                                $fn($ref,$key);
327                                $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
328                                $rs->MoveNext();
329                        }
330                        $rs->Close();
331                       
332                        $ADODB_SESS_CONN->CommitTrans();
333                       
334                }
335        } else {
336                $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time();
337                $ADODB_SESS_CONN->Execute($qry);
338       
339                if ($ADODB_SESS_DEBUG) ADOConnection::outp("<p><b>Garbage Collection</b>: $qry</p>");
340        }
341        // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
342        if (defined('ADODB_SESSION_OPTIMIZE')) {
343        global $ADODB_SESSION_DRIVER;
344       
345                switch( $ADODB_SESSION_DRIVER ) {
346                        case 'mysql':
347                        case 'mysqlt':
348                                $opt_qry = 'OPTIMIZE TABLE '.$ADODB_SESSION_TBL;
349                                break;
350                        case 'postgresql':
351                        case 'postgresql7':
352                                $opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;       
353                                break;
354                }
355                if (!empty($opt_qry)) {
356                        $ADODB_SESS_CONN->Execute($opt_qry);
357                }
358        }
359        if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select  TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL;
360        else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL;
361       
362        $rs =& $ADODB_SESS_CONN->SelectLimit($sql,1);
363        if ($rs && !$rs->EOF) {
364       
365                $dbts = reset($rs->fields);
366                $rs->Close();
367                $dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts);
368                $t = time();
369       
370                if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) {
371                global $HTTP_SERVER_VARS;
372                        $msg =
373                        __FILE__.": Server time for webserver {$HTTP_SERVER_VARS['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)";
374                        error_log($msg);
375                        if ($ADODB_SESS_DEBUG) ADOConnection::outp("<p>$msg</p>");
376                }
377        }
378       
379        return true;
380}
381
382session_module_name('user');
383session_set_save_handler(
384        "adodb_sess_open",
385        "adodb_sess_close",
386        "adodb_sess_read",
387        "adodb_sess_write",
388        "adodb_sess_destroy",
389        "adodb_sess_gc");
390}
391
392/*  TEST SCRIPT -- UNCOMMENT */
393
394if (0) {
395GLOBAL $HTTP_SESSION_VARS;
396
397        session_start();
398        session_register('AVAR');
399        $HTTP_SESSION_VARS['AVAR'] += 1;
400        ADOConnection::outp( "<p>\$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>",false);
401}
402
403?>
Note: See TracBrowser for help on using the repository browser.