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

Revision 6065, 13.8 KB checked in by marcosw, 12 years ago (diff)

Ticket #2398 - Compatibilizacao com PHP-5.3 em alguns módulos do expresso

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