1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Control; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Configurable; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A set of static methods for manipulating cookies. |
10
|
|
|
*/ |
11
|
|
|
class Cookie |
12
|
|
|
{ |
13
|
|
|
use Configurable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @config |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private static $report_errors = true; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Fetch the current instance of the cookie backend. |
24
|
|
|
* |
25
|
|
|
* @return Cookie_Backend |
26
|
|
|
*/ |
27
|
|
|
public static function get_inst() |
28
|
|
|
{ |
29
|
|
|
return Injector::inst()->get('SilverStripe\\Control\\Cookie_Backend'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set a cookie variable. |
34
|
|
|
* |
35
|
|
|
* Expiry time is set in days, and defaults to 90. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @param mixed $value |
39
|
|
|
* @param int $expiry |
40
|
|
|
* @param string $path |
41
|
|
|
* @param string $domain |
42
|
|
|
* @param bool $secure |
43
|
|
|
* @param bool $httpOnly |
44
|
|
|
* |
45
|
|
|
* See http://php.net/set_session |
46
|
|
|
*/ |
47
|
|
|
public static function set( |
48
|
|
|
$name, |
49
|
|
|
$value, |
50
|
|
|
$expiry = 90, |
51
|
|
|
$path = null, |
52
|
|
|
$domain = null, |
53
|
|
|
$secure = false, |
54
|
|
|
$httpOnly = true |
55
|
|
|
) { |
56
|
|
|
return self::get_inst()->set($name, $value, $expiry, $path, $domain, $secure, $httpOnly); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the cookie value by name. Returns null if not set. |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @param bool $includeUnsent |
64
|
|
|
* |
65
|
|
|
* @return null|string |
66
|
|
|
*/ |
67
|
|
|
public static function get($name, $includeUnsent = true) |
68
|
|
|
{ |
69
|
|
|
return self::get_inst()->get($name, $includeUnsent); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get all the cookies. |
74
|
|
|
* |
75
|
|
|
* @param bool $includeUnsent |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public static function get_all($includeUnsent = true) |
80
|
|
|
{ |
81
|
|
|
return self::get_inst()->getAll($includeUnsent); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $name |
86
|
|
|
* @param null|string $path |
87
|
|
|
* @param null|string $domain |
88
|
|
|
* @param bool $secure |
89
|
|
|
* @param bool $httpOnly |
90
|
|
|
*/ |
91
|
|
|
public static function force_expiry($name, $path = null, $domain = null, $secure = false, $httpOnly = true) |
92
|
|
|
{ |
93
|
|
|
return self::get_inst()->forceExpiry($name, $path, $domain, $secure, $httpOnly); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|