[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/guzzlehttp/guzzle/src/Cookie/ -> CookieJar.php (source)

   1  <?php
   2  namespace GuzzleHttp\Cookie;
   3  
   4  use GuzzleHttp\Message\RequestInterface;
   5  use GuzzleHttp\Message\ResponseInterface;
   6  use GuzzleHttp\ToArrayInterface;
   7  
   8  /**
   9   * Cookie jar that stores cookies an an array
  10   */
  11  class CookieJar implements CookieJarInterface, ToArrayInterface
  12  {
  13      /** @var SetCookie[] Loaded cookie data */
  14      private $cookies = [];
  15  
  16      /** @var bool */
  17      private $strictMode;
  18  
  19      /**
  20       * @param bool $strictMode   Set to true to throw exceptions when invalid
  21       *                           cookies are added to the cookie jar.
  22       * @param array $cookieArray Array of SetCookie objects or a hash of arrays
  23       *                           that can be used with the SetCookie constructor
  24       */
  25      public function __construct($strictMode = false, $cookieArray = [])
  26      {
  27          $this->strictMode = $strictMode;
  28  
  29          foreach ($cookieArray as $cookie) {
  30              if (!($cookie instanceof SetCookie)) {
  31                  $cookie = new SetCookie($cookie);
  32              }
  33              $this->setCookie($cookie);
  34          }
  35      }
  36  
  37      /**
  38       * Create a new Cookie jar from an associative array and domain.
  39       *
  40       * @param array  $cookies Cookies to create the jar from
  41       * @param string $domain  Domain to set the cookies to
  42       *
  43       * @return self
  44       */
  45      public static function fromArray(array $cookies, $domain)
  46      {
  47          $cookieJar = new self();
  48          foreach ($cookies as $name => $value) {
  49              $cookieJar->setCookie(new SetCookie([
  50                  'Domain'  => $domain,
  51                  'Name'    => $name,
  52                  'Value'   => $value,
  53                  'Discard' => true
  54              ]));
  55          }
  56  
  57          return $cookieJar;
  58      }
  59  
  60      /**
  61       * Quote the cookie value if it is not already quoted and it contains
  62       * problematic characters.
  63       *
  64       * @param string $value Value that may or may not need to be quoted
  65       *
  66       * @return string
  67       */
  68      public static function getCookieValue($value)
  69      {
  70          if (substr($value, 0, 1) !== '"' &&
  71              substr($value, -1, 1) !== '"' &&
  72              strpbrk($value, ';,')
  73          ) {
  74              $value = '"' . $value . '"';
  75          }
  76  
  77          return $value;
  78      }
  79  
  80      public function toArray()
  81      {
  82          return array_map(function (SetCookie $cookie) {
  83              return $cookie->toArray();
  84          }, $this->getIterator()->getArrayCopy());
  85      }
  86  
  87      public function clear($domain = null, $path = null, $name = null)
  88      {
  89          if (!$domain) {
  90              $this->cookies = [];
  91              return;
  92          } elseif (!$path) {
  93              $this->cookies = array_filter(
  94                  $this->cookies,
  95                  function (SetCookie $cookie) use ($path, $domain) {
  96                      return !$cookie->matchesDomain($domain);
  97                  }
  98              );
  99          } 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  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1