Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Utility.php
Go to the documentation of this file.
1 <?php
23 #require_once 'Zend/Oauth.php';
24 
26 #require_once 'Zend/Oauth/Http.php';
27 
35 {
45  public function assembleParams(
46  $url,
48  array $serviceProviderParams = null
49  ) {
50  $params = array(
51  'oauth_consumer_key' => $config->getConsumerKey(),
52  'oauth_nonce' => $this->generateNonce(),
53  'oauth_signature_method' => $config->getSignatureMethod(),
54  'oauth_timestamp' => $this->generateTimestamp(),
55  'oauth_version' => $config->getVersion(),
56  );
57 
58  if ($config->getToken()->getToken() != null) {
59  $params['oauth_token'] = $config->getToken()->getToken();
60  }
61 
62 
63  if ($serviceProviderParams !== null) {
64  $params = array_merge($params, $serviceProviderParams);
65  }
66 
67  $params['oauth_signature'] = $this->sign(
68  $params,
69  $config->getSignatureMethod(),
70  $config->getConsumerSecret(),
71  $config->getToken()->getTokenSecret(),
72  $config->getRequestMethod(),
73  $url
74  );
75 
76  return $params;
77  }
78 
88  public function toEncodedQueryString(array $params, $customParamsOnly = false)
89  {
90  if ($customParamsOnly) {
91  foreach ($params as $key=>$value) {
92  if (preg_match("/^oauth_/", $key)) {
93  unset($params[$key]);
94  }
95  }
96  }
97  $encodedParams = array();
98  foreach ($params as $key => $value) {
99  $encodedParams[] = self::urlEncode($key)
100  . '='
102  }
103  return implode('&', $encodedParams);
104  }
105 
114  public function toAuthorizationHeader(array $params, $realm = null, $excludeCustomParams = true)
115  {
116  $headerValue = array(
117  'OAuth realm="' . $realm . '"',
118  );
119 
120  foreach ($params as $key => $value) {
121  if ($excludeCustomParams) {
122  if (!preg_match("/^oauth_/", $key)) {
123  continue;
124  }
125  }
126  $headerValue[] = self::urlEncode($key)
127  . '="'
128  . self::urlEncode($value) . '"';
129  }
130  return implode(",", $headerValue);
131  }
132 
144  public function sign(
145  array $params, $signatureMethod, $consumerSecret, $tokenSecret = null, $method = null, $url = null
146  ) {
147  $className = '';
148  $hashAlgo = null;
149  $parts = explode('-', $signatureMethod);
150  if (count($parts) > 1) {
151  $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($parts[0]));
152  $hashAlgo = $parts[1];
153  } else {
154  $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($signatureMethod));
155  }
156 
157  #require_once str_replace('_', '/', $className) . '.php';
158  $signatureObject = new $className($consumerSecret, $tokenSecret, $hashAlgo);
159  return $signatureObject->sign($params, $method, $url);
160  }
161 
168  public function parseQueryString($query)
169  {
170  $params = array();
171  if (empty($query)) {
172  return array();
173  }
174 
175  // Not remotely perfect but beats parse_str() which converts
176  // periods and uses urldecode, not rawurldecode.
177  $parts = explode('&', $query);
178  foreach ($parts as $pair) {
179  $kv = explode('=', $pair);
180  $params[rawurldecode($kv[0])] = rawurldecode($kv[1]);
181  }
182  return $params;
183  }
184 
190  public function generateNonce()
191  {
192  return md5(uniqid(rand(), true));
193  }
194 
200  public function generateTimestamp()
201  {
202  return time();
203  }
204 
211  public static function urlEncode($value)
212  {
213  $encoded = rawurlencode($value);
214  $encoded = str_replace('%7E', '~', $encoded);
215  return $encoded;
216  }
217 }
static urlEncode($value)
Definition: Utility.php:211
$config
Definition: fraud_order.php:17
assembleParams( $url, Zend_Oauth_Config_ConfigInterface $config, array $serviceProviderParams=null)
Definition: Utility.php:45
$value
Definition: gender.phtml:16
sign(array $params, $signatureMethod, $consumerSecret, $tokenSecret=null, $method=null, $url=null)
Definition: Utility.php:144
$method
Definition: info.phtml:13
toAuthorizationHeader(array $params, $realm=null, $excludeCustomParams=true)
Definition: Utility.php:114
toEncodedQueryString(array $params, $customParamsOnly=false)
Definition: Utility.php:88
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31