[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/rmccue/requests/src/Cookie/ -> Jar.php (source)

   1  <?php
   2  /**
   3   * Cookie holder object
   4   *
   5   * @package Requests\Cookies
   6   */
   7  
   8  namespace WpOrg\Requests\Cookie;
   9  
  10  use ArrayAccess;
  11  use ArrayIterator;
  12  use IteratorAggregate;
  13  use ReturnTypeWillChange;
  14  use WpOrg\Requests\Cookie;
  15  use WpOrg\Requests\Exception;
  16  use WpOrg\Requests\Exception\InvalidArgument;
  17  use WpOrg\Requests\HookManager;
  18  use WpOrg\Requests\Iri;
  19  use WpOrg\Requests\Response;
  20  
  21  /**
  22   * Cookie holder object
  23   *
  24   * @package Requests\Cookies
  25   */
  26  class Jar implements ArrayAccess, IteratorAggregate {
  27      /**
  28       * Actual item data
  29       *
  30       * @var array
  31       */
  32      protected $cookies = [];
  33  
  34      /**
  35       * Create a new jar
  36       *
  37       * @param array $cookies Existing cookie values
  38       *
  39       * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array.
  40       */
  41  	public function __construct($cookies = []) {
  42          if (is_array($cookies) === false) {
  43              throw InvalidArgument::create(1, '$cookies', 'array', gettype($cookies));
  44          }
  45  
  46          $this->cookies = $cookies;
  47      }
  48  
  49      /**
  50       * Normalise cookie data into a \WpOrg\Requests\Cookie
  51       *
  52       * @param string|\WpOrg\Requests\Cookie $cookie
  53       * @return \WpOrg\Requests\Cookie
  54       */
  55  	public function normalize_cookie($cookie, $key = '') {
  56          if ($cookie instanceof Cookie) {
  57              return $cookie;
  58          }
  59  
  60          return Cookie::parse($cookie, $key);
  61      }
  62  
  63      /**
  64       * Check if the given item exists
  65       *
  66       * @param string $offset Item key
  67       * @return boolean Does the item exist?
  68       */
  69      #[ReturnTypeWillChange]
  70  	public function offsetExists($offset) {
  71          return isset($this->cookies[$offset]);
  72      }
  73  
  74      /**
  75       * Get the value for the item
  76       *
  77       * @param string $offset Item key
  78       * @return string|null Item value (null if offsetExists is false)
  79       */
  80      #[ReturnTypeWillChange]
  81  	public function offsetGet($offset) {
  82          if (!isset($this->cookies[$offset])) {
  83              return null;
  84          }
  85  
  86          return $this->cookies[$offset];
  87      }
  88  
  89      /**
  90       * Set the given item
  91       *
  92       * @param string $offset Item name
  93       * @param string $value Item value
  94       *
  95       * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
  96       */
  97      #[ReturnTypeWillChange]
  98  	public function offsetSet($offset, $value) {
  99          if ($offset === null) {
 100              throw new Exception('Object is a dictionary, not a list', 'invalidset');
 101          }
 102  
 103          $this->cookies[$offset] = $value;
 104      }
 105  
 106      /**
 107       * Unset the given header
 108       *
 109       * @param string $offset
 110       */
 111      #[ReturnTypeWillChange]
 112  	public function offsetUnset($offset) {
 113          unset($this->cookies[$offset]);
 114      }
 115  
 116      /**
 117       * Get an iterator for the data
 118       *
 119       * @return \ArrayIterator
 120       */
 121      #[ReturnTypeWillChange]
 122  	public function getIterator() {
 123          return new ArrayIterator($this->cookies);
 124      }
 125  
 126      /**
 127       * Register the cookie handler with the request's hooking system
 128       *
 129       * @param \WpOrg\Requests\HookManager $hooks Hooking system
 130       */
 131  	public function register(HookManager $hooks) {
 132          $hooks->register('requests.before_request', [$this, 'before_request']);
 133          $hooks->register('requests.before_redirect_check', [$this, 'before_redirect_check']);
 134      }
 135  
 136      /**
 137       * Add Cookie header to a request if we have any
 138       *
 139       * As per RFC 6265, cookies are separated by '; '
 140       *
 141       * @param string $url
 142       * @param array $headers
 143       * @param array $data
 144       * @param string $type
 145       * @param array $options
 146       */
 147  	public function before_request($url, &$headers, &$data, &$type, &$options) {
 148          if (!$url instanceof Iri) {
 149              $url = new Iri($url);
 150          }
 151  
 152          if (!empty($this->cookies)) {
 153              $cookies = [];
 154              foreach ($this->cookies as $key => $cookie) {
 155                  $cookie = $this->normalize_cookie($cookie, $key);
 156  
 157                  // Skip expired cookies
 158                  if ($cookie->is_expired()) {
 159                      continue;
 160                  }
 161  
 162                  if ($cookie->domain_matches($url->host)) {
 163                      $cookies[] = $cookie->format_for_header();
 164                  }
 165              }
 166  
 167              $headers['Cookie'] = implode('; ', $cookies);
 168          }
 169      }
 170  
 171      /**
 172       * Parse all cookies from a response and attach them to the response
 173       *
 174       * @param \WpOrg\Requests\Response $response
 175       */
 176  	public function before_redirect_check(Response $response) {
 177          $url = $response->url;
 178          if (!$url instanceof Iri) {
 179              $url = new Iri($url);
 180          }
 181  
 182          $cookies           = Cookie::parse_from_headers($response->headers, $url);
 183          $this->cookies     = array_merge($this->cookies, $cookies);
 184          $response->cookies = $this;
 185      }
 186  }


Generated: Tue Jan 21 05:10:11 2025 Cross-referenced by PHPXref 0.7.1