cookie.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. // --------------------------------------------------------------------
  14. class Session_Cookie extends \Session_Driver
  15. {
  16. /**
  17. * array of driver config defaults
  18. */
  19. protected static $_defaults = array(
  20. 'cookie_name' => 'fuelcid',
  21. );
  22. // --------------------------------------------------------------------
  23. public function __construct($config = array())
  24. {
  25. // merge the driver config with the global config
  26. $this->config = array_merge($config, (isset($config['cookie']) and is_array($config['cookie'])) ? $config['cookie'] : static::$_defaults);
  27. $this->config = $this->_validate_config($this->config);
  28. }
  29. // --------------------------------------------------------------------
  30. /**
  31. * create a new session
  32. *
  33. * @access public
  34. * @return Fuel\Core\Session_Cookie
  35. */
  36. public function create()
  37. {
  38. // create a new session
  39. $this->keys['session_id'] = $this->_new_session_id();
  40. $this->keys['ip_hash'] = md5(\Input::ip().\Input::real_ip());
  41. $this->keys['user_agent'] = \Input::user_agent();
  42. $this->keys['created'] = $this->time->get_timestamp();
  43. $this->keys['updated'] = $this->keys['created'];
  44. $this->keys['payload'] = '';
  45. return $this;
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * read the session
  50. *
  51. * @access public
  52. * @param boolean, set to true if we want to force a new session to be created
  53. * @return Fuel\Core\Session_Driver
  54. */
  55. public function read($force = false)
  56. {
  57. // initialize the session
  58. $this->data = array();
  59. $this->keys = array();
  60. $this->flash = array();
  61. // get the session cookie
  62. $payload = $this->_get_cookie();
  63. // validate it
  64. if ($payload === false or $force )
  65. {
  66. // not a valid cookie, or a forced session reset
  67. }
  68. elseif ( ! isset($payload[0]) or ! is_array($payload[0]))
  69. {
  70. // not a valid cookie payload
  71. }
  72. elseif ($payload[0]['updated'] + $this->config['expiration_time'] <= $this->time->get_timestamp())
  73. {
  74. // session has expired
  75. }
  76. elseif ($this->config['match_ip'] and $payload[0]['ip_hash'] !== md5(\Input::ip().\Input::real_ip()))
  77. {
  78. // IP address doesn't match
  79. }
  80. elseif ($this->config['match_ua'] and $payload[0]['user_agent'] !== \Input::user_agent())
  81. {
  82. // user agent doesn't match
  83. }
  84. else
  85. {
  86. // session is valid, retrieve the payload
  87. if (isset($payload[0]) and is_array($payload[0])) $this->keys = $payload[0];
  88. if (isset($payload[1]) and is_array($payload[1])) $this->data = $payload[1];
  89. if (isset($payload[2]) and is_array($payload[2])) $this->flash = $payload[2];
  90. }
  91. return parent::read();
  92. }
  93. // --------------------------------------------------------------------
  94. /**
  95. * write the current session
  96. *
  97. * @access public
  98. * @return Fuel\Core\Session_Cookie
  99. */
  100. public function write()
  101. {
  102. // do we have something to write?
  103. if ( ! empty($this->keys) or ! empty($this->data) or ! empty($this->flash))
  104. {
  105. parent::write();
  106. // rotate the session id if needed
  107. $this->rotate(false);
  108. // record the last update time of the session
  109. $this->keys['updated'] = $this->time->get_timestamp();
  110. // then update the cookie
  111. $this->_set_cookie(array($this->keys, $this->data, $this->flash));
  112. }
  113. return $this;
  114. }
  115. // --------------------------------------------------------------------
  116. /**
  117. * validate a driver config value
  118. *
  119. * @param array array with configuration values
  120. * @access public
  121. * @return array validated and consolidated config
  122. */
  123. public function _validate_config($config)
  124. {
  125. $validated = array();
  126. foreach ($config as $name => $item)
  127. {
  128. // filter out any driver config
  129. if (!is_array($item))
  130. {
  131. switch ($name)
  132. {
  133. case 'cookie_name':
  134. if ( empty($item) or ! is_string($item))
  135. {
  136. $item = 'fuelcid';
  137. }
  138. break;
  139. default:
  140. // no config item for this driver
  141. break;
  142. }
  143. // global config, was validated in the driver
  144. $validated[$name] = $item;
  145. }
  146. }
  147. // validate all global settings as well
  148. return parent::_validate_config($validated);
  149. }
  150. }