Cookie.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. /**
  10. * Using the cookie
  11. *
  12. * <code>
  13. * // Get the value of the "favorite" cookie
  14. * $favorite = Cookie::get('favorite');
  15. *
  16. * // Get the value of a cookie or return a default value
  17. * $favorite = Cookie::get('framework', 'Pimf');
  18. *
  19. * // Set the value of the "favorite" cookie
  20. * Cookie::put('favorite', 'Pimf');
  21. *
  22. * // Set the value of the "favorite" cookie for twenty minutes
  23. * Cookie::put('favorite', 'Pimf', 20);
  24. *
  25. * // Set a cookie that should last one year
  26. * Cookie::forever('favorite', 'Blue');
  27. *
  28. * </code>
  29. *
  30. * @package Pimf
  31. * @author Gjero Krsteski <[email protected]>
  32. */
  33. class Cookie
  34. {
  35. /**
  36. * How long is forever (in minutes)?
  37. *
  38. * @var int
  39. */
  40. const FOREVER = 2628000;
  41. /**
  42. * The cookies that have been set.
  43. *
  44. * @var array
  45. */
  46. public static $jar = array();
  47. /**
  48. * Determine if a cookie exists.
  49. *
  50. * @param string $name
  51. *
  52. * @return bool
  53. */
  54. public static function has($name)
  55. {
  56. return (static::get($name) !== null);
  57. }
  58. /**
  59. * Get the value of a cookie.
  60. *
  61. * @param $name
  62. * @param null $default
  63. *
  64. * @return null|string
  65. */
  66. public static function get($name, $default = null)
  67. {
  68. if (isset(static::$jar[$name])) {
  69. return static::parse(static::$jar[$name]['value']);
  70. }
  71. $cookie = Request::$cookieData;
  72. if (!is_null($value = $cookie->get($name))) {
  73. return static::parse($value);
  74. }
  75. return $default;
  76. }
  77. /**
  78. * Set the value of a cookie.
  79. *
  80. * @param $name
  81. * @param $value
  82. * @param int $expiration
  83. * @param string $path
  84. * @param null $domain
  85. * @param bool $secure
  86. *
  87. * @return bool
  88. * @throws \RuntimeException
  89. */
  90. public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
  91. {
  92. if ($expiration !== 0) {
  93. $expiration = time() + ($expiration * 60);
  94. }
  95. $value = static::hash($value) . '+' . $value;
  96. // If we are attempting to send a secure cookie over the insecure HTTP.
  97. $conf = Registry::get('conf');
  98. if ($secure === true and $conf['ssl'] === false) {
  99. throw new \RuntimeException("Attempting to set secure cookie over HTTP!");
  100. }
  101. static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
  102. return true;
  103. }
  104. /**
  105. * Set a "permanent" cookie. The cookie will last for one year.
  106. *
  107. * @param $name
  108. * @param $value
  109. * @param string $path
  110. * @param null $domain
  111. * @param bool $secure
  112. *
  113. * @return bool
  114. */
  115. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  116. {
  117. return static::put($name, $value, static::FOREVER, $path, $domain, $secure);
  118. }
  119. /**
  120. * Delete a cookie.
  121. *
  122. * @param string $name
  123. * @param string $path
  124. * @param null $domain
  125. * @param bool $secure
  126. *
  127. * @return bool
  128. */
  129. public static function forget($name, $path = '/', $domain = null, $secure = false)
  130. {
  131. return static::put($name, null, -2000, $path, $domain, $secure);
  132. }
  133. /**
  134. * Hash the given cookie value.
  135. *
  136. * @param string $value
  137. *
  138. * @return string
  139. */
  140. public static function hash($value)
  141. {
  142. $conf = Registry::get('conf');
  143. return hash_hmac('sha1', $value, $conf['app']['key']);
  144. }
  145. /**
  146. * Parse a hash fingerprinted cookie value.
  147. *
  148. * @param string $value
  149. *
  150. * @return string
  151. */
  152. protected static function parse($value)
  153. {
  154. $segments = explode('+', $value);
  155. // check if the cookie is invalid.
  156. if (!(count($segments) >= 2)) {
  157. return null;
  158. }
  159. $value = implode('+', array_slice($segments, 1));
  160. // check the SHA-1 hash from the cookie.
  161. if ($segments[0] == static::hash($value)) {
  162. return $value;
  163. }
  164. return null;
  165. }
  166. /**
  167. * Send along with the rest of the HTTP headers.
  168. */
  169. public static function send()
  170. {
  171. foreach (static::$jar as $cookie) {
  172. setcookie($cookie['name'], $cookie['value'], $cookie['expiration'], $cookie['path'], $cookie['domain'], $cookie['secure'], true);
  173. }
  174. }
  175. }