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