PageRenderTime 71ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/cookie.php

https://bitbucket.org/arkross/venus
PHP | 121 lines | 36 code | 13 blank | 72 comment | 0 complexity | d4c006aa8a2a93e0e228689c40648a47 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Cookie class
  15. *
  16. * @package Fuel
  17. * @category Helpers
  18. * @author Kohana Team
  19. * @modified Fuel Development Team
  20. * @copyright (c) 2008-2010 Kohana Team
  21. * @license http://kohanaframework.org/license
  22. * @link http://docs.fuelphp.com/classes/cookie.html
  23. */
  24. class Cookie
  25. {
  26. /**
  27. * @var array Cookie class configuration defaults
  28. */
  29. private static $config = array(
  30. 'expiration' => 0,
  31. 'path' => '/',
  32. 'domain' => null,
  33. 'secure' => false,
  34. 'http_only' => false,
  35. );
  36. /*
  37. * initialisation and auto configuration
  38. */
  39. public static function _init()
  40. {
  41. static::$config = array_merge(static::$config, \Config::get('cookie', array()));
  42. }
  43. /**
  44. * Gets the value of a signed cookie. Cookies without signatures will not
  45. * be returned. If the cookie signature is present, but invalid, the cookie
  46. * will be deleted.
  47. *
  48. * // Get the "theme" cookie, or use "blue" if the cookie does not exist
  49. * $theme = Cookie::get('theme', 'blue');
  50. *
  51. * @param string cookie name
  52. * @param mixed default value to return
  53. * @return string
  54. */
  55. public static function get($name, $default = null)
  56. {
  57. return \Input::cookie($name, $default);
  58. }
  59. /**
  60. * Sets a signed cookie. Note that all cookie values must be strings and no
  61. * automatic serialization will be performed!
  62. *
  63. * // Set the "theme" cookie
  64. * Cookie::set('theme', 'red');
  65. *
  66. * @param string name of cookie
  67. * @param string value of cookie
  68. * @param integer lifetime in seconds
  69. * @param string path of the cookie
  70. * @param string domain of the cookie
  71. * @param boolean if true, the cookie should only be transmitted over a secure HTTPS connection
  72. * @param boolean if true, the cookie will be made accessible only through the HTTP protocol
  73. * @return boolean
  74. */
  75. public static function set($name, $value, $expiration = null, $path = null, $domain = null, $secure = null, $http_only = null)
  76. {
  77. $value = \Fuel::value($value);
  78. // use the class defaults for the other parameters if not provided
  79. is_null($expiration) and $expiration = static::$config['expiration'];
  80. is_null($path) and $path = static::$config['path'];
  81. is_null($domain) and $domain = static::$config['domain'];
  82. is_null($secure) and $secure = static::$config['secure'];
  83. is_null($http_only) and $http_only = static::$config['http_only'];
  84. // add the current time so we have an offset
  85. $expiration = $expiration > 0 ? $expiration + time() : 0;
  86. return setcookie($name, $value, $expiration, $path, $domain, $secure, $http_only);
  87. }
  88. /**
  89. * Deletes a cookie by making the value null and expiring it.
  90. *
  91. * Cookie::delete('theme');
  92. *
  93. * @param string cookie name
  94. * @param string path of the cookie
  95. * @param string domain of the cookie
  96. * @param boolean if true, the cookie should only be transmitted over a secure HTTPS connection
  97. * @param boolean if true, the cookie will be made accessible only through the HTTP protocol
  98. * @return boolean
  99. * @uses static::set
  100. */
  101. public static function delete($name, $path = null, $domain = null, $secure = null, $http_only = null)
  102. {
  103. // Remove the cookie
  104. unset($_COOKIE[$name]);
  105. // Nullify the cookie and make it expire
  106. return static::set($name, null, -86400, $path, $domain, $secure, $http_only);
  107. }
  108. }