Skip to content

Commit 2b721c3

Browse files
authored
Merge pull request #5 from dorantor/master
Added SameSite support for cookies.
2 parents 581c0df + f01c168 commit 2b721c3

File tree

3 files changed

+84
-59
lines changed

3 files changed

+84
-59
lines changed

src/PHPixie/HTTP/Builder.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ public function sapiSession()
137137

138138
/**
139139
* Build a single cookie update
140-
* @param $name
141-
* @param $value
142-
* @param null $expires
143-
* @param string $path
144-
* @param null $domain
145-
* @param bool $secure
146-
* @param bool $httpOnly
140+
* @param string $name
141+
* @param mixed $value
142+
* @param int|null $expires
143+
* @param string $path
144+
* @param string|null $domain
145+
* @param bool $secure
146+
* @param bool $httpOnly
147+
* @param string|null $sameSite
147148
* @return Context\Cookies\Update
148149
*/
149150
public function cookiesUpdate(
@@ -153,7 +154,8 @@ public function cookiesUpdate(
153154
$path = '/',
154155
$domain = null,
155156
$secure = false,
156-
$httpOnly = false
157+
$httpOnly = false,
158+
$sameSite = null
157159
)
158160
{
159161
return new Context\Cookies\Update(
@@ -163,8 +165,9 @@ public function cookiesUpdate(
163165
$path,
164166
$domain,
165167
$secure,
166-
$httpOnly
167-
);
168+
$httpOnly,
169+
$sameSite
170+
);
168171
}
169172

170173
/**
@@ -187,7 +190,7 @@ protected function instance($name)
187190
$method = 'build'.ucfirst($name);
188191
$this->instances[$name] = $this->$method();
189192
}
190-
193+
191194
return $this->instances[$name];
192195
}
193196

@@ -214,4 +217,4 @@ protected function buildOutput()
214217
{
215218
return new Output();
216219
}
217-
}
220+
}

src/PHPixie/HTTP/Context/Cookies.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function __construct($builder, $cookies = array())
4242
*/
4343
public function get($name, $default = null)
4444
{
45-
if($this->exists($name)) {
45+
if ($this->exists($name)) {
4646
return $this->cookies[$name];
4747
}
48-
48+
4949
return $default;
5050
}
5151

@@ -57,24 +57,25 @@ public function get($name, $default = null)
5757
*/
5858
public function getRequired($name)
5959
{
60-
if($this->exists($name)) {
60+
if ($this->exists($name)) {
6161
return $this->cookies[$name];
6262
}
63-
63+
6464
throw new \PHPixie\HTTP\Exception("Cookie '$name' is not set");
6565
}
6666

6767
/**
6868
* Set cookie
6969
*
7070
* See the PHP setcookie() function for more details
71-
* @param string $name
72-
* @param mixed $value
73-
* @param int|null $lifetime
74-
* @param string|null $path
71+
* @param string $name
72+
* @param mixed $value
73+
* @param int|null $lifetime
74+
* @param string $path
7575
* @param string|null $domain
76-
* @param boolean $secure
77-
* @param bool $httpOnly
76+
* @param bool $secure
77+
* @param bool $httpOnly
78+
* @param string|null $sameSite
7879
* @return void
7980
*/
8081
public function set(
@@ -84,31 +85,31 @@ public function set(
8485
$path = '/',
8586
$domain = null,
8687
$secure = false,
87-
$httpOnly = false
88+
$httpOnly = false,
89+
$sameSite = null
8890
)
8991
{
90-
if($lifetime !== null) {
92+
if ($lifetime !== null) {
9193
$expires = time() + $lifetime;
92-
93-
}else{
94+
} else {
9495
$expires = null;
9596
}
96-
97-
if($lifetime < 0) {
97+
98+
if ($lifetime < 0) {
9899
unset($this->cookies[$name]);
99-
100-
}else {
101-
$this->cookies[$name] = $value;
100+
} else {
101+
$this->cookies[$name] = $value;
102102
}
103-
103+
104104
$this->updates[$name] = $this->builder->cookiesUpdate(
105105
$name,
106106
$value,
107107
$expires,
108108
$path,
109109
$domain,
110110
$secure,
111-
$httpOnly
111+
$httpOnly,
112+
$sameSite
112113
);
113114
}
114115

@@ -149,4 +150,4 @@ public function asArray()
149150
{
150151
return $this->cookies;
151152
}
152-
}
153+
}

src/PHPixie/HTTP/Context/Cookies/Update.php

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,21 @@ class Update {
4242
*/
4343
protected $httpOnly;
4444

45+
/**
46+
* @var string|null
47+
*/
48+
protected $sameSite;
49+
4550
/**
4651
* Constructor
47-
* @param string $name
48-
* @param mixed $value
49-
* @param int|null $expires
50-
* @param string $path
52+
* @param string $name
53+
* @param mixed $value
54+
* @param int|null $expires
55+
* @param string $path
5156
* @param string|null $domain
52-
* @param bool $secure
53-
* @param bool $httpOnly
57+
* @param bool $secure
58+
* @param bool $httpOnly
59+
* @param string|null $sameSite
5460
*/
5561
public function __construct(
5662
$name,
@@ -59,7 +65,8 @@ public function __construct(
5965
$path = '/',
6066
$domain = null,
6167
$secure = false,
62-
$httpOnly = false
68+
$httpOnly = false,
69+
$sameSite = null
6370
)
6471
{
6572
$this->name = $name;
@@ -69,6 +76,7 @@ public function __construct(
6976
$this->domain = $domain;
7077
$this->secure = $secure;
7178
$this->httpOnly = $httpOnly;
79+
$this->sameSite = in_array(strtolower($sameSite), ['lax', 'strict', 'none']) ? $sameSite : null;
7280
}
7381

7482
/**
@@ -134,34 +142,47 @@ public function httpOnly()
134142
return $this->httpOnly;
135143
}
136144

145+
/**
146+
* Same site "flag"
147+
* @return null|string
148+
*/
149+
public function sameSite()
150+
{
151+
return $this->sameSite;
152+
}
153+
137154
/**
138155
* Get header representation
139156
* @return string
140157
*/
141158
public function asHeader()
142159
{
143-
$header = urlencode($this->name).'='.urlencode((string) $this->value);
144-
145-
if($this->domain !== null) {
146-
$header.= '; domain='.$this->domain;
160+
$header = urlencode($this->name) . '=' . urlencode((string) $this->value);
161+
162+
if ($this->domain !== null) {
163+
$header .= '; domain=' . $this->domain;
147164
}
148-
149-
if($this->path !== null) {
150-
$header.= '; path='.$this->path;
165+
166+
if ($this->path !== null) {
167+
$header .= '; path=' . $this->path;
151168
}
152-
153-
if($this->expires !== null) {
154-
$header.= '; expires=' . gmdate('D, d-M-Y H:i:s e', $this->expires);
169+
170+
if ($this->expires !== null) {
171+
$header .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $this->expires);
155172
}
156-
157-
if($this->secure) {
158-
$header.= '; secure';
173+
174+
if ($this->secure) {
175+
$header .= '; secure';
159176
}
160-
161-
if($this->httpOnly) {
162-
$header.= '; HttpOnly';
177+
178+
if ($this->httpOnly) {
179+
$header .= '; HttpOnly';
180+
}
181+
182+
if ($this->sameSite) {
183+
$header .= '; SameSite=' . $this->sameSite;
163184
}
164-
185+
165186
return $header;
166187
}
167-
}
188+
}

0 commit comments

Comments
 (0)