web/lib/Zend/Service/Amazon/Authentication/S3.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_Service_Amazon
       
    17  * @subpackage Authentication
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_Amazon_Authentication
       
    25  */
       
    26 require_once 'Zend/Service/Amazon/Authentication.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Crypt_Hmac
       
    30  */
       
    31 require_once 'Zend/Crypt/Hmac.php';
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_Service_Amazon
       
    36  * @subpackage Authentication
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_Service_Amazon_Authentication_S3 extends Zend_Service_Amazon_Authentication
       
    41 {
       
    42     /**
       
    43      * Add the S3 Authorization signature to the request headers
       
    44      *
       
    45      * @param  string $method
       
    46      * @param  string $path
       
    47      * @param  array &$headers
       
    48      * @return string
       
    49      */
       
    50     public function generateSignature($method, $path, &$headers)
       
    51     {
       
    52         if (! is_array($headers)) {
       
    53             $headers = array($headers);
       
    54         }
       
    55         
       
    56         $type = $md5 = $date = '';
       
    57         
       
    58         // Search for the Content-type, Content-MD5 and Date headers
       
    59         foreach ($headers as $key => $val) {
       
    60             if (strcasecmp($key, 'content-type') == 0) {
       
    61                 $type = $val;
       
    62             } else if (strcasecmp($key, 'content-md5') == 0) {
       
    63                 $md5 = $val;
       
    64             } else if (strcasecmp($key, 'date') == 0) {
       
    65                 $date = $val;
       
    66             }
       
    67         }
       
    68         
       
    69         // If we have an x-amz-date header, use that instead of the normal Date
       
    70         if (isset($headers['x-amz-date']) && isset($date)) {
       
    71             $date = '';
       
    72         }
       
    73         
       
    74         $sig_str = "$method\n$md5\n$type\n$date\n";
       
    75 
       
    76         // For x-amz- headers, combine like keys, lowercase them, sort them
       
    77         // alphabetically and remove excess spaces around values
       
    78         $amz_headers = array();
       
    79         foreach ($headers as $key => $val) {
       
    80             $key = strtolower($key);
       
    81             if (substr($key, 0, 6) == 'x-amz-') {
       
    82                 if (is_array($val)) {
       
    83                     $amz_headers[$key] = $val;
       
    84                 } else {
       
    85                     $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
       
    86                 }
       
    87             }
       
    88         }
       
    89         if (!empty($amz_headers)) {
       
    90             ksort($amz_headers);
       
    91             foreach ($amz_headers as $key => $val) {
       
    92                 $sig_str .= $key . ':' . implode(',', $val) . "\n";
       
    93             }
       
    94         }
       
    95         
       
    96         $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
       
    97         if (strpos($path, '?location') !== false) {
       
    98             $sig_str .= '?location';
       
    99         } else 
       
   100             if (strpos($path, '?acl') !== false) {
       
   101                 $sig_str .= '?acl';
       
   102             } else 
       
   103                 if (strpos($path, '?torrent') !== false) {
       
   104                     $sig_str .= '?torrent';
       
   105                 }
       
   106         
       
   107         $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_secretKey, 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
       
   108         $headers['Authorization'] = 'AWS ' . $this->_accessKey . ':' . $signature;
       
   109 
       
   110         return $sig_str;
       
   111     }
       
   112 }