forked from zendframework/zf1
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathClient.php
329 lines (310 loc) · 10.8 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Oauth
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/** Zend_Oauth */
#require_once 'Zend/Oauth.php';
/** Zend_Http_Client */
#require_once 'Zend/Http/Client.php';
/** Zend_Oauth_Http_Utility */
#require_once 'Zend/Oauth/Http/Utility.php';
/** Zend_Oauth_Config */
#require_once 'Zend/Oauth/Config.php';
/**
* @category Zend
* @package Zend_Oauth
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Oauth_Client extends Zend_Http_Client
{
/**
* Flag to indicate that the client has detected the server as supporting
* OAuth 1.0a
*/
public static $supportsRevisionA = false;
/**
* Holds the current OAuth Configuration set encapsulated in an instance
* of Zend_Oauth_Config; it's not a Zend_Config instance since that level
* of abstraction is unnecessary and doesn't let me escape the accessors
* and mutators anyway!
*
* @var Zend_Oauth_Config
*/
protected $_config = null;
/**
* True if this request is being made with data supplied by
* a stream object instead of a raw encoded string.
*
* @var bool
*/
protected $_streamingRequest = null;
/**
* Constructor; creates a new HTTP Client instance which itself is
* just a typical Zend_Http_Client subclass with some OAuth icing to
* assist in automating OAuth parameter generation, addition and
* cryptographioc signing of requests.
*
* @param array|Zend_Config $oauthOptions
* @param string $uri
* @param array|Zend_Config $config
* @return void
*/
public function __construct($oauthOptions, $uri = null, $config = null)
{
if ($config instanceof Zend_Config && !isset($config->rfc3986_strict)) {
$config = $config->toArray();
$config['rfc3986_strict'] = true;
} else if (null === $config ||
(is_array($config) && !isset($config['rfc3986_strict']))) {
$config['rfc3986_strict'] = true;
}
parent::__construct($uri, $config);
$this->_config = new Zend_Oauth_Config;
if ($oauthOptions !== null) {
if ($oauthOptions instanceof Zend_Config) {
$oauthOptions = $oauthOptions->toArray();
}
$this->_config->setOptions($oauthOptions);
}
}
/**
* Load the connection adapter
*
* @param Zend_Http_Client_Adapter_Interface $adapter
* @return void
*/
public function setAdapter($adapter)
{
if ($adapter == null) {
$this->adapter = $adapter;
} else {
parent::setAdapter($adapter);
}
}
/**
* Set the streamingRequest variable which controls whether we are
* sending the raw (already encoded) POST data from a stream source.
*
* @param boolean $value The value to set.
* @return void
*/
public function setStreamingRequest($value)
{
$this->_streamingRequest = $value;
}
/**
* Check whether the client is set to perform streaming requests.
*
* @return boolean True if yes, false otherwise.
*/
public function getStreamingRequest()
{
if ($this->_streamingRequest) {
return true;
} else {
return false;
}
}
/**
* Prepare the request body (for POST and PUT requests)
*
* @return string
* @throws Zend_Http_Client_Exception
*/
protected function _prepareBody()
{
if($this->_streamingRequest) {
$this->setHeaders(self::CONTENT_LENGTH,
$this->raw_post_data->getTotalSize());
return $this->raw_post_data;
}
else {
return parent::_prepareBody();
}
}
/**
* Clear all custom parameters we set.
*
* @return Zend_Http_Client
*/
public function resetParameters($clearAll = false)
{
$this->_streamingRequest = false;
return parent::resetParameters($clearAll);
}
/**
* Set the raw (already encoded) POST data from a stream source.
*
* This is used to support POSTing from open file handles without
* caching the entire body into memory. It is a wrapper around
* Zend_Http_Client::setRawData().
*
* @param string $data The request data
* @param string $enctype The encoding type
* @return Zend_Http_Client
*/
public function setRawDataStream($data, $enctype = null)
{
$this->_streamingRequest = true;
return $this->setRawData($data, $enctype);
}
/**
* Same as Zend_Http_Client::setMethod() except it also creates an
* Oauth specific reference to the method type.
* Might be defunct and removed in a later iteration.
*
* @param string $method
* @return Zend_Http_Client
*/
public function setMethod($method = self::GET)
{
if ($method == self::GET) {
$this->setRequestMethod(self::GET);
} elseif($method == self::POST) {
$this->setRequestMethod(self::POST);
} elseif($method == self::PUT) {
$this->setRequestMethod(self::PUT);
} elseif($method == self::DELETE) {
$this->setRequestMethod(self::DELETE);
} elseif($method == self::HEAD) {
$this->setRequestMethod(self::HEAD);
} elseif($method == self::OPTIONS) {
$this->setRequestMethod(self::OPTIONS);
}
return parent::setMethod($method);
}
/**
* Same as Zend_Http_Client::request() except just before the request is
* executed, we automatically append any necessary OAuth parameters and
* sign the request using the relevant signature method.
*
* @param string $method
* @return Zend_Http_Response
*/
public function request($method = null)
{
if ($method !== null) {
$this->setMethod($method);
}
$this->prepareOauth();
return parent::request();
}
/**
* Performs OAuth preparation on the request before sending.
*
* This primarily means taking a request, correctly encoding and signing
* all parameters, and applying the correct OAuth scheme to the method
* being used.
*
* @return void
* @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
*/
public function prepareOauth()
{
$requestScheme = $this->getRequestScheme();
$requestMethod = $this->getRequestMethod();
$query = null;
if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
$oauthHeaderValue = $this->getToken()->toHeader(
$this->getUri(true),
$this->_config,
$this->_getSignableParametersAsQueryString(),
$this->getRealm()
);
$this->setHeaders('Authorization', $oauthHeaderValue);
} elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
if ($requestMethod == self::GET) {
#require_once 'Zend/Oauth/Exception.php';
throw new Zend_Oauth_Exception(
'The client is configured to'
. ' pass OAuth parameters through a POST body but request method'
. ' is set to GET'
);
}
$raw = $this->getToken()->toQueryString(
$this->getUri(true),
$this->_config,
$this->_getSignableParametersAsQueryString()
);
$this->setRawData($raw, 'application/x-www-form-urlencoded');
$this->paramsPost = array();
} elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
$params = $this->paramsGet;
$query = $this->getUri()->getQuery();
if ($query) {
$queryParts = explode('&', $this->getUri()->getQuery());
foreach ($queryParts as $queryPart) {
$kvTuple = explode('=', $queryPart);
$params[urldecode($kvTuple[0])] =
(array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : null);
}
}
if (!empty($this->paramsPost)) {
$params = array_merge($params, $this->paramsPost);
$query = $this->getToken()->toQueryString(
$this->getUri(true), $this->_config, $params
);
}
$query = $this->getToken()->toQueryString(
$this->getUri(true), $this->_config, $params
);
$this->getUri()->setQuery($query);
$this->paramsGet = array();
} else {
#require_once 'Zend/Oauth/Exception.php';
throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
}
}
/**
* Collect all signable parameters into a single array across query string
* and POST body. Don't include POST parameters if content type is multipart POST.
*
* @return array
*/
protected function _getSignableParametersAsQueryString()
{
$params = array();
if (!empty($this->paramsGet)) {
$params = array_merge($params, $this->paramsGet);
}
if ($this->enctype != self::ENC_FORMDATA && !empty($this->paramsPost)) {
$params = array_merge($params, $this->paramsPost);
}
return $params;
}
/**
* Simple Proxy to the current Zend_Oauth_Config method. It's that instance
* which holds all configuration methods and values this object also presents
* as it's API.
*
* @param string $method
* @param array $args
* @return mixed
* @throws Zend_Oauth_Exception if method does not exist in config object
*/
public function __call($method, array $args)
{
if (!method_exists($this->_config, $method)) {
#require_once 'Zend/Oauth/Exception.php';
throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
}
return call_user_func_array(array($this->_config,$method), $args);
}
}