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