SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Cookie.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use Kohana;
  19.  
  20. /**
  21.  * Eases reading and writing cookies.
  22.  */
  23. class Cookie
  24. {
  25.  
  26. /**
  27.   * Sets a cookie with the given parameters.
  28.   *
  29.   * @param string cookie name
  30.   * @param string cookie value
  31.   * @param integer number of seconds before the cookie expires
  32.   * @param string URL path to allow
  33.   * @param string URL domain to allow
  34.   * @param boolean HTTPS only
  35.   * @param boolean HTTP only (requires PHP 5.2 or higher)
  36.   * @return boolean
  37.   */
  38. public static function set($name, $value = NULL, $expire = NULL, $path = NULL, $domain = NULL, $secure = NULL, $httponly = NULL)
  39. {
  40. if (headers_sent())
  41. return FALSE;
  42.  
  43. // Fetch default options
  44. $config = Kohana::config('cookie');
  45.  
  46. foreach (array('value', 'expire', 'domain', 'path', 'secure', 'httponly') as $item)
  47. {
  48. if ($$item === NULL AND isset($config[$item]))
  49. {
  50. $$item = $config[$item];
  51. }
  52. }
  53.  
  54. // Expiration timestamp
  55. $expire = ($expire == 0) ? 0 : time() + (int) $expire;
  56.  
  57. return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
  58. }
  59.  
  60. /**
  61.   * Fetch a cookie value
  62.   *
  63.   * @param string cookie name
  64.   * @param mixed default value
  65.   * @return string
  66.   */
  67. public static function get($name, $default = NULL)
  68. {
  69. return (isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default);
  70. }
  71.  
  72. /**
  73.   * Nullify and unset a cookie.
  74.   *
  75.   * @param string cookie name
  76.   * @param string URL path
  77.   * @param string URL domain
  78.   * @return boolean
  79.   */
  80. public static function delete($name, $path = NULL, $domain = NULL)
  81. {
  82. if ( ! isset($_COOKIE[$name]))
  83. return FALSE;
  84.  
  85. // Delete the cookie from globals
  86. unset($_COOKIE[$name]);
  87.  
  88. // Sets the cookie value to an empty string, and the expiration to 24 hours ago
  89. return Cookie::set($name, '', -86400, $path, $domain, FALSE, FALSE);
  90. }
  91.  
  92. } // End cookie
  93.