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