/system/helpers/cookie.php

https://github.com/dscassel/friendfeedarchiver · PHP · 88 lines · 33 code · 10 blank · 45 comment · 4 complexity · 0aea55bf7eea381973d9311fd90cf3e0 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Cookie helper class.
  4. *
  5. * $Id: cookie.php 1970 2008-02-06 21:54:29Z Shadowhand $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class cookie_Core {
  13. /**
  14. * Sets a cookie with the given parameters.
  15. *
  16. * @param string cookie name or array of config options
  17. * @param string cookie value
  18. * @param integer number of seconds before the cookie expires
  19. * @param string URL path to allow
  20. * @param string URL domain to allow
  21. * @param boolean HTTPS only
  22. * @param boolean HTTP only (requires PHP 5.2 or higher)
  23. * @param string collision-prevention prefix
  24. * @return boolean
  25. */
  26. public static function set($name, $value = NULL, $expire = NULL, $path = NULL, $domain = NULL, $secure = NULL, $httponly = NULL, $prefix = NULL)
  27. {
  28. if (headers_sent())
  29. return FALSE;
  30. // If the name param is an array, we import it
  31. is_array($name) and extract($name, EXTR_OVERWRITE);
  32. // Fetch default options
  33. $config = Config::item('cookie');
  34. foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly') as $item)
  35. {
  36. if ($$item === NULL AND isset($config[$item]))
  37. {
  38. $$item = $config[$item];
  39. }
  40. }
  41. // Expiration timestamp
  42. $expire = ($expire == 0) ? 0 : time() + (int) $expire;
  43. // Only set httponly if possible
  44. return (version_compare(PHP_VERSION, '5.2', '>='))
  45. ? setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly)
  46. : setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
  47. }
  48. /**
  49. * Fetch a cookie value, using the Input library.
  50. *
  51. * @param string cookie name
  52. * @param string collision-prevention prefix
  53. * @param boolean use XSS cleaning on the value
  54. * @return string
  55. */
  56. public static function get($name, $prefix = NULL, $xss_clean = FALSE)
  57. {
  58. if ($prefix === NULL)
  59. {
  60. $prefix = (string) Config::item('cookie.prefix');
  61. }
  62. return Input::instance()->cookie($prefix.$name, $xss_clean);
  63. }
  64. /**
  65. * Nullify and unset a cookie.
  66. *
  67. * @param string cookie name
  68. * @param string URL path
  69. * @param string URL domain
  70. * @param string collision-prevention prefix
  71. * @return boolean
  72. */
  73. public static function delete($name, $path = NULL, $domain = NULL, $prefix = NULL)
  74. {
  75. // Sets the cookie value to an empty string, and the expiration to 24 hours ago
  76. return cookie::set($name, '', -86400, $path, $domain, FALSE, FALSE, $prefix);
  77. }
  78. } // End cookie