Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

kernel/ezsession/classes/ezsession.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezsession.php 9818 2003-05-16 13:15:58Z br $
5 //
6 // Definition of eZSession class
7 //
8 // Created on: <25-Sep-2000 15:21:18 bf>
9 //
10 // This source file is part of Exponential Basic, publishing software.
11 //
12 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
27 //
28
29
32
59 // include_once( "classes/ezdb.php" );
60 // include_once( "classes/ezdatetime.php" );
61 // include_once( "classes/ezhttptool.php" );
62
63 class eZSession
64 {
68 function __construct( $id="", $fetch=true )
69 {
70 $this->IsFetched = false;
71
72 if ( $id != "" )
73 {
74 $this->ID = $id;
75 if ( $fetch == true )
76 {
77 $this->get( $this->ID );
78 }
79 }
80 }
81
85 function store()
86 {
87 $db =& eZDB::globalDatabase();
88 $dbError = false;
89 $db->begin( );
90
91 // lock the table
92 $db->lock( "eZSession_Session" );
93
94 if ( isset( $GLOBALS["eZSessionCookie"] ) && isset( $_COOKIE["eZSessionCookie"] ) && strlen( $GLOBALS["eZSessionCookie"] ) == 32 )
95 {
96 $this->Hash = $_COOKIE["eZSessionCookie"];
97 }
98 else
99 {
100 // set the cookie
101 $this->Hash = md5( microTime() );
102 }
103
104 if ( $GLOBALS["UsePHPSessions"] == true )
105 {
106 session_register( "eZSession" );
107 $GLOBALS["eZSession"] =& $this->Hash;
108 }
109
110 // expire after 40 days
111 setcookie ( "eZSessionCookie", $this->Hash, time()+3456000, "/" )
112 or print( "Error: could not set cookie." );
113
114 // eZHTTPTool::setCookie ( "eZSessionCookie", $this->Hash );
115
116 $remoteIP = $_SERVER["REMOTE_ADDR"];
117
118 // escape hash
119 $hash = $db->escapeString( $this->Hash );
120
121 if ( !isset( $this->ID ) )
122 {
123 $nextID = $db->nextID( "eZSession_Session", "ID" );
124 $timestampObject = new eZDateTime();
125 $timeStamp =& $timestampObject->timeStamp( true );
126
127 $res = $db->query( "INSERT INTO eZSession_Session
128 ( ID, Created, LastAccessed, Hash )
129 VALUES ( '$nextID',
130 '$timeStamp',
131 '$timeStamp',
132 '$hash'
133 )" );
134 if ( $res == false )
135 $dbError = true;
136
137 $this->ID = $nextID;
138 $this->HasRefreshed = true;
139 }
140 else
141 {
142 $dateTime = new eZDateTime( );
143 $timeStamp = $dateTime->timeStamp();
144
145 $db->query( "UPDATE eZSession_Session SET
146 Created=Created,
147 LastAccessed='$timeStamp',
148 Hash='$hash'
149 WHERE ID='$this->ID'
150 " );
151 $this->HasRefreshed = true;
152 }
153
154 $db->unlock();
155
156 if ( $dbError == true )
157 $db->rollback( );
158 else
159 $db->commit();
160
161 $this->setVariable( "SessionIP", $remoteIP );
162
163 return true;
164 }
165
169 function get( $id="" )
170 {
171 if ( is_array( $id ) )
172 {
173 $this->fill( $id );
174 return true;
175 }
176
177 $db =& eZDB::globalDatabase();
178 $ret = false;
179 $session_array = array();
180
181 if ( $id != "" )
182 {
183 if( $db->isA() == "mysql" )
184 {
185 $db->array_query( $session_array, "SELECT * FROM eZSession_Session WHERE ID='$id'" );
186 if ( count( $session_array ) > 1 )
187 {
188 die( "Error: Session's with the same ID was found in the database. This should not happen." );
189 }
190 else if ( count( $session_array ) == 1 )
191 {
192 $this->fill( $session_array[0] );
193 $ret = true;
194 }
195 }
196 elseif( $db->isA() == "sqlite" )
197 {
198 $db->array_query( $session_array, "SELECT * FROM eZSession_Session WHERE ID='$id'" );
199 if ( count( $session_array ) > 1 )
200 {
201 // $db->query( "DELETE FROM eZSession_Session WHERE ID='$id'" );
202 eZDebug::writeError( "Error: Session's with the same ID was found in the database. This should not happen.", __METHOD__ );
203 }
204 else if ( count( $session_array ) == 1 )
205 {
206 $this->fill( $session_array[0] );
207 $ret = true;
208 }
209 }
210
211
212 }
213 return $ret;
214 }
215
219 function fill( $session_array )
220 {
221 $db =& eZDB::globalDatabase();
222
223 $this->ID =& $session_array[$db->fieldName("ID")];
224 $this->Hash =& $session_array[$db->fieldName("Hash")];
225 $this->LastAccessed =& $session_array[$db->fieldName("LastAccessed")];
226 $this->Created =& $session_array[$db->fieldName( "Created") ];
227 }
228
236 function fetch( $refresh = true )
237 {
238 $ret = false;
239
240 if ( $this->IsFetched != true )
241 {
242 $db =& eZDB::globalDatabase();
243 $ret = false;
244
245 // prefer cookie
246 if ( isset( $_COOKIE["eZSessionCookie"] ) )
247 {
248 $hash = $_COOKIE["eZSessionCookie"];
249 }
250 else
251 {
252 if ( $GLOBALS["UsePHPSessions"] == true )
253 {
254 $hash = $GLOBALS["eZSession"];
255 }
256 }
257
258 if ( isset( $hash ) && $hash !== null )
259 {
260 // escape the hash value.
261 $hash = $db->escapeString( $hash );
262 $db->array_query( $session_array, "SELECT * FROM eZSession_Session WHERE Hash='$hash'" );
263
264 if ( count( $session_array ) == 1 )
265 {
266 $ret = $this->get( $session_array[0] );
267
268 if ( $ret == true )
269 {
270 $this->IsFetched = true;
271 }
272
273 if ( $refresh == true )
274 {
275 $this->refresh();
276 }
277 }
278 }
279 }
280 else
281 {
282 if ( $refresh == true )
283 {
284 $this->refresh();
285 }
286 $ret = true;
287 }
288
289 return $ret;
290 }
291
295 function refresh( )
296 {
297 if ( !isset( $this->HasRefreshed ) || !$this->HasRefreshed )
298 {
299 $db =& eZDB::globalDatabase();
300 $db->begin();
301
302 $timestampObject = new eZDateTime();
303 $timeStamp =& $timestampObject->timeStamp( true );
304 // update session
305 $ret = $db->query( "UPDATE eZSession_Session SET
306 LastAccessed='$timeStamp'
307 WHERE ID='$this->ID'" );
308
309 if ( $ret == false )
310 {
311 $db->rollback();
312 }
313 else
314 {
315 $db->commit();
316 $this->HasRefreshed = true;
317 }
318 }
319 }
320
324 function delete()
325 {
326 $db =& eZDB::globalDatabase();
327
328 if ( isset( $this->ID ) )
329 {
330 $db->query( "DELETE FROM eZSession_SessionVariable
331 WHERE SessionID='$this->ID'" );
332
333 $db->query( "DELETE FROM eZSession_Session WHERE ID='$this->ID'" );
334 }
335
336 return true;
337 }
338
342 function id( )
343 {
344 return $this->ID;
345 }
346
350 function hash( )
351 {
352 return $this->Hash;
353 }
354
358 function lastAccessed( )
359 {
360 $dateTime = new eZDateTime();
361 $dateTime->setTimeStamp( $this->LastAccessed );
362
363 return $dateTime;
364 }
365
369 function created( )
370 {
371 $dateTime = new eZDateTime();
372 $dateTime->setTimeStamp( $this->Created );
373
374 return $dateTime;
375 }
376
380 function setHash( $value )
381 {
382 $this->Hash = $value;
383 }
384
390 function variable( $name, $group = false )
391 {
392 if ( isset( $this->StoredVariables[$group][$name] ) )
393 {
394 return $this->StoredVariables[$group][$name];
395 }
396 $ret = false;
397 $db =& eZDB::globalDatabase();
398
399 if ( !is_bool( $group ) )
400 $group_sql = "GroupName='$group'";
401 else
402 $group_sql = "GroupName=''";
403 $db->array_query( $value_array, "SELECT Value FROM eZSession_SessionVariable
404 WHERE SessionID='$this->ID' AND Name='$name'
405 AND $group_sql" );
406
407 if ( count( $value_array ) == 1 )
408 {
409 $ret = trim( $value_array[0][$db->fieldName("Value")] );
410 $this->StoredVariables[$group][$name] = $ret;
411 }
412 return $ret;
413 }
414
419 function getByVariable( $name )
420 {
421 $ret = array();
422 $db =& eZDB::globalDatabase();
423
424 $db->array_query( $value_array, "SELECT eZSession_Session.ID
425 FROM eZSession_Session, eZSession_SessionVariable
426 WHERE eZSession_Session.ID=eZSession_SessionVariable.SessionID
427 AND eZSession_SessionVariable.Name='AuthenticatedUser' ORDER BY eZSession_Session.LastAccessed" );
428
429 foreach ( $value_array as $value )
430 {
431 $ret[] =& $value[$db->fieldName("ID")];
432 }
433
434 return $ret;
435 }
436
440 function idle( )
441 {
442 if ( isset( $this->StoredIdle ) && is_numeric( $this->StoredIdle ) )
443 return $this->StoredIdle;
444
445 $db =& eZDB::globalDatabase();
446
447 $value_array = array();
448
449 $timestampObject = new eZDateTime();
450 $timeStamp =& $timestampObject->timeStamp( true );
451 $db->array_query( $value_array, "SELECT ( $timeStamp - LastAccessed ) AS Idle
452 FROM eZSession_Session WHERE ID='$this->ID'" );
453 $ret = false;
454 if ( count( $value_array ) == 1 )
455 {
456 $ret = $value_array[0][$db->fieldName("Idle")];
457
458 $this->StoredIdle = $ret;
459 }
460
461 return $ret;
462 }
463
471 function cleanup( $maxIdle=48 )
472 {
473 $db =& eZDB::globalDatabase();
474
475 $value_array = array();
476 $timeStamp = (new eZDateTime())->timeStamp( true );
477
478 $db->array_query( $value_array, "SELECT ID, ( $timeStamp - LastAccessed ) AS Idle
479 FROM eZSession_Session
480 HAVING Idle>(60*60*$maxIdle) ORDER BY ID DESC" );
481
482 if( isset( $value_array ) && count( $value_array ) > 0 )
483 {
484 $sid = $value_array[0]["ID"];
485
486 $db->query( "DELETE FROM eZSession_SessionVariable WHERE SessionID<='$sid'" );
487 $db->query( "DELETE FROM eZSession_Session WHERE ID<='$sid'" ); }
488 }
489
493 function setVariable( $name, $value, $group = false )
494 {
495 // print( "setvar: " . (is_bool( $group ) ? ($group ? "true" : "false") : $group ) . "<br>" );
496
497 $db =& eZDB::globalDatabase();
498
499 $dbError = false;
500 $value_array = array();
501 $db->begin( );
502
503 // lock the table
504 $db->lock( "eZSession_SessionVariable" );
505
506 $value = $db->escapeString( $value );
507
508 if ( isset( $this->StoredVariables[$group][$name] ) )
509 {
510 $this->StoredVariables[$group][$name] = $value;
511 }
512
513 if ( !is_bool( $group ) )
514 $group_sql = "GroupName='$group'";
515 else
516 {
517 $group_sql = "GroupName=''";
518 }
519
520 $query = "SELECT ID FROM eZSession_SessionVariable
521 WHERE SessionID='$this->ID' AND Name='$name'
522 AND $group_sql";
523
524 $db->array_query( $value_array, $query );
525
526
527 if ( count( $value_array ) == 1 )
528 {
529 $valueID = $value_array[0][$db->fieldName("ID")];
530 $res = $db->query( "UPDATE eZSession_SessionVariable SET
531 Value='$value' WHERE ID='$valueID'
532 " );
533 }
534 else
535 {
536 if ( is_bool( $group ) )
537 $group_sql = "";
538 else
539 $group_sql = "$group";
540
541 $nextID = $db->nextID( "eZSession_SessionVariable", "ID" );
542 $query = "INSERT INTO eZSession_SessionVariable ( ID, SessionID, Name, Value, GroupName ) VALUES
543 ( '$nextID',
544 '$this->ID',
545 '$name',
546 '$value',
547 '$group_sql' );";
548 $res = $db->query( $query );
549 if ( $res == false )
550 $dbError = true;
551 }
552
553 $db->unlock();
554
555 if ( $dbError == true )
556 $db->rollback( );
557 else
558 $db->commit();
559 }
560
561 function setArray( $name, $array, $append=false )
562 {
563 $string = false;
564 if ( $append )
565 {
566 $string = $this->variable( $name );
567 if ( $string )
568 $string .= ";";
569 }
570
571 $i=0;
572 foreach( $array as $key => $val )
573 {
574 if ( ( !$string ) || ( $i == 0 ) )
575 $string .= $key . "->" . $val;
576 else
577 $string .= ";" . $key . "->" . $val;
578 $i++;
579 }
580 if ( !$string )
581 $string = "";
582
583 $this->setVariable( $name, $string );
584 }
585
586 function arrayValue( $name )
587 {
588 $tmpString = $this->variable( $name );
589 if ( $tmpString )
590 {
591 $tmpString = explode( ";", $tmpString );
592 foreach( $tmpString as $string )
593 {
594 $stringArray = explode( "->", $string );
595 $key = $stringArray[0];
596 $value = $stringArray[1];
597
598 $returnArray[$key] = $value;
599 }
600
601 return $returnArray;
602 }
603 return array();
604 }
605
606
614 static public function &globalSession( $id="", $fetch=true )
615 {
616 $session =& $GLOBALS["eZSessionObject"];
617 if ( !is_a( $session, "eZSession" ) )
618 {
619 $session = new eZSession();
620 if ( !$session->fetch() )
621 {
622 $session->store();
623 }
624 }
625
626 return $session;
627 }
628
635 static public function stop()
636 {
637 // if ( !self::$hasStarted )
638 // {
639 // return false;
640 // }
641 session_write_close();
642 //self::$hasStarted = false;
643 //self::$handlerInstance = null;
644 return true;
645 }
646
647 var $ID;
648 var $Hash;
649 var $Modified;
650 var $Created;
651 var $LastAccessed;
652
653 var $StoredVariables;
654 var $StoredIdle;
655
656 var $HasRefreshed;
657 var $IsFetched;
658 }
659
660 ?>
$i
$i
Definition: admin/adlist.php:91
$group
$group
Definition: admin/articleedit.php:799
$session
$session
Definition: admin/articlelist.php:44
$dateTime
$dateTime
Definition: authorview.php:77
eZDB\globalDatabase
static & globalDatabase()
Definition: ezdb.php:134
eZDateTime
Definition: ezdatetime.php:55
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:546
eZSession
Definition: lib/ezsession/classes/ezsession.php:65
eZSession\$Created
$Created
Definition: kernel/ezsession/classes/ezsession.php:650
eZSession\hash
hash()
Definition: kernel/ezsession/classes/ezsession.php:350
eZSession\$HasRefreshed
$HasRefreshed
Definition: kernel/ezsession/classes/ezsession.php:656
eZSession\arrayValue
arrayValue( $name)
Definition: kernel/ezsession/classes/ezsession.php:586
eZSession\$LastAccessed
$LastAccessed
Definition: kernel/ezsession/classes/ezsession.php:651
eZSession\$ID
$ID
Definition: kernel/ezsession/classes/ezsession.php:647
eZSession\$StoredVariables
$StoredVariables
Definition: kernel/ezsession/classes/ezsession.php:653
eZSession\store
store()
Definition: kernel/ezsession/classes/ezsession.php:85
eZSession\__construct
__construct( $id="", $fetch=true)
Definition: kernel/ezsession/classes/ezsession.php:68
eZSession\fill
fill( $session_array)
Definition: kernel/ezsession/classes/ezsession.php:219
eZSession\$StoredIdle
$StoredIdle
Definition: kernel/ezsession/classes/ezsession.php:654
eZSession\id
id()
Definition: kernel/ezsession/classes/ezsession.php:342
eZSession\setVariable
setVariable( $name, $value, $group=false)
Definition: kernel/ezsession/classes/ezsession.php:493
eZSession\fetch
fetch( $refresh=true)
Definition: kernel/ezsession/classes/ezsession.php:236
eZSession\lastAccessed
lastAccessed()
Definition: kernel/ezsession/classes/ezsession.php:358
eZSession\$Hash
$Hash
Definition: kernel/ezsession/classes/ezsession.php:648
eZSession\setHash
setHash( $value)
Definition: kernel/ezsession/classes/ezsession.php:380
eZSession\variable
variable( $name, $group=false)
Definition: kernel/ezsession/classes/ezsession.php:390
eZSession\globalSession
static & globalSession( $id="", $fetch=true)
Definition: kernel/ezsession/classes/ezsession.php:614
eZSession\idle
idle()
Definition: kernel/ezsession/classes/ezsession.php:440
eZSession\getByVariable
getByVariable( $name)
Definition: kernel/ezsession/classes/ezsession.php:419
eZSession\$IsFetched
$IsFetched
Definition: kernel/ezsession/classes/ezsession.php:657
eZSession\created
created()
Definition: kernel/ezsession/classes/ezsession.php:369
eZSession\setArray
setArray( $name, $array, $append=false)
Definition: kernel/ezsession/classes/ezsession.php:561
eZSession\$Modified
$Modified
Definition: kernel/ezsession/classes/ezsession.php:649
eZSession\cleanup
cleanup( $maxIdle=48)
Definition: kernel/ezsession/classes/ezsession.php:471
eZSession\stop
static stop()
Definition: kernel/ezsession/classes/ezsession.php:635
eZSession\refresh
refresh()
Definition: kernel/ezsession/classes/ezsession.php:295
$name
$name
Definition: companytypelist.php:194
$id
$id
Definition: companytypelist.php:193
$query
if(isset( $Year) &&!is_numeric( $Year)||isset( $Month) &&!is_numeric( $Month)) $query
Definition: entryexitpages.php:65
$db
$db
Definition: ezarticle/user/authorlist.php:53
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
$res
$res
Definition: ezstats/admin/cron.php:45
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.