]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/Requests/Requests/Cookie/Jar.php
Add Requests 1.6
[Github/YOURLS.git] / includes / Requests / Requests / Cookie / Jar.php
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 normalizeCookie($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          * Check if the given item exists
48          *
49          * @param string $key Item key
50          * @return boolean Does the item exist?
51          */
52         public function offsetExists($key) {
53                 return isset($this->cookies[$key]);
54         }
55
56         /**
57          * Get the value for the item
58          *
59          * @param string $key Item key
60          * @return string Item value
61          */
62         public function offsetGet($key) {
63                 if (!isset($this->cookies[$key]))
64                         return null;
65
66                 return $this->cookies[$key];
67         }
68
69         /**
70          * Set the given item
71          *
72          * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
73          *
74          * @param string $key Item name
75          * @param string $value Item value
76          */
77         public function offsetSet($key, $value) {
78                 if ($key === null) {
79                         throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
80                 }
81
82                 $this->cookies[$key] = $value;
83         }
84
85         /**
86          * Unset the given header
87          *
88          * @param string $key
89          */
90         public function offsetUnset($key) {
91                 unset($this->cookies[$key]);
92         }
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         /**
104          * Register the cookie handler with the request's hooking system
105          *
106          * @param Requests_Hooker $hooks Hooking system
107          */
108         public function register(Requests_Hooker $hooks) {
109                 $hooks->register('requests.before_request', array($this, 'before_request'));
110                 $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check'));
111         }
112
113         /**
114          * Add Cookie header to a request if we have any
115          *
116          * As per RFC 6265, cookies are separated by '; '
117          *
118          * @param string $url
119          * @param array $headers
120          * @param array $data
121          * @param string $type
122          * @param array $options
123          */
124         public function before_request(&$url, &$headers, &$data, &$type, &$options) {
125                 if (!empty($this->cookies)) {
126                         $cookies = array();
127                         foreach ($this->cookies as $key => $cookie) {
128                                 $cookie = $this->normalizeCookie($cookie, $key);
129                                 $cookies[] = $cookie->formatForHeader();
130                         }
131
132                         $headers['Cookie'] = implode('; ', $cookies);
133                 }
134         }
135
136         /**
137          * Parse all cookies from a response and attach them to the response
138          *
139          * @var Requests_Response $response
140          */
141         public function before_redirect_check(Requests_Response &$return) {
142                 $cookies = Requests_Cookie::parseFromHeaders($return->headers);
143                 $this->cookies = array_merge($this->cookies, $cookies);
144                 $return->cookies = $this;
145         }
146 }