PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/Amazon/Authentication/S3.php

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