source: trunk/phpgwapi/inc/adodb/session/adodb-session2.php @ 7673

Revision 7673, 21.0 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

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