PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Oauth/Http/Utility.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 217 lines | 111 code | 18 blank | 88 comment | 10 complexity | b423ef547710255a45cac24c0f2212b6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Oauth
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Oauth */
  21. require_once 'Zend/Oauth.php';
  22. /** Zend_Oauth_Http */
  23. require_once 'Zend/Oauth/Http.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Oauth
  27. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Oauth_Http_Utility
  31. {
  32. /**
  33. * Assemble all parameters for a generic OAuth request - i.e. no special
  34. * params other than the defaults expected for any OAuth query.
  35. *
  36. * @param string $url
  37. * @param Zend_Oauth_Config_ConfigInterface $config
  38. * @param null|array $serviceProviderParams
  39. * @return array
  40. */
  41. public function assembleParams(
  42. $url,
  43. Zend_Oauth_Config_ConfigInterface $config,
  44. array $serviceProviderParams = null
  45. ) {
  46. $params = array(
  47. 'oauth_consumer_key' => $config->getConsumerKey(),
  48. 'oauth_nonce' => $this->generateNonce(),
  49. 'oauth_signature_method' => $config->getSignatureMethod(),
  50. 'oauth_timestamp' => $this->generateTimestamp(),
  51. 'oauth_version' => $config->getVersion(),
  52. );
  53. if ($config->getToken()->getToken() != null) {
  54. $params['oauth_token'] = $config->getToken()->getToken();
  55. }
  56. if ($serviceProviderParams !== null) {
  57. $params = array_merge($params, $serviceProviderParams);
  58. }
  59. $params['oauth_signature'] = $this->sign(
  60. $params,
  61. $config->getSignatureMethod(),
  62. $config->getConsumerSecret(),
  63. $config->getToken()->getTokenSecret(),
  64. $config->getRequestMethod(),
  65. $url
  66. );
  67. return $params;
  68. }
  69. /**
  70. * Given both OAuth parameters and any custom parametere, generate an
  71. * encoded query string. This method expects parameters to have been
  72. * assembled and signed beforehand.
  73. *
  74. * @param array $params
  75. * @param bool $customParamsOnly Ignores OAuth params e.g. for requests using OAuth Header
  76. * @return string
  77. */
  78. public function toEncodedQueryString(array $params, $customParamsOnly = false)
  79. {
  80. if ($customParamsOnly) {
  81. foreach ($params as $key=>$value) {
  82. if (preg_match("/^oauth_/", $key)) {
  83. unset($params[$key]);
  84. }
  85. }
  86. }
  87. $encodedParams = array();
  88. foreach ($params as $key => $value) {
  89. $encodedParams[] = self::urlEncode($key)
  90. . '='
  91. . self::urlEncode($value);
  92. }
  93. return implode('&', $encodedParams);
  94. }
  95. /**
  96. * Cast to authorization header
  97. *
  98. * @param array $params
  99. * @param null|string $realm
  100. * @param bool $excludeCustomParams
  101. * @return void
  102. */
  103. public function toAuthorizationHeader(array $params, $realm = null, $excludeCustomParams = true)
  104. {
  105. $headerValue = array(
  106. 'OAuth realm="' . $realm . '"',
  107. );
  108. foreach ($params as $key => $value) {
  109. if ($excludeCustomParams) {
  110. if (!preg_match("/^oauth_/", $key)) {
  111. continue;
  112. }
  113. }
  114. $headerValue[] = self::urlEncode($key)
  115. . '="'
  116. . self::urlEncode($value) . '"';
  117. }
  118. return implode(",", $headerValue);
  119. }
  120. /**
  121. * Sign request
  122. *
  123. * @param array $params
  124. * @param string $signatureMethod
  125. * @param string $consumerSecret
  126. * @param null|string $tokenSecret
  127. * @param null|string $method
  128. * @param null|string $url
  129. * @return string
  130. */
  131. public function sign(
  132. array $params, $signatureMethod, $consumerSecret, $tokenSecret = null, $method = null, $url = null
  133. ) {
  134. $className = '';
  135. $hashAlgo = null;
  136. $parts = explode('-', $signatureMethod);
  137. if (count($parts) > 1) {
  138. $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($parts[0]));
  139. $hashAlgo = $parts[1];
  140. } else {
  141. $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($signatureMethod));
  142. }
  143. require_once str_replace('_', '/', $className) . '.php';
  144. $signatureObject = new $className($consumerSecret, $tokenSecret, $hashAlgo);
  145. return $signatureObject->sign($params, $method, $url);
  146. }
  147. /**
  148. * Parse query string
  149. *
  150. * @param mixed $query
  151. * @return array
  152. */
  153. public function parseQueryString($query)
  154. {
  155. $params = array();
  156. if (empty($query)) {
  157. return array();
  158. }
  159. // Not remotely perfect but beats parse_str() which converts
  160. // periods and uses urldecode, not rawurldecode.
  161. $parts = explode('&', $query);
  162. foreach ($parts as $pair) {
  163. $kv = explode('=', $pair);
  164. $params[rawurldecode($kv[0])] = rawurldecode($kv[1]);
  165. }
  166. return $params;
  167. }
  168. /**
  169. * Generate nonce
  170. *
  171. * @return string
  172. */
  173. public function generateNonce()
  174. {
  175. return md5(uniqid(rand(), true));
  176. }
  177. /**
  178. * Generate timestamp
  179. *
  180. * @return int
  181. */
  182. public function generateTimestamp()
  183. {
  184. return time();
  185. }
  186. /**
  187. * urlencode a value
  188. *
  189. * @param string $value
  190. * @return string
  191. */
  192. public static function urlEncode($value)
  193. {
  194. $encoded = rawurlencode($value);
  195. $encoded = str_replace('%7E', '~', $encoded);
  196. return $encoded;
  197. }
  198. }