Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Client.php
Go to the documentation of this file.
1 <?php
23 #require_once 'Zend/Oauth.php';
24 
26 #require_once 'Zend/Http/Client.php';
27 
29 #require_once 'Zend/Oauth/Http/Utility.php';
30 
32 #require_once 'Zend/Oauth/Config.php';
33 
41 {
46  public static $supportsRevisionA = false;
47 
56  protected $_config = null;
57 
64  protected $_streamingRequest = null;
65 
77  public function __construct($oauthOptions, $uri = null, $config = null)
78  {
79  if ($config instanceof Zend_Config && !isset($config->rfc3986_strict)) {
80  $config = $config->toArray();
81  $config['rfc3986_strict'] = true;
82  } else if (null === $config ||
83  (is_array($config) && !isset($config['rfc3986_strict']))) {
84  $config['rfc3986_strict'] = true;
85  }
86  parent::__construct($uri, $config);
87  $this->_config = new Zend_Oauth_Config;
88  if ($oauthOptions !== null) {
89  if ($oauthOptions instanceof Zend_Config) {
90  $oauthOptions = $oauthOptions->toArray();
91  }
92  $this->_config->setOptions($oauthOptions);
93  }
94  }
95 
102  public function setAdapter($adapter)
103  {
104  if ($adapter == null) {
105  $this->adapter = $adapter;
106  } else {
107  parent::setAdapter($adapter);
108  }
109  }
110 
118  public function setStreamingRequest($value)
119  {
120  $this->_streamingRequest = $value;
121  }
122 
128  public function getStreamingRequest()
129  {
130  if ($this->_streamingRequest) {
131  return true;
132  } else {
133  return false;
134  }
135  }
136 
143  protected function _prepareBody()
144  {
145  if($this->_streamingRequest) {
146  $this->setHeaders(self::CONTENT_LENGTH,
147  $this->raw_post_data->getTotalSize());
148  return $this->raw_post_data;
149  }
150  else {
151  return parent::_prepareBody();
152  }
153  }
154 
160  public function resetParameters($clearAll = false)
161  {
162  $this->_streamingRequest = false;
163  return parent::resetParameters($clearAll);
164  }
165 
177  public function setRawDataStream($data, $enctype = null)
178  {
179  $this->_streamingRequest = true;
180  return $this->setRawData($data, $enctype);
181  }
182 
191  public function setMethod($method = self::GET)
192  {
193  if ($method == self::GET) {
194  $this->setRequestMethod(self::GET);
195  } elseif($method == self::POST) {
196  $this->setRequestMethod(self::POST);
197  } elseif($method == self::PUT) {
198  $this->setRequestMethod(self::PUT);
199  } elseif($method == self::DELETE) {
200  $this->setRequestMethod(self::DELETE);
201  } elseif($method == self::HEAD) {
202  $this->setRequestMethod(self::HEAD);
203  } elseif($method == self::OPTIONS) {
204  $this->setRequestMethod(self::OPTIONS);
205  }
206  return parent::setMethod($method);
207  }
208 
217  public function request($method = null)
218  {
219  if ($method !== null) {
220  $this->setMethod($method);
221  }
222  $this->prepareOauth();
223  return parent::request();
224  }
225 
236  public function prepareOauth()
237  {
238  $requestScheme = $this->getRequestScheme();
239  $requestMethod = $this->getRequestMethod();
240  $query = null;
241  if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
242  $oauthHeaderValue = $this->getToken()->toHeader(
243  $this->getUri(true),
244  $this->_config,
246  $this->getRealm()
247  );
248  $this->setHeaders('Authorization', $oauthHeaderValue);
249  } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
250  if ($requestMethod == self::GET) {
251  #require_once 'Zend/Oauth/Exception.php';
252  throw new Zend_Oauth_Exception(
253  'The client is configured to'
254  . ' pass OAuth parameters through a POST body but request method'
255  . ' is set to GET'
256  );
257  }
258  $raw = $this->getToken()->toQueryString(
259  $this->getUri(true),
260  $this->_config,
262  );
263  $this->setRawData($raw, 'application/x-www-form-urlencoded');
264  $this->paramsPost = array();
265  } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
267  $query = $this->getUri()->getQuery();
268  if ($query) {
269  $queryParts = explode('&', $this->getUri()->getQuery());
270  foreach ($queryParts as $queryPart) {
271  $kvTuple = explode('=', $queryPart);
272  $params[urldecode($kvTuple[0])] =
273  (array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : null);
274  }
275  }
276  if (!empty($this->paramsPost)) {
277  $params = array_merge($params, $this->paramsPost);
278  $query = $this->getToken()->toQueryString(
279  $this->getUri(true), $this->_config, $params
280  );
281  }
282  $query = $this->getToken()->toQueryString(
283  $this->getUri(true), $this->_config, $params
284  );
285  $this->getUri()->setQuery($query);
286  $this->paramsGet = array();
287  } else {
288  #require_once 'Zend/Oauth/Exception.php';
289  throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
290  }
291  }
292 
300  {
301  $params = array();
302  if (!empty($this->paramsGet)) {
303  $params = array_merge($params, $this->paramsGet);
304  }
305  if ($this->enctype != self::ENC_FORMDATA && !empty($this->paramsPost)) {
306  $params = array_merge($params, $this->paramsPost);
307  }
308  return $params;
309  }
310 
321  public function __call($method, array $args)
322  {
323  if (!method_exists($this->_config, $method)) {
324  #require_once 'Zend/Oauth/Exception.php';
325  throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
326  }
327  return call_user_func_array(array($this->_config,$method), $args);
328  }
329 }
resetParameters($clearAll=false)
Definition: Client.php:160
getUri($as_string=false)
Definition: Client.php:342
const REQUEST_SCHEME_HEADER
Definition: Oauth.php:33
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setHeaders($name, $value=null)
Definition: Client.php:433
setAdapter($adapter)
Definition: Client.php:102
setMethod($method=self::GET)
Definition: Client.php:191
setRawData($data, $enctype=null)
Definition: Client.php:809
static $supportsRevisionA
Definition: Client.php:46
setStreamingRequest($value)
Definition: Client.php:118
setOptions(array $options)
Definition: Config.php:181
$value
Definition: gender.phtml:16
const REQUEST_SCHEME_QUERYSTRING
Definition: Oauth.php:35
_getSignableParametersAsQueryString()
Definition: Client.php:299
setRawDataStream($data, $enctype=null)
Definition: Client.php:177
__call($method, array $args)
Definition: Client.php:321
__construct($oauthOptions, $uri=null, $config=null)
Definition: Client.php:77
request($method=null)
Definition: Client.php:217
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
const REQUEST_SCHEME_POSTBODY
Definition: Oauth.php:34