Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

CookieJar.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 7.27 KiB


001  <?php
002  namespace GuzzleHttp\Cookie;
003   
004  use GuzzleHttp\Message\RequestInterface;
005  use GuzzleHttp\Message\ResponseInterface;
006  use GuzzleHttp\ToArrayInterface;
007   
008  /**
009   * Cookie jar that stores cookies an an array
010   */
011  class CookieJar implements CookieJarInterface, ToArrayInterface
012  {
013      /** @var SetCookie[] Loaded cookie data */
014      private $cookies = [];
015   
016      /** @var bool */
017      private $strictMode;
018   
019      /**
020       * @param bool $strictMode   Set to true to throw exceptions when invalid
021       *                           cookies are added to the cookie jar.
022       * @param array $cookieArray Array of SetCookie objects or a hash of arrays
023       *                           that can be used with the SetCookie 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       * Quote the cookie value if it is not already quoted and it contains
062       * problematic characters.
063       *
064       * @param string $value Value that may or may not need to be quoted
065       *
066       * @return string
067       */
068      public static function getCookieValue($value)
069      {
070          if (substr($value, 0, 1) !== '"' &&
071              substr($value, -1, 1) !== '"' &&
072              strpbrk($value, ';,')
073          ) {
074              $value = '"' . $value . '"';
075          }
076   
077          return $value;
078      }
079   
080      public function toArray()
081      {
082          return array_map(function (SetCookie $cookie) {
083              return $cookie->toArray();
084          }, $this->getIterator()->getArrayCopy());
085      }
086   
087      public function clear($domain = null, $path = null, $name = null)
088      {
089          if (!$domain) {
090              $this->cookies = [];
091              return;
092          } elseif (!$path) {
093              $this->cookies = array_filter(
094                  $this->cookies,
095                  function (SetCookie $cookie) use ($path, $domain) {
096                      return !$cookie->matchesDomain($domain);
097                  }
098              );
099          } elseif (!$name) {
100              $this->cookies = array_filter(
101                  $this->cookies,
102                  function (SetCookie $cookie) use ($path, $domain) {
103                      return !($cookie->matchesPath($path) &&
104                          $cookie->matchesDomain($domain));
105                  }
106              );
107          } else {
108              $this->cookies = array_filter(
109                  $this->cookies,
110                  function (SetCookie $cookie) use ($path, $domain, $name) {
111                      return !($cookie->getName() == $name &&
112                          $cookie->matchesPath($path) &&
113                          $cookie->matchesDomain($domain));
114                  }
115              );
116          }
117      }
118   
119      public function clearSessionCookies()
120      {
121          $this->cookies = array_filter(
122              $this->cookies,
123              function (SetCookie $cookie) {
124                  return !$cookie->getDiscard() && $cookie->getExpires();
125              }
126          );
127      }
128   
129      public function setCookie(SetCookie $cookie)
130      {
131          // Only allow cookies with set and valid domain, name, value
132          $result = $cookie->validate();
133          if ($result !== true) {
134              if ($this->strictMode) {
135                  throw new \RuntimeException('Invalid cookie: ' . $result);
136              } else {
137                  $this->removeCookieIfEmpty($cookie);
138                  return false;
139              }
140          }
141   
142          // Resolve conflicts with previously set cookies
143          foreach ($this->cookies as $i => $c) {
144   
145              // Two cookies are identical, when their path, and domain are
146              // identical.
147              if ($c->getPath() != $cookie->getPath() ||
148                  $c->getDomain() != $cookie->getDomain() ||
149                  $c->getName() != $cookie->getName()
150              ) {
151                  continue;
152              }
153   
154              // The previously set cookie is a discard cookie and this one is
155              // not so allow the new cookie to be set
156              if (!$cookie->getDiscard() && $c->getDiscard()) {
157                  unset($this->cookies[$i]);
158                  continue;
159              }
160   
161              // If the new cookie's expiration is further into the future, then
162              // replace the old cookie
163              if ($cookie->getExpires() > $c->getExpires()) {
164                  unset($this->cookies[$i]);
165                  continue;
166              }
167   
168              // If the value has changed, we better change it
169              if ($cookie->getValue() !== $c->getValue()) {
170                  unset($this->cookies[$i]);
171                  continue;
172              }
173   
174              // The cookie exists, so no need to continue
175              return false;
176          }
177   
178          $this->cookies[] = $cookie;
179   
180          return true;
181      }
182   
183      public function count()
184      {
185          return count($this->cookies);
186      }
187   
188      public function getIterator()
189      {
190          return new \ArrayIterator(array_values($this->cookies));
191      }
192   
193      public function extractCookies(
194          RequestInterface $request,
195          ResponseInterface $response
196      ) {
197          if ($cookieHeader = $response->getHeaderAsArray('Set-Cookie')) {
198              foreach ($cookieHeader as $cookie) {
199                  $sc = SetCookie::fromString($cookie);
200                  if (!$sc->getDomain()) {
201                      $sc->setDomain($request->getHost());
202                  }
203                  $this->setCookie($sc);
204              }
205          }
206      }
207   
208      public function addCookieHeader(RequestInterface $request)
209      {
210          $values = [];
211          $scheme = $request->getScheme();
212          $host = $request->getHost();
213          $path = $request->getPath();
214   
215          foreach ($this->cookies as $cookie) {
216              if ($cookie->matchesPath($path) &&
217                  $cookie->matchesDomain($host) &&
218                  !$cookie->isExpired() &&
219                  (!$cookie->getSecure() || $scheme == 'https')
220              ) {
221                  $values[] = $cookie->getName() . '='
222                      . self::getCookieValue($cookie->getValue());
223              }
224          }
225   
226          if ($values) {
227              $request->setHeader('Cookie', implode('; ', $values));
228          }
229      }
230   
231      /**
232       * If a cookie already exists and the server asks to set it again with a
233       * null value, the cookie must be deleted.
234       *
235       * @param SetCookie $cookie
236       */
237      private function removeCookieIfEmpty(SetCookie $cookie)
238      {
239          $cookieValue = $cookie->getValue();
240          if ($cookieValue === null || $cookieValue === '') {
241              $this->clear(
242                  $cookie->getDomain(),
243                  $cookie->getPath(),
244                  $cookie->getName()
245              );
246          }
247      }
248  }
249