cookie.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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
  29. // the browser. This simply makes testing all of the cookie stuff very
  30. // easy since the jar can be inspected by 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. $value = static::sign($name, $value);
  47. // A cookie payload can't exceed 4096 bytes, so if the cookie payload
  48. // is greater than that, we'll raise an error to warn the developer
  49. // since it could cause cookie session problems.
  50. if (strlen($value) > 4000)
  51. {
  52. throw new \Exception("Payload too large for cookie.");
  53. }
  54. else
  55. {
  56. // We don't want to send secure cookies over HTTP unless the developer has
  57. // turned off the "SSL" application configuration option, which is used
  58. // while developing the application but should be true in production.
  59. if ($secure and ! Request::secure() and Config::get('application.ssl'))
  60. {
  61. return;
  62. }
  63. setcookie($name, $value, $time, $path, $domain, $secure);
  64. }
  65. }
  66. /**
  67. * Get the value of a cookie.
  68. *
  69. * <code>
  70. * // Get the value of the "favorite" cookie
  71. * $favorite = Cookie::get('favorite');
  72. *
  73. * // Get the value of a cookie or return a default value
  74. * $favorite = Cookie::get('framework', 'Laravel');
  75. * </code>
  76. *
  77. * @param string $name
  78. * @param mixed $default
  79. * @return string
  80. */
  81. public static function get($name, $default = null)
  82. {
  83. if (isset(static::$jar[$name])) return static::$jar[$name]['value'];
  84. $value = array_get($_COOKIE, $name);
  85. if ( ! is_null($value) and isset($value[40]) and $value[40] == '~')
  86. {
  87. // The hash signature and the cookie value are separated by a tilde
  88. // character for convenience. To separate the hash and the payload
  89. // we can simply expode on that character.
  90. list($hash, $value) = explode('~', $value, 2);
  91. // By re-feeding the cookie value into the "hash" method we should
  92. // be able to generate a hash that matches the one taken from the
  93. // cookie. If they don't, we return null.
  94. if (static::hash($name, $value) === $hash)
  95. {
  96. return $value;
  97. }
  98. }
  99. return value($default);
  100. }
  101. /**
  102. * Set the value of a cookie.
  103. *
  104. * <code>
  105. * // Set the value of the "favorite" cookie
  106. * Cookie::put('favorite', 'Laravel');
  107. *
  108. * // Set the value of the "favorite" cookie for twenty minutes
  109. * Cookie::put('favorite', 'Laravel', 20);
  110. * </code>
  111. *
  112. * @param string $name
  113. * @param string $value
  114. * @param int $minutes
  115. * @param string $path
  116. * @param string $domain
  117. * @param bool $secure
  118. * @return void
  119. */
  120. public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
  121. {
  122. static::$jar[$name] = compact('name', 'value', 'minutes', 'path', 'domain', 'secure');
  123. }
  124. /**
  125. * Set a "permanent" cookie. The cookie will last for one year.
  126. *
  127. * <code>
  128. * // Set a cookie that should last one year
  129. * Cookie::forever('favorite', 'Blue');
  130. * </code>
  131. *
  132. * @param string $name
  133. * @param string $value
  134. * @param string $path
  135. * @param string $domain
  136. * @param bool $secure
  137. * @return bool
  138. */
  139. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  140. {
  141. return static::put($name, $value, 525600, $path, $domain, $secure);
  142. }
  143. /**
  144. * Generate a cookie signature based on the contents.
  145. *
  146. * @param string $name
  147. * @param string $value
  148. * @return string
  149. */
  150. public static function sign($name, $value)
  151. {
  152. return static::hash($name, $value).'~'.$value;
  153. }
  154. /**
  155. * Generate a cookie hash based on the contents.
  156. *
  157. * @param string $name
  158. * @param string $value
  159. * @return string
  160. */
  161. protected static function hash($name, $value)
  162. {
  163. return sha1($name.$value.Config::get('application.key'));
  164. }
  165. /**
  166. * Delete a cookie.
  167. *
  168. * @param string $name
  169. * @param string $path
  170. * @param string $domain
  171. * @param bool $secure
  172. * @return bool
  173. */
  174. public static function forget($name, $path = '/', $domain = null, $secure = false)
  175. {
  176. return static::put($name, null, -2000, $path, $domain, $secure);
  177. }
  178. }