/wee/http/weeCookies.class.php

https://github.com/extend/wee · PHP · 143 lines · 45 code · 19 blank · 79 comment · 9 complexity · 31d8b2347a72314470dca937b7690ad9 MD5 · raw file

  1. <?php
  2. /*
  3. Web:Extend
  4. Copyright (c) 2006-2010 Dev:Extend
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. if (!defined('ALLOW_INCLUSION')) die;
  18. /**
  19. Wrapper around the $_COOKIE array.
  20. Allows you to retrieve cookies from the array or to set and delete cookies.
  21. The class can be used as an array, with a few nuances when unsetting or setting cookies.
  22. */
  23. class weeCookies implements ArrayAccess
  24. {
  25. /**
  26. Path used when setting or deleting cookies.
  27. */
  28. protected $sCookiePath;
  29. /**
  30. Initialize the cookies class.
  31. An optional single parameter is allowed:
  32. * path: cookie path used when setting or deleting the cookies
  33. @param $aParams A list of parameters to configure the cookies class.
  34. */
  35. public function __construct($aParams = array())
  36. {
  37. if (!empty($aParams['path']))
  38. $this->sCookiePath = $aParams['path'];
  39. else {
  40. // Check if a custom cookie path was defined and use it
  41. $this->sCookiePath = parse_url(APP_PATH, PHP_URL_PATH);
  42. // TODO: this might be problematic on certain hosts...
  43. if ($this->sCookiePath == BASE_PATH || $this->sCookiePath == BASE_PATH . ROOT_PATH) {
  44. // Otherwise it's basically APP_PATH without the http://host part
  45. $this->sCookiePath = dirname($_SERVER['SCRIPT_NAME']);
  46. if ($this->sCookiePath != '/')
  47. $this->sCookiePath .= '/';
  48. }
  49. }
  50. }
  51. /**
  52. Return whether offset exists.
  53. @param $offset Offset name.
  54. @return bool Whether the offset exists.
  55. @see http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html
  56. */
  57. public function offsetExists($offset)
  58. {
  59. return isset($_COOKIE[$offset]);
  60. }
  61. /**
  62. Return value at given offset.
  63. @param $offset Offset name.
  64. @return mixed Value at given offset
  65. @see http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html
  66. */
  67. public function offsetGet($offset)
  68. {
  69. array_key_exists($offset, $_COOKIE) or burn('InvalidArgumentException',
  70. sprintf(_WT('The cookie named "%s" has not been received.'), $offset));
  71. return $_COOKIE[$offset];
  72. }
  73. /**
  74. Send a cookie to the browser.
  75. This aliases weeCookies::set with a default 3rd parameter.
  76. This does NOT add the value directly in the cookies array.
  77. The value is only accessible on the next request from this user.
  78. @param $offset Offset name.
  79. @param $value New value for this offset.
  80. @see http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html
  81. */
  82. public function offsetSet($offset, $value)
  83. {
  84. $this->set($offset, $value);
  85. }
  86. /**
  87. Delete the specified cookie.
  88. This does NOT remove the value directly from the cookies array.
  89. The value will be deleted on the next request from this user.
  90. @param $offset Offset name.
  91. @see http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html
  92. */
  93. public function offsetUnset($offset)
  94. {
  95. headers_sent() and burn('IllegalStateException',
  96. _WT('The HTTP headers have already been sent.'));
  97. setcookie($offset, '', 0, $this->sCookiePath);
  98. }
  99. /**
  100. Send a cookie to the browser.
  101. @param $sName Name of the cookie.
  102. @param $sValue Value of the cookie.
  103. @param $iExpire Expiration time (UNIX timestamp, in seconds).
  104. */
  105. public function set($sName, $sValue, $iExpire = 0)
  106. {
  107. headers_sent() and burn('IllegalStateException',
  108. _WT('The HTTP headers have already been sent.'));
  109. setcookie($sName, $sValue, $iExpire, $this->sCookiePath);
  110. }
  111. }