Jar.php 3.7 KB

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