Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / browser-kit / CookieJar.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\BrowserKit;
13
14 /**
15  * CookieJar.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class CookieJar
20 {
21     protected $cookieJar = array();
22
23     public function set(Cookie $cookie)
24     {
25         $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
26     }
27
28     /**
29      * Gets a cookie by name.
30      *
31      * You should never use an empty domain, but if you do so,
32      * this method returns the first cookie for the given name/path
33      * (this behavior ensures a BC behavior with previous versions of
34      * Symfony).
35      *
36      * @param string $name   The cookie name
37      * @param string $path   The cookie path
38      * @param string $domain The cookie domain
39      *
40      * @return Cookie|null A Cookie instance or null if the cookie does not exist
41      */
42     public function get($name, $path = '/', $domain = null)
43     {
44         $this->flushExpiredCookies();
45
46         foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
47             if ($cookieDomain && $domain) {
48                 $cookieDomain = '.'.ltrim($cookieDomain, '.');
49                 if ($cookieDomain !== substr('.'.$domain, -\strlen($cookieDomain))) {
50                     continue;
51                 }
52             }
53
54             foreach ($pathCookies as $cookiePath => $namedCookies) {
55                 if (0 !== strpos($path, $cookiePath)) {
56                     continue;
57                 }
58                 if (isset($namedCookies[$name])) {
59                     return $namedCookies[$name];
60                 }
61             }
62         }
63     }
64
65     /**
66      * Removes a cookie by name.
67      *
68      * You should never use an empty domain, but if you do so,
69      * all cookies for the given name/path expire (this behavior
70      * ensures a BC behavior with previous versions of Symfony).
71      *
72      * @param string $name   The cookie name
73      * @param string $path   The cookie path
74      * @param string $domain The cookie domain
75      */
76     public function expire($name, $path = '/', $domain = null)
77     {
78         if (null === $path) {
79             $path = '/';
80         }
81
82         if (empty($domain)) {
83             // an empty domain means any domain
84             // this should never happen but it allows for a better BC
85             $domains = array_keys($this->cookieJar);
86         } else {
87             $domains = array($domain);
88         }
89
90         foreach ($domains as $domain) {
91             unset($this->cookieJar[$domain][$path][$name]);
92
93             if (empty($this->cookieJar[$domain][$path])) {
94                 unset($this->cookieJar[$domain][$path]);
95
96                 if (empty($this->cookieJar[$domain])) {
97                     unset($this->cookieJar[$domain]);
98                 }
99             }
100         }
101     }
102
103     /**
104      * Removes all the cookies from the jar.
105      */
106     public function clear()
107     {
108         $this->cookieJar = array();
109     }
110
111     /**
112      * Updates the cookie jar from a response Set-Cookie headers.
113      *
114      * @param array  $setCookies Set-Cookie headers from an HTTP response
115      * @param string $uri        The base URL
116      */
117     public function updateFromSetCookie(array $setCookies, $uri = null)
118     {
119         $cookies = array();
120
121         foreach ($setCookies as $cookie) {
122             foreach (explode(',', $cookie) as $i => $part) {
123                 if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
124                     $cookies[] = ltrim($part);
125                 } else {
126                     $cookies[count($cookies) - 1] .= ','.$part;
127                 }
128             }
129         }
130
131         foreach ($cookies as $cookie) {
132             try {
133                 $this->set(Cookie::fromString($cookie, $uri));
134             } catch (\InvalidArgumentException $e) {
135                 // invalid cookies are just ignored
136             }
137         }
138     }
139
140     /**
141      * Updates the cookie jar from a Response object.
142      *
143      * @param Response $response A Response object
144      * @param string   $uri      The base URL
145      */
146     public function updateFromResponse(Response $response, $uri = null)
147     {
148         $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
149     }
150
151     /**
152      * Returns not yet expired cookies.
153      *
154      * @return Cookie[] An array of cookies
155      */
156     public function all()
157     {
158         $this->flushExpiredCookies();
159
160         $flattenedCookies = array();
161         foreach ($this->cookieJar as $path) {
162             foreach ($path as $cookies) {
163                 foreach ($cookies as $cookie) {
164                     $flattenedCookies[] = $cookie;
165                 }
166             }
167         }
168
169         return $flattenedCookies;
170     }
171
172     /**
173      * Returns not yet expired cookie values for the given URI.
174      *
175      * @param string $uri             A URI
176      * @param bool   $returnsRawValue Returns raw value or urldecoded value
177      *
178      * @return array An array of cookie values
179      */
180     public function allValues($uri, $returnsRawValue = false)
181     {
182         $this->flushExpiredCookies();
183
184         $parts = array_replace(array('path' => '/'), parse_url($uri));
185         $cookies = array();
186         foreach ($this->cookieJar as $domain => $pathCookies) {
187             if ($domain) {
188                 $domain = '.'.ltrim($domain, '.');
189                 if ($domain != substr('.'.$parts['host'], -strlen($domain))) {
190                     continue;
191                 }
192             }
193
194             foreach ($pathCookies as $path => $namedCookies) {
195                 if ($path != substr($parts['path'], 0, strlen($path))) {
196                     continue;
197                 }
198
199                 foreach ($namedCookies as $cookie) {
200                     if ($cookie->isSecure() && 'https' != $parts['scheme']) {
201                         continue;
202                     }
203
204                     $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
205                 }
206             }
207         }
208
209         return $cookies;
210     }
211
212     /**
213      * Returns not yet expired raw cookie values for the given URI.
214      *
215      * @param string $uri A URI
216      *
217      * @return array An array of cookie values
218      */
219     public function allRawValues($uri)
220     {
221         return $this->allValues($uri, true);
222     }
223
224     /**
225      * Removes all expired cookies.
226      */
227     public function flushExpiredCookies()
228     {
229         foreach ($this->cookieJar as $domain => $pathCookies) {
230             foreach ($pathCookies as $path => $namedCookies) {
231                 foreach ($namedCookies as $name => $cookie) {
232                     if ($cookie->isExpired()) {
233                         unset($this->cookieJar[$domain][$path][$name]);
234                     }
235                 }
236             }
237         }
238     }
239 }