S3.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. $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. $client->setUri(self::S3_ENDPOINT.'/'.$path);
  331. $client->setHeaders($headers);
  332. if (is_array($params)) {
  333. foreach ($params as $name=>$value) {
  334. $client->setParameterGet($name, $value);
  335. }
  336. }
  337. if (($method == 'PUT') && ($data !== null)) {
  338. if (!isset($headers['Content-type'])) {
  339. $headers['Content-type'] = self::getMimeType($path);
  340. }
  341. $client->setRawData($data, $headers['Content-type']);
  342. }
  343. do {
  344. $retry = false;
  345. $response = $client->request($method);
  346. $response_code = $response->getStatus();
  347. // Some 5xx errors are expected, so retry automatically
  348. if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
  349. $retry = true;
  350. $retry_count++;
  351. sleep($retry_count / 4 * $retry_count);
  352. }
  353. else if ($response_code == 307) {
  354. // Need to redirect, new S3 endpoint given
  355. // This should never happen as Zend_Http_Client will redirect automatically
  356. }
  357. else if ($response_code == 100) {
  358. echo 'OK to Continue';
  359. }
  360. } while ($retry);
  361. return $response;
  362. }
  363. /**
  364. * Add the S3 Authorization signature to the request headers
  365. *
  366. * @param string $method
  367. * @param string $path
  368. * @param array &$headers
  369. * @return string
  370. */
  371. protected function addSignature($method, $path, &$headers)
  372. {
  373. if (!is_array($headers)) {
  374. $headers = array($headers);
  375. }
  376. $type = $md5 = $date = '';
  377. // Search for the Content-type, Content-MD5 and Date headers
  378. foreach ($headers as $key=>$val) {
  379. if (strcasecmp($key, 'content-type') == 0) {
  380. $type = $val;
  381. }
  382. else if (strcasecmp($key, 'content-md5') == 0) {
  383. $md5 = $val;
  384. }
  385. else if (strcasecmp($key, 'date') == 0) {
  386. $date = $val;
  387. }
  388. }
  389. // If we have an x-amz-date header, use that instead of the normal Date
  390. if (isset($headers['x-amz-date']) && isset($date)) {
  391. $date = '';
  392. }
  393. $sig_str = "$method\n$md5\n$type\n$date\n";
  394. // For x-amz- headers, combine like keys, lowercase them, sort them
  395. // alphabetically and remove excess spaces around values
  396. $amz_headers = array();
  397. foreach ($headers as $key=>$val) {
  398. $key = strtolower($key);
  399. if (substr($key, 0, 6) == 'x-amz-') {
  400. if (is_array($val)) {
  401. $amz_headers[$key] = $val;
  402. }
  403. else {
  404. $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
  405. }
  406. }
  407. }
  408. if (!empty($amz_headers)) {
  409. ksort($amz_headers);
  410. foreach ($amz_headers as $key=>$val) {
  411. $sig_str .= $key.':'.implode(',', $val)."\n";
  412. }
  413. }
  414. $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
  415. if (strpos($path, '?location') !== false) {
  416. $sig_str .= '?location';
  417. }
  418. else if (strpos($path, '?acl') !== false) {
  419. $sig_str .= '?acl';
  420. }
  421. else if (strpos($path, '?torrent') !== false) {
  422. $sig_str .= '?torrent';
  423. }
  424. $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
  425. $headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;
  426. return $sig_str;
  427. }
  428. /**
  429. * Attempt to get the content-type of a file based on the extension
  430. *
  431. * TODO: move this to Zend_Mime
  432. *
  433. * @param string $path
  434. * @return string
  435. */
  436. public static function getMimeType($path)
  437. {
  438. $ext = substr(strrchr($path, '.'), 1);
  439. if(!$ext) {
  440. // shortcut
  441. return 'binary/octet-stream';
  442. }
  443. switch ($ext) {
  444. case 'xls':
  445. $content_type = 'application/excel';
  446. break;
  447. case 'hqx':
  448. $content_type = 'application/macbinhex40';
  449. break;
  450. case 'doc':
  451. case 'dot':
  452. case 'wrd':
  453. $content_type = 'application/msword';
  454. break;
  455. case 'pdf':
  456. $content_type = 'application/pdf';
  457. break;
  458. case 'pgp':
  459. $content_type = 'application/pgp';
  460. break;
  461. case 'ps':
  462. case 'eps':
  463. case 'ai':
  464. $content_type = 'application/postscript';
  465. break;
  466. case 'ppt':
  467. $content_type = 'application/powerpoint';
  468. break;
  469. case 'rtf':
  470. $content_type = 'application/rtf';
  471. break;
  472. case 'tgz':
  473. case 'gtar':
  474. $content_type = 'application/x-gtar';
  475. break;
  476. case 'gz':
  477. $content_type = 'application/x-gzip';
  478. break;
  479. case 'php':
  480. case 'php3':
  481. case 'php4':
  482. $content_type = 'application/x-httpd-php';
  483. break;
  484. case 'js':
  485. $content_type = 'application/x-javascript';
  486. break;
  487. case 'ppd':
  488. case 'psd':
  489. $content_type = 'application/x-photoshop';
  490. break;
  491. case 'swf':
  492. case 'swc':
  493. case 'rf':
  494. $content_type = 'application/x-shockwave-flash';
  495. break;
  496. case 'tar':
  497. $content_type = 'application/x-tar';
  498. break;
  499. case 'zip':
  500. $content_type = 'application/zip';
  501. break;
  502. case 'mid':
  503. case 'midi':
  504. case 'kar':
  505. $content_type = 'audio/midi';
  506. break;
  507. case 'mp2':
  508. case 'mp3':
  509. case 'mpga':
  510. $content_type = 'audio/mpeg';
  511. break;
  512. case 'ra':
  513. $content_type = 'audio/x-realaudio';
  514. break;
  515. case 'wav':
  516. $content_type = 'audio/wav';
  517. break;
  518. case 'bmp':
  519. $content_type = 'image/bitmap';
  520. break;
  521. case 'gif':
  522. $content_type = 'image/gif';
  523. break;
  524. case 'iff':
  525. $content_type = 'image/iff';
  526. break;
  527. case 'jb2':
  528. $content_type = 'image/jb2';
  529. break;
  530. case 'jpg':
  531. case 'jpe':
  532. case 'jpeg':
  533. $content_type = 'image/jpeg';
  534. break;
  535. case 'jpx':
  536. $content_type = 'image/jpx';
  537. break;
  538. case 'png':
  539. $content_type = 'image/png';
  540. break;
  541. case 'tif':
  542. case 'tiff':
  543. $content_type = 'image/tiff';
  544. break;
  545. case 'wbmp':
  546. $content_type = 'image/vnd.wap.wbmp';
  547. break;
  548. case 'xbm':
  549. $content_type = 'image/xbm';
  550. break;
  551. case 'css':
  552. $content_type = 'text/css';
  553. break;
  554. case 'txt':
  555. $content_type = 'text/plain';
  556. break;
  557. case 'htm':
  558. case 'html':
  559. $content_type = 'text/html';
  560. break;
  561. case 'xml':
  562. $content_type = 'text/xml';
  563. break;
  564. case 'xsl':
  565. $content_type = 'text/xsl';
  566. break;
  567. case 'mpg':
  568. case 'mpe':
  569. case 'mpeg':
  570. $content_type = 'video/mpeg';
  571. break;
  572. case 'qt':
  573. case 'mov':
  574. $content_type = 'video/quicktime';
  575. break;
  576. case 'avi':
  577. $content_type = 'video/x-ms-video';
  578. break;
  579. case 'eml':
  580. $content_type = 'message/rfc822';
  581. break;
  582. default:
  583. $content_type = 'binary/octet-stream';
  584. break;
  585. }
  586. return $content_type;
  587. }
  588. /**
  589. * Register this object as stream wrapper client
  590. *
  591. * @param string $name
  592. * @return Zend_Service_Amazon_S3
  593. */
  594. public function registerAsClient($name)
  595. {
  596. self::$_wrapperClients[$name] = $this;
  597. return $this;
  598. }
  599. /**
  600. * Unregister this object as stream wrapper client
  601. *
  602. * @param string $name
  603. * @return Zend_Service_Amazon_S3
  604. */
  605. public function unregisterAsClient($name)
  606. {
  607. unset(self::$_wrapperClients[$name]);
  608. return $this;
  609. }
  610. /**
  611. * Get wrapper client for stream type
  612. *
  613. * @param string $name
  614. * @return Zend_Service_Amazon_S3
  615. */
  616. public static function getWrapperClient($name)
  617. {
  618. return self::$_wrapperClients[$name];
  619. }
  620. /**
  621. * Register this object as stream wrapper
  622. *
  623. * @param string $name
  624. * @return Zend_Service_Amazon_S3
  625. */
  626. public function registerStreamWrapper($name='s3')
  627. {
  628. /**
  629. * @see Zend_Service_Amazon_S3_Stream
  630. */
  631. require_once 'Zend/Service/Amazon/S3/Stream.php';
  632. stream_register_wrapper($name, 'Zend_Service_Amazon_S3_Stream');
  633. $this->registerAsClient($name);
  634. }
  635. /**
  636. * Unregister this object as stream wrapper
  637. *
  638. * @param string $name
  639. * @return Zend_Service_Amazon_S3
  640. */
  641. public function unregisterStreamWrapper($name='s3')
  642. {
  643. stream_wrapper_unregister($name);
  644. $this->unregisterAsClient($name);
  645. }
  646. }