S3.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 = '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. $object = $this->_fixupObjectName($object);
  132. $response = $this->_makeRequest('HEAD', $object);
  133. if ($response->getStatus() == 200) {
  134. $info['type'] = $response->getHeader('Content-type');
  135. $info['size'] = $response->getHeader('Content-length');
  136. $info['mtime'] = strtotime($response->getHeader('Last-modified'));
  137. $info['etag'] = $response->getHeader('ETag');
  138. }
  139. else {
  140. return false;
  141. }
  142. return $info;
  143. }
  144. /**
  145. * List the S3 buckets
  146. *
  147. * @return array|false
  148. */
  149. public function getBuckets()
  150. {
  151. $response = $this->_makeRequest('GET');
  152. if ($response->getStatus() != 200) {
  153. return false;
  154. }
  155. $xml = new SimpleXMLElement($response->getBody());
  156. $buckets = array();
  157. foreach ($xml->Buckets->Bucket as $bucket) {
  158. $buckets[] = (string)$bucket->Name;
  159. }
  160. return $buckets;
  161. }
  162. /**
  163. * Remove all objects in the bucket.
  164. *
  165. * @param string $bucket
  166. * @return boolean
  167. */
  168. public function cleanBucket($bucket)
  169. {
  170. $objects = $this->getObjectsByBucket($bucket);
  171. if (!$objects) {
  172. return false;
  173. }
  174. foreach ($objects as $object) {
  175. $this->removeObject("$bucket/$object");
  176. }
  177. return true;
  178. }
  179. /**
  180. * List the objects in a bucket.
  181. *
  182. * Provides the list of object keys that are contained in the bucket.
  183. *
  184. * @param string $bucket
  185. * @return array|false
  186. */
  187. public function getObjectsByBucket($bucket)
  188. {
  189. $response = $this->_makeRequest('GET', $bucket);
  190. if ($response->getStatus() != 200) {
  191. return false;
  192. }
  193. $xml = new SimpleXMLElement($response->getBody());
  194. $objects = array();
  195. if (isset($xml->Contents)) {
  196. foreach ($xml->Contents as $contents) {
  197. foreach ($contents->Key as $object) {
  198. $objects[] = (string)$object;
  199. }
  200. }
  201. }
  202. return $objects;
  203. }
  204. protected function _fixupObjectName($object)
  205. {
  206. $nameparts = explode('/', $object, 2);
  207. if (preg_match('/[^a-z0-9\._-]/', $nameparts[0])) {
  208. /**
  209. * @see Zend_Service_Amazon_S3_Exception
  210. */
  211. require_once 'Zend/Service/Amazon/S3/Exception.php';
  212. throw new Zend_Service_Amazon_S3_Exception("Bucket name contains invalid characters");
  213. }
  214. if(empty($nameparts[1])) {
  215. return $object;
  216. }
  217. return $nameparts[0].'/'.urlencode($nameparts[1]);
  218. }
  219. /**
  220. * Get an object
  221. *
  222. * @param string $object
  223. * @return string|false
  224. */
  225. public function getObject($object)
  226. {
  227. $object = $this->_fixupObjectName($object);
  228. $response = $this->_makeRequest('GET', $object);
  229. if ($response->getStatus() != 200) {
  230. return false;
  231. }
  232. return $response->getBody();
  233. }
  234. /**
  235. * Upload an object by a PHP string
  236. *
  237. * @param string $object Object name
  238. * @param string $data Object data
  239. * @param array $meta Metadata
  240. * @return boolean
  241. */
  242. public function putObject($object, $data, $meta=null)
  243. {
  244. $object = $this->_fixupObjectName($object);
  245. $headers = (is_array($meta)) ? $meta : array();
  246. $headers['Content-MD5'] = base64_encode(md5($data, true));
  247. $headers['Expect'] = '100-continue';
  248. if (!isset($headers[self::S3_CONTENT_TYPE_HEADER])) {
  249. $headers[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($object);
  250. }
  251. $response = $this->_makeRequest('PUT', $object, null, $headers, $data);
  252. // Check the MD5 Etag returned by S3 against and MD5 of the buffer
  253. if ($response->getStatus() == 200) {
  254. // It is escaped by double quotes for some reason
  255. $etag = str_replace('"', '', $response->getHeader('Etag'));
  256. if ($etag == md5($data)) {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. /**
  263. * Put file to S3 as object
  264. *
  265. * @param string $path File name
  266. * @param string $object Object name
  267. * @param array $meta Metadata
  268. * @return boolean
  269. */
  270. public function putFile($path, $object, $meta=null)
  271. {
  272. $object = $this->_fixupObjectName($object);
  273. $data = @file_get_contents($path);
  274. if ($data === false) {
  275. /**
  276. * @see Zend_Service_Amazon_S3_Exception
  277. */
  278. require_once 'Zend/Service/Amazon/S3/Exception.php';
  279. throw new Zend_Service_Amazon_S3_Exception("Cannot read file $path");
  280. }
  281. if (!is_array($meta)) {
  282. $meta = array();
  283. }
  284. if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
  285. $meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
  286. }
  287. return $this->putObject($object, $data, $meta);
  288. }
  289. /**
  290. * Remove a given object
  291. *
  292. * @param string $object
  293. * @return boolean
  294. */
  295. public function removeObject($object)
  296. {
  297. $object = $this->_fixupObjectName($object);
  298. $response = $this->_makeRequest('DELETE', $object);
  299. // Look for a 204 No Content response
  300. return ($response->getStatus() == 204);
  301. }
  302. /**
  303. * Make a request to Amazon S3
  304. *
  305. * TODO: support bucket.s3.amazon.com style
  306. *
  307. * @param string $method
  308. * @param string $path
  309. * @param array $params
  310. * @param array $headers
  311. * @param string $data
  312. * @return Zend_Http_Response
  313. */
  314. public function _makeRequest($method, $path='', $params=null, $headers=array(), $data=null)
  315. {
  316. $retry_count = 0;
  317. if (!is_array($headers)) {
  318. $headers = array($headers);
  319. }
  320. $headers['Date'] = gmdate(DATE_RFC1123, time());
  321. self::addSignature($method, $path, $headers);
  322. $client = self::getHttpClient();
  323. $client->resetParameters();
  324. // Work around buglet in HTTP client - it doesn't clean headers
  325. // Remove when ZHC is fixed
  326. $client->setHeaders(array('Content-MD5' => null,
  327. 'Expect' => null,
  328. 'Range' => null,
  329. 'x-amz-acl' => null));
  330. // build the end point out
  331. $parts = explode('/', $path);
  332. if(count($parts) > 1) {
  333. $endpoint = 'http://' . $parts[0] . '.' . self::S3_ENDPOINT;
  334. array_shift($parts);
  335. $client->setUri($endpoint . '/' . implode('/', $parts));
  336. } else {
  337. $client->setUri('http://' . self::S3_ENDPOINT.'/'.$path);
  338. }
  339. $client->setHeaders($headers);
  340. if (is_array($params)) {
  341. foreach ($params as $name=>$value) {
  342. $client->setParameterGet($name, $value);
  343. }
  344. }
  345. if (($method == 'PUT') && ($data !== null)) {
  346. if (!isset($headers['Content-type'])) {
  347. $headers['Content-type'] = self::getMimeType($path);
  348. }
  349. $client->setRawData($data, $headers['Content-type']);
  350. }
  351. do {
  352. $retry = false;
  353. $response = $client->request($method);
  354. $response_code = $response->getStatus();
  355. // Some 5xx errors are expected, so retry automatically
  356. if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
  357. $retry = true;
  358. $retry_count++;
  359. sleep($retry_count / 4 * $retry_count);
  360. }
  361. else if ($response_code == 307) {
  362. // Need to redirect, new S3 endpoint given
  363. // This should never happen as Zend_Http_Client will redirect automatically
  364. }
  365. else if ($response_code == 100) {
  366. echo 'OK to Continue';
  367. }
  368. } while ($retry);
  369. return $response;
  370. }
  371. /**
  372. * Add the S3 Authorization signature to the request headers
  373. *
  374. * @param string $method
  375. * @param string $pathhttp://
  376. * @param array &$headers
  377. * @return string
  378. */
  379. protected function addSignature($method, $path, &$headers)
  380. {
  381. if (!is_array($headers)) {
  382. $headers = array($headers);
  383. }
  384. $type = $md5 = $date = '';
  385. // Search for the Content-type, Content-MD5 and Date headers
  386. foreach ($headers as $key=>$val) {
  387. if (strcasecmp($key, 'content-type') == 0) {
  388. $type = $val;
  389. }
  390. else if (strcasecmp($key, 'content-md5') == 0) {
  391. $md5 = $val;
  392. }
  393. else if (strcasecmp($key, 'date') == 0) {
  394. $date = $val;
  395. }
  396. }
  397. // If we have an x-amz-date header, use that instead of the normal Date
  398. if (isset($headers['x-amz-date']) && isset($date)) {
  399. $date = '';
  400. }
  401. $sig_str = "$method\n$md5\n$type\n$date\n";
  402. // For x-amz- headers, combine like keys, lowercase them, sort them
  403. // alphabetically and remove excess spaces around values
  404. $amz_headers = array();
  405. foreach ($headers as $key=>$val) {
  406. $key = strtolower($key);
  407. if (substr($key, 0, 6) == 'x-amz-') {
  408. if (is_array($val)) {
  409. $amz_headers[$key] = $val;
  410. }
  411. else {
  412. $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
  413. }
  414. }
  415. }
  416. if (!empty($amz_headers)) {
  417. ksort($amz_headers);
  418. foreach ($amz_headers as $key=>$val) {
  419. $sig_str .= $key.':'.implode(',', $val)."\n";
  420. }
  421. }
  422. $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
  423. if (strpos($path, '?location') !== false) {
  424. $sig_str .= '?location';
  425. }
  426. else if (strpos($path, '?acl') !== false) {
  427. $sig_str .= '?acl';
  428. }
  429. else if (strpos($path, '?torrent') !== false) {
  430. $sig_str .= '?torrent';
  431. }
  432. $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
  433. $headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;
  434. return $sig_str;
  435. }
  436. /**
  437. * Attempt to get the content-type of a file based on the extension
  438. *
  439. * TODO: move this to Zend_Mime
  440. *
  441. * @param string $path
  442. * @return string
  443. */
  444. public static function getMimeType($path)
  445. {
  446. $ext = substr(strrchr($path, '.'), 1);
  447. if(!$ext) {
  448. // shortcut
  449. return 'binary/octet-stream';
  450. }
  451. switch ($ext) {
  452. case 'xls':
  453. $content_type = 'application/excel';
  454. break;
  455. case 'hqx':
  456. $content_type = 'application/macbinhex40';
  457. break;
  458. case 'doc':
  459. case 'dot':
  460. case 'wrd':
  461. $content_type = 'application/msword';
  462. break;
  463. case 'pdf':
  464. $content_type = 'application/pdf';
  465. break;
  466. case 'pgp':
  467. $content_type = 'application/pgp';
  468. break;
  469. case 'ps':
  470. case 'eps':
  471. case 'ai':
  472. $content_type = 'application/postscript';
  473. break;
  474. case 'ppt':
  475. $content_type = 'application/powerpoint';
  476. break;
  477. case 'rtf':
  478. $content_type = 'application/rtf';
  479. break;
  480. case 'tgz':
  481. case 'gtar':
  482. $content_type = 'application/x-gtar';
  483. break;
  484. case 'gz':
  485. $content_type = 'application/x-gzip';
  486. break;
  487. case 'php':
  488. case 'php3':
  489. case 'php4':
  490. $content_type = 'application/x-httpd-php';
  491. break;
  492. case 'js':
  493. $content_type = 'application/x-javascript';
  494. break;
  495. case 'ppd':
  496. case 'psd':
  497. $content_type = 'application/x-photoshop';
  498. break;
  499. case 'swf':
  500. case 'swc':
  501. case 'rf':
  502. $content_type = 'application/x-shockwave-flash';
  503. break;
  504. case 'tar':
  505. $content_type = 'application/x-tar';
  506. break;
  507. case 'zip':
  508. $content_type = 'application/zip';
  509. break;
  510. case 'mid':
  511. case 'midi':
  512. case 'kar':
  513. $content_type = 'audio/midi';
  514. break;
  515. case 'mp2':
  516. case 'mp3':
  517. case 'mpga':
  518. $content_type = 'audio/mpeg';
  519. break;
  520. case 'ra':
  521. $content_type = 'audio/x-realaudio';
  522. break;
  523. case 'wav':
  524. $content_type = 'audio/wav';
  525. break;
  526. case 'bmp':
  527. $content_type = 'image/bitmap';
  528. break;
  529. case 'gif':
  530. $content_type = 'image/gif';
  531. break;
  532. case 'iff':
  533. $content_type = 'image/iff';
  534. break;
  535. case 'jb2':
  536. $content_type = 'image/jb2';
  537. break;
  538. case 'jpg':
  539. case 'jpe':
  540. case 'jpeg':
  541. $content_type = 'image/jpeg';
  542. break;
  543. case 'jpx':
  544. $content_type = 'image/jpx';
  545. break;
  546. case 'png':
  547. $content_type = 'image/png';
  548. break;
  549. case 'tif':
  550. case 'tiff':
  551. $content_type = 'image/tiff';
  552. break;
  553. case 'wbmp':
  554. $content_type = 'image/vnd.wap.wbmp';
  555. break;
  556. case 'xbm':
  557. $content_type = 'image/xbm';
  558. break;
  559. case 'css':
  560. $content_type = 'text/css';
  561. break;
  562. case 'txt':
  563. $content_type = 'text/plain';
  564. break;
  565. case 'htm':
  566. case 'html':
  567. $content_type = 'text/html';
  568. break;
  569. case 'xml':
  570. $content_type = 'text/xml';
  571. break;
  572. case 'xsl':
  573. $content_type = 'text/xsl';
  574. break;
  575. case 'mpg':
  576. case 'mpe':
  577. case 'mpeg':
  578. $content_type = 'video/mpeg';
  579. break;
  580. case 'qt':
  581. case 'mov':
  582. $content_type = 'video/quicktime';
  583. break;
  584. case 'avi':
  585. $content_type = 'video/x-ms-video';
  586. break;
  587. case 'eml':
  588. $content_type = 'message/rfc822';
  589. break;
  590. default:
  591. $content_type = 'binary/octet-stream';
  592. break;
  593. }
  594. return $content_type;
  595. }
  596. /**
  597. * Register this object as stream wrapper client
  598. *
  599. * @param string $name
  600. * @return Zend_Service_Amazon_S3
  601. */
  602. public function registerAsClient($name)
  603. {
  604. self::$_wrapperClients[$name] = $this;
  605. return $this;
  606. }
  607. /**
  608. * Unregister this object as stream wrapper client
  609. *
  610. * @param string $name
  611. * @return Zend_Service_Amazon_S3
  612. */
  613. public function unregisterAsClient($name)
  614. {
  615. unset(self::$_wrapperClients[$name]);
  616. return $this;
  617. }
  618. /**
  619. * Get wrapper client for stream type
  620. *
  621. * @param string $name
  622. * @return Zend_Service_Amazon_S3
  623. */
  624. public static function getWrapperClient($name)
  625. {
  626. return self::$_wrapperClients[$name];
  627. }
  628. /**
  629. * Register this object as stream wrapper
  630. *
  631. * @param string $name
  632. * @return Zend_Service_Amazon_S3
  633. */
  634. public function registerStreamWrapper($name='s3')
  635. {
  636. /**
  637. * @see Zend_Service_Amazon_S3_Stream
  638. */
  639. require_once 'Zend/Service/Amazon/S3/Stream.php';
  640. stream_register_wrapper($name, 'Zend_Service_Amazon_S3_Stream');
  641. $this->registerAsClient($name);
  642. }
  643. /**
  644. * Unregister this object as stream wrapper
  645. *
  646. * @param string $name
  647. * @return Zend_Service_Amazon_S3
  648. */
  649. public function unregisterStreamWrapper($name='s3')
  650. {
  651. stream_wrapper_unregister($name);
  652. $this->unregisterAsClient($name);
  653. }
  654. }