1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24:
25: App::uses('Set', 'Utility');
26: App::uses('Security', 'Utility');
27:
28: 29: 30: 31: 32: 33: 34: 35:
36: class CakeSession {
37:
38: 39: 40: 41: 42:
43: public static $valid = false;
44:
45: 46: 47: 48: 49:
50: public static $error = false;
51:
52: 53: 54: 55: 56:
57: protected static $_userAgent = '';
58:
59: 60: 61: 62: 63:
64: public static $path = '/';
65:
66: 67: 68: 69: 70:
71: public static $lastError = null;
72:
73: 74: 75: 76: 77:
78: public static $security = null;
79:
80: 81: 82: 83: 84:
85: public static $time = false;
86:
87: 88: 89: 90: 91:
92: public static $cookieLifeTime;
93:
94: 95: 96: 97: 98:
99: public static $sessionTime = false;
100:
101: 102: 103: 104: 105:
106: public static $id = null;
107:
108: 109: 110: 111: 112:
113: public static $host = null;
114:
115: 116: 117: 118: 119:
120: public static $timeout = null;
121:
122: 123: 124: 125: 126: 127: 128:
129: public static $requestCountdown = 10;
130:
131: 132: 133: 134: 135: 136: 137:
138: public static function init($base = null, $start = true) {
139: self::$time = time();
140:
141: $checkAgent = Configure::read('Session.checkAgent');
142: if (($checkAgent === true || $checkAgent === null) && env('HTTP_USER_AGENT') != null) {
143: self::$_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt'));
144: }
145: self::_setPath($base);
146: self::_setHost(env('HTTP_HOST'));
147: }
148:
149: 150: 151: 152: 153: 154:
155: protected static function _setPath($base = null) {
156: if (empty($base)) {
157: self::$path = '/';
158: return;
159: }
160: if (strpos($base, 'index.php') !== false) {
161: $base = str_replace('index.php', '', $base);
162: }
163: if (strpos($base, '?') !== false) {
164: $base = str_replace('?', '', $base);
165: }
166: self::$path = $base;
167: }
168:
169: 170: 171: 172: 173: 174:
175: protected static function _setHost($host) {
176: self::$host = $host;
177: if (strpos(self::$host, ':') !== false) {
178: self::$host = substr(self::$host, 0, strpos(self::$host, ':'));
179: }
180: }
181:
182: 183: 184: 185: 186:
187: public static function start() {
188: if (self::started()) {
189: return true;
190: }
191: $id = self::id();
192: session_write_close();
193: self::_configureSession();
194: self::_startSession();
195:
196: if (!$id && self::started()) {
197: self::_checkValid();
198: }
199:
200: self::$error = false;
201: return self::started();
202: }
203:
204: 205: 206: 207: 208:
209: public static function started() {
210: return isset($_SESSION) && session_id();
211: }
212:
213: 214: 215: 216: 217: 218:
219: public static function check($name = null) {
220: if (!self::started() && !self::start()) {
221: return false;
222: }
223: if (empty($name)) {
224: return false;
225: }
226: $result = Set::classicExtract($_SESSION, $name);
227: return isset($result);
228: }
229:
230: 231: 232: 233: 234: 235:
236: public static function id($id = null) {
237: if ($id) {
238: self::$id = $id;
239: session_id(self::$id);
240: }
241: if (self::started()) {
242: return session_id();
243: }
244: return self::$id;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public static function delete($name) {
254: if (self::check($name)) {
255: self::_overwrite($_SESSION, Set::remove($_SESSION, $name));
256: return (self::check($name) == false);
257: }
258: self::_setError(2, __d('cake_dev', "%s doesn't exist", $name));
259: return false;
260: }
261:
262: 263: 264: 265: 266: 267: 268:
269: protected static function _overwrite(&$old, $new) {
270: if (!empty($old)) {
271: foreach ($old as $key => $var) {
272: if (!isset($new[$key])) {
273: unset($old[$key]);
274: }
275: }
276: }
277: foreach ($new as $key => $var) {
278: $old[$key] = $var;
279: }
280: }
281:
282: 283: 284: 285: 286: 287:
288: protected static function _error($errorNumber) {
289: if (!is_array(self::$error) || !array_key_exists($errorNumber, self::$error)) {
290: return false;
291: } else {
292: return self::$error[$errorNumber];
293: }
294: }
295:
296: 297: 298: 299: 300:
301: public static function error() {
302: if (self::$lastError) {
303: return self::_error(self::$lastError);
304: }
305: return false;
306: }
307:
308: 309: 310: 311: 312:
313: public static function valid() {
314: if (self::read('Config')) {
315: if (self::_validAgentAndTime() && self::$error === false) {
316: self::$valid = true;
317: } else {
318: self::$valid = false;
319: self::_setError(1, 'Session Highjacking Attempted !!!');
320: }
321: }
322: return self::$valid;
323: }
324:
325: 326: 327: 328: 329: 330: 331: 332:
333: protected static function _validAgentAndTime() {
334: $config = self::read('Config');
335: $validAgent = (
336: Configure::read('Session.checkAgent') === false ||
337: self::$_userAgent == $config['userAgent']
338: );
339: return ($validAgent && self::$time <= $config['time']);
340: }
341:
342: 343: 344: 345: 346: 347:
348: public static function userAgent($userAgent = null) {
349: if ($userAgent) {
350: self::$_userAgent = $userAgent;
351: }
352: return self::$_userAgent;
353: }
354:
355: 356: 357: 358: 359: 360:
361: public static function read($name = null) {
362: if (!self::started() && !self::start()) {
363: return false;
364: }
365: if (is_null($name)) {
366: return self::_returnSessionVars();
367: }
368: if (empty($name)) {
369: return false;
370: }
371: $result = Set::classicExtract($_SESSION, $name);
372:
373: if (!is_null($result)) {
374: return $result;
375: }
376: self::_setError(2, "$name doesn't exist");
377: return null;
378: }
379:
380: 381: 382: 383: 384:
385: protected static function _returnSessionVars() {
386: if (!empty($_SESSION)) {
387: return $_SESSION;
388: }
389: self::_setError(2, 'No Session vars set');
390: return false;
391: }
392:
393: 394: 395: 396: 397: 398: 399:
400: public static function write($name, $value = null) {
401: if (!self::started() && !self::start()) {
402: return false;
403: }
404: if (empty($name)) {
405: return false;
406: }
407: $write = $name;
408: if (!is_array($name)) {
409: $write = array($name => $value);
410: }
411: foreach ($write as $key => $val) {
412: self::_overwrite($_SESSION, Set::insert($_SESSION, $key, $val));
413: if (Set::classicExtract($_SESSION, $key) !== $val) {
414: return false;
415: }
416: }
417: return true;
418: }
419:
420: 421: 422: 423: 424:
425: public static function destroy() {
426: if (self::started()) {
427: session_destroy();
428: }
429: self::clear();
430: }
431:
432: 433: 434: 435: 436:
437: public static function clear() {
438: $_SESSION = null;
439: self::$id = null;
440: self::start();
441: self::renew();
442: }
443:
444: 445: 446: 447: 448: 449: 450: 451:
452: protected static function _configureSession() {
453: $sessionConfig = Configure::read('Session');
454: $iniSet = function_exists('ini_set');
455:
456: if (isset($sessionConfig['defaults'])) {
457: $defaults = self::_defaultConfig($sessionConfig['defaults']);
458: if ($defaults) {
459: $sessionConfig = Set::merge($defaults, $sessionConfig);
460: }
461: }
462: if (!isset($sessionConfig['ini']['session.cookie_secure']) && env('HTTPS')) {
463: $sessionConfig['ini']['session.cookie_secure'] = 1;
464: }
465: if (isset($sessionConfig['timeout']) && !isset($sessionConfig['cookieTimeout'])) {
466: $sessionConfig['cookieTimeout'] = $sessionConfig['timeout'];
467: }
468: if (!isset($sessionConfig['ini']['session.cookie_lifetime'])) {
469: $sessionConfig['ini']['session.cookie_lifetime'] = $sessionConfig['cookieTimeout'] * 60;
470: }
471: if (!isset($sessionConfig['ini']['session.name'])) {
472: $sessionConfig['ini']['session.name'] = $sessionConfig['cookie'];
473: }
474: if (!empty($sessionConfig['handler'])) {
475: $sessionConfig['ini']['session.save_handler'] = 'user';
476: }
477:
478: if (empty($_SESSION)) {
479: if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) {
480: foreach ($sessionConfig['ini'] as $setting => $value) {
481: if (ini_set($setting, $value) === false) {
482: throw new CakeSessionException(sprintf(
483: __d('cake_dev', 'Unable to configure the session, setting %s failed.'),
484: $setting
485: ));
486: }
487: }
488: }
489: }
490: if (!empty($sessionConfig['handler']) && !isset($sessionConfig['handler']['engine'])) {
491: call_user_func_array('session_set_save_handler', $sessionConfig['handler']);
492: }
493: if (!empty($sessionConfig['handler']['engine'])) {
494: $handler = self::_getHandler($sessionConfig['handler']['engine']);
495: session_set_save_handler(
496: array($handler, 'open'),
497: array($handler, 'close'),
498: array($handler, 'read'),
499: array($handler, 'write'),
500: array($handler, 'destroy'),
501: array($handler, 'gc')
502: );
503: }
504: Configure::write('Session', $sessionConfig);
505: self::$sessionTime = self::$time + ($sessionConfig['timeout'] * 60);
506: }
507:
508: 509: 510: 511: 512: 513: 514:
515: protected static function _getHandler($handler) {
516: list($plugin, $class) = pluginSplit($handler, true);
517: App::uses($class, $plugin . 'Model/Datasource/Session');
518: if (!class_exists($class)) {
519: throw new CakeSessionException(__d('cake_dev', 'Could not load %s to handle the session.', $class));
520: }
521: $handler = new $class();
522: if ($handler instanceof CakeSessionHandlerInterface) {
523: return $handler;
524: }
525: throw new CakeSessionException(__d('cake_dev', 'Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.'));
526: }
527:
528: 529: 530: 531: 532: 533:
534: protected static function _defaultConfig($name) {
535: $defaults = array(
536: 'php' => array(
537: 'cookie' => 'CAKEPHP',
538: 'timeout' => 240,
539: 'cookieTimeout' => 240,
540: 'ini' => array(
541: 'session.use_trans_sid' => 0,
542: 'session.cookie_path' => self::$path
543: )
544: ),
545: 'cake' => array(
546: 'cookie' => 'CAKEPHP',
547: 'timeout' => 240,
548: 'cookieTimeout' => 240,
549: 'ini' => array(
550: 'session.use_trans_sid' => 0,
551: 'url_rewriter.tags' => '',
552: 'session.serialize_handler' => 'php',
553: 'session.use_cookies' => 1,
554: 'session.cookie_path' => self::$path,
555: 'session.auto_start' => 0,
556: 'session.save_path' => TMP . 'sessions',
557: 'session.save_handler' => 'files'
558: )
559: ),
560: 'cache' => array(
561: 'cookie' => 'CAKEPHP',
562: 'timeout' => 240,
563: 'cookieTimeout' => 240,
564: 'ini' => array(
565: 'session.use_trans_sid' => 0,
566: 'url_rewriter.tags' => '',
567: 'session.auto_start' => 0,
568: 'session.use_cookies' => 1,
569: 'session.cookie_path' => self::$path,
570: 'session.save_handler' => 'user',
571: ),
572: 'handler' => array(
573: 'engine' => 'CacheSession',
574: 'config' => 'default'
575: )
576: ),
577: 'database' => array(
578: 'cookie' => 'CAKEPHP',
579: 'timeout' => 240,
580: 'cookieTimeout' => 240,
581: 'ini' => array(
582: 'session.use_trans_sid' => 0,
583: 'url_rewriter.tags' => '',
584: 'session.auto_start' => 0,
585: 'session.use_cookies' => 1,
586: 'session.cookie_path' => self::$path,
587: 'session.save_handler' => 'user',
588: 'session.serialize_handler' => 'php',
589: ),
590: 'handler' => array(
591: 'engine' => 'DatabaseSession',
592: 'model' => 'Session'
593: )
594: )
595: );
596: if (isset($defaults[$name])) {
597: return $defaults[$name];
598: }
599: return false;
600: }
601:
602: 603: 604: 605: 606:
607: protected static function _startSession() {
608: if (headers_sent()) {
609: if (empty($_SESSION)) {
610: $_SESSION = array();
611: }
612: } elseif (!isset($_SESSION)) {
613: session_cache_limiter ("must-revalidate");
614: session_start();
615: header ('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
616: } else {
617: session_start();
618: }
619: return true;
620: }
621:
622: 623: 624: 625: 626:
627: protected static function _checkValid() {
628: if (!self::started() && !self::start()) {
629: self::$valid = false;
630: return false;
631: }
632: if ($config = self::read('Config')) {
633: $sessionConfig = Configure::read('Session');
634:
635: if (self::_validAgentAndTime()) {
636: self::write('Config.time', self::$sessionTime);
637: if (isset($sessionConfig['autoRegenerate']) && $sessionConfig['autoRegenerate'] === true) {
638: $check = $config['countdown'];
639: $check -= 1;
640: self::write('Config.countdown', $check);
641:
642: if ($check < 1) {
643: self::renew();
644: self::write('Config.countdown', self::$requestCountdown);
645: }
646: }
647: self::$valid = true;
648: } else {
649: self::destroy();
650: self::$valid = false;
651: self::_setError(1, 'Session Highjacking Attempted !!!');
652: }
653: } else {
654: self::write('Config.userAgent', self::$_userAgent);
655: self::write('Config.time', self::$sessionTime);
656: self::write('Config.countdown', self::$requestCountdown);
657: self::$valid = true;
658: }
659: }
660:
661: 662: 663: 664: 665:
666: public static function renew() {
667: if (session_id()) {
668: if (session_id() != '' || isset($_COOKIE[session_name()])) {
669: setcookie(Configure::read('Session.cookie'), '', time() - 42000, self::$path);
670: }
671: session_regenerate_id(true);
672: }
673: }
674:
675: 676: 677: 678: 679: 680: 681:
682: protected static function _setError($errorNumber, $errorMessage) {
683: if (self::$error === false) {
684: self::$error = array();
685: }
686: self::$error[$errorNumber] = $errorMessage;
687: self::$lastError = $errorNumber;
688: }
689: }
690:
691:
692: 693: 694: 695: 696: 697:
698: interface CakeSessionHandlerInterface {
699: 700: 701: 702: 703:
704: public function open();
705:
706: 707: 708: 709: 710:
711: public function close();
712:
713: 714: 715: 716: 717: 718:
719: public function read($id);
720:
721: 722: 723: 724: 725: 726: 727:
728: public function write($id, $data);
729:
730: 731: 732: 733: 734: 735:
736: public function destroy($id);
737:
738: 739: 740: 741: 742: 743: 744:
745: public function gc($expires = null);
746: }
747:
748:
749:
750: CakeSession::init();
751: