File indexing completed on 2024-12-29 05:28:00

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_Service_Amazon
0017  * @subpackage Authentication
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 
0023 /**
0024  * @see Zend_Service_Amazon_Authentication
0025  */
0026 // require_once 'Zend/Service/Amazon/Authentication.php';
0027 
0028 /**
0029  * @see Zend_Crypt_Hmac
0030  */
0031 // require_once 'Zend/Crypt/Hmac.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_Service_Amazon
0036  * @subpackage Authentication
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Service_Amazon_Authentication_S3 extends Zend_Service_Amazon_Authentication
0041 {
0042     /**
0043      * Add the S3 Authorization signature to the request headers
0044      *
0045      * @param  string $method
0046      * @param  string $path
0047      * @param  array &$headers
0048      * @return string
0049      */
0050     public function generateSignature($method, $path, &$headers)
0051     {
0052         if (! is_array($headers)) {
0053             $headers = array($headers);
0054         }
0055 
0056         $type = $md5 = $date = '';
0057 
0058         // Search for the Content-type, Content-MD5 and Date headers
0059         foreach ($headers as $key => $val) {
0060             if (strcasecmp($key, 'content-type') == 0) {
0061                 $type = $val;
0062             } else if (strcasecmp($key, 'content-md5') == 0) {
0063                 $md5 = $val;
0064             } else if (strcasecmp($key, 'date') == 0) {
0065                 $date = $val;
0066             }
0067         }
0068 
0069         // If we have an x-amz-date header, use that instead of the normal Date
0070         if (isset($headers['x-amz-date']) && isset($date)) {
0071             $date = '';
0072         }
0073 
0074         $sig_str = "$method\n$md5\n$type\n$date\n";
0075 
0076         // For x-amz- headers, combine like keys, lowercase them, sort them
0077         // alphabetically and remove excess spaces around values
0078         $amz_headers = array();
0079         foreach ($headers as $key => $val) {
0080             $key = strtolower($key);
0081             if (substr($key, 0, 6) == 'x-amz-') {
0082                 if (is_array($val)) {
0083                     $amz_headers[$key] = $val;
0084                 } else {
0085                     $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
0086                 }
0087             }
0088         }
0089         if (!empty($amz_headers)) {
0090             ksort($amz_headers);
0091             foreach ($amz_headers as $key => $val) {
0092                 $sig_str .= $key . ':' . implode(',', $val) . "\n";
0093             }
0094         }
0095 
0096         $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
0097         if (strpos($path, '?location') !== false) {
0098             $sig_str .= '?location';
0099         } else
0100             if (strpos($path, '?acl') !== false) {
0101                 $sig_str .= '?acl';
0102             } else
0103                 if (strpos($path, '?torrent') !== false) {
0104                     $sig_str .= '?torrent';
0105                 }
0106 
0107         $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_secretKey, 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
0108         $headers['Authorization'] = 'AWS ' . $this->_accessKey . ':' . $signature;
0109 
0110         return $sig_str;
0111     }
0112 }