Cookie.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Cookie helper.
  4. *
  5. * @package KO7
  6. * @category Helpers
  7. *
  8. * @copyright (c) 2007-2016 Kohana Team
  9. * @copyright (c) since 2016 Koseven Team
  10. * @license https://koseven.dev/LICENSE
  11. */
  12. class KO7_Cookie {
  13. /**
  14. * @var string Magic salt to add to the cookie
  15. */
  16. public static $salt = NULL;
  17. /**
  18. * @var integer Number of seconds before the cookie expires
  19. */
  20. public static $expiration = 0;
  21. /**
  22. * @var string Restrict the path that the cookie is available to
  23. */
  24. public static $path = '/';
  25. /**
  26. * @var string Restrict the domain that the cookie is available to
  27. */
  28. public static $domain = NULL;
  29. /**
  30. * @var boolean Only transmit cookies over secure connections
  31. */
  32. public static $secure = FALSE;
  33. /**
  34. * @var boolean Only transmit cookies over HTTP, disabling Javascript access
  35. */
  36. public static $httponly = FALSE;
  37. /**
  38. * Gets the value of a signed cookie. Cookies without signatures will not
  39. * be returned. If the cookie signature is present, but invalid, the cookie
  40. * will be deleted.
  41. *
  42. * // Get the "theme" cookie, or use "blue" if the cookie does not exist
  43. * $theme = Cookie::get('theme', 'blue');
  44. *
  45. * @param string $key cookie name
  46. * @param mixed $default default value to return
  47. * @return string
  48. */
  49. public static function get($key, $default = NULL)
  50. {
  51. if ( ! isset($_COOKIE[$key]))
  52. {
  53. // The cookie does not exist
  54. return $default;
  55. }
  56. // Get the cookie value
  57. $cookie = $_COOKIE[$key];
  58. // Find the position of the split between salt and contents
  59. $split = strlen(Cookie::salt($key, NULL));
  60. if (isset($cookie[$split]) AND $cookie[$split] === '~')
  61. {
  62. // Separate the salt and the value
  63. list ($hash, $value) = explode('~', $cookie, 2);
  64. if (Security::slow_equals(Cookie::salt($key, $value), $hash))
  65. {
  66. // Cookie signature is valid
  67. return $value;
  68. }
  69. // The cookie signature is invalid, delete it
  70. static::delete($key);
  71. }
  72. return $default;
  73. }
  74. /**
  75. * Sets a signed cookie. Note that all cookie values must be strings and no
  76. * automatic serialization will be performed!
  77. *
  78. * [!!] By default, Cookie::$expiration is 0 - if you skip/pass NULL for the optional
  79. * lifetime argument your cookies will expire immediately unless you have separately
  80. * configured Cookie::$expiration.
  81. *
  82. *
  83. * // Set the "theme" cookie
  84. * Cookie::set('theme', 'red');
  85. *
  86. * @param string $name name of cookie
  87. * @param string $value value of cookie
  88. * @param integer $lifetime lifetime in seconds
  89. * @return boolean
  90. * @uses Cookie::salt
  91. */
  92. public static function set($name, $value, $lifetime = NULL)
  93. {
  94. if ($lifetime === NULL)
  95. {
  96. // Use the default expiration
  97. $lifetime = Cookie::$expiration;
  98. }
  99. if ($lifetime !== 0)
  100. {
  101. // The expiration is expected to be a UNIX timestamp
  102. $lifetime += static::_time();
  103. }
  104. // Add the salt to the cookie value
  105. $value = Cookie::salt($name, $value).'~'.$value;
  106. return static::_setcookie($name, $value, $lifetime, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
  107. }
  108. /**
  109. * Deletes a cookie by making the value NULL and expiring it.
  110. *
  111. * Cookie::delete('theme');
  112. *
  113. * @param string $name cookie name
  114. * @return boolean
  115. */
  116. public static function delete($name)
  117. {
  118. // Remove the cookie
  119. unset($_COOKIE[$name]);
  120. // Nullify the cookie and make it expire
  121. return static::_setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
  122. }
  123. /**
  124. * Generates a salt string for a cookie based on the name and value.
  125. *
  126. * $salt = Cookie::salt('theme', 'red');
  127. *
  128. * @param string $name name of cookie
  129. * @param string $value value of cookie
  130. *
  131. * @throws KO7_Exception if Cookie::$salt is not configured
  132. * @return string
  133. */
  134. public static function salt($name, $value)
  135. {
  136. // Require a valid salt
  137. if ( ! Cookie::$salt)
  138. {
  139. throw new KO7_Exception('A valid cookie salt is required. Please set Cookie::$salt in your bootstrap.php. For more information check the documentation');
  140. }
  141. return hash_hmac('sha1', $name.$value.Cookie::$salt, Cookie::$salt);
  142. }
  143. /**
  144. * Proxy for the native setcookie function - to allow mocking in unit tests so that they do not fail when headers
  145. * have been sent.
  146. *
  147. * @param string $name
  148. * @param string $value
  149. * @param integer $expire
  150. * @param string $path
  151. * @param string $domain
  152. * @param boolean $secure
  153. * @param boolean $httponly
  154. *
  155. * @return bool
  156. * @see setcookie
  157. */
  158. protected static function _setcookie($name, $value, $expire, $path, $domain, $secure, $httponly)
  159. {
  160. return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
  161. }
  162. /**
  163. * Proxy for the native time function - to allow mocking of time-related logic in unit tests
  164. *
  165. * @return int
  166. * @see time
  167. */
  168. protected static function _time()
  169. {
  170. return time();
  171. }
  172. }