PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Oauth/Http/Utility.php

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