Utility.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * @version $Id$
  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-2010 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_token' => $config->getToken()->getToken(),
  53. 'oauth_version' => $config->getVersion(),
  54. );
  55. if (!is_null($serviceProviderParams)) {
  56. $params = array_merge($params, $serviceProviderParams);
  57. }
  58. $params['oauth_signature'] = $this->sign(
  59. $params,
  60. $config->getSignatureMethod(),
  61. $config->getConsumerSecret(),
  62. $config->getToken()->getTokenSecret(),
  63. $config->getRequestMethod(),
  64. $url
  65. );
  66. return $params;
  67. }
  68. /**
  69. * Given both OAuth parameters and any custom parametere, generate an
  70. * encoded query string. This method expects parameters to have been
  71. * assembled and signed beforehand.
  72. *
  73. * @param array $params
  74. * @param bool $customParamsOnly Ignores OAuth params e.g. for requests using OAuth Header
  75. * @return string
  76. */
  77. public function toEncodedQueryString(array $params, $customParamsOnly = false)
  78. {
  79. if ($customParamsOnly) {
  80. foreach ($params as $key=>$value) {
  81. if (preg_match("/^oauth_/", $key)) {
  82. unset($params[$key]);
  83. }
  84. }
  85. }
  86. $encodedParams = array();
  87. foreach ($params as $key => $value) {
  88. $encodedParams[] = self::urlEncode($key)
  89. . '='
  90. . self::urlEncode($value);
  91. }
  92. return implode('&', $encodedParams);
  93. }
  94. /**
  95. * Cast to authorization header
  96. *
  97. * @param array $params
  98. * @param null|string $realm
  99. * @param bool $excludeCustomParams
  100. * @return void
  101. */
  102. public function toAuthorizationHeader(array $params, $realm = null, $excludeCustomParams = true)
  103. {
  104. $headerValue = array(
  105. 'OAuth realm="' . $realm . '"',
  106. );
  107. foreach ($params as $key => $value) {
  108. if ($excludeCustomParams) {
  109. if (!preg_match("/^oauth_/", $key)) {
  110. continue;
  111. }
  112. }
  113. $headerValue[] = self::urlEncode($key)
  114. . '="'
  115. . self::urlEncode($value) . '"';
  116. }
  117. return implode(",", $headerValue);
  118. }
  119. /**
  120. * Sign request
  121. *
  122. * @param array $params
  123. * @param string $signatureMethod
  124. * @param string $consumerSecret
  125. * @param null|string $tokenSecret
  126. * @param null|string $method
  127. * @param null|string $url
  128. * @return string
  129. */
  130. public function sign(
  131. array $params, $signatureMethod, $consumerSecret, $tokenSecret = null, $method = null, $url = null
  132. ) {
  133. $className = '';
  134. $hashAlgo = null;
  135. $parts = explode('-', $signatureMethod);
  136. if (count($parts) > 1) {
  137. $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($parts[0]));
  138. $hashAlgo = $parts[1];
  139. } else {
  140. $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($signatureMethod));
  141. }
  142. require_once str_replace('_', '/', $className) . '.php';
  143. $signatureObject = new $className($consumerSecret, $tokenSecret, $hashAlgo);
  144. return $signatureObject->sign($params, $method, $url);
  145. }
  146. /**
  147. * Parse query string
  148. *
  149. * @param mixed $query
  150. * @return array
  151. */
  152. public function parseQueryString($query)
  153. {
  154. $params = array();
  155. if (empty($query)) {
  156. return array();
  157. }
  158. // Not remotely perfect but beats parse_str() which converts
  159. // periods and uses urldecode, not rawurldecode.
  160. $parts = explode('&', $query);
  161. foreach ($parts as $pair) {
  162. $kv = explode('=', $pair);
  163. $params[rawurldecode($kv[0])] = rawurldecode($kv[1]);
  164. }
  165. return $params;
  166. }
  167. /**
  168. * Generate nonce
  169. *
  170. * @return string
  171. */
  172. public function generateNonce()
  173. {
  174. return md5(uniqid(rand(), true));
  175. }
  176. /**
  177. * Generate timestamp
  178. *
  179. * @return int
  180. */
  181. public function generateTimestamp()
  182. {
  183. return time();
  184. }
  185. /**
  186. * urlencode a value
  187. *
  188. * @param string $value
  189. * @return string
  190. */
  191. public static function urlEncode($value)
  192. {
  193. $encoded = rawurlencode($value);
  194. $encoded = str_replace('%7E', '~', $encoded);
  195. return $encoded;
  196. }
  197. }