00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 function eZSessionOpen( )
00085 {
00086
00087 }
00088
00089 function eZSessionClose( )
00090 {
00091
00092 }
00093
00094 function &eZSessionRead( $key )
00095 {
00096 include_once( 'lib/ezdb/classes/ezdb.php' );
00097 $db =& eZDB::instance();
00098
00099 $key = $db->escapeString( $key );
00100
00101 $sessionRes = $db->arrayQuery( "SELECT data, user_id, expiration_time FROM ezsession WHERE session_key='$key'" );
00102
00103 if ( $sessionRes !== false and count( $sessionRes ) == 1 )
00104 {
00105 $ini =& eZINI::instance();
00106
00107 $sessionUpdatesTime = $sessionRes[0]['expiration_time'] - $ini->variable( 'Session', 'SessionTimeout' );
00108 $sessionIdle = time() - $sessionUpdatesTime;
00109
00110 $data =& $sessionRes[0]['data'];
00111 $GLOBALS['eZSessionUserID'] = $sessionRes[0]['user_id'];
00112 $GLOBALS['eZSessionIdleTime'] = $sessionIdle;
00113
00114
00115
00116
00117
00118
00119 require_once( 'kernel/classes/ezcontentclass.php' );
00120
00121 return $data;
00122 }
00123 else
00124 {
00125 $retVal = false;
00126 return $retVal;
00127 }
00128 }
00129
00130
00131
00132
00133 function eZSessionWrite( $key, $value )
00134 {
00135
00136
00137 if ( isset( $GLOBALS["eZRequestError"] ) && $GLOBALS["eZRequestError"] )
00138 {
00139 return;
00140 }
00141
00142 $db =& eZDB::instance();
00143 $ini =& eZIni::instance();
00144 $expirationTime = time() + $ini->variable( 'Session', 'SessionTimeout' );
00145
00146 if ( $db->bindingType() != EZ_DB_BINDING_NO )
00147 {
00148 $value = $db->bindVariable( $value, array( 'name' => 'data' ) );
00149 }
00150 else
00151 {
00152 $value = '\'' . $db->escapeString( $value ) . '\'';
00153
00154 }
00155
00156 $escKey = $db->escapeString( $key );
00157
00158
00159 $userID = 0;
00160 if ( isset( $GLOBALS['eZSessionUserID'] ) )
00161 $userID = $GLOBALS['eZSessionUserID'];
00162 $userID = $db->escapeString( $userID );
00163
00164 $sessionRes = $db->arrayQuery( "SELECT session_key FROM ezsession WHERE session_key='$escKey'" );
00165
00166 if ( count( $sessionRes ) == 1 )
00167 {
00168 if ( isset( $GLOBALS['eZSessionFunctions']['update_pre'] ) )
00169 {
00170 foreach ( $GLOBALS['eZSessionFunctions']['update_pre'] as $func )
00171 {
00172 $func( $db, $key, $escKey, $expirationTime, $userID, $value );
00173 }
00174 }
00175
00176 $updateQuery = "UPDATE ezsession
00177 SET expiration_time='$expirationTime', data=$value, user_id='$userID'
00178 WHERE session_key='$escKey'";
00179
00180 $ret = $db->query( $updateQuery );
00181
00182 if ( isset( $GLOBALS['eZSessionFunctions']['update_post'] ) )
00183 {
00184 foreach ( $GLOBALS['eZSessionFunctions']['update_post'] as $func )
00185 {
00186 $func( $db, $key, $escKey, $expirationTime, $userID, $value );
00187 }
00188 }
00189 }
00190 else
00191 {
00192 if ( isset( $GLOBALS['eZSessionFunctions']['insert_pre'] ) )
00193 {
00194 foreach ( $GLOBALS['eZSessionFunctions']['insert_pre'] as $func )
00195 {
00196 $func( $db, $key, $escKey, $expirationTime, $userID, $value );
00197 }
00198 }
00199
00200 $insertQuery = "INSERT INTO ezsession
00201 ( session_key, expiration_time, data, user_id )
00202 VALUES ( '$escKey', '$expirationTime', $value, '$userID' )";
00203
00204 $ret = $db->query( $insertQuery );
00205
00206 if ( isset( $GLOBALS['eZSessionFunctions']['insert_post'] ) )
00207 {
00208 foreach ( $GLOBALS['eZSessionFunctions']['insert_post'] as $func )
00209 {
00210 $func( $db, $key, $escKey, $expirationTime, $userID, $value );
00211 }
00212 }
00213 }
00214 }
00215
00216
00217
00218
00219 function eZSessionDestroy( $key )
00220 {
00221 include_once( 'lib/ezdb/classes/ezdb.php' );
00222 $db =& eZDB::instance();
00223
00224 $escKey = $db->escapeString( $key );
00225 if ( isset( $GLOBALS['eZSessionFunctions']['destroy_pre'] ) )
00226 {
00227 foreach ( $GLOBALS['eZSessionFunctions']['destroy_pre'] as $func )
00228 {
00229 $func( $db, $key, $escKey );
00230 }
00231 }
00232
00233 $query = "DELETE FROM ezsession WHERE session_key='$escKey'";
00234 $db->query( $query );
00235
00236 if ( isset( $GLOBALS['eZSessionFunctions']['destroy_post'] ) )
00237 {
00238 foreach ( $GLOBALS['eZSessionFunctions']['destroy_post'] as $func )
00239 {
00240 $func( $db, $key, $escKey );
00241 }
00242 }
00243 }
00244
00245
00246
00247
00248 function eZSessionGarbageCollector()
00249 {
00250 include_once( 'lib/ezdb/classes/ezdb.php' );
00251 $db =& eZDB::instance();
00252 $time = time();
00253
00254 if ( isset( $GLOBALS['eZSessionFunctions']['gc_pre'] ) )
00255 {
00256 foreach ( $GLOBALS['eZSessionFunctions']['gc_pre'] as $func )
00257 {
00258 $func( $db, $time );
00259 }
00260 }
00261
00262 $query = "DELETE FROM ezsession WHERE expiration_time < " . $time;
00263
00264 $db->query( $query );
00265
00266 if ( isset( $GLOBALS['eZSessionFunctions']['gc_post'] ) )
00267 {
00268 foreach ( $GLOBALS['eZSessionFunctions']['gc_post'] as $func )
00269 {
00270 $func( $db, $time );
00271 }
00272 }
00273 }
00274
00275
00276
00277
00278 function eZSessionEmpty()
00279 {
00280 include_once( 'lib/ezdb/classes/ezdb.php' );
00281 $db =& eZDB::instance();
00282
00283 if ( isset( $GLOBALS['eZSessionFunctions']['empty_pre'] ) )
00284 {
00285 foreach ( $GLOBALS['eZSessionFunctions']['empty_pre'] as $func )
00286 {
00287 $func( $db );
00288 }
00289 }
00290
00291 $query = "TRUNCATE TABLE ezsession";
00292
00293 $db->query( $query );
00294
00295 if ( isset( $GLOBALS['eZSessionFunctions']['empty_post'] ) )
00296 {
00297 foreach ( $GLOBALS['eZSessionFunctions']['empty_post'] as $func )
00298 {
00299 $func( $db );
00300 }
00301 }
00302 }
00303
00304
00305
00306
00307 function eZSessionCountActive()
00308 {
00309 include_once( 'lib/ezdb/classes/ezdb.php' );
00310 $db =& eZDB::instance();
00311 $query = "SELECT count( * ) AS count FROM ezsession";
00312
00313 $rows = $db->arrayQuery( $query );
00314 return $rows[0]['count'];
00315 }
00316
00317
00318
00319
00320
00321 function eZRegisterSessionFunctions()
00322 {
00323 session_module_name( 'user' );
00324 $ini =& eZIni::instance();
00325 if ( $ini->variable( 'Session', 'SessionNameHandler' ) == 'custom' )
00326 {
00327 $sessionName = $ini->variable( 'Session', 'SessionNamePrefix' );
00328 if ( $ini->variable( 'Session', 'SessionNamePerSiteAccess' ) == 'enabled' )
00329 {
00330 $access = $GLOBALS['eZCurrentAccess'];
00331 $sessionName .= $access['name'];
00332 }
00333 session_name( $sessionName );
00334 }
00335 session_set_save_handler(
00336 'ezsessionopen',
00337 'ezsessionclose',
00338 'ezsessionread',
00339 'ezsessionwrite',
00340 'ezsessiondestroy',
00341 'ezsessiongarbagecollector' );
00342 }
00343
00344
00345
00346
00347
00348 function eZSessionStart()
00349 {
00350
00351 if ( isset( $GLOBALS['eZSiteBasics'] ) and
00352 isset( $GLOBALS['eZSiteBasics']['session-required'] ) and
00353 !$GLOBALS['eZSiteBasics']['session-required'] )
00354 return false;
00355 $hasStarted =& $GLOBALS['eZSessionIsStarted'];
00356 if ( isset( $hasStarted ) and
00357 $hasStarted )
00358 return false;
00359 include_once( 'lib/ezdb/classes/ezdb.php' );
00360 $db =& eZDB::instance();
00361 if ( !$db->isConnected() )
00362 return false;
00363 eZRegisterSessionFunctions();
00364 $ini =& eZINI::instance();
00365 $cookieTimeout = isset( $GLOBALS['RememberMeTimeout'] ) ? $GLOBALS['RememberMeTimeout'] : $ini->variable( 'Session', 'CookieTimeout' );
00366
00367 if ( is_numeric( $cookieTimeout ) )
00368 {
00369 session_set_cookie_params( (int)$cookieTimeout );
00370 }
00371 session_start();
00372 $hasStarted = true;
00373 return true;
00374 }
00375
00376
00377
00378
00379 function eZSessionStop()
00380 {
00381 $hasStarted =& $GLOBALS['eZSessionIsStarted'];
00382 if ( isset( $hasStarted ) and
00383 !$hasStarted )
00384 return false;
00385 include_once( 'lib/ezdb/classes/ezdb.php' );
00386 $db =& eZDB::instance();
00387 if ( !$db->isConnected() )
00388 return false;
00389 session_write_close();
00390 $hasStarted = false;
00391 return true;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400 function eZSessionRegenerate()
00401 {
00402 $hasStarted =& $GLOBALS['eZSessionIsStarted'];
00403 if ( isset( $hasStarted ) and
00404 !$hasStarted )
00405 return false;
00406 if ( !function_exists( 'session_regenerate_id' ) )
00407 return false;
00408
00409
00410 return true;
00411 }
00412
00413
00414
00415
00416 function eZSessionRemove()
00417 {
00418 $hasStarted =& $GLOBALS['eZSessionIsStarted'];
00419 if ( isset( $hasStarted ) and
00420 !$hasStarted )
00421 return false;
00422 include_once( 'lib/ezdb/classes/ezdb.php' );
00423 $db =& eZDB::instance();
00424 if ( !$db->isConnected() )
00425 return false;
00426 $_SESSION = array();
00427 session_destroy();
00428 $hasStarted = false;
00429 return true;
00430 }
00431
00432
00433
00434
00435
00436
00437
00438 function eZSessionSetUserID( $userID )
00439 {
00440 $GLOBALS['eZSessionUserID'] = $userID;
00441 }
00442
00443
00444
00445
00446
00447
00448 function eZSessionUserID()
00449 {
00450 if ( isset( $GLOBALS['eZSessionUserID'] ) )
00451 return $GLOBALS['eZSessionUserID'];
00452 return 0;
00453 }
00454
00455 ?>