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