Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 266 lines (235 sloc) 7.552 kb
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
4953471 Fabien Potencier replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
2d3bf28 blue-eyes normalized license messages in PHP files
blue-eyes authored
12 namespace Symfony\Component\BrowserKit;
13
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
14 /**
15 * CookieJar.
16 *
4953471 Fabien Potencier replaced symfony-project.org by symfony.com
fabpot authored
17 * @author Fabien Potencier <fabien@symfony.com>
9839e64 Fabien Potencier [BrowserKit] tagged the guaranteed BC API
fabpot authored
18 *
19 * @api
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
20 */
21 class CookieJar
22 {
23 protected $cookieJar = array();
24
25 /**
26 * Sets a cookie.
27 *
28 * @param Cookie $cookie A Cookie instance
9839e64 Fabien Potencier [BrowserKit] tagged the guaranteed BC API
fabpot authored
29 *
30 * @api
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
31 */
32 public function set(Cookie $cookie)
33 {
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
34 $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
35 }
36
37 /**
38 * Gets a cookie by name.
39 *
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
40 * You should never use an empty domain, but if you do so,
41 * this method returns the first cookie for the given name/path
42 * (this behavior ensures a BC behavior with previous versions of
43 * Symfony).
44 *
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
45 * @param string $name The cookie name
46 * @param string $path The cookie path
47 * @param string $domain The cookie domain
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
48 *
49 * @return Cookie|null A Cookie instance or null if the cookie does not exist
9839e64 Fabien Potencier [BrowserKit] tagged the guaranteed BC API
fabpot authored
50 *
51 * @api
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
52 */
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
53 public function get($name, $path = '/', $domain = null)
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
54 {
55 $this->flushExpiredCookies();
56
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
57 if (!empty($domain)) {
155f40c Jerzy Zawadzki [BrowserKit] fixes #8311 CookieJar is totally ignorant of RFC 6265 edge ...
jzawadzki authored
58 foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
59 if ($cookieDomain) {
60 $cookieDomain = '.'.ltrim($cookieDomain, '.');
61 if ($cookieDomain != substr('.'.$domain, -strlen($cookieDomain))) {
62 continue;
63 }
64 }
65
66 foreach ($pathCookies as $cookiePath => $namedCookies) {
67 if ($cookiePath != substr($path, 0, strlen($cookiePath))) {
68 continue;
69 }
70 if (isset($namedCookies[$name])) {
71 return $namedCookies[$name];
72 }
73 }
74 }
75
9175571 Fabien Potencier unified return null usages
fabpot authored
76 return;
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
77 }
78
79 // avoid relying on this behavior that is mainly here for BC reasons
1d2e681 Eduardo Oliveira CookieJar remove unneeded var, Client remove unneeded else
entering authored
80 foreach ($this->cookieJar as $cookies) {
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
81 if (isset($cookies[$path][$name])) {
82 return $cookies[$path][$name];
83 }
84 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
85 }
86
87 /**
88 * Removes a cookie by name.
89 *
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
90 * You should never use an empty domain, but if you do so,
91 * all cookies for the given name/path expire (this behavior
92 * ensures a BC behavior with previous versions of Symfony).
93 *
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
94 * @param string $name The cookie name
95 * @param string $path The cookie path
96 * @param string $domain The cookie domain
9839e64 Fabien Potencier [BrowserKit] tagged the guaranteed BC API
fabpot authored
97 *
98 * @api
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
99 */
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
100 public function expire($name, $path = '/', $domain = null)
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
101 {
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
102 if (null === $path) {
103 $path = '/';
104 }
105
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
106 if (empty($domain)) {
107 // an empty domain means any domain
108 // this should never happen but it allows for a better BC
109 $domains = array_keys($this->cookieJar);
110 } else {
111 $domains = array($domain);
112 }
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
113
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
114 foreach ($domains as $domain) {
115 unset($this->cookieJar[$domain][$path][$name]);
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
116
0bb8f07 Fabien Potencier [BrowserKit] fixed BC break done recently
fabpot authored
117 if (empty($this->cookieJar[$domain][$path])) {
118 unset($this->cookieJar[$domain][$path]);
119
120 if (empty($this->cookieJar[$domain])) {
121 unset($this->cookieJar[$domain]);
122 }
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
123 }
124 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
125 }
126
127 /**
128 * Removes all the cookies from the jar.
9839e64 Fabien Potencier [BrowserKit] tagged the guaranteed BC API
fabpot authored
129 *
130 * @api
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
131 */
132 public function clear()
133 {
134 $this->cookieJar = array();
135 }
136
137 /**
e33fef5 Jérôme Tamarelle [BrowserKit] Remove dependency of CookieJar to Response
GromNaN authored
138 * Updates the cookie jar from a response Set-Cookie headers.
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
139 *
6ab1b5f Fabien Potencier fixed phpdoc @param alignment
fabpot authored
140 * @param array $setCookies Set-Cookie headers from an HTTP response
141 * @param string $uri The base URL
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
142 */
e33fef5 Jérôme Tamarelle [BrowserKit] Remove dependency of CookieJar to Response
GromNaN authored
143 public function updateFromSetCookie(array $setCookies, $uri = null)
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
144 {
acd299e Jakub Zalas [BrowserKit] Fixed CookieJar issue being unable to parse multiple cookie...
jakzal authored
145 $cookies = array();
146
e33fef5 Jérôme Tamarelle [BrowserKit] Remove dependency of CookieJar to Response
GromNaN authored
147 foreach ($setCookies as $cookie) {
acd299e Jakub Zalas [BrowserKit] Fixed CookieJar issue being unable to parse multiple cookie...
jakzal authored
148 foreach (explode(',', $cookie) as $i => $part) {
f03173e Jakub Zalas [BrowserKit] Fixed cs.
jakzal authored
149 if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
acd299e Jakub Zalas [BrowserKit] Fixed CookieJar issue being unable to parse multiple cookie...
jakzal authored
150 $cookies[] = ltrim($part);
151 } else {
f03173e Jakub Zalas [BrowserKit] Fixed cs.
jakzal authored
152 $cookies[count($cookies) - 1] .= ','.$part;
acd299e Jakub Zalas [BrowserKit] Fixed CookieJar issue being unable to parse multiple cookie...
jakzal authored
153 }
154 }
155 }
156
157 foreach ($cookies as $cookie) {
f3a6dff Jean-François Simon [BrowserKit] added ability to ignored malformed set-cookie header
jfsimon authored
158 try {
159 $this->set(Cookie::fromString($cookie, $uri));
160 } catch (\InvalidArgumentException $e) {
161 // invalid cookies are just ignored
162 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
163 }
164 }
165
166 /**
e33fef5 Jérôme Tamarelle [BrowserKit] Remove dependency of CookieJar to Response
GromNaN authored
167 * Updates the cookie jar from a Response object.
168 *
169 * @param Response $response A Response object
6ab1b5f Fabien Potencier fixed phpdoc @param alignment
fabpot authored
170 * @param string $uri The base URL
e33fef5 Jérôme Tamarelle [BrowserKit] Remove dependency of CookieJar to Response
GromNaN authored
171 */
172 public function updateFromResponse(Response $response, $uri = null)
173 {
174 $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
175 }
176
177 /**
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
178 * Returns not yet expired cookies.
179 *
c6799a2 Thomas Lallement CS Fixes - Replaced "array of type" by "Type[]" in PHPDoc block
raziel057 authored
180 * @return Cookie[] An array of cookies
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
181 */
182 public function all()
183 {
184 $this->flushExpiredCookies();
185
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
186 $flattenedCookies = array();
187 foreach ($this->cookieJar as $path) {
188 foreach ($path as $cookies) {
189 foreach ($cookies as $cookie) {
190 $flattenedCookies[] = $cookie;
191 }
192 }
193 }
194
195 return $flattenedCookies;
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
196 }
197
198 /**
199 * Returns not yet expired cookie values for the given URI.
200 *
de4f2ed Graham Campbell Docblock fixes
GrahamCampbell authored
201 * @param string $uri A URI
202 * @param bool $returnsRawValue Returns raw value or urldecoded value
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
203 *
204 * @return array An array of cookie values
205 */
b7e5b01 hnw [BrowserKit] dealing with raw value for cookies
hnw authored
206 public function allValues($uri, $returnsRawValue = false)
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
207 {
208 $this->flushExpiredCookies();
209
b7deffa Fabien Potencier [BrowserKit] fixed cookie management (see RFC 2109)
fabpot authored
210 $parts = array_replace(array('path' => '/'), parse_url($uri));
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
211 $cookies = array();
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
212 foreach ($this->cookieJar as $domain => $pathCookies) {
213 if ($domain) {
c31e650 Povilas Skruibis bugix: CookieJar returns cookies with domain "domain.com" for domain "fo...
povilas authored
214 $domain = '.'.ltrim($domain, '.');
215 if ($domain != substr('.'.$parts['host'], -strlen($domain))) {
b7deffa Fabien Potencier [BrowserKit] fixed cookie management (see RFC 2109)
fabpot authored
216 continue;
217 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
218 }
219
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
220 foreach ($pathCookies as $path => $namedCookies) {
221 if ($path != substr($parts['path'], 0, strlen($path))) {
222 continue;
223 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
224
4dde6de Hugo Hamon [BrowserKit] removed unused $name variable in CookieJar::allValues() met...
hhamon authored
225 foreach ($namedCookies as $cookie) {
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
226 if ($cookie->isSecure() && 'https' != $parts['scheme']) {
227 continue;
228 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
229
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
230 $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
231 }
232 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
233 }
234
235 return $cookies;
236 }
237
238 /**
b7e5b01 hnw [BrowserKit] dealing with raw value for cookies
hnw authored
239 * Returns not yet expired raw cookie values for the given URI.
240 *
241 * @param string $uri A URI
242 *
243 * @return array An array of cookie values
244 */
a6dc07c Fabien Potencier [BrowserKit] fixed CS
fabpot authored
245 public function allRawValues($uri)
246 {
b7e5b01 hnw [BrowserKit] dealing with raw value for cookies
hnw authored
247 return $this->allValues($uri, true);
248 }
249
250 /**
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
251 * Removes all expired cookies.
252 */
253 public function flushExpiredCookies()
254 {
32e60ff Fabien Potencier [BrowserKit] first attempt at fixing CookieJar (closes #2209)
fabpot authored
255 foreach ($this->cookieJar as $domain => $pathCookies) {
256 foreach ($pathCookies as $path => $namedCookies) {
257 foreach ($namedCookies as $name => $cookie) {
258 if ($cookie->isExpired()) {
259 unset($this->cookieJar[$domain][$path][$name]);
260 }
261 }
72d921b Fabien Potencier renamed Symfony\Components to Symfony\Component
fabpot authored
262 }
263 }
264 }
265 }
Something went wrong with that request. Please try again.