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 if (!class_exists('Set')) {
00035 require LIBS . 'set.php';
00036 }
00037 if (!class_exists('Security')) {
00038 require LIBS . 'security.php';
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 class CakeSession extends Object {
00050
00051
00052
00053
00054
00055
00056 var $valid = false;
00057
00058
00059
00060
00061
00062
00063 var $error = false;
00064
00065
00066
00067
00068
00069
00070 var $_userAgent = '';
00071
00072
00073
00074
00075
00076
00077 var $path = '/';
00078
00079
00080
00081
00082
00083
00084 var $lastError = null;
00085
00086
00087
00088
00089
00090
00091 var $security = null;
00092
00093
00094
00095
00096
00097
00098 var $time = false;
00099
00100
00101
00102
00103
00104
00105 var $sessionTime = false;
00106
00107
00108
00109
00110
00111
00112 var $watchKeys = array();
00113
00114
00115
00116
00117
00118
00119 var $id = null;
00120
00121
00122
00123
00124
00125
00126
00127 function __construct($base = null, $start = true) {
00128 if (Configure::read('Session.save') === 'database' && !class_exists('ConnectionManager')) {
00129 App::import('Core', 'ConnectionManager');
00130 }
00131
00132 if (Configure::read('Session.checkAgent') === true || Configure::read('Session.checkAgent') === null) {
00133 if (env('HTTP_USER_AGENT') != null) {
00134 $this->_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt'));
00135 }
00136 }
00137 $this->time = time();
00138
00139 if ($start === true) {
00140 if (!empty($base)) {
00141 $this->path = $base;
00142 if (strpos($base, 'index.php') !== false) {
00143 $this->path = str_replace('index.php', '', $base);
00144 }
00145 if (strpos($base, '?') !== false) {
00146 $this->path = str_replace('?', '', $base);
00147 }
00148 }
00149 $this->host = env('HTTP_HOST');
00150
00151 if (strpos($this->host, ':') !== false) {
00152 $this->host = substr($this->host, 0, strpos($this->host, ':'));
00153 }
00154 if (!class_exists('Security')) {
00155 App::import('Core', 'Security');
00156 }
00157 $this->sessionTime = $this->time + (Security::inactiveMins() * Configure::read('Session.timeout'));
00158 $this->security = Configure::read('Security.level');
00159 }
00160 parent::__construct();
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 function start() {
00170 if (function_exists('session_write_close')) {
00171 session_write_close();
00172 }
00173 $this->__initSession();
00174 return $this->__startSession();
00175 }
00176
00177
00178
00179
00180
00181
00182 function started() {
00183 if (isset($_SESSION)) {
00184 return true;
00185 }
00186 return false;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 function check($name) {
00196 $var = $this->__validateKeys($name);
00197 if (empty($var)) {
00198 return false;
00199 }
00200 $result = Set::extract($_SESSION, $var);
00201 return isset($result);
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 function id($id = null) {
00211 if ($id) {
00212 $this->id = $id;
00213 session_id($this->id);
00214 }
00215 if (isset($_SESSION)) {
00216 return session_id();
00217 } else {
00218 return $this->id;
00219 }
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 function del($name) {
00229 if ($this->check($name)) {
00230 if ($var = $this->__validateKeys($name)) {
00231 if (in_array($var, $this->watchKeys)) {
00232 trigger_error('Deleting session key {' . $var . '}', E_USER_NOTICE);
00233 }
00234 $this->__overwrite($_SESSION, Set::remove($_SESSION, $var));
00235 return ($this->check($var) == false);
00236 }
00237 }
00238 $this->__setError(2, "$name doesn't exist");
00239 return false;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 function __overwrite(&$old, $new) {
00249 if (!empty($old)) {
00250 foreach ($old as $key => $var) {
00251 if (!isset($new[$key])) {
00252 unset($old[$key]);
00253 }
00254 }
00255 }
00256 foreach ($new as $key => $var) {
00257 $old[$key] = $var;
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266
00267 function __error($errorNumber) {
00268 if (!is_array($this->error) || !array_key_exists($errorNumber, $this->error)) {
00269 return false;
00270 } else {
00271 return $this->error[$errorNumber];
00272 }
00273 }
00274
00275
00276
00277
00278
00279
00280 function error() {
00281 if ($this->lastError) {
00282 return $this->__error($this->lastError);
00283 } else {
00284 return false;
00285 }
00286 }
00287
00288
00289
00290
00291
00292
00293 function valid() {
00294 if ($this->read('Config')) {
00295 if ((Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read('Config.userAgent')) && $this->time <= $this->read('Config.time')) {
00296 if ($this->error === false) {
00297 $this->valid = true;
00298 }
00299 } else {
00300 $this->valid = false;
00301 $this->__setError(1, 'Session Highjacking Attempted !!!');
00302 }
00303 }
00304 return $this->valid;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313 function read($name = null) {
00314 if (is_null($name)) {
00315 return $this->__returnSessionVars();
00316 }
00317 if (empty($name)) {
00318 return false;
00319 }
00320 $result = Set::extract($_SESSION, $name);
00321
00322 if (!is_null($result)) {
00323 return $result;
00324 }
00325 $this->__setError(2, "$name doesn't exist");
00326 return null;
00327 }
00328
00329
00330
00331
00332
00333
00334 function __returnSessionVars() {
00335 if (!empty($_SESSION)) {
00336 return $_SESSION;
00337 }
00338 $this->__setError(2, "No Session vars set");
00339 return false;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348 function watch($var) {
00349 $var = $this->__validateKeys($var);
00350 if (empty($var)) {
00351 return false;
00352 }
00353 if (!in_array($var, $this->watchKeys, true)) {
00354 $this->watchKeys[] = $var;
00355 }
00356 }
00357
00358
00359
00360
00361
00362
00363
00364 function ignore($var) {
00365 $var = $this->__validateKeys($var);
00366 if (!in_array($var, $this->watchKeys)) {
00367 return;
00368 }
00369 foreach ($this->watchKeys as $i => $key) {
00370 if ($key == $var) {
00371 unset($this->watchKeys[$i]);
00372 $this->watchKeys = array_values($this->watchKeys);
00373 return;
00374 }
00375 }
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385 function write($name, $value) {
00386 $var = $this->__validateKeys($name);
00387
00388 if (empty($var)) {
00389 return false;
00390 }
00391 if (in_array($var, $this->watchKeys)) {
00392 trigger_error('Writing session key {' . $var . '}: ' . Debugger::exportVar($value), E_USER_NOTICE);
00393 }
00394 $this->__overwrite($_SESSION, Set::insert($_SESSION, $var, $value));
00395 return (Set::extract($_SESSION, $var) === $value);
00396 }
00397
00398
00399
00400
00401
00402
00403 function destroy() {
00404 $_SESSION = array();
00405 $this->__construct($this->path);
00406 $this->start();
00407 $this->renew();
00408 $this->_checkValid();
00409 }
00410
00411
00412
00413
00414
00415 function __initSession() {
00416 $iniSet = function_exists('ini_set');
00417
00418 if ($iniSet && env('HTTPS')) {
00419 ini_set('session.cookie_secure', 1);
00420 }
00421
00422 switch ($this->security) {
00423 case 'high':
00424 $this->cookieLifeTime = 0;
00425 if ($iniSet) {
00426 ini_set('session.referer_check', $this->host);
00427 }
00428 break;
00429 case 'medium':
00430 $this->cookieLifeTime = 7 * 86400;
00431 if ($iniSet) {
00432 ini_set('session.referer_check', $this->host);
00433 }
00434 break;
00435 case 'low':
00436 default:
00437 $this->cookieLifeTime = 788940000;
00438 break;
00439 }
00440
00441 switch (Configure::read('Session.save')) {
00442 case 'cake':
00443 if (empty($_SESSION)) {
00444 if ($iniSet) {
00445 ini_set('session.use_trans_sid', 0);
00446 ini_set('url_rewriter.tags', '');
00447 ini_set('session.serialize_handler', 'php');
00448 ini_set('session.use_cookies', 1);
00449 ini_set('session.name', Configure::read('Session.cookie'));
00450 ini_set('session.cookie_lifetime', $this->cookieLifeTime);
00451 ini_set('session.cookie_path', $this->path);
00452 ini_set('session.auto_start', 0);
00453 ini_set('session.save_path', TMP . 'sessions');
00454 }
00455 }
00456 break;
00457 case 'database':
00458 if (empty($_SESSION)) {
00459 if (Configure::read('Session.table') === null) {
00460 trigger_error(__("You must set the all Configure::write('Session.*') in core.php to use database storage"), E_USER_WARNING);
00461 exit();
00462 } elseif (Configure::read('Session.database') === null) {
00463 Configure::write('Session.database', 'default');
00464 }
00465 if ($iniSet) {
00466 ini_set('session.use_trans_sid', 0);
00467 ini_set('url_rewriter.tags', '');
00468 ini_set('session.save_handler', 'user');
00469 ini_set('session.serialize_handler', 'php');
00470 ini_set('session.use_cookies', 1);
00471 ini_set('session.name', Configure::read('Session.cookie'));
00472 ini_set('session.cookie_lifetime', $this->cookieLifeTime);
00473 ini_set('session.cookie_path', $this->path);
00474 ini_set('session.auto_start', 0);
00475 }
00476 }
00477 session_set_save_handler(array('CakeSession','__open'),
00478 array('CakeSession', '__close'),
00479 array('CakeSession', '__read'),
00480 array('CakeSession', '__write'),
00481 array('CakeSession', '__destroy'),
00482 array('CakeSession', '__gc'));
00483 break;
00484 case 'php':
00485 if (empty($_SESSION)) {
00486 if ($iniSet) {
00487 ini_set('session.use_trans_sid', 0);
00488 ini_set('session.name', Configure::read('Session.cookie'));
00489 ini_set('session.cookie_lifetime', $this->cookieLifeTime);
00490 ini_set('session.cookie_path', $this->path);
00491 }
00492 }
00493 break;
00494 case 'cache':
00495 if (empty($_SESSION)) {
00496 if (!class_exists('Cache')) {
00497 uses('Cache');
00498 }
00499 if ($iniSet) {
00500 ini_set('session.use_trans_sid', 0);
00501 ini_set('url_rewriter.tags', '');
00502 ini_set('session.save_handler', 'user');
00503 ini_set('session.use_cookies', 1);
00504 ini_set('session.name', Configure::read('Session.cookie'));
00505 ini_set('session.cookie_lifetime', $this->cookieLifeTime);
00506 ini_set('session.cookie_path', $this->path);
00507 }
00508 }
00509 session_set_save_handler(array('CakeSession','__open'),
00510 array('CakeSession', '__close'),
00511 array('Cache', 'read'),
00512 array('Cache', 'write'),
00513 array('Cache', 'delete'),
00514 array('Cache', 'gc'));
00515 break;
00516 default:
00517 if (empty($_SESSION)) {
00518 $config = CONFIGS . Configure::read('Session.save') . '.php';
00519
00520 if (is_file($config)) {
00521 require_once ($config);
00522 }
00523 }
00524 break;
00525 }
00526 }
00527
00528
00529
00530
00531
00532 function __startSession() {
00533 if (headers_sent()) {
00534 if (empty($_SESSION)) {
00535 $_SESSION = array();
00536 }
00537 return false;
00538 } elseif (!isset($_SESSION)) {
00539 session_cache_limiter ("must-revalidate");
00540 session_start();
00541 header ('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
00542 return true;
00543 } else {
00544 session_start();
00545 return true;
00546 }
00547 }
00548
00549
00550
00551
00552
00553
00554 function _checkValid() {
00555 if ($this->read('Config')) {
00556 if ((Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read('Config.userAgent')) && $this->time <= $this->read('Config.time')) {
00557 $time = $this->read('Config.time');
00558 $this->write('Config.time', $this->sessionTime);
00559
00560 if (Configure::read('Security.level') === 'high') {
00561 $check = $this->read('Config.timeout');
00562 $check = $check - 1;
00563 $this->write('Config.timeout', $check);
00564
00565 if (time() > ($time - (Security::inactiveMins() * Configure::read('Session.timeout')) + 2) || $check < 1) {
00566 $this->renew();
00567 $this->write('Config.timeout', 10);
00568 }
00569 }
00570 $this->valid = true;
00571 } else {
00572 $this->destroy();
00573 $this->valid = false;
00574 $this->__setError(1, 'Session Highjacking Attempted !!!');
00575 }
00576 } else {
00577 $this->write('Config.userAgent', $this->_userAgent);
00578 $this->write('Config.time', $this->sessionTime);
00579 $this->write('Config.timeout', 10);
00580 $this->valid = true;
00581 $this->__setError(1, 'Session is valid');
00582 }
00583 }
00584
00585
00586
00587
00588
00589
00590 function __regenerateId() {
00591 $oldSessionId = session_id();
00592 if ($oldSessionId) {
00593 $sessionpath = session_save_path();
00594 if (empty($sessionpath)) {
00595 $sessionpath = "/tmp";
00596 }
00597 if (session_id() != "" || isset($_COOKIE[session_name()])) {
00598 setcookie(Configure::read('Session.cookie'), '', time() - 42000, $this->path);
00599 }
00600 session_regenerate_id(true);
00601 if (PHP_VERSION < 5.1) {
00602 $newSessid = session_id();
00603
00604 if (function_exists('session_write_close')) {
00605 session_write_close();
00606 }
00607 $this->__initSession();
00608 session_id($oldSessionId);
00609 session_start();
00610 session_destroy();
00611 $file = $sessionpath . DS . "sess_$oldSessionId";
00612 @unlink($file);
00613 $this->__initSession();
00614 session_id($newSessid);
00615 session_start();
00616 }
00617 }
00618 }
00619
00620
00621
00622
00623
00624 function renew() {
00625 $this->__regenerateId();
00626 }
00627
00628
00629
00630
00631
00632
00633
00634
00635 function __validateKeys($name) {
00636 if (is_string($name) && preg_match("/^[ 0-9a-zA-Z._-]*$/", $name)) {
00637 return $name;
00638 }
00639 $this->__setError(3, "$name is not a string");
00640 return false;
00641 }
00642
00643
00644
00645
00646
00647
00648
00649
00650 function __setError($errorNumber, $errorMessage) {
00651 if ($this->error === false) {
00652 $this->error = array();
00653 }
00654 $this->error[$errorNumber] = $errorMessage;
00655 $this->lastError = $errorNumber;
00656 }
00657
00658
00659
00660
00661
00662
00663 function __open() {
00664 return true;
00665 }
00666
00667
00668
00669
00670
00671
00672 function __close() {
00673 $probability = mt_rand(1, 150);
00674 if ($probability <= 3) {
00675 switch (Configure::read('Session.save')) {
00676 case 'cache':
00677 Cache::gc();
00678 break;
00679 default:
00680 CakeSession::__gc();
00681 break;
00682 }
00683 }
00684 return true;
00685 }
00686
00687
00688
00689
00690
00691
00692
00693 function __read($key) {
00694 $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
00695 $table = $db->fullTableName(Configure::read('Session.table'), false);
00696 $row = $db->query("SELECT " . $db->name($table.'.data') . " FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $db->value($key), false);
00697
00698 if ($row && !isset($row[0][$table]) && isset($row[0][0])) {
00699 $table = 0;
00700 }
00701
00702 if ($row && $row[0][$table]['data']) {
00703 return $row[0][$table]['data'];
00704 } else {
00705 return false;
00706 }
00707 }
00708
00709
00710
00711
00712
00713
00714
00715
00716 function __write($key, $value) {
00717 $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
00718 $table = $db->fullTableName(Configure::read('Session.table'));
00719
00720 switch (Configure::read('Security.level')) {
00721 case 'high':
00722 $factor = 10;
00723 break;
00724 case 'medium':
00725 $factor = 100;
00726 break;
00727 case 'low':
00728 $factor = 300;
00729 break;
00730 default:
00731 $factor = 10;
00732 break;
00733 }
00734 $expires = time() + Configure::read('Session.timeout') * $factor;
00735 $row = $db->query("SELECT COUNT(id) AS count FROM " . $db->name($table) . " WHERE "
00736 . $db->name('id') . " = "
00737 . $db->value($key), false);
00738
00739 if ($row[0][0]['count'] > 0) {
00740 $db->execute("UPDATE " . $db->name($table) . " SET " . $db->name('data') . " = "
00741 . $db->value($value) . ", " . $db->name('expires') . " = "
00742 . $db->value($expires) . " WHERE " . $db->name('id') . " = "
00743 . $db->value($key));
00744 } else {
00745 $db->execute("INSERT INTO " . $db->name($table) . " (" . $db->name('data') . ","
00746 . $db->name('expires') . "," . $db->name('id')
00747 . ") VALUES (" . $db->value($value) . ", " . $db->value($expires) . ", "
00748 . $db->value($key) . ")");
00749 }
00750 return true;
00751 }
00752
00753
00754
00755
00756
00757
00758
00759 function __destroy($key) {
00760 $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
00761 $table = $db->fullTableName(Configure::read('Session.table'));
00762 $db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $db->value($key));
00763 return true;
00764 }
00765
00766
00767
00768
00769
00770
00771
00772 function __gc($expires = null) {
00773 $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
00774 $table = $db->fullTableName(Configure::read('Session.table'));
00775 $db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.expires') . " < ". $db->value(time()));
00776 return true;
00777 }
00778 }
00779 ?>