cookie.php 4.3 KB

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