cookie.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php namespace Laravel;
  2. class Cookie {
  3. /**
  4. * How long is forever (in minutes)?
  5. *
  6. * @var int
  7. */
  8. const forever = 2628000;
  9. /**
  10. * The cookies that have been set.
  11. *
  12. * @var array
  13. */
  14. public static $jar = array();
  15. /**
  16. * Determine if a cookie exists.
  17. *
  18. * @param string $name
  19. * @return bool
  20. */
  21. public static function has($name)
  22. {
  23. return ! is_null(static::get($name));
  24. }
  25. /**
  26. * Get the value of a cookie.
  27. *
  28. * <code>
  29. * // Get the value of the "favorite" cookie
  30. * $favorite = Cookie::get('favorite');
  31. *
  32. * // Get the value of a cookie or return a default value
  33. * $favorite = Cookie::get('framework', 'Laravel');
  34. * </code>
  35. *
  36. * @param string $name
  37. * @param mixed $default
  38. * @return string
  39. */
  40. public static function get($name, $default = null)
  41. {
  42. if (isset(static::$jar[$name])) return static::parse(static::$jar[$name]['value']);
  43. if ( ! is_null($value = Request::foundation()->cookies->get($name)))
  44. {
  45. return static::parse($value);
  46. }
  47. return value($default);
  48. }
  49. /**
  50. * Set the value of a cookie.
  51. *
  52. * <code>
  53. * // Set the value of the "favorite" cookie
  54. * Cookie::put('favorite', 'Laravel');
  55. *
  56. * // Set the value of the "favorite" cookie for twenty minutes
  57. * Cookie::put('favorite', 'Laravel', 20);
  58. * </code>
  59. *
  60. * @param string $name
  61. * @param string $value
  62. * @param int $expiration
  63. * @param string $path
  64. * @param string $domain
  65. * @param bool $secure
  66. * @return void
  67. */
  68. public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
  69. {
  70. if ($expiration !== 0)
  71. {
  72. $expiration = time() + ($expiration * 60);
  73. }
  74. $value = static::hash($value).'+'.$value;
  75. // If the developer has explicitly disabled SLL, then we shouldn't force
  76. // this cookie over SSL.
  77. $secure = $secure && Config::get('application.ssl');
  78. // If the secure option is set to true, yet the request is not over HTTPS
  79. // we'll throw an exception to let the developer know that they are
  80. // attempting to send a secure cookie over the insecure HTTP.
  81. if ($secure and ! Request::secure())
  82. {
  83. throw new \Exception("Attempting to set secure cookie over HTTP.");
  84. }
  85. static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
  86. }
  87. /**
  88. * Set a "permanent" cookie. The cookie will last for one year.
  89. *
  90. * <code>
  91. * // Set a cookie that should last one year
  92. * Cookie::forever('favorite', 'Blue');
  93. * </code>
  94. *
  95. * @param string $name
  96. * @param string $value
  97. * @param string $path
  98. * @param string $domain
  99. * @param bool $secure
  100. * @return bool
  101. */
  102. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  103. {
  104. return static::put($name, $value, static::forever, $path, $domain, $secure);
  105. }
  106. /**
  107. * Delete a cookie.
  108. *
  109. * @param string $name
  110. * @param string $path
  111. * @param string $domain
  112. * @param bool $secure
  113. * @return bool
  114. */
  115. public static function forget($name, $path = '/', $domain = null, $secure = false)
  116. {
  117. return static::put($name, null, -2000, $path, $domain, $secure);
  118. }
  119. /**
  120. * Hash the given cookie value.
  121. *
  122. * @param string $value
  123. * @return string
  124. */
  125. public static function hash($value)
  126. {
  127. return hash_hmac('sha1', $value, Config::get('application.key'));
  128. }
  129. /**
  130. * Parse a hash fingerprinted cookie value.
  131. *
  132. * @param string $value
  133. * @return string
  134. */
  135. protected static function parse($value)
  136. {
  137. $segments = explode('+', $value);
  138. // First we will make sure the cookie actually has enough segments to even
  139. // be valid as being set by the application. If it does not we will go
  140. // ahead and throw exceptions now since there the cookie is invalid.
  141. if ( ! (count($segments) >= 2))
  142. {
  143. return null;
  144. }
  145. $value = implode('+', array_slice($segments, 1));
  146. // Now we will check if the SHA-1 hash present in the first segment matches
  147. // the ShA-1 hash of the rest of the cookie value, since the hash should
  148. // have been set when the cookie was first created by the application.
  149. if ($segments[0] == static::hash($value))
  150. {
  151. return $value;
  152. }
  153. return null;
  154. }
  155. }