PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/cookie.php

https://bitbucket.org/rlm3/mrs
PHP | 161 lines | 55 code | 23 blank | 83 comment | 6 complexity | 597aab2d44c9764344eb4f90582d5951 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Cookie helper.
  4. *
  5. * @package Kohana
  6. * @category Helpers
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2011 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_Cookie {
  12. /**
  13. * @var string Magic salt to add to the cookie
  14. */
  15. public static $salt = NULL;
  16. /**
  17. * @var integer Number of seconds before the cookie expires
  18. */
  19. public static $expiration = 0;
  20. /**
  21. * @var string Restrict the path that the cookie is available to
  22. */
  23. public static $path = '/';
  24. /**
  25. * @var string Restrict the domain that the cookie is available to
  26. */
  27. public static $domain = NULL;
  28. /**
  29. * @var boolean Only transmit cookies over secure connections
  30. */
  31. public static $secure = FALSE;
  32. /**
  33. * @var boolean Only transmit cookies over HTTP, disabling Javascript access
  34. */
  35. public static $httponly = FALSE;
  36. /**
  37. * Gets the value of a signed cookie. Cookies without signatures will not
  38. * be returned. If the cookie signature is present, but invalid, the cookie
  39. * will be deleted.
  40. *
  41. * // Get the "theme" cookie, or use "blue" if the cookie does not exist
  42. * $theme = Cookie::get('theme', 'blue');
  43. *
  44. * @param string cookie name
  45. * @param mixed default value to return
  46. * @return string
  47. */
  48. public static function get($key, $default = NULL)
  49. {
  50. if ( ! isset($_COOKIE[$key]))
  51. {
  52. // The cookie does not exist
  53. return $default;
  54. }
  55. // Get the cookie value
  56. $cookie = $_COOKIE[$key];
  57. // Find the position of the split between salt and contents
  58. $split = strlen(Cookie::salt($key, NULL));
  59. if (isset($cookie[$split]) AND $cookie[$split] === '~')
  60. {
  61. // Separate the salt and the value
  62. list ($hash, $value) = explode('~', $cookie, 2);
  63. if (Cookie::salt($key, $value) === $hash)
  64. {
  65. // Cookie signature is valid
  66. return $value;
  67. }
  68. // The cookie signature is invalid, delete it
  69. Cookie::delete($key);
  70. }
  71. return $default;
  72. }
  73. /**
  74. * Sets a signed cookie. Note that all cookie values must be strings and no
  75. * automatic serialization will be performed!
  76. *
  77. * // Set the "theme" cookie
  78. * Cookie::set('theme', 'red');
  79. *
  80. * @param string name of cookie
  81. * @param string value of cookie
  82. * @param integer lifetime in seconds
  83. * @return boolean
  84. * @uses Cookie::salt
  85. */
  86. public static function set($name, $value, $expiration = NULL)
  87. {
  88. if ($expiration === NULL)
  89. {
  90. // Use the default expiration
  91. $expiration = Cookie::$expiration;
  92. }
  93. if ($expiration !== 0)
  94. {
  95. // The expiration is expected to be a UNIX timestamp
  96. $expiration += time();
  97. }
  98. // Add the salt to the cookie value
  99. $value = Cookie::salt($name, $value).'~'.$value;
  100. return setcookie($name, $value, $expiration, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
  101. }
  102. /**
  103. * Deletes a cookie by making the value NULL and expiring it.
  104. *
  105. * Cookie::delete('theme');
  106. *
  107. * @param string cookie name
  108. * @return boolean
  109. * @uses Cookie::set
  110. */
  111. public static function delete($name)
  112. {
  113. // Remove the cookie
  114. unset($_COOKIE[$name]);
  115. // Nullify the cookie and make it expire
  116. return setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
  117. }
  118. /**
  119. * Generates a salt string for a cookie based on the name and value.
  120. *
  121. * $salt = Cookie::salt('theme', 'red');
  122. *
  123. * @param string name of cookie
  124. * @param string value of cookie
  125. * @return string
  126. */
  127. public static function salt($name, $value)
  128. {
  129. // Require a valid salt
  130. if ( ! Cookie::$salt)
  131. {
  132. throw new Kohana_Exception('A valid cookie salt is required. Please set Cookie::$salt.');
  133. }
  134. // Determine the user agent
  135. $agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';
  136. return sha1($agent.$name.$value.Cookie::$salt);
  137. }
  138. } // End cookie