CakeSession.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. /**
  3. * Session class for CakePHP.
  4. *
  5. * CakePHP abstracts the handling of sessions.
  6. * There are several convenient methods to access session information.
  7. * This class is the implementation of those methods.
  8. * They are mostly used by the Session Component.
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * For full copyright and license information, please see the LICENSE.txt
  15. * Redistributions of files must retain the above copyright notice.
  16. *
  17. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  18. * @link http://cakephp.org CakePHP(tm) Project
  19. * @package Cake.Model.Datasource
  20. * @since CakePHP(tm) v .0.10.0.1222
  21. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  22. */
  23. App::uses('Hash', 'Utility');
  24. App::uses('Security', 'Utility');
  25. /**
  26. * Session class for CakePHP.
  27. *
  28. * CakePHP abstracts the handling of sessions. There are several convenient methods to access session information.
  29. * This class is the implementation of those methods. They are mostly used by the Session Component.
  30. *
  31. * @package Cake.Model.Datasource
  32. */
  33. class CakeSession {
  34. /**
  35. * True if the Session is still valid
  36. *
  37. * @var bool
  38. */
  39. public static $valid = false;
  40. /**
  41. * Error messages for this session
  42. *
  43. * @var array
  44. */
  45. public static $error = false;
  46. /**
  47. * User agent string
  48. *
  49. * @var string
  50. */
  51. protected static $_userAgent = '';
  52. /**
  53. * Path to where the session is active.
  54. *
  55. * @var string
  56. */
  57. public static $path = '/';
  58. /**
  59. * Error number of last occurred error
  60. *
  61. * @var int
  62. */
  63. public static $lastError = null;
  64. /**
  65. * Start time for this session.
  66. *
  67. * @var int
  68. */
  69. public static $time = false;
  70. /**
  71. * Cookie lifetime
  72. *
  73. * @var int
  74. */
  75. public static $cookieLifeTime;
  76. /**
  77. * Time when this session becomes invalid.
  78. *
  79. * @var int
  80. */
  81. public static $sessionTime = false;
  82. /**
  83. * Current Session id
  84. *
  85. * @var string
  86. */
  87. public static $id = null;
  88. /**
  89. * Hostname
  90. *
  91. * @var string
  92. */
  93. public static $host = null;
  94. /**
  95. * Session timeout multiplier factor
  96. *
  97. * @var int
  98. */
  99. public static $timeout = null;
  100. /**
  101. * Number of requests that can occur during a session time without the session being renewed.
  102. * This feature is only used when config value `Session.autoRegenerate` is set to true.
  103. *
  104. * @var int
  105. * @see CakeSession::_checkValid()
  106. */
  107. public static $requestCountdown = 10;
  108. /**
  109. * Whether or not the init function in this class was already called
  110. *
  111. * @var bool
  112. */
  113. protected static $_initialized = false;
  114. /**
  115. * Session cookie name
  116. *
  117. * @var string
  118. */
  119. protected static $_cookieName = null;
  120. /**
  121. * Pseudo constructor.
  122. *
  123. * @param string $base The base path for the Session
  124. * @return void
  125. */
  126. public static function init($base = null) {
  127. self::$time = time();
  128. if (env('HTTP_USER_AGENT')) {
  129. self::$_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt'));
  130. }
  131. self::_setPath($base);
  132. self::_setHost(env('HTTP_HOST'));
  133. if (!self::$_initialized) {
  134. register_shutdown_function('session_write_close');
  135. }
  136. self::$_initialized = true;
  137. }
  138. /**
  139. * Setup the Path variable
  140. *
  141. * @param string $base base path
  142. * @return void
  143. */
  144. protected static function _setPath($base = null) {
  145. if (empty($base)) {
  146. self::$path = '/';
  147. return;
  148. }
  149. if (strpos($base, 'index.php') !== false) {
  150. $base = str_replace('index.php', '', $base);
  151. }
  152. if (strpos($base, '?') !== false) {
  153. $base = str_replace('?', '', $base);
  154. }
  155. self::$path = $base;
  156. }
  157. /**
  158. * Set the host name
  159. *
  160. * @param string $host Hostname
  161. * @return void
  162. */
  163. protected static function _setHost($host) {
  164. self::$host = $host;
  165. if (strpos(self::$host, ':') !== false) {
  166. self::$host = substr(self::$host, 0, strpos(self::$host, ':'));
  167. }
  168. }
  169. /**
  170. * Starts the Session.
  171. *
  172. * @return bool True if session was started
  173. */
  174. public static function start() {
  175. if (self::started()) {
  176. return true;
  177. }
  178. $id = self::id();
  179. self::_startSession();
  180. if (!$id && self::started()) {
  181. self::_checkValid();
  182. }
  183. self::$error = false;
  184. self::$valid = true;
  185. return self::started();
  186. }
  187. /**
  188. * Determine if Session has been started.
  189. *
  190. * @return bool True if session has been started.
  191. */
  192. public static function started() {
  193. return isset($_SESSION) && session_id();
  194. }
  195. /**
  196. * Returns true if given variable is set in session.
  197. *
  198. * @param string $name Variable name to check for
  199. * @return bool True if variable is there
  200. */
  201. public static function check($name = null) {
  202. if (empty($name) || !self::_hasSession() || !self::start()) {
  203. return false;
  204. }
  205. return Hash::get($_SESSION, $name) !== null;
  206. }
  207. /**
  208. * Returns the session id.
  209. * Calling this method will not auto start the session. You might have to manually
  210. * assert a started session.
  211. *
  212. * Passing an id into it, you can also replace the session id if the session
  213. * has not already been started.
  214. * Note that depending on the session handler, not all characters are allowed
  215. * within the session id. For example, the file session handler only allows
  216. * characters in the range a-z A-Z 0-9 , (comma) and - (minus).
  217. *
  218. * @param string $id Id to replace the current session id
  219. * @return string Session id
  220. */
  221. public static function id($id = null) {
  222. if ($id) {
  223. self::$id = $id;
  224. session_id(self::$id);
  225. }
  226. if (self::started()) {
  227. return session_id();
  228. }
  229. return self::$id;
  230. }
  231. /**
  232. * Removes a variable from session.
  233. *
  234. * @param string $name Session variable to remove
  235. * @return bool Success
  236. */
  237. public static function delete($name) {
  238. if (self::check($name)) {
  239. self::_overwrite($_SESSION, Hash::remove($_SESSION, $name));
  240. return !self::check($name);
  241. }
  242. return false;
  243. }
  244. /**
  245. * Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.
  246. *
  247. * @param array &$old Set of old variables => values
  248. * @param array $new New set of variable => value
  249. * @return void
  250. */
  251. protected static function _overwrite(&$old, $new) {
  252. if (!empty($old)) {
  253. foreach ($old as $key => $var) {
  254. if (!isset($new[$key])) {
  255. unset($old[$key]);
  256. }
  257. }
  258. }
  259. foreach ($new as $key => $var) {
  260. $old[$key] = $var;
  261. }
  262. }
  263. /**
  264. * Return error description for given error number.
  265. *
  266. * @param int $errorNumber Error to set
  267. * @return string Error as string
  268. */
  269. protected static function _error($errorNumber) {
  270. if (!is_array(self::$error) || !array_key_exists($errorNumber, self::$error)) {
  271. return false;
  272. }
  273. return self::$error[$errorNumber];
  274. }
  275. /**
  276. * Returns last occurred error as a string, if any.
  277. *
  278. * @return mixed Error description as a string, or false.
  279. */
  280. public static function error() {
  281. if (self::$lastError) {
  282. return self::_error(self::$lastError);
  283. }
  284. return false;
  285. }
  286. /**
  287. * Returns true if session is valid.
  288. *
  289. * @return bool Success
  290. */
  291. public static function valid() {
  292. if (self::start() && self::read('Config')) {
  293. if (self::_validAgentAndTime() && self::$error === false) {
  294. self::$valid = true;
  295. } else {
  296. self::$valid = false;
  297. self::_setError(1, 'Session Highjacking Attempted !!!');
  298. }
  299. }
  300. return self::$valid;
  301. }
  302. /**
  303. * Tests that the user agent is valid and that the session hasn't 'timed out'.
  304. * Since timeouts are implemented in CakeSession it checks the current self::$time
  305. * against the time the session is set to expire. The User agent is only checked
  306. * if Session.checkAgent == true.
  307. *
  308. * @return bool
  309. */
  310. protected static function _validAgentAndTime() {
  311. $config = self::read('Config');
  312. $validAgent = (
  313. Configure::read('Session.checkAgent') === false ||
  314. self::$_userAgent == $config['userAgent']
  315. );
  316. return ($validAgent && self::$time <= $config['time']);
  317. }
  318. /**
  319. * Get / Set the user agent
  320. *
  321. * @param string $userAgent Set the user agent
  322. * @return string Current user agent
  323. */
  324. public static function userAgent($userAgent = null) {
  325. if ($userAgent) {
  326. self::$_userAgent = $userAgent;
  327. }
  328. if (empty(self::$_userAgent)) {
  329. CakeSession::init(self::$path);
  330. }
  331. return self::$_userAgent;
  332. }
  333. /**
  334. * Returns given session variable, or all of them, if no parameters given.
  335. *
  336. * @param string|array $name The name of the session variable (or a path as sent to Set.extract)
  337. * @return mixed The value of the session variable, null if session not available,
  338. * session not started, or provided name not found in the session.
  339. */
  340. public static function read($name = null) {
  341. if (empty($name) && $name !== null) {
  342. return false;
  343. }
  344. if (!self::_hasSession() || !self::start()) {
  345. return null;
  346. }
  347. if ($name === null) {
  348. return self::_returnSessionVars();
  349. }
  350. $result = Hash::get($_SESSION, $name);
  351. if (isset($result)) {
  352. return $result;
  353. }
  354. return null;
  355. }
  356. /**
  357. * Returns all session variables.
  358. *
  359. * @return mixed Full $_SESSION array, or false on error.
  360. */
  361. protected static function _returnSessionVars() {
  362. if (!empty($_SESSION)) {
  363. return $_SESSION;
  364. }
  365. self::_setError(2, 'No Session vars set');
  366. return false;
  367. }
  368. /**
  369. * Writes value to given session variable name.
  370. *
  371. * @param string|array $name Name of variable
  372. * @param string $value Value to write
  373. * @return bool True if the write was successful, false if the write failed
  374. */
  375. public static function write($name, $value = null) {
  376. if (empty($name) || !self::start()) {
  377. return false;
  378. }
  379. $write = $name;
  380. if (!is_array($name)) {
  381. $write = array($name => $value);
  382. }
  383. foreach ($write as $key => $val) {
  384. self::_overwrite($_SESSION, Hash::insert($_SESSION, $key, $val));
  385. if (Hash::get($_SESSION, $key) !== $val) {
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. /**
  392. * Helper method to destroy invalid sessions.
  393. *
  394. * @return void
  395. */
  396. public static function destroy() {
  397. if (!self::started()) {
  398. self::_startSession();
  399. }
  400. session_destroy();
  401. $_SESSION = null;
  402. self::$id = null;
  403. self::$_cookieName = null;
  404. }
  405. /**
  406. * Clears the session, the session id, and renews the session.
  407. *
  408. * @return void
  409. */
  410. public static function clear() {
  411. $_SESSION = null;
  412. self::$id = null;
  413. self::renew();
  414. }
  415. /**
  416. * Helper method to initialize a session, based on CakePHP core settings.
  417. *
  418. * Sessions can be configured with a few shortcut names as well as have any number of ini settings declared.
  419. *
  420. * @return void
  421. * @throws CakeSessionException Throws exceptions when ini_set() fails.
  422. */
  423. protected static function _configureSession() {
  424. $sessionConfig = Configure::read('Session');
  425. if (isset($sessionConfig['defaults'])) {
  426. $defaults = self::_defaultConfig($sessionConfig['defaults']);
  427. if ($defaults) {
  428. $sessionConfig = Hash::merge($defaults, $sessionConfig);
  429. }
  430. }
  431. if (!isset($sessionConfig['ini']['session.cookie_secure']) && env('HTTPS')) {
  432. $sessionConfig['ini']['session.cookie_secure'] = 1;
  433. }
  434. if (isset($sessionConfig['timeout']) && !isset($sessionConfig['cookieTimeout'])) {
  435. $sessionConfig['cookieTimeout'] = $sessionConfig['timeout'];
  436. }
  437. if (!isset($sessionConfig['ini']['session.cookie_lifetime'])) {
  438. $sessionConfig['ini']['session.cookie_lifetime'] = $sessionConfig['cookieTimeout'] * 60;
  439. }
  440. if (!isset($sessionConfig['ini']['session.name'])) {
  441. $sessionConfig['ini']['session.name'] = $sessionConfig['cookie'];
  442. }
  443. self::$_cookieName = $sessionConfig['ini']['session.name'];
  444. if (!empty($sessionConfig['handler'])) {
  445. $sessionConfig['ini']['session.save_handler'] = 'user';
  446. }
  447. if (!isset($sessionConfig['ini']['session.gc_maxlifetime'])) {
  448. $sessionConfig['ini']['session.gc_maxlifetime'] = $sessionConfig['timeout'] * 60;
  449. }
  450. if (!isset($sessionConfig['ini']['session.cookie_httponly'])) {
  451. $sessionConfig['ini']['session.cookie_httponly'] = 1;
  452. }
  453. if (empty($_SESSION)) {
  454. if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) {
  455. foreach ($sessionConfig['ini'] as $setting => $value) {
  456. if (ini_set($setting, $value) === false) {
  457. throw new CakeSessionException(__d('cake_dev', 'Unable to configure the session, setting %s failed.', $setting));
  458. }
  459. }
  460. }
  461. }
  462. if (!empty($sessionConfig['handler']) && !isset($sessionConfig['handler']['engine'])) {
  463. call_user_func_array('session_set_save_handler', $sessionConfig['handler']);
  464. }
  465. if (!empty($sessionConfig['handler']['engine'])) {
  466. $handler = self::_getHandler($sessionConfig['handler']['engine']);
  467. session_set_save_handler(
  468. array($handler, 'open'),
  469. array($handler, 'close'),
  470. array($handler, 'read'),
  471. array($handler, 'write'),
  472. array($handler, 'destroy'),
  473. array($handler, 'gc')
  474. );
  475. }
  476. Configure::write('Session', $sessionConfig);
  477. self::$sessionTime = self::$time + ($sessionConfig['timeout'] * 60);
  478. }
  479. /**
  480. * Get session cookie name.
  481. *
  482. * @return string
  483. */
  484. protected static function _cookieName() {
  485. if (self::$_cookieName !== null) {
  486. return self::$_cookieName;
  487. }
  488. self::init();
  489. self::_configureSession();
  490. return self::$_cookieName = session_name();
  491. }
  492. /**
  493. * Returns whether a session exists
  494. *
  495. * @return bool
  496. */
  497. protected static function _hasSession() {
  498. return self::started() || isset($_COOKIE[self::_cookieName()]);
  499. }
  500. /**
  501. * Find the handler class and make sure it implements the correct interface.
  502. *
  503. * @param string $handler Handler name.
  504. * @return void
  505. * @throws CakeSessionException
  506. */
  507. protected static function _getHandler($handler) {
  508. list($plugin, $class) = pluginSplit($handler, true);
  509. App::uses($class, $plugin . 'Model/Datasource/Session');
  510. if (!class_exists($class)) {
  511. throw new CakeSessionException(__d('cake_dev', 'Could not load %s to handle the session.', $class));
  512. }
  513. $handler = new $class();
  514. if ($handler instanceof CakeSessionHandlerInterface) {
  515. return $handler;
  516. }
  517. throw new CakeSessionException(__d('cake_dev', 'Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.'));
  518. }
  519. /**
  520. * Get one of the prebaked default session configurations.
  521. *
  522. * @param string $name Config name.
  523. * @return bool|array
  524. */
  525. protected static function _defaultConfig($name) {
  526. $defaults = array(
  527. 'php' => array(
  528. 'cookie' => 'CAKEPHP',
  529. 'timeout' => 240,
  530. 'ini' => array(
  531. 'session.use_trans_sid' => 0,
  532. 'session.cookie_path' => self::$path
  533. )
  534. ),
  535. 'cake' => array(
  536. 'cookie' => 'CAKEPHP',
  537. 'timeout' => 240,
  538. 'ini' => array(
  539. 'session.use_trans_sid' => 0,
  540. 'url_rewriter.tags' => '',
  541. 'session.serialize_handler' => 'php',
  542. 'session.use_cookies' => 1,
  543. 'session.cookie_path' => self::$path,
  544. 'session.save_path' => TMP . 'sessions',
  545. 'session.save_handler' => 'files'
  546. )
  547. ),
  548. 'cache' => array(
  549. 'cookie' => 'CAKEPHP',
  550. 'timeout' => 240,
  551. 'ini' => array(
  552. 'session.use_trans_sid' => 0,
  553. 'url_rewriter.tags' => '',
  554. 'session.use_cookies' => 1,
  555. 'session.cookie_path' => self::$path,
  556. 'session.save_handler' => 'user',
  557. ),
  558. 'handler' => array(
  559. 'engine' => 'CacheSession',
  560. 'config' => 'default'
  561. )
  562. ),
  563. 'database' => array(
  564. 'cookie' => 'CAKEPHP',
  565. 'timeout' => 240,
  566. 'ini' => array(
  567. 'session.use_trans_sid' => 0,
  568. 'url_rewriter.tags' => '',
  569. 'session.use_cookies' => 1,
  570. 'session.cookie_path' => self::$path,
  571. 'session.save_handler' => 'user',
  572. 'session.serialize_handler' => 'php',
  573. ),
  574. 'handler' => array(
  575. 'engine' => 'DatabaseSession',
  576. 'model' => 'Session'
  577. )
  578. )
  579. );
  580. if (isset($defaults[$name])) {
  581. return $defaults[$name];
  582. }
  583. return false;
  584. }
  585. /**
  586. * Helper method to start a session
  587. *
  588. * @return bool Success
  589. */
  590. protected static function _startSession() {
  591. self::init();
  592. session_write_close();
  593. self::_configureSession();
  594. if (headers_sent()) {
  595. if (empty($_SESSION)) {
  596. $_SESSION = array();
  597. }
  598. } else {
  599. // For IE<=8
  600. session_cache_limiter("must-revalidate");
  601. session_start();
  602. }
  603. return true;
  604. }
  605. /**
  606. * Helper method to create a new session.
  607. *
  608. * @return void
  609. */
  610. protected static function _checkValid() {
  611. $config = self::read('Config');
  612. if ($config) {
  613. $sessionConfig = Configure::read('Session');
  614. if (self::valid()) {
  615. self::write('Config.time', self::$sessionTime);
  616. if (isset($sessionConfig['autoRegenerate']) && $sessionConfig['autoRegenerate'] === true) {
  617. $check = $config['countdown'];
  618. $check -= 1;
  619. self::write('Config.countdown', $check);
  620. if ($check < 1) {
  621. self::renew();
  622. self::write('Config.countdown', self::$requestCountdown);
  623. }
  624. }
  625. } else {
  626. $_SESSION = array();
  627. self::destroy();
  628. self::_setError(1, 'Session Highjacking Attempted !!!');
  629. self::_startSession();
  630. self::_writeConfig();
  631. }
  632. } else {
  633. self::_writeConfig();
  634. }
  635. }
  636. /**
  637. * Writes configuration variables to the session
  638. *
  639. * @return void
  640. */
  641. protected static function _writeConfig() {
  642. self::write('Config.userAgent', self::$_userAgent);
  643. self::write('Config.time', self::$sessionTime);
  644. self::write('Config.countdown', self::$requestCountdown);
  645. }
  646. /**
  647. * Restarts this session.
  648. *
  649. * @return void
  650. */
  651. public static function renew() {
  652. if (!session_id()) {
  653. return;
  654. }
  655. if (isset($_COOKIE[session_name()])) {
  656. setcookie(Configure::read('Session.cookie'), '', time() - 42000, self::$path);
  657. }
  658. session_regenerate_id(true);
  659. }
  660. /**
  661. * Helper method to set an internal error message.
  662. *
  663. * @param int $errorNumber Number of the error
  664. * @param string $errorMessage Description of the error
  665. * @return void
  666. */
  667. protected static function _setError($errorNumber, $errorMessage) {
  668. if (self::$error === false) {
  669. self::$error = array();
  670. }
  671. self::$error[$errorNumber] = $errorMessage;
  672. self::$lastError = $errorNumber;
  673. }
  674. }