File indexing completed on 2024-06-09 05:55:28

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Oauth
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** Zend_Oauth_Http_Utility */
0023 // require_once 'Zend/Oauth/Http/Utility.php';
0024 
0025 /** Zend_Uri_Http */
0026 // require_once 'Zend/Uri/Http.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Oauth
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 abstract class Zend_Oauth_Signature_SignatureAbstract
0035 {
0036     /**
0037      * Hash algorithm to use when generating signature
0038      * @var string
0039      */
0040     protected $_hashAlgorithm = null;
0041 
0042     /**
0043      * Key to use when signing
0044      * @var string
0045      */
0046     protected $_key = null;
0047 
0048     /**
0049      * Consumer secret
0050      * @var string
0051      */
0052     protected $_consumerSecret = null;
0053 
0054     /**
0055      * Token secret
0056      * @var string
0057      */
0058     protected $_tokenSecret = '';
0059 
0060     /**
0061      * Constructor
0062      *
0063      * @param  string $consumerSecret
0064      * @param  null|string $tokenSecret
0065      * @param  null|string $hashAlgo
0066      * @return void
0067      */
0068     public function __construct($consumerSecret, $tokenSecret = null, $hashAlgo = null)
0069     {
0070         $this->_consumerSecret = $consumerSecret;
0071         if (isset($tokenSecret)) {
0072             $this->_tokenSecret = $tokenSecret;
0073         }
0074         $this->_key = $this->_assembleKey();
0075         if (isset($hashAlgo)) {
0076             $this->_hashAlgorithm = $hashAlgo;
0077         }
0078     }
0079 
0080     /**
0081      * Sign a request
0082      *
0083      * @param  array $params
0084      * @param  null|string $method
0085      * @param  null|string $url
0086      * @return string
0087      */
0088     public abstract function sign(array $params, $method = null, $url = null);
0089 
0090     /**
0091      * Normalize the base signature URL
0092      *
0093      * @param  string $url
0094      * @return string
0095      */
0096     public function normaliseBaseSignatureUrl($url)
0097     {
0098         $uri = Zend_Uri_Http::fromString($url);
0099         if ($uri->getScheme() == 'http' && $uri->getPort() == '80') {
0100             $uri->setPort('');
0101         } elseif ($uri->getScheme() == 'https' && $uri->getPort() == '443') {
0102             $uri->setPort('');
0103         }
0104         $uri->setQuery('');
0105         $uri->setFragment('');
0106         $uri->setHost(strtolower($uri->getHost()));
0107         return $uri->getUri(true);
0108     }
0109 
0110     /**
0111      * Assemble key from consumer and token secrets
0112      *
0113      * @return string
0114      */
0115     protected function _assembleKey()
0116     {
0117         $parts = array($this->_consumerSecret);
0118         if ($this->_tokenSecret !== null) {
0119             $parts[] = $this->_tokenSecret;
0120         }
0121         foreach ($parts as $key => $secret) {
0122             $parts[$key] = Zend_Oauth_Http_Utility::urlEncode($secret);
0123         }
0124         return implode('&', $parts);
0125     }
0126 
0127     /**
0128      * Get base signature string
0129      *
0130      * @param  array $params
0131      * @param  null|string $method
0132      * @param  null|string $url
0133      * @return string
0134      */
0135     protected function _getBaseSignatureString(array $params, $method = null, $url = null)
0136     {
0137         $encodedParams = array();
0138         foreach ($params as $key => $value) {
0139             $encodedParams[Zend_Oauth_Http_Utility::urlEncode($key)] =
0140                 Zend_Oauth_Http_Utility::urlEncode($value);
0141         }
0142         $baseStrings = array();
0143         if (isset($method)) {
0144             $baseStrings[] = strtoupper($method);
0145         }
0146         if (isset($url)) {
0147             // should normalise later
0148             $baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
0149                 $this->normaliseBaseSignatureUrl($url)
0150             );
0151         }
0152         if (isset($encodedParams['oauth_signature'])) {
0153             unset($encodedParams['oauth_signature']);
0154         }
0155         $baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
0156             $this->_toByteValueOrderedQueryString($encodedParams)
0157         );
0158         return implode('&', $baseStrings);
0159     }
0160 
0161     /**
0162      * Transform an array to a byte value ordered query string
0163      *
0164      * @param  array $params
0165      * @return string
0166      */
0167     protected function _toByteValueOrderedQueryString(array $params)
0168     {
0169         $return = array();
0170         uksort($params, 'strnatcmp');
0171         foreach ($params as $key => $value) {
0172             if (is_array($value)) {
0173                 natsort($value);
0174                 foreach ($value as $keyduplicate) {
0175                     $return[] = $key . '=' . $keyduplicate;
0176                 }
0177             } else {
0178                 $return[] = $key . '=' . $value;
0179             }
0180         }
0181         return implode('&', $return);
0182     }
0183 }