S3.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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
  17. * @subpackage Amazon_S3
  18. * @copyright Copyright (c) 2009, Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: S3.php 14190 2009-02-28 04:32:18Z jplock $
  21. */
  22. /**
  23. * @see Zend_Service_Amazon_Abstract
  24. */
  25. require_once 'Zend/Service/Amazon/Abstract.php';
  26. /**
  27. * @see Zend_Crypt_Hmac
  28. */
  29. require_once 'Zend/Crypt/Hmac.php';
  30. /**
  31. * Amazon S3 PHP connection class
  32. *
  33. * @category Zend
  34. * @package Zend_Service
  35. * @subpackage Amazon_S3
  36. * @copyright Copyright (c) 2005-2008, Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Service_Amazon_S3 extends Zend_Service_Amazon_Abstract
  40. {
  41. /**
  42. * Store for stream wrapper clients
  43. *
  44. * @var array
  45. */
  46. protected static $_wrapperClients = array();
  47. const S3_ENDPOINT = 'http://s3.amazonaws.com';
  48. const S3_ACL_PRIVATE = 'private';
  49. const S3_ACL_PUBLIC_READ = 'public-read';
  50. const S3_ACL_PUBLIC_WRITE = 'public-read-write';
  51. const S3_ACL_AUTH_READ = 'authenticated-read';
  52. const S3_ACL_HEADER = 'x-amz-acl';
  53. const S3_CONTENT_TYPE_HEADER = 'Content-Type';
  54. /**
  55. * Add a new bucket
  56. *
  57. * @param string $bucket
  58. * @return boolean
  59. */
  60. public function createBucket($bucket)
  61. {
  62. $len = strlen($bucket);
  63. if ($len < 3 || $len > 255) {
  64. /**
  65. * @see Zend_Service_Amazon_S3_Exception
  66. */
  67. require_once 'Zend/Service/Amazon/S3/Exception.php';
  68. throw new Zend_Service_Amazon_S3_Exception("Bucket name \"$bucket\" must be between 3 and 255 characters long");
  69. }
  70. if (preg_match('/[^a-z0-9\._-]/', $bucket)) {
  71. /**
  72. * @see Zend_Service_Amazon_S3_Exception
  73. */
  74. require_once 'Zend/Service/Amazon/S3/Exception.php';
  75. throw new Zend_Service_Amazon_S3_Exception("Bucket name \"$bucket\" contains invalid characters");
  76. }
  77. if (preg_match('/(\d+).(\d+).(\d+).(\d+)/', $bucket)) {
  78. /**
  79. * @see Zend_Service_Amazon_S3_Exception
  80. */
  81. require_once 'Zend/Service/Amazon/S3/Exception.php';
  82. throw new Zend_Service_Amazon_S3_Exception("Bucket name \"$bucket\" cannot be an IP address");
  83. }
  84. $response = $this->_makeRequest('PUT', $bucket);
  85. return ($response->getStatus() == 200);
  86. }
  87. /**
  88. * Checks if a given bucket name is available
  89. *
  90. * @param string $bucket
  91. * @return boolean
  92. */
  93. public function isBucketAvailable($bucket)
  94. {
  95. $response = $this->_makeRequest('HEAD', $bucket, array('max-keys'=>0));
  96. return ($response->getStatus() != 404);
  97. }
  98. /**
  99. * Checks if a given object exists
  100. *
  101. * @param string $object
  102. * @return boolean
  103. */
  104. public function isObjectAvailable($object)
  105. {
  106. $response = $this->_makeRequest('HEAD', $object);
  107. return ($response->getStatus() == 200);
  108. }
  109. /**
  110. * Remove a given bucket. All objects in the bucket must be removed prior
  111. * to removing the bucket.
  112. *
  113. * @param string $bucket
  114. * @return boolean
  115. */
  116. public function removeBucket($bucket)
  117. {
  118. $response = $this->_makeRequest('DELETE', $bucket);
  119. // Look for a 204 No Content response
  120. return ($response->getStatus() == 204);
  121. }
  122. /**
  123. * Get metadata information for a given object
  124. *
  125. * @param string $object
  126. * @return array|false
  127. */
  128. public function getInfo($object)
  129. {
  130. $info = array();
  131. $response = $this->_makeRequest('HEAD', $object);
  132. if ($response->getStatus() == 200) {
  133. $info['type'] = $response->getHeader('Content-type');
  134. $info['size'] = $response->getHeader('Content-length');
  135. $info['mtime'] = strtotime($response->getHeader('Last-modified'));
  136. $info['etag'] = $response->getHeader('ETag');
  137. }
  138. else {
  139. return false;
  140. }
  141. return $info;
  142. }
  143. /**
  144. * List the S3 buckets
  145. *
  146. * @return array|false
  147. */
  148. public function getBuckets()
  149. {
  150. $response = $this->_makeRequest('GET');
  151. if ($response->getStatus() != 200) {
  152. return false;
  153. }
  154. $xml = new SimpleXMLElement($response->getBody());
  155. $buckets = array();
  156. foreach ($xml->Buckets->Bucket as $bucket) {
  157. $buckets[] = (string)$bucket->Name;
  158. }
  159. return $buckets;
  160. }
  161. /**
  162. * Remove all objects in the bucket.
  163. *
  164. * @param string $bucket
  165. * @return boolean
  166. */
  167. public function cleanBucket($bucket)
  168. {
  169. $objects = $this->getObjectsByBucket($bucket);
  170. if (!$objects) {
  171. return false;
  172. }
  173. foreach ($objects as $object) {
  174. $this->removeObject("$bucket/$object");
  175. }
  176. return true;
  177. }
  178. /**
  179. * List the objects in a bucket.
  180. *
  181. * Provides the list of object keys that are contained in the bucket.
  182. *
  183. * @param string $bucket
  184. * @return array|false
  185. */
  186. public function getObjectsByBucket($bucket)
  187. {
  188. $response = $this->_makeRequest('GET', $bucket);
  189. if ($response->getStatus() != 200) {
  190. return false;
  191. }
  192. $xml = new SimpleXMLElement($response->getBody());
  193. $objects = array();
  194. if (isset($xml->Contents)) {
  195. foreach ($xml->Contents as $contents) {
  196. foreach ($contents->Key as $object) {
  197. $objects[] = (string)$object;
  198. }
  199. }
  200. }
  201. return $objects;
  202. }
  203. /**
  204. * Get an object
  205. *
  206. * @param string $object
  207. * @return string|false
  208. */
  209. public function getObject($object)
  210. {
  211. $response = $this->_makeRequest('GET', $object);
  212. if ($response->getStatus() != 200) {
  213. return false;
  214. }
  215. return $response->getBody();
  216. }
  217. /**
  218. * Upload an object by a PHP string
  219. *
  220. * @param string $object Object name
  221. * @param string $data Object data
  222. * @param array $meta Metadata
  223. * @return boolean
  224. */
  225. public function putObject($object, $data, $meta=null)
  226. {
  227. $headers = (is_array($meta)) ? $meta : array();
  228. $headers['Content-MD5'] = base64_encode(md5($data, true));
  229. $headers['Expect'] = '100-continue';
  230. if (!isset($headers[self::S3_CONTENT_TYPE_HEADER])) {
  231. $headers[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($object);
  232. }
  233. $response = $this->_makeRequest('PUT', $object, null, $headers, $data);
  234. // Check the MD5 Etag returned by S3 against and MD5 of the buffer
  235. if ($response->getStatus() == 200) {
  236. // It is escaped by double quotes for some reason
  237. $etag = str_replace('"', '', $response->getHeader('Etag'));
  238. if ($etag == md5($data)) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. /**
  245. * Put file to S3 as object
  246. *
  247. * @param string $path File name
  248. * @param string $object Object name
  249. * @param array $meta Metadata
  250. * @return boolean
  251. */
  252. public function putFile($path, $object, $meta=null)
  253. {
  254. $data = @file_get_contents($path);
  255. if ($data === false) {
  256. /**
  257. * @see Zend_Service_Amazon_S3_Exception
  258. */
  259. require_once 'Zend/Service/Amazon/S3/Exception.php';
  260. throw new Zend_Service_Amazon_S3_Exception("Cannot read file $path");
  261. }
  262. if (!is_array($meta)) {
  263. $meta = array();
  264. }
  265. if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
  266. $meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
  267. }
  268. return $this->putObject($object, $data, $meta);
  269. }
  270. /**
  271. * Remove a given object
  272. *
  273. * @param string $object
  274. * @return boolean
  275. */
  276. public function removeObject($object)
  277. {
  278. $response = $this->_makeRequest('DELETE', $object);
  279. // Look for a 204 No Content response
  280. return ($response->getStatus() == 204);
  281. }
  282. /**
  283. * Make a request to Amazon S3
  284. *
  285. * TODO: support bucket.s3.amazon.com style
  286. *
  287. * @param string $method
  288. * @param string $path
  289. * @param array $params
  290. * @param array $headers
  291. * @param string $data
  292. * @return Zend_Http_Response
  293. */
  294. public function _makeRequest($method, $path='', $params=null, $headers=array(), $data=null)
  295. {
  296. $retry_count = 0;
  297. if (!is_array($headers)) {
  298. $headers = array($headers);
  299. }
  300. $headers['Date'] = gmdate(DATE_RFC1123, time());
  301. self::addSignature($method, $path, $headers);
  302. $client = self::getHttpClient();
  303. $client->resetParameters();
  304. // Work around buglet in HTTP client - it doesn't clean headers
  305. // Remove when ZHC is fixed
  306. $client->setHeaders(array('Content-MD5' => null,
  307. 'Expect' => null,
  308. 'Range' => null,
  309. 'x-amz-acl' => null));
  310. $client->setUri(self::S3_ENDPOINT.'/'.$path);
  311. $client->setHeaders($headers);
  312. if (is_array($params)) {
  313. foreach ($params as $name=>$value) {
  314. $client->setParameterGet($name, $value);
  315. }
  316. }
  317. if (($method == 'PUT') && ($data !== null)) {
  318. if (!isset($headers['Content-type'])) {
  319. $headers['Content-type'] = self::getMimeType($path);
  320. }
  321. $client->setRawData($data, $headers['Content-type']);
  322. }
  323. do {
  324. $retry = false;
  325. $response = $client->request($method);
  326. $response_code = $response->getStatus();
  327. // Some 5xx errors are expected, so retry automatically
  328. if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
  329. $retry = true;
  330. $retry_count++;
  331. sleep($retry_count / 4 * $retry_count);
  332. }
  333. else if ($response_code == 307) {
  334. // Need to redirect, new S3 endpoint given
  335. // This should never happen as Zend_Http_Client will redirect automatically
  336. }
  337. else if ($response_code == 100) {
  338. echo 'OK to Continue';
  339. }
  340. } while ($retry);
  341. return $response;
  342. }
  343. /**
  344. * Add the S3 Authorization signature to the request headers
  345. *
  346. * @param string $method
  347. * @param string $path
  348. * @param array &$headers
  349. * @return string
  350. */
  351. protected function addSignature($method, $path, &$headers)
  352. {
  353. if (!is_array($headers)) {
  354. $headers = array($headers);
  355. }
  356. $type = $md5 = $date = '';
  357. // Search for the Content-type, Content-MD5 and Date headers
  358. foreach ($headers as $key=>$val) {
  359. if (strcasecmp($key, 'content-type') == 0) {
  360. $type = $val;
  361. }
  362. else if (strcasecmp($key, 'content-md5') == 0) {
  363. $md5 = $val;
  364. }
  365. else if (strcasecmp($key, 'date') == 0) {
  366. $date = $val;
  367. }
  368. }
  369. // If we have an x-amz-date header, use that instead of the normal Date
  370. if (isset($headers['x-amz-date']) && isset($date)) {
  371. $date = '';
  372. }
  373. $sig_str = "$method\n$md5\n$type\n$date\n";
  374. // For x-amz- headers, combine like keys, lowercase them, sort them
  375. // alphabetically and remove excess spaces around values
  376. $amz_headers = array();
  377. foreach ($headers as $key=>$val) {
  378. $key = strtolower($key);
  379. if (substr($key, 0, 6) == 'x-amz-') {
  380. if (is_array($val)) {
  381. $amz_headers[$key] = $val;
  382. }
  383. else {
  384. $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
  385. }
  386. }
  387. }
  388. if (!empty($amz_headers)) {
  389. ksort($amz_headers);
  390. foreach ($amz_headers as $key=>$val) {
  391. $sig_str .= $key.':'.implode(',', $val)."\n";
  392. }
  393. }
  394. $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
  395. if (strpos($path, '?location') !== false) {
  396. $sig_str .= '?location';
  397. }
  398. else if (strpos($path, '?acl') !== false) {
  399. $sig_str .= '?acl';
  400. }
  401. else if (strpos($path, '?torrent') !== false) {
  402. $sig_str .= '?torrent';
  403. }
  404. $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
  405. $headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;
  406. return $sig_str;
  407. }
  408. /**
  409. * Attempt to get the content-type of a file based on the extension
  410. *
  411. * TODO: move this to Zend_Mime
  412. *
  413. * @param string $path
  414. * @return string
  415. */
  416. public static function getMimeType($path)
  417. {
  418. $ext = substr(strrchr($path, '.'), 1);
  419. if(!$ext) {
  420. // shortcut
  421. return 'binary/octet-stream';
  422. }
  423. switch ($ext) {
  424. case 'xls':
  425. $content_type = 'application/excel';
  426. break;
  427. case 'hqx':
  428. $content_type = 'application/macbinhex40';
  429. break;
  430. case 'doc':
  431. case 'dot':
  432. case 'wrd':
  433. $content_type = 'application/msword';
  434. break;
  435. case 'pdf':
  436. $content_type = 'application/pdf';
  437. break;
  438. case 'pgp':
  439. $content_type = 'application/pgp';
  440. break;
  441. case 'ps':
  442. case 'eps':
  443. case 'ai':
  444. $content_type = 'application/postscript';
  445. break;
  446. case 'ppt':
  447. $content_type = 'application/powerpoint';
  448. break;
  449. case 'rtf':
  450. $content_type = 'application/rtf';
  451. break;
  452. case 'tgz':
  453. case 'gtar':
  454. $content_type = 'application/x-gtar';
  455. break;
  456. case 'gz':
  457. $content_type = 'application/x-gzip';
  458. break;
  459. case 'php':
  460. case 'php3':
  461. case 'php4':
  462. $content_type = 'application/x-httpd-php';
  463. break;
  464. case 'js':
  465. $content_type = 'application/x-javascript';
  466. break;
  467. case 'ppd':
  468. case 'psd':
  469. $content_type = 'application/x-photoshop';
  470. break;
  471. case 'swf':
  472. case 'swc':
  473. case 'rf':
  474. $content_type = 'application/x-shockwave-flash';
  475. break;
  476. case 'tar':
  477. $content_type = 'application/x-tar';
  478. break;
  479. case 'zip':
  480. $content_type = 'application/zip';
  481. break;
  482. case 'mid':
  483. case 'midi':
  484. case 'kar':
  485. $content_type = 'audio/midi';
  486. break;
  487. case 'mp2':
  488. case 'mp3':
  489. case 'mpga':
  490. $content_type = 'audio/mpeg';
  491. break;
  492. case 'ra':
  493. $content_type = 'audio/x-realaudio';
  494. break;
  495. case 'wav':
  496. $content_type = 'audio/wav';
  497. break;
  498. case 'bmp':
  499. $content_type = 'image/bitmap';
  500. break;
  501. case 'gif':
  502. $content_type = 'image/gif';
  503. break;
  504. case 'iff':
  505. $content_type = 'image/iff';
  506. break;
  507. case 'jb2':
  508. $content_type = 'image/jb2';
  509. break;
  510. case 'jpg':
  511. case 'jpe':
  512. case 'jpeg':
  513. $content_type = 'image/jpeg';
  514. break;
  515. case 'jpx':
  516. $content_type = 'image/jpx';
  517. break;
  518. case 'png':
  519. $content_type = 'image/png';
  520. break;
  521. case 'tif':
  522. case 'tiff':
  523. $content_type = 'image/tiff';
  524. break;
  525. case 'wbmp':
  526. $content_type = 'image/vnd.wap.wbmp';
  527. break;
  528. case 'xbm':
  529. $content_type = 'image/xbm';
  530. break;
  531. case 'css':
  532. $content_type = 'text/css';
  533. break;
  534. case 'txt':
  535. $content_type = 'text/plain';
  536. break;
  537. case 'htm':
  538. case 'html':
  539. $content_type = 'text/html';
  540. break;
  541. case 'xml':
  542. $content_type = 'text/xml';
  543. break;
  544. case 'xsl':
  545. $content_type = 'text/xsl';
  546. break;
  547. case 'mpg':
  548. case 'mpe':
  549. case 'mpeg':
  550. $content_type = 'video/mpeg';
  551. break;
  552. case 'qt':
  553. case 'mov':
  554. $content_type = 'video/quicktime';
  555. break;
  556. case 'avi':
  557. $content_type = 'video/x-ms-video';
  558. break;
  559. case 'eml':
  560. $content_type = 'message/rfc822';
  561. break;
  562. default:
  563. $content_type = 'binary/octet-stream';
  564. break;
  565. }
  566. return $content_type;
  567. }
  568. /**
  569. * Register this object as stream wrapper client
  570. *
  571. * @param string $name
  572. * @return Zend_Service_Amazon_S3
  573. */
  574. public function registerAsClient($name)
  575. {
  576. self::$_wrapperClients[$name] = $this;
  577. return $this;
  578. }
  579. /**
  580. * Unregister this object as stream wrapper client
  581. *
  582. * @param string $name
  583. * @return Zend_Service_Amazon_S3
  584. */
  585. public function unregisterAsClient($name)
  586. {
  587. unset(self::$_wrapperClients[$name]);
  588. return $this;
  589. }
  590. /**
  591. * Get wrapper client for stream type
  592. *
  593. * @param string $name
  594. * @return Zend_Service_Amazon_S3
  595. */
  596. public static function getWrapperClient($name)
  597. {
  598. return self::$_wrapperClients[$name];
  599. }
  600. /**
  601. * Register this object as stream wrapper
  602. *
  603. * @param string $name
  604. * @return Zend_Service_Amazon_S3
  605. */
  606. public function registerStreamWrapper($name='s3')
  607. {
  608. /**
  609. * @see Zend_Service_Amazon_S3_Stream
  610. */
  611. require_once 'Zend/Service/Amazon/S3/Stream.php';
  612. stream_register_wrapper($name, 'Zend_Service_Amazon_S3_Stream');
  613. $this->registerAsClient($name);
  614. }
  615. /**
  616. * Unregister this object as stream wrapper
  617. *
  618. * @param string $name
  619. * @return Zend_Service_Amazon_S3
  620. */
  621. public function unregisterStreamWrapper($name='s3')
  622. {
  623. stream_wrapper_unregister($name);
  624. $this->unregisterAsClient($name);
  625. }
  626. }