cookie.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php namespace Laravel; defined('DS') or die('No direct script access.');
  2. use Closure;
  3. class Cookie {
  4. /**
  5. * The cookies that have been set.
  6. *
  7. * @var array
  8. */
  9. public static $jar = array();
  10. /**
  11. * Determine if a cookie exists.
  12. *
  13. * @param string $name
  14. * @return bool
  15. */
  16. public static function has($name)
  17. {
  18. return ! is_null(static::get($name));
  19. }
  20. /**
  21. * Send all of the cookies to the browser.
  22. *
  23. * @return void
  24. */
  25. public static function send()
  26. {
  27. if (headers_sent()) return false;
  28. // All cookies are stored in the "jar" when set and not sent directly to the
  29. // browser. This simply makes testing all of the cookie stuff very easy
  30. // since the jar can be inspected by the tests.
  31. foreach (static::$jar as $cookie)
  32. {
  33. static::set($cookie);
  34. }
  35. }
  36. /**
  37. * Send a cookie from the cookie jar back to the browser.
  38. *
  39. * @param array $cookie
  40. * @return void
  41. */
  42. protected static function set($cookie)
  43. {
  44. extract($cookie);
  45. $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
  46. // A cookie payload can't exceed 4096 bytes, so if the payload is greater
  47. // than that, we'll raise an error to warn the developer since it could
  48. // cause serious cookie-based session problems.
  49. $value = static::sign($name, $value);
  50. if (strlen($value) > 4000)
  51. {
  52. throw new \Exception("Payload too large for cookie.");
  53. }
  54. setcookie($name, $value, $time, $path, $domain, $secure);
  55. }
  56. /**
  57. * Get the value of a cookie.
  58. *
  59. * <code>
  60. * // Get the value of the "favorite" cookie
  61. * $favorite = Cookie::get('favorite');
  62. *
  63. * // Get the value of a cookie or return a default value
  64. * $favorite = Cookie::get('framework', 'Laravel');
  65. * </code>
  66. *
  67. * @param string $name
  68. * @param mixed $default
  69. * @return string
  70. */
  71. public static function get($name, $default = null)
  72. {
  73. if (isset(static::$jar[$name])) return static::$jar[$name]['value'];
  74. $value = array_get($_COOKIE, $name);
  75. if ( ! is_null($value) and isset($value[40]) and $value[40] == '~')
  76. {
  77. // The hash signature and the cookie value are separated by a tilde
  78. // character for convenience. To separate the hash and the contents
  79. // we can simply expode on that character.
  80. //
  81. // By re-feeding the cookie value into the "sign" method we should
  82. // be able to generate a hash that matches the one taken from the
  83. // cookie. If they don't, the cookie value has been changed.
  84. list($hash, $value) = explode('~', $value, 2);
  85. if (static::hash($name, $value) === $hash)
  86. {
  87. return $value;
  88. }
  89. }
  90. return value($default);
  91. }
  92. /**
  93. * Set the value of a cookie.
  94. *
  95. * <code>
  96. * // Set the value of the "favorite" cookie
  97. * Cookie::put('favorite', 'Laravel');
  98. *
  99. * // Set the value of the "favorite" cookie for twenty minutes
  100. * Cookie::put('favorite', 'Laravel', 20);
  101. * </code>
  102. *
  103. * @param string $name
  104. * @param string $value
  105. * @param int $minutes
  106. * @param string $path
  107. * @param string $domain
  108. * @param bool $secure
  109. * @return void
  110. */
  111. public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
  112. {
  113. static::$jar[$name] = compact('name', 'value', 'minutes', 'path', 'domain', 'secure');
  114. }
  115. /**
  116. * Set a "permanent" cookie. The cookie will last for one year.
  117. *
  118. * <code>
  119. * // Set a cookie that should last one year
  120. * Cookie::forever('favorite', 'Blue');
  121. * </code>
  122. *
  123. * @param string $name
  124. * @param string $value
  125. * @param string $path
  126. * @param string $domain
  127. * @param bool $secure
  128. * @return bool
  129. */
  130. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  131. {
  132. return static::put($name, $value, 525600, $path, $domain, $secure);
  133. }
  134. /**
  135. * Generate a cookie signature based on the contents.
  136. *
  137. * @param string $name
  138. * @param string $value
  139. * @return string
  140. */
  141. public static function sign($name, $value)
  142. {
  143. return static::hash($name, $value).'~'.$value;
  144. }
  145. /**
  146. * Generate a cookie hash based on the contents.
  147. *
  148. * @param string $name
  149. * @param string $value
  150. * @return string
  151. */
  152. protected static function hash($name, $value)
  153. {
  154. return sha1($name.$value.Config::get('application.key'));
  155. }
  156. /**
  157. * Delete a cookie.
  158. *
  159. * @param string $name
  160. * @param string $path
  161. * @param string $domain
  162. * @param bool $secure
  163. * @return bool
  164. */
  165. public static function forget($name, $path = '/', $domain = null, $secure = false)
  166. {
  167. return static::put($name, null, -2000, $path, $domain, $secure);
  168. }
  169. }