|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CookieSettings.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license https://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec <[email protected]> |
|
8
|
|
|
* @package iPublikuj:MobileDetect! |
|
9
|
|
|
* @subpackage Helpers |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 23.04.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\MobileDetect\Helpers; |
|
18
|
|
|
|
|
19
|
|
|
use Nette; |
|
20
|
|
|
|
|
21
|
|
|
use IPub\MobileDetect\Exceptions; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Cookies creator helper |
|
25
|
|
|
* |
|
26
|
|
|
* @package iPublikuj:MobileDetect! |
|
27
|
|
|
* @subpackage Helpers |
|
28
|
|
|
* |
|
29
|
|
|
* @author Adam Kadlec <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
1 |
|
final class CookieSettings |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Implement nette smart magic |
|
35
|
|
|
*/ |
|
36
|
1 |
|
use Nette\SmartObject; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $name; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string|NULL |
|
45
|
|
|
*/ |
|
46
|
|
|
private $domain; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $expire; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $path; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var bool |
|
60
|
|
|
*/ |
|
61
|
|
|
private $secure; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var bool |
|
65
|
|
|
*/ |
|
66
|
|
|
private $httpOnly; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $name The name of the cookie |
|
70
|
|
|
* @param string $expireAfter The time the cookie expires |
|
71
|
|
|
* @param string $path The path on the server in which the cookie will be available on |
|
72
|
|
|
* @param string $domain The domain that the cookie is available to |
|
73
|
|
|
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client |
|
74
|
|
|
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol |
|
75
|
|
|
* |
|
76
|
|
|
* @throws Exceptions\InvalidArgumentException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct(string $name, ?string $domain = null, ?string $expireAfter = null, string $path = '/', bool $secure = false, bool $httpOnly = true) |
|
79
|
|
|
{ |
|
80
|
|
|
// from PHP source code |
|
81
|
1 |
|
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) { |
|
82
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
if (empty($name)) { |
|
86
|
|
|
throw new Exceptions\InvalidArgumentException('The cookie name cannot be empty.'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
$expire = new \DateTime; |
|
90
|
|
|
|
|
91
|
1 |
|
if ($expireAfter !== null) { |
|
92
|
1 |
|
$expire->modify($expireAfter); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$this->name = $name; |
|
96
|
1 |
|
$this->domain = $domain; |
|
97
|
1 |
|
$this->expire = (int) $expire->format('U'); |
|
|
|
|
|
|
98
|
1 |
|
$this->path = empty($path) ? '/' : $path; |
|
99
|
1 |
|
$this->secure = $secure; |
|
100
|
1 |
|
$this->httpOnly = $httpOnly; |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Gets the name of the cookie |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getName() : string |
|
109
|
|
|
{ |
|
110
|
1 |
|
return $this->name; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets the domain that the cookie is available to |
|
115
|
|
|
* |
|
116
|
|
|
* @return string|NULL |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getDomain() : ?string |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->domain; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Gets the time the cookie expires |
|
125
|
|
|
* |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getExpiresTime() : int |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->expire; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the path on the server in which the cookie will be available on |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getPath() : string |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->path; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function isSecure() : bool |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->secure; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Checks whether the cookie will be made accessible only through the HTTP protocol |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
|
|
public function isHttpOnly() : bool |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->httpOnly; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.