cookie.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 the tests.
  31. foreach (static::$jar as $cookie)
  32. {
  33. extract($cookie);
  34. $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
  35. // A cookie payload can't exceed 4096 bytes, so if the payload is greater
  36. // than that, we'll raise an error to warn the developer since it could
  37. // cause serious session problems.
  38. $value = static::sign($name, $value);
  39. if (strlen($value) > 4000)
  40. {
  41. throw new \Exception("Payload too large for cookie.");
  42. }
  43. setcookie($name, $value, $time, $path, $domain, $secure);
  44. }
  45. }
  46. /**
  47. * Get the value of a cookie.
  48. *
  49. * <code>
  50. * // Get the value of the "favorite" cookie
  51. * $favorite = Cookie::get('favorite');
  52. *
  53. * // Get the value of a cookie or return a default value
  54. * $favorite = Cookie::get('framework', 'Laravel');
  55. * </code>
  56. *
  57. * @param string $name
  58. * @param mixed $default
  59. * @return string
  60. */
  61. public static function get($name, $default = null)
  62. {
  63. if (isset(static::$jar[$name])) return static::$jar[$name]['value'];
  64. $value = array_get($_COOKIE, $name);
  65. if ( ! is_null($value) and isset($value[40]) and $value[40] == '~')
  66. {
  67. // The hash signature and the cookie value are separated by a tilde
  68. // character for convenience. To separate the hash and the contents
  69. // we can simply expode on that character.
  70. //
  71. // By re-feeding the cookie value into the "sign" method we should
  72. // be able to generate a hash that matches the one taken from the
  73. // cookie. If they don't, the cookie value has been changed.
  74. list($hash, $value) = explode('~', $value, 2);
  75. if (static::hash($name, $value) === $hash)
  76. {
  77. return $value;
  78. }
  79. }
  80. return value($default);
  81. }
  82. /**
  83. * Set the value of a cookie.
  84. *
  85. * <code>
  86. * // Set the value of the "favorite" cookie
  87. * Cookie::put('favorite', 'Laravel');
  88. *
  89. * // Set the value of the "favorite" cookie for twenty minutes
  90. * Cookie::put('favorite', 'Laravel', 20);
  91. * </code>
  92. *
  93. * @param string $name
  94. * @param string $value
  95. * @param int $minutes
  96. * @param string $path
  97. * @param string $domain
  98. * @param bool $secure
  99. * @return void
  100. */
  101. public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
  102. {
  103. static::$jar[$name] = compact('name', 'value', 'minutes', 'path', 'domain', 'secure');
  104. }
  105. /**
  106. * Set a "permanent" cookie. The cookie will last for one year.
  107. *
  108. * <code>
  109. * // Set a cookie that should last one year
  110. * Cookie::forever('favorite', 'Blue');
  111. * </code>
  112. *
  113. * @param string $name
  114. * @param string $value
  115. * @param string $path
  116. * @param string $domain
  117. * @param bool $secure
  118. * @return bool
  119. */
  120. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  121. {
  122. return static::put($name, $value, 525600, $path, $domain, $secure);
  123. }
  124. /**
  125. * Generate a cookie signature based on the contents.
  126. *
  127. * @param string $name
  128. * @param string $value
  129. * @return string
  130. */
  131. public static function sign($name, $value)
  132. {
  133. return static::hash($name, $value).'~'.$value;
  134. }
  135. /**
  136. * Generate a cookie hash based on the contents.
  137. *
  138. * @param string $name
  139. * @param string $value
  140. * @return string
  141. */
  142. protected static function hash($name, $value)
  143. {
  144. return sha1($name.$value.Config::get('application.key'));
  145. }
  146. /**
  147. * Delete a cookie.
  148. *
  149. * @param string $name
  150. * @param string $path
  151. * @param string $domain
  152. * @param bool $secure
  153. * @return bool
  154. */
  155. public static function forget($name, $path = '/', $domain = null, $secure = false)
  156. {
  157. return static::put($name, null, -2000, $path, $domain, $secure);
  158. }
  159. }