web/lib/Zend/Service/Amazon/Authentication/S3.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Service_Amazon
    16  * @package    Zend_Service_Amazon
    17  * @subpackage Authentication
    17  * @subpackage Authentication
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  */
    20  */
    21 
    21 
    22 
    22 
    23 /**
    23 /**
    32 
    32 
    33 /**
    33 /**
    34  * @category   Zend
    34  * @category   Zend
    35  * @package    Zend_Service_Amazon
    35  * @package    Zend_Service_Amazon
    36  * @subpackage Authentication
    36  * @subpackage Authentication
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  */
    39  */
    40 class Zend_Service_Amazon_Authentication_S3 extends Zend_Service_Amazon_Authentication
    40 class Zend_Service_Amazon_Authentication_S3 extends Zend_Service_Amazon_Authentication
    41 {
    41 {
    42     /**
    42     /**
    50     public function generateSignature($method, $path, &$headers)
    50     public function generateSignature($method, $path, &$headers)
    51     {
    51     {
    52         if (! is_array($headers)) {
    52         if (! is_array($headers)) {
    53             $headers = array($headers);
    53             $headers = array($headers);
    54         }
    54         }
    55         
    55 
    56         $type = $md5 = $date = '';
    56         $type = $md5 = $date = '';
    57         
    57 
    58         // Search for the Content-type, Content-MD5 and Date headers
    58         // Search for the Content-type, Content-MD5 and Date headers
    59         foreach ($headers as $key => $val) {
    59         foreach ($headers as $key => $val) {
    60             if (strcasecmp($key, 'content-type') == 0) {
    60             if (strcasecmp($key, 'content-type') == 0) {
    61                 $type = $val;
    61                 $type = $val;
    62             } else if (strcasecmp($key, 'content-md5') == 0) {
    62             } else if (strcasecmp($key, 'content-md5') == 0) {
    63                 $md5 = $val;
    63                 $md5 = $val;
    64             } else if (strcasecmp($key, 'date') == 0) {
    64             } else if (strcasecmp($key, 'date') == 0) {
    65                 $date = $val;
    65                 $date = $val;
    66             }
    66             }
    67         }
    67         }
    68         
    68 
    69         // If we have an x-amz-date header, use that instead of the normal Date
    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)) {
    70         if (isset($headers['x-amz-date']) && isset($date)) {
    71             $date = '';
    71             $date = '';
    72         }
    72         }
    73         
    73 
    74         $sig_str = "$method\n$md5\n$type\n$date\n";
    74         $sig_str = "$method\n$md5\n$type\n$date\n";
    75 
    75 
    76         // For x-amz- headers, combine like keys, lowercase them, sort them
    76         // For x-amz- headers, combine like keys, lowercase them, sort them
    77         // alphabetically and remove excess spaces around values
    77         // alphabetically and remove excess spaces around values
    78         $amz_headers = array();
    78         $amz_headers = array();
    90             ksort($amz_headers);
    90             ksort($amz_headers);
    91             foreach ($amz_headers as $key => $val) {
    91             foreach ($amz_headers as $key => $val) {
    92                 $sig_str .= $key . ':' . implode(',', $val) . "\n";
    92                 $sig_str .= $key . ':' . implode(',', $val) . "\n";
    93             }
    93             }
    94         }
    94         }
    95         
    95 
    96         $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
    96         $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
    97         if (strpos($path, '?location') !== false) {
    97         if (strpos($path, '?location') !== false) {
    98             $sig_str .= '?location';
    98             $sig_str .= '?location';
    99         } else 
    99         } else
   100             if (strpos($path, '?acl') !== false) {
   100             if (strpos($path, '?acl') !== false) {
   101                 $sig_str .= '?acl';
   101                 $sig_str .= '?acl';
   102             } else 
   102             } else
   103                 if (strpos($path, '?torrent') !== false) {
   103                 if (strpos($path, '?torrent') !== false) {
   104                     $sig_str .= '?torrent';
   104                     $sig_str .= '?torrent';
   105                 }
   105                 }
   106         
   106 
   107         $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_secretKey, 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
   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;
   108         $headers['Authorization'] = 'AWS ' . $this->_accessKey . ':' . $signature;
   109 
   109 
   110         return $sig_str;
   110         return $sig_str;
   111     }
   111     }