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
22 /** Zend_Oauth_Http_Utility */
23 require_once 'Zend/Oauth/Http/Utility.php';
26 require_once 'Zend/Uri/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 abstract class Zend_Oauth_Signature_SignatureAbstract
37 * Hash algorithm to use when generating signature
40 protected $_hashAlgorithm = null;
43 * Key to use when signing
46 protected $_key = null;
52 protected $_consumerSecret = null;
58 protected $_tokenSecret = '';
63 * @param string $consumerSecret
64 * @param null|string $tokenSecret
65 * @param null|string $hashAlgo
68 public function __construct($consumerSecret, $tokenSecret = null, $hashAlgo = null)
70 $this->_consumerSecret = $consumerSecret;
71 if (isset($tokenSecret)) {
72 $this->_tokenSecret = $tokenSecret;
74 $this->_key = $this->_assembleKey();
75 if (isset($hashAlgo)) {
76 $this->_hashAlgorithm = $hashAlgo;
83 * @param array $params
84 * @param null|string $method
85 * @param null|string $url
88 public abstract function sign(array $params, $method = null, $url = null);
91 * Normalize the base signature URL
96 public function normaliseBaseSignatureUrl($url)
98 $uri = Zend_Uri_Http::fromString($url);
99 if ($uri->getScheme() == 'http' && $uri->getPort() == '80') {
101 } elseif ($uri->getScheme() == 'https' && $uri->getPort() == '443') {
105 $uri->setFragment('');
106 $uri->setHost(strtolower($uri->getHost()));
107 return $uri->getUri(true);
111 * Assemble key from consumer and token secrets
115 protected function _assembleKey()
117 $parts = array($this->_consumerSecret);
118 if ($this->_tokenSecret !== null) {
119 $parts[] = $this->_tokenSecret;
121 foreach ($parts as $key => $secret) {
122 $parts[$key] = Zend_Oauth_Http_Utility::urlEncode($secret);
124 return implode('&', $parts);
128 * Get base signature string
130 * @param array $params
131 * @param null|string $method
132 * @param null|string $url
135 protected function _getBaseSignatureString(array $params, $method = null, $url = null)
137 $encodedParams = array();
138 foreach ($params as $key => $value) {
139 $encodedParams[Zend_Oauth_Http_Utility::urlEncode($key)] =
140 Zend_Oauth_Http_Utility::urlEncode($value);
142 $baseStrings = array();
143 if (isset($method)) {
144 $baseStrings[] = strtoupper($method);
147 // should normalise later
148 $baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
149 $this->normaliseBaseSignatureUrl($url)
152 if (isset($encodedParams['oauth_signature'])) {
153 unset($encodedParams['oauth_signature']);
155 $baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
156 $this->_toByteValueOrderedQueryString($encodedParams)
158 return implode('&', $baseStrings);
162 * Transform an array to a byte value ordered query string
164 * @param array $params
167 protected function _toByteValueOrderedQueryString(array $params)
170 uksort($params, 'strnatcmp');
171 foreach ($params as $key => $value) {
172 if (is_array($value)) {
174 foreach ($value as $keyduplicate) {
175 $return[] = $key . '=' . $keyduplicate;
178 $return[] = $key . '=' . $value;
181 return implode('&', $return);