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.
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
23 require_once 'Zend/Oauth.php';
25 /** Zend_Oauth_Http */
26 require_once 'Zend/Oauth/Http.php';
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Oauth_Http_Utility
37 * Assemble all parameters for a generic OAuth request - i.e. no special
38 * params other than the defaults expected for any OAuth query.
41 * @param Zend_Oauth_Config_ConfigInterface $config
42 * @param null|array $serviceProviderParams
45 public function assembleParams(
47 Zend_Oauth_Config_ConfigInterface $config,
48 array $serviceProviderParams = null
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(),
58 if ($config->getToken()->getToken() != null) {
59 $params['oauth_token'] = $config->getToken()->getToken();
63 if ($serviceProviderParams !== null) {
64 $params = array_merge($params, $serviceProviderParams);
67 $params['oauth_signature'] = $this->sign(
69 $config->getSignatureMethod(),
70 $config->getConsumerSecret(),
71 $config->getToken()->getTokenSecret(),
72 $config->getRequestMethod(),
80 * Given both OAuth parameters and any custom parametere, generate an
81 * encoded query string. This method expects parameters to have been
82 * assembled and signed beforehand.
84 * @param array $params
85 * @param bool $customParamsOnly Ignores OAuth params e.g. for requests using OAuth Header
88 public function toEncodedQueryString(array $params, $customParamsOnly = false)
90 if ($customParamsOnly) {
91 foreach ($params as $key=>$value) {
92 if (preg_match("/^oauth_/", $key)) {
97 $encodedParams = array();
98 foreach ($params as $key => $value) {
99 $encodedParams[] = self::urlEncode($key)
101 . self::urlEncode($value);
103 return implode('&', $encodedParams);
107 * Cast to authorization header
109 * @param array $params
110 * @param null|string $realm
111 * @param bool $excludeCustomParams
114 public function toAuthorizationHeader(array $params, $realm = null, $excludeCustomParams = true)
116 $headerValue = array(
117 'OAuth realm="' . $realm . '"',
120 foreach ($params as $key => $value) {
121 if ($excludeCustomParams) {
122 if (!preg_match("/^oauth_/", $key)) {
126 $headerValue[] = self::urlEncode($key)
128 . self::urlEncode($value) . '"';
130 return implode(",", $headerValue);
136 * @param array $params
137 * @param string $signatureMethod
138 * @param string $consumerSecret
139 * @param null|string $tokenSecret
140 * @param null|string $method
141 * @param null|string $url
144 public function sign(
145 array $params, $signatureMethod, $consumerSecret, $tokenSecret = null, $method = null, $url = null
149 $parts = explode('-', $signatureMethod);
150 if (count($parts) > 1) {
151 $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($parts[0]));
152 $hashAlgo = $parts[1];
154 $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($signatureMethod));
157 require_once str_replace('_', '/', $className) . '.php';
158 $signatureObject = new $className($consumerSecret, $tokenSecret, $hashAlgo);
159 return $signatureObject->sign($params, $method, $url);
165 * @param mixed $query
168 public function parseQueryString($query)
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]);
190 public function generateNonce()
192 return md5(uniqid(rand(), true));
200 public function generateTimestamp()
208 * @param string $value
211 public static function urlEncode($value)
213 $encoded = rawurlencode($value);
214 $encoded = str_replace('%7E', '~', $encoded);