2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* A set of static methods for manipulating cookies.
|
2011-12-17 02:21:09 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage misc
|
|
|
|
*/
|
2010-05-25 06:19:57 +02:00
|
|
|
class Cookie {
|
2012-06-29 18:32:47 +02:00
|
|
|
|
2011-12-17 02:21:09 +01:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
static $report_errors = true;
|
2012-06-29 18:32:47 +02:00
|
|
|
|
|
|
|
/**
|
2012-07-10 10:06:05 +02:00
|
|
|
* @var string cookie class
|
2012-06-29 18:32:47 +02:00
|
|
|
*/
|
|
|
|
static $cookie_class = 'Cookie';
|
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
private static $inst = null;
|
2012-06-29 18:32:47 +02:00
|
|
|
|
|
|
|
public static function get_inst() {
|
|
|
|
if(is_null(self::$inst)) {
|
|
|
|
self::$inst = new self::$cookie_class();
|
|
|
|
}
|
|
|
|
return self::$inst;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set a cookie variable
|
2012-06-29 18:32:47 +02:00
|
|
|
*
|
2009-09-10 05:22:50 +02:00
|
|
|
* @param string $name The variable name
|
2012-07-10 10:06:05 +02:00
|
|
|
* @param mixed $value The variable value.
|
2011-12-17 02:21:09 +01:00
|
|
|
* @param int $expiry The expiry time, in days. Defaults to 90.
|
2009-09-10 05:22:50 +02:00
|
|
|
* @param string $path See http://php.net/set_session
|
|
|
|
* @param string $domain See http://php.net/set_session
|
|
|
|
* @param boolean $secure See http://php.net/set_session
|
2012-03-31 09:08:54 +02:00
|
|
|
* @param boolean $httpOnly See http://php.net/set_session
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-03 16:16:19 +02:00
|
|
|
public static function set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false,
|
|
|
|
$httpOnly = false
|
|
|
|
) {
|
2012-06-29 18:32:47 +02:00
|
|
|
return self::get_inst()->inst_set($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-10 10:06:05 +02:00
|
|
|
* Get a cookie variable.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return mixed
|
2012-06-29 18:32:47 +02:00
|
|
|
*/
|
2012-07-10 10:06:05 +02:00
|
|
|
public static function get($name) {
|
2012-06-29 18:32:47 +02:00
|
|
|
return self::get_inst()->inst_get($name);
|
|
|
|
}
|
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
* @param string
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
public static function forceExpiry($name, $path = null, $domain = null) {
|
|
|
|
Deprecation::notice('3.1', 'Use Cookie::force_expiry instead.');
|
|
|
|
|
|
|
|
return self::force_expiry($name, $path, $domain);
|
2012-06-29 18:32:47 +02:00
|
|
|
}
|
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
* @param string
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
public static function force_expiry($name, $path = null, $domain = null) {
|
|
|
|
return self::get_inst()->inst_force_expiry($name, $path, $domain);
|
2012-06-29 18:32:47 +02:00
|
|
|
}
|
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @param bool
|
|
|
|
*/
|
|
|
|
public static function set_report_errors($reportErrors) {
|
|
|
|
self::get_inst()->inst_set_report_errors($reportErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function report_errors() {
|
2012-06-29 18:32:47 +02:00
|
|
|
return self::get_inst()->inst_report_errors();
|
|
|
|
}
|
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* Set a cookie variable
|
|
|
|
*
|
|
|
|
* @param string $name The variable name
|
|
|
|
* @param mixed $value The variable value.
|
|
|
|
* @param int $expiry The expiry time, in days. Defaults to 90.
|
|
|
|
* @param string $path See http://php.net/set_session
|
|
|
|
* @param string $domain See http://php.net/set_session
|
|
|
|
* @param boolean $secure See http://php.net/set_session
|
|
|
|
* @param boolean $httpOnly See http://php.net/set_session
|
|
|
|
*/
|
2012-06-29 18:42:14 +02:00
|
|
|
protected function inst_set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!headers_sent($file, $line)) {
|
2011-12-17 02:21:09 +01:00
|
|
|
$expiry = $expiry > 0 ? time()+(86400*$expiry) : $expiry;
|
2009-09-10 05:22:50 +02:00
|
|
|
$path = ($path) ? $path : Director::baseURL();
|
2012-03-31 09:08:54 +02:00
|
|
|
setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2012-03-31 09:08:54 +02:00
|
|
|
if(self::$report_errors) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Cookie '$name' can't be set. The site started outputting content at line $line in $file",
|
|
|
|
E_USER_WARNING);
|
2012-03-31 09:08:54 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-29 18:32:47 +02:00
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-06-29 18:42:14 +02:00
|
|
|
protected function inst_get($name) {
|
2012-06-29 18:32:47 +02:00
|
|
|
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
|
|
|
|
}
|
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
protected function inst_force_expiry($name, $path = null, $domain = null) {
|
2008-04-26 08:31:36 +02:00
|
|
|
if(!headers_sent($file, $line)) {
|
2011-12-17 02:21:09 +01:00
|
|
|
self::set($name, null, -20, $path, $domain);
|
2008-04-26 08:31:36 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-06-29 18:32:47 +02:00
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @param bool
|
|
|
|
*/
|
2012-06-29 18:42:14 +02:00
|
|
|
protected function inst_set_report_errors($reportErrors) {
|
2008-11-22 04:33:00 +01:00
|
|
|
self::$report_errors = $reportErrors;
|
|
|
|
}
|
2012-06-29 18:32:47 +02:00
|
|
|
|
2012-07-10 10:06:05 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function inst_report_errors() {
|
2008-11-22 04:33:00 +01:00
|
|
|
return self::$report_errors;
|
|
|
|
}
|
2012-02-16 02:59:56 +01:00
|
|
|
}
|