Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
CookieJar.php
001 <?php
002 namespace GuzzleHttp\Cookie;
003
004 use Psr\Http\Message\RequestInterface;
005 use Psr\Http\Message\ResponseInterface;
006
007 /**
008 * Cookie jar that stores cookies as an array
009 */
010 class CookieJar implements CookieJarInterface
011 {
012 /** @var SetCookie[] Loaded cookie data */
013 private $cookies = [];
014
015 /** @var bool */
016 private $strictMode;
017
018 /**
019 * @param bool $strictMode Set to true to throw exceptions when invalid
020 * cookies are added to the cookie jar.
021 * @param array $cookieArray Array of SetCookie objects or a hash of
022 * arrays that can be used with the SetCookie
023 * constructor
024 */
025 public function __construct($strictMode = false, $cookieArray = [])
026 {
027 $this->strictMode = $strictMode;
028
029 foreach ($cookieArray as $cookie) {
030 if (!($cookie instanceof SetCookie)) {
031 $cookie = new SetCookie($cookie);
032 }
033 $this->setCookie($cookie);
034 }
035 }
036
037 /**
038 * Create a new Cookie jar from an associative array and domain.
039 *
040 * @param array $cookies Cookies to create the jar from
041 * @param string $domain Domain to set the cookies to
042 *
043 * @return self
044 */
045 public static function fromArray(array $cookies, $domain)
046 {
047 $cookieJar = new self();
048 foreach ($cookies as $name => $value) {
049 $cookieJar->setCookie(new SetCookie([
050 'Domain' => $domain,
051 'Name' => $name,
052 'Value' => $value,
053 'Discard' => true
054 ]));
055 }
056
057 return $cookieJar;
058 }
059
060 /**
061 * @deprecated
062 */
063 public static function getCookieValue($value)
064 {
065 return $value;
066 }
067
068 /**
069 * Evaluate if this cookie should be persisted to storage
070 * that survives between requests.
071 *
072 * @param SetCookie $cookie Being evaluated.
073 * @param bool $allowSessionCookies If we should persist session cookies
074 * @return bool
075 */
076 public static function shouldPersist(
077 SetCookie $cookie,
078 $allowSessionCookies = false
079 ) {
080 if ($cookie->getExpires() || $allowSessionCookies) {
081 if (!$cookie->getDiscard()) {
082 return true;
083 }
084 }
085
086 return false;
087 }
088
089 /**
090 * Finds and returns the cookie based on the name
091 *
092 * @param string $name cookie name to search for
093 * @return SetCookie|null cookie that was found or null if not found
094 */
095 public function getCookieByName($name)
096 {
097 // don't allow a non string name
098 if ($name === null || !is_scalar($name)) {
099 return null;
100 }
101 foreach ($this->cookies as $cookie) {
102 if ($cookie->getName() !== null && strcasecmp($cookie->getName(), $name) === 0) {
103 return $cookie;
104 }
105 }
106
107 return null;
108 }
109
110 public function toArray()
111 {
112 return array_map(function (SetCookie $cookie) {
113 return $cookie->toArray();
114 }, $this->getIterator()->getArrayCopy());
115 }
116
117 public function clear($domain = null, $path = null, $name = null)
118 {
119 if (!$domain) {
120 $this->cookies = [];
121 return;
122 } elseif (!$path) {
123 $this->cookies = array_filter(
124 $this->cookies,
125 function (SetCookie $cookie) use ($domain) {
126 return !$cookie->matchesDomain($domain);
127 }
128 );
129 } elseif (!$name) {
130 $this->cookies = array_filter(
131 $this->cookies,
132 function (SetCookie $cookie) use ($path, $domain) {
133 return !($cookie->matchesPath($path) &&
134 $cookie->matchesDomain($domain));
135 }
136 );
137 } else {
138 $this->cookies = array_filter(
139 $this->cookies,
140 function (SetCookie $cookie) use ($path, $domain, $name) {
141 return !($cookie->getName() == $name &&
142 $cookie->matchesPath($path) &&
143 $cookie->matchesDomain($domain));
144 }
145 );
146 }
147 }
148
149 public function clearSessionCookies()
150 {
151 $this->cookies = array_filter(
152 $this->cookies,
153 function (SetCookie $cookie) {
154 return !$cookie->getDiscard() && $cookie->getExpires();
155 }
156 );
157 }
158
159 public function setCookie(SetCookie $cookie)
160 {
161 // If the name string is empty (but not 0), ignore the set-cookie
162 // string entirely.
163 $name = $cookie->getName();
164 if (!$name && $name !== '0') {
165 return false;
166 }
167
168 // Only allow cookies with set and valid domain, name, value
169 $result = $cookie->validate();
170 if ($result !== true) {
171 if ($this->strictMode) {
172 throw new \RuntimeException('Invalid cookie: ' . $result);
173 } else {
174 $this->removeCookieIfEmpty($cookie);
175 return false;
176 }
177 }
178
179 // Resolve conflicts with previously set cookies
180 foreach ($this->cookies as $i => $c) {
181
182 // Two cookies are identical, when their path, and domain are
183 // identical.
184 if ($c->getPath() != $cookie->getPath() ||
185 $c->getDomain() != $cookie->getDomain() ||
186 $c->getName() != $cookie->getName()
187 ) {
188 continue;
189 }
190
191 // The previously set cookie is a discard cookie and this one is
192 // not so allow the new cookie to be set
193 if (!$cookie->getDiscard() && $c->getDiscard()) {
194 unset($this->cookies[$i]);
195 continue;
196 }
197
198 // If the new cookie's expiration is further into the future, then
199 // replace the old cookie
200 if ($cookie->getExpires() > $c->getExpires()) {
201 unset($this->cookies[$i]);
202 continue;
203 }
204
205 // If the value has changed, we better change it
206 if ($cookie->getValue() !== $c->getValue()) {
207 unset($this->cookies[$i]);
208 continue;
209 }
210
211 // The cookie exists, so no need to continue
212 return false;
213 }
214
215 $this->cookies[] = $cookie;
216
217 return true;
218 }
219
220 public function count()
221 {
222 return count($this->cookies);
223 }
224
225 public function getIterator()
226 {
227 return new \ArrayIterator(array_values($this->cookies));
228 }
229
230 public function extractCookies(
231 RequestInterface $request,
232 ResponseInterface $response
233 ) {
234 if ($cookieHeader = $response->getHeader('Set-Cookie')) {
235 foreach ($cookieHeader as $cookie) {
236 $sc = SetCookie::fromString($cookie);
237 if (!$sc->getDomain()) {
238 $sc->setDomain($request->getUri()->getHost());
239 }
240 if (0 !== strpos($sc->getPath(), '/')) {
241 $sc->setPath($this->getCookiePathFromRequest($request));
242 }
243 if (!$sc->matchesDomain($request->getUri()->getHost())) {
244 continue;
245 }
246 // Note: At this point `$sc->getDomain()` being a public suffix should
247 // be rejected, but we don't want to pull in the full PSL dependency.
248 $this->setCookie($sc);
249 }
250 }
251 }
252
253 /**
254 * Computes cookie path following RFC 6265 section 5.1.4
255 *
256 * @link https://tools.ietf.org/html/rfc6265#section-5.1.4
257 *
258 * @param RequestInterface $request
259 * @return string
260 */
261 private function getCookiePathFromRequest(RequestInterface $request)
262 {
263 $uriPath = $request->getUri()->getPath();
264 if ('' === $uriPath) {
265 return '/';
266 }
267 if (0 !== strpos($uriPath, '/')) {
268 return '/';
269 }
270 if ('/' === $uriPath) {
271 return '/';
272 }
273 if (0 === $lastSlashPos = strrpos($uriPath, '/')) {
274 return '/';
275 }
276
277 return substr($uriPath, 0, $lastSlashPos);
278 }
279
280 public function withCookieHeader(RequestInterface $request)
281 {
282 $values = [];
283 $uri = $request->getUri();
284 $scheme = $uri->getScheme();
285 $host = $uri->getHost();
286 $path = $uri->getPath() ?: '/';
287
288 foreach ($this->cookies as $cookie) {
289 if ($cookie->matchesPath($path) &&
290 $cookie->matchesDomain($host) &&
291 !$cookie->isExpired() &&
292 (!$cookie->getSecure() || $scheme === 'https')
293 ) {
294 $values[] = $cookie->getName() . '='
295 . $cookie->getValue();
296 }
297 }
298
299 return $values
300 ? $request->withHeader('Cookie', implode('; ', $values))
301 : $request;
302 }
303
304 /**
305 * If a cookie already exists and the server asks to set it again with a
306 * null value, the cookie must be deleted.
307 *
308 * @param SetCookie $cookie
309 */
310 private function removeCookieIfEmpty(SetCookie $cookie)
311 {
312 $cookieValue = $cookie->getValue();
313 if ($cookieValue === null || $cookieValue === '') {
314 $this->clear(
315 $cookie->getDomain(),
316 $cookie->getPath(),
317 $cookie->getName()
318 );
319 }
320 }
321 }
322