|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\http; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\contents\transformation\TransformerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Http Cookies utilities |
|
9
|
|
|
* Ubiquity\utils\http$UCookie |
|
10
|
|
|
* This class is part of Ubiquity |
|
11
|
|
|
* |
|
12
|
|
|
* @author jcheron <[email protected]> |
|
13
|
|
|
* @version 1.1.1 |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class UCookie { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
* @var TransformerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $transformer; |
|
23
|
|
|
public static $useTransformer = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sends a cookie |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $name the name of the cookie |
|
29
|
|
|
* @param string $value The value of the cookie. |
|
30
|
|
|
* @param int $duration default : 1 day |
|
31
|
|
|
* @param string $path default : / the cookie will be available within the entire domain |
|
32
|
|
|
* @param boolean $secure Indicates that the cookie should only be transmitted over asecure HTTPS |
|
33
|
|
|
* @param boolean $httpOnly When true the cookie will be made accessible only through the HTTPprotocol |
|
34
|
|
|
* @return boolean |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public static function set($name, $value, $duration = 60 * 60 * 24, $path = '/', $secure = false, $httpOnly = false): bool { |
|
37
|
3 |
|
if (self::$useTransformer && isset ( self::$transformer )) { |
|
38
|
|
|
$value = self::$transformer->transform ( $value ); |
|
39
|
|
|
} |
|
40
|
3 |
|
return \setcookie ( $name, $value, $duration ? (\time () + $duration) : null, $path, $secure, $httpOnly ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the Cookie with the name $name |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @param string $default |
|
48
|
|
|
* @return string|array|null |
|
49
|
|
|
*/ |
|
50
|
34 |
|
public static function get($name, $default = null) { |
|
51
|
34 |
|
$v = $_COOKIE [$name] ?? $default; |
|
52
|
34 |
|
if (self::$useTransformer && isset ( self::$transformer )) { |
|
53
|
|
|
return self::$transformer->reverse ( $v ); |
|
54
|
|
|
} |
|
55
|
34 |
|
return $v; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Removes the cookie with the name $name |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @param string $path |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public static function delete($name, $path = '/'): bool { |
|
65
|
2 |
|
if (isset ( $_COOKIE [$name] )) { |
|
66
|
1 |
|
unset ( $_COOKIE [$name] ); |
|
67
|
|
|
} |
|
68
|
2 |
|
return \setcookie ( $name, '', \time () - 3600, $path ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Deletes all cookies |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public static function deleteAll($path = '/'): void { |
|
75
|
1 |
|
foreach ( $_COOKIE as $name => $_ ) { |
|
76
|
|
|
self::delete ( $name, $path ); |
|
77
|
|
|
} |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Tests the existence of a cookie |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
* @since Ubiquity 2.0.11 |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function exists($name): bool { |
|
88
|
|
|
return isset ( $_COOKIE [$name] ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Sends a raw cookie without urlencoding the cookie value |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $name the name of the cookie |
|
95
|
|
|
* @param string $value The value of the cookie. |
|
96
|
|
|
* @param int $duration default : 1 day |
|
97
|
|
|
* @param string $path default : / the cookie will be available within the entire domain |
|
98
|
|
|
* @param boolean $secure Indicates that the cookie should only be transmitted over asecure HTTPS |
|
99
|
|
|
* @param boolean $httpOnly When true the cookie will be made accessible only through the HTTPprotocol |
|
100
|
|
|
* @return boolean |
|
101
|
|
|
* @since Ubiquity 2.0.11 |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function setRaw($name, $value, $duration = 60 * 60 * 24, $path = '/', $secure = false, $httpOnly = false): bool { |
|
104
|
|
|
if (self::$useTransformer && isset ( self::$transformer )) { |
|
105
|
|
|
$value = self::$transformer->transform ( $value ); |
|
106
|
|
|
} |
|
107
|
|
|
return \setrawcookie ( $name, $value, \time () + $duration, $path, $secure, $httpOnly ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static function setTransformer(TransformerInterface $transformer) { |
|
111
|
|
|
self::$transformer = $transformer; |
|
112
|
|
|
self::$useTransformer = true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public static function getTransformerClass(): ?string { |
|
116
|
|
|
if (isset ( self::$transformer )) { |
|
117
|
|
|
return \get_class ( self::$transformer ); |
|
118
|
|
|
} |
|
119
|
|
|
return null; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|