cookie.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 secure option is set to true, yet the request is not over HTTPS
  76. // we'll throw an exception to let the developer know that they are
  77. // attempting to send a secure cookie over the insecure HTTP.
  78. if ($secure and ! Request::secure())
  79. {
  80. throw new \Exception("Attempting to set secure cookie over HTTP.");
  81. }
  82. static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
  83. }
  84. /**
  85. * Set a "permanent" cookie. The cookie will last for one year.
  86. *
  87. * <code>
  88. * // Set a cookie that should last one year
  89. * Cookie::forever('favorite', 'Blue');
  90. * </code>
  91. *
  92. * @param string $name
  93. * @param string $value
  94. * @param string $path
  95. * @param string $domain
  96. * @param bool $secure
  97. * @return bool
  98. */
  99. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  100. {
  101. return static::put($name, $value, static::forever, $path, $domain, $secure);
  102. }
  103. /**
  104. * Delete a cookie.
  105. *
  106. * @param string $name
  107. * @param string $path
  108. * @param string $domain
  109. * @param bool $secure
  110. * @return bool
  111. */
  112. public static function forget($name, $path = '/', $domain = null, $secure = false)
  113. {
  114. return static::put($name, null, -2000, $path, $domain, $secure);
  115. }
  116. /**
  117. * Hash the given cookie value.
  118. *
  119. * @param string $value
  120. * @return string
  121. */
  122. public static function hash($value)
  123. {
  124. return hash_hmac('sha1', $value, Config::get('application.key'));
  125. }
  126. /**
  127. * Parse a hash fingerprinted cookie value.
  128. *
  129. * @param string $value
  130. * @return string
  131. */
  132. protected static function parse($value)
  133. {
  134. $segments = explode('+', $value);
  135. // First we will make sure the cookie actually has enough segments to even
  136. // be valid as being set by the application. If it does not we will go
  137. // ahead and throw exceptions now since there the cookie is invalid.
  138. if ( ! (count($segments) >= 2))
  139. {
  140. return null;
  141. }
  142. $value = implode('+', array_slice($segments, 1));
  143. // Now we will check if the SHA-1 hash present in the first segment matches
  144. // the ShA-1 hash of the rest of the cookie value, since the hash should
  145. // have been set when the cookie was first created by the application.
  146. if ($segments[0] == static::hash($value))
  147. {
  148. return $value;
  149. }
  150. return null;
  151. }
  152. }