PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/zendframework/zf2
PHP | 968 lines | 580 code | 111 blank | 277 comment | 103 complexity | 454a6772b73250cb3e57f31bf1923206 MD5 | raw file
Possible License(s): BSD-3-Clause
  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) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Service\Amazon\S3;
  25. use Zend\Crypt,
  26. Zend\Service\Amazon,
  27. Zend\Service\Amazon\S3\Exception,
  28. Zend\Uri;
  29. /**
  30. * Amazon S3 PHP connection class
  31. *
  32. * @category Zend
  33. * @package Zend_Service
  34. * @subpackage Amazon_S3
  35. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
  38. */
  39. class S3 extends \Zend\Service\Amazon\AbstractAmazon
  40. {
  41. /**
  42. * Store for stream wrapper clients
  43. *
  44. * @var array
  45. */
  46. protected static $_wrapperClients = array();
  47. /**
  48. * Endpoint for the service
  49. *
  50. * @var Uri\Uri
  51. */
  52. protected $_endpoint;
  53. const S3_ENDPOINT = 's3.amazonaws.com';
  54. const S3_ACL_PRIVATE = 'private';
  55. const S3_ACL_PUBLIC_READ = 'public-read';
  56. const S3_ACL_PUBLIC_WRITE = 'public-read-write';
  57. const S3_ACL_AUTH_READ = 'authenticated-read';
  58. const S3_REQUESTPAY_HEADER = 'x-amz-request-payer';
  59. const S3_ACL_HEADER = 'x-amz-acl';
  60. const S3_CONTENT_TYPE_HEADER = 'Content-Type';
  61. /**
  62. * Set S3 endpoint to use
  63. *
  64. * @param string|Uri\Uri $endpoint
  65. * @return S3
  66. */
  67. public function setEndpoint($endpoint)
  68. {
  69. if (!($endpoint instanceof Uri\Uri)) {
  70. $endpoint = Uri\UriFactory::factory($endpoint);
  71. }
  72. if (!$endpoint->isValid()) {
  73. throw new Exception\InvalidArgumentException('Invalid endpoint supplied');
  74. }
  75. $this->_endpoint = $endpoint;
  76. return $this;
  77. }
  78. /**
  79. * Get current S3 endpoint
  80. *
  81. * @return Uri\Uri
  82. */
  83. public function getEndpoint()
  84. {
  85. return $this->_endpoint;
  86. }
  87. /**
  88. * Constructor
  89. *
  90. * @param string $accessKey
  91. * @param string $secretKey
  92. * @param string $region
  93. */
  94. public function __construct($accessKey=null, $secretKey=null, $region=null)
  95. {
  96. parent::__construct($accessKey, $secretKey, $region);
  97. $this->setEndpoint('http://'.self::S3_ENDPOINT);
  98. }
  99. /**
  100. * Verify if the bucket name is valid
  101. *
  102. * @param string $bucket
  103. * @return boolean
  104. */
  105. public function _validBucketName($bucket)
  106. {
  107. $len = strlen($bucket);
  108. if ($len < 3 || $len > 255) {
  109. throw new Exception\InvalidArgumentException("Bucket name \"$bucket\" must be between 3 and 255 characters long");
  110. }
  111. if (preg_match('/[^a-z0-9\._-]/', $bucket)) {
  112. throw new Exception\InvalidArgumentException("Bucket name \"$bucket\" contains invalid characters");
  113. }
  114. if (preg_match('/(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/', $bucket)) {
  115. throw new Exception\InvalidArgumentException("Bucket name \"$bucket\" cannot be an IP address");
  116. }
  117. return true;
  118. }
  119. /**
  120. * Add a new bucket
  121. *
  122. * @param string $bucket
  123. * @return boolean
  124. */
  125. public function createBucket($bucket, $location = null)
  126. {
  127. $this->_validBucketName($bucket);
  128. if($location) {
  129. $data = '<CreateBucketConfiguration><LocationConstraint>'.$location.'</LocationConstraint></CreateBucketConfiguration>';
  130. }
  131. else {
  132. $data = null;
  133. }
  134. $response = $this->_makeRequest('PUT', $bucket, null, array(), $data);
  135. return ($response->getStatus() == 200);
  136. }
  137. /**
  138. * Checks if a given bucket name is available
  139. *
  140. * @param string $bucket
  141. * @return boolean
  142. */
  143. public function isBucketAvailable($bucket)
  144. {
  145. $response = $this->_makeRequest('HEAD', $bucket, array('max-keys'=>0));
  146. return ($response->getStatus() != 404);
  147. }
  148. /**
  149. * Checks if a given object exists
  150. *
  151. * @param string $object
  152. * @return boolean
  153. */
  154. public function isObjectAvailable($object)
  155. {
  156. $response = $this->_makeRequest('HEAD', $object);
  157. return ($response->getStatus() == 200);
  158. }
  159. /**
  160. * Remove a given bucket. All objects in the bucket must be removed prior
  161. * to removing the bucket.
  162. *
  163. * @param string $bucket
  164. * @return boolean
  165. */
  166. public function removeBucket($bucket)
  167. {
  168. $response = $this->_makeRequest('DELETE', $bucket);
  169. // Look for a 204 No Content response
  170. return ($response->getStatus() == 204);
  171. }
  172. /**
  173. * Get metadata information for a given object
  174. *
  175. * @param string $object
  176. * @return array|false
  177. */
  178. public function getInfo($object)
  179. {
  180. $info = array();
  181. $object = $this->_fixupObjectName($object);
  182. $response = $this->_makeRequest('HEAD', $object);
  183. if ($response->getStatus() == 200) {
  184. $info['type'] = $response->getHeader('Content-type');
  185. $info['size'] = $response->getHeader('Content-length');
  186. $info['mtime'] = strtotime($response->getHeader('Last-modified'));
  187. $info['etag'] = $response->getHeader('ETag');
  188. }
  189. else {
  190. return false;
  191. }
  192. return $info;
  193. }
  194. /**
  195. * List the S3 buckets
  196. *
  197. * @return array|false
  198. */
  199. public function getBuckets()
  200. {
  201. $response = $this->_makeRequest('GET');
  202. if ($response->getStatus() != 200) {
  203. return false;
  204. }
  205. $xml = new \SimpleXMLElement($response->getBody());
  206. $buckets = array();
  207. foreach ($xml->Buckets->Bucket as $bucket) {
  208. $buckets[] = (string)$bucket->Name;
  209. }
  210. return $buckets;
  211. }
  212. /**
  213. * Remove all objects in the bucket.
  214. *
  215. * @param string $bucket
  216. * @return boolean
  217. */
  218. public function cleanBucket($bucket)
  219. {
  220. $objects = $this->getObjectsByBucket($bucket);
  221. if (!$objects) {
  222. return false;
  223. }
  224. foreach ($objects as $object) {
  225. $this->removeObject("$bucket/$object");
  226. }
  227. return true;
  228. }
  229. /**
  230. * List the objects in a bucket.
  231. *
  232. * Provides the list of object keys that are contained in the bucket. Valid params include the following.
  233. * prefix - Limits the response to keys which begin with the indicated prefix. You can use prefixes to separate a bucket into different sets of keys in a way similar to how a file system uses folders.
  234. * marker - Indicates where in the bucket to begin listing. The list will only include keys that occur lexicographically after marker. This is convenient for pagination: To get the next page of results use the last key of the current page as the marker.
  235. * max-keys - The maximum number of keys you'd like to see in the response body. The server might return fewer than this many keys, but will not return more.
  236. * delimiter - Causes keys that contain the same string between the prefix and the first occurrence of the delimiter to be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response.
  237. *
  238. * @param string $bucket
  239. * @param array $params S3 GET Bucket Paramater
  240. * @return array|false
  241. */
  242. public function getObjectsByBucket($bucket, $params = array())
  243. {
  244. $response = $this->_makeRequest('GET', $bucket, $params);
  245. if ($response->getStatus() != 200) {
  246. return false;
  247. }
  248. $xml = new \SimpleXMLElement($response->getBody());
  249. $objects = array();
  250. if (isset($xml->Contents)) {
  251. foreach ($xml->Contents as $contents) {
  252. foreach ($contents->Key as $object) {
  253. $objects[] = (string)$object;
  254. }
  255. }
  256. }
  257. if(count($objects) === 0) {
  258. return false;
  259. }
  260. return $objects;
  261. }
  262. /**
  263. * List the objects and common prefixes in a bucket.
  264. *
  265. * Provides the list of object keys and common prefixes that are contained in the bucket. Valid params include the following.
  266. * prefix - Limits the response to keys which begin with the indicated prefix. You can use prefixes to separate a bucket into different sets of keys in a way similar to how a file system uses folders.
  267. * marker - Indicates where in the bucket to begin listing. The list will only include keys that occur lexicographically after marker. This is convenient for pagination: To get the next page of results use the last key of the current page as the marker.
  268. * max-keys - The maximum number of keys you'd like to see in the response body. The server might return fewer than this many keys, but will not return more.
  269. * delimiter - Causes keys that contain the same string between the prefix and the first occurrence of the delimiter to be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response.
  270. *
  271. * @param string $bucket
  272. * @param array $params S3 GET Bucket Paramater
  273. * @return array|false
  274. */
  275. public function getObjectsAndPrefixesByBucket($bucket, $params = array())
  276. {
  277. $response = $this->_makeRequest('GET', $bucket, $params);
  278. if ($response->getStatus() != 200) {
  279. return false;
  280. }
  281. $xml = new \SimpleXMLElement($response->getBody());
  282. $objects = array();
  283. if (isset($xml->Contents)) {
  284. foreach ($xml->Contents as $contents) {
  285. foreach ($contents->Key as $object) {
  286. $objects[] = (string)$object;
  287. }
  288. }
  289. }
  290. $prefixes = array();
  291. if (isset($xml->CommonPrefixes)) {
  292. foreach ($xml->CommonPrefixes as $prefix) {
  293. foreach ($prefix->Prefix as $object) {
  294. $prefixes[] = (string)$object;
  295. }
  296. }
  297. }
  298. return array(
  299. 'objects' => $objects,
  300. 'prefixes' => $prefixes
  301. );
  302. }
  303. /**
  304. * Make sure the object name is valid
  305. *
  306. * @param string $object
  307. * @return string
  308. */
  309. protected function _fixupObjectName($object)
  310. {
  311. $nameparts = explode('/', $object);
  312. $this->_validBucketName($nameparts[0]);
  313. $firstpart = array_shift($nameparts);
  314. if (count($nameparts) == 0) {
  315. return $firstpart;
  316. }
  317. return $firstpart.'/'.implode('/', array_map('rawurlencode', $nameparts));
  318. }
  319. /**
  320. * Get an object
  321. *
  322. * @param string $object
  323. * @param bool $paidobject This is "requestor pays" object
  324. * @return string|false
  325. */
  326. public function getObject($object, $paidobject=false)
  327. {
  328. $object = $this->_fixupObjectName($object);
  329. if ($paidobject) {
  330. $response = $this->_makeRequest('GET', $object, null, array(self::S3_REQUESTPAY_HEADER => 'requester'));
  331. }
  332. else {
  333. $response = $this->_makeRequest('GET', $object);
  334. }
  335. if ($response->getStatus() != 200) {
  336. return false;
  337. }
  338. return $response->getBody();
  339. }
  340. /**
  341. * Get an object using streaming
  342. *
  343. * Can use either provided filename for storage or create a temp file if none provided.
  344. *
  345. * @param string $object Object path
  346. * @param string $streamfile File to write the stream to
  347. * @param bool $paidobject This is "requestor pays" object
  348. * @return Zend_Http_Response_Stream|false
  349. */
  350. public function getObjectStream($object, $streamfile = null, $paidobject=false)
  351. {
  352. $object = $this->_fixupObjectName($object);
  353. $this->getHttpClient()->setStream($streamfile?$streamfile:true);
  354. if ($paidobject) {
  355. $response = $this->_makeRequest('GET', $object, null, array(self::S3_REQUESTPAY_HEADER => 'requester'));
  356. }
  357. else {
  358. $response = $this->_makeRequest('GET', $object);
  359. }
  360. $this->getHttpClient()->setStream(null);
  361. if ($response->getStatus() != 200 || !($response instanceof \Zend\Http\Response\Stream)) {
  362. return false;
  363. }
  364. return $response;
  365. }
  366. /**
  367. * Upload an object by a PHP string
  368. *
  369. * @param string $object Object name
  370. * @param string|resource $data Object data (can be string or stream)
  371. * @param array $meta Metadata
  372. * @return boolean
  373. */
  374. public function putObject($object, $data, $meta=null)
  375. {
  376. $object = $this->_fixupObjectName($object);
  377. $headers = (is_array($meta)) ? $meta : array();
  378. if(!is_resource($data)) {
  379. $headers['Content-MD5'] = base64_encode(md5($data, true));
  380. }
  381. $headers['Expect'] = '100-continue';
  382. if (!isset($headers[self::S3_CONTENT_TYPE_HEADER])) {
  383. $headers[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($object);
  384. }
  385. $response = $this->_makeRequest('PUT', $object, null, $headers, $data);
  386. // Check the MD5 Etag returned by S3 against and MD5 of the buffer
  387. if ($response->getStatus() == 200) {
  388. // It is escaped by double quotes for some reason
  389. $etag = str_replace('"', '', $response->getHeader('Etag'));
  390. if (is_resource($data) || $etag == md5($data)) {
  391. return true;
  392. }
  393. }
  394. return false;
  395. }
  396. /**
  397. * Put file to S3 as object
  398. *
  399. * @param string $path File name
  400. * @param string $object Object name
  401. * @param array $meta Metadata
  402. * @return boolean
  403. */
  404. public function putFile($path, $object, $meta=null)
  405. {
  406. $data = @file_get_contents($path);
  407. if ($data === false) {
  408. throw new Exception\RuntimeException("Cannot read file $path");
  409. }
  410. if (!is_array($meta)) {
  411. $meta = array();
  412. }
  413. if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
  414. $meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
  415. }
  416. return $this->putObject($object, $data, $meta);
  417. }
  418. /**
  419. * Put file to S3 as object, using streaming
  420. *
  421. * @param string $path File name
  422. * @param string $object Object name
  423. * @param array $meta Metadata
  424. * @return boolean
  425. */
  426. public function putFileStream($path, $object, $meta=null)
  427. {
  428. $data = @fopen($path, "rb");
  429. if ($data === false) {
  430. throw new Exception\RuntimeException("Cannot open file $path");
  431. }
  432. if (!is_array($meta)) {
  433. $meta = array();
  434. }
  435. if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
  436. $meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
  437. }
  438. if (!isset($meta['Content-MD5'])) {
  439. $meta['Content-MD5'] = base64_encode(md5_file($path, true));
  440. }
  441. return $this->putObject($object, $data, $meta);
  442. }
  443. /**
  444. * Remove a given object
  445. *
  446. * @param string $object
  447. * @return boolean
  448. */
  449. public function removeObject($object)
  450. {
  451. $object = $this->_fixupObjectName($object);
  452. $response = $this->_makeRequest('DELETE', $object);
  453. // Look for a 204 No Content response
  454. return ($response->getStatus() == 204);
  455. }
  456. /**
  457. * Copy an object
  458. *
  459. * @param string $sourceObject Source object name
  460. * @param string $destObject Destination object name
  461. * @param array $meta (OPTIONAL) Metadata to apply to desination object.
  462. * Set to null to copy metadata from source object.
  463. * @return boolean
  464. */
  465. public function copyObject($sourceObject, $destObject, $meta = null)
  466. {
  467. $sourceObject = $this->_fixupObjectName($sourceObject);
  468. $destObject = $this->_fixupObjectName($destObject);
  469. $headers = (is_array($meta)) ? $meta : array();
  470. $headers['x-amz-copy-source'] = $sourceObject;
  471. $headers['x-amz-metadata-directive'] = $meta === null ? 'COPY' : 'REPLACE';
  472. $response = $this->_makeRequest('PUT', $destObject, null, $headers);
  473. if ($response->getStatus() == 200 && !stristr($response->getBody(), '<Error>')) {
  474. return true;
  475. }
  476. return false;
  477. }
  478. /**
  479. * Move an object
  480. *
  481. * Performs a copy to dest + verify + remove source
  482. *
  483. * @param string $sourceObject Source object name
  484. * @param string $destObject Destination object name
  485. * @param array $meta (OPTIONAL) Metadata to apply to destination object.
  486. * Set to null to retain existing metadata.
  487. */
  488. public function moveObject($sourceObject, $destObject, $meta = null)
  489. {
  490. $sourceInfo = $this->getInfo($sourceObject);
  491. $this->copyObject($sourceObject, $destObject, $meta);
  492. $destInfo = $this->getInfo($destObject);
  493. if ($sourceInfo['etag'] === $destInfo['etag']) {
  494. return $this->removeObject($sourceObject);
  495. } else {
  496. return false;
  497. }
  498. }
  499. /**
  500. * Make a request to Amazon S3
  501. *
  502. * @param string $method Request method
  503. * @param string $path Path to requested object
  504. * @param array $params Request parameters
  505. * @param array $headers HTTP headers
  506. * @param string|resource $data Request data
  507. * @return Zend_Http_Response
  508. */
  509. public function _makeRequest($method, $path='', $params=null, $headers=array(), $data=null)
  510. {
  511. $retry_count = 0;
  512. if (!is_array($headers)) {
  513. $headers = array($headers);
  514. }
  515. $headers['Date'] = gmdate(DATE_RFC1123, time());
  516. if(is_resource($data) && $method != 'PUT') {
  517. throw new Exception\InvalidArgumentException("Only PUT request supports stream data");
  518. }
  519. // build the end point out
  520. $parts = explode('/', $path, 2);
  521. $endpoint = clone($this->_endpoint);
  522. if ($parts[0]) {
  523. // prepend bucket name to the hostname
  524. $endpoint->setHost($parts[0].'.'.$endpoint->getHost());
  525. }
  526. if (!empty($parts[1])) {
  527. $endpoint->setPath('/'.$parts[1]);
  528. }
  529. else {
  530. $endpoint->setPath('/');
  531. if ($parts[0]) {
  532. $path = $parts[0].'/';
  533. }
  534. }
  535. self::addSignature($method, $path, $headers);
  536. $client = $this->getHttpClient();
  537. $client->resetParameters();
  538. $client->setUri($endpoint);
  539. $client->setAuth(false);
  540. // Work around buglet in HTTP client - it doesn't clean headers
  541. // Remove when ZHC is fixed
  542. $client->setHeaders(array('Content-MD5' => null,
  543. 'Expect' => null,
  544. 'Range' => null,
  545. 'x-amz-acl' => null,
  546. 'x-amz-copy-source' => null,
  547. 'x-amz-metadata-directive' => null));
  548. $client->setHeaders($headers);
  549. if (is_array($params)) {
  550. foreach ($params as $name=>$value) {
  551. $client->setParameterGet($name, $value);
  552. }
  553. }
  554. if (($method == 'PUT') && ($data !== null)) {
  555. if (!isset($headers['Content-type'])) {
  556. $headers['Content-type'] = self::getMimeType($path);
  557. }
  558. $client->setRawData($data, $headers['Content-type']);
  559. }
  560. do {
  561. $retry = false;
  562. $response = $client->request($method);
  563. $response_code = $response->getStatus();
  564. // Some 5xx errors are expected, so retry automatically
  565. if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
  566. $retry = true;
  567. $retry_count++;
  568. sleep($retry_count / 4 * $retry_count);
  569. }
  570. else if ($response_code == 307) {
  571. // Need to redirect, new S3 endpoint given
  572. // This should never happen as Zend_Http_Client will redirect automatically
  573. }
  574. else if ($response_code == 100) {
  575. // echo 'OK to Continue';
  576. }
  577. } while ($retry);
  578. return $response;
  579. }
  580. /**
  581. * Add the S3 Authorization signature to the request headers
  582. *
  583. * @param string $method
  584. * @param string $path
  585. * @param array &$headers
  586. * @return string
  587. */
  588. protected function addSignature($method, $path, &$headers)
  589. {
  590. if (!is_array($headers)) {
  591. $headers = array($headers);
  592. }
  593. $type = $md5 = $date = '';
  594. // Search for the Content-type, Content-MD5 and Date headers
  595. foreach ($headers as $key=>$val) {
  596. if (strcasecmp($key, 'content-type') == 0) {
  597. $type = $val;
  598. }
  599. else if (strcasecmp($key, 'content-md5') == 0) {
  600. $md5 = $val;
  601. }
  602. else if (strcasecmp($key, 'date') == 0) {
  603. $date = $val;
  604. }
  605. }
  606. // If we have an x-amz-date header, use that instead of the normal Date
  607. if (isset($headers['x-amz-date']) && isset($date)) {
  608. $date = '';
  609. }
  610. $sig_str = "$method\n$md5\n$type\n$date\n";
  611. // For x-amz- headers, combine like keys, lowercase them, sort them
  612. // alphabetically and remove excess spaces around values
  613. $amz_headers = array();
  614. foreach ($headers as $key=>$val) {
  615. $key = strtolower($key);
  616. if (substr($key, 0, 6) == 'x-amz-') {
  617. if (is_array($val)) {
  618. $amz_headers[$key] = $val;
  619. }
  620. else {
  621. $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
  622. }
  623. }
  624. }
  625. if (!empty($amz_headers)) {
  626. ksort($amz_headers);
  627. foreach ($amz_headers as $key=>$val) {
  628. $sig_str .= $key.':'.implode(',', $val)."\n";
  629. }
  630. }
  631. $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
  632. if (strpos($path, '?location') !== false) {
  633. $sig_str .= '?location';
  634. }
  635. else if (strpos($path, '?acl') !== false) {
  636. $sig_str .= '?acl';
  637. }
  638. else if (strpos($path, '?torrent') !== false) {
  639. $sig_str .= '?torrent';
  640. }
  641. $signature = base64_encode(Crypt\Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Crypt\Hmac::BINARY));
  642. $headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;
  643. return $sig_str;
  644. }
  645. /**
  646. * Attempt to get the content-type of a file based on the extension
  647. *
  648. * @param string $path
  649. * @return string
  650. */
  651. public static function getMimeType($path)
  652. {
  653. $ext = substr(strrchr($path, '.'), 1);
  654. if(!$ext) {
  655. // shortcut
  656. return 'binary/octet-stream';
  657. }
  658. switch (strtolower($ext)) {
  659. case 'xls':
  660. $content_type = 'application/excel';
  661. break;
  662. case 'hqx':
  663. $content_type = 'application/macbinhex40';
  664. break;
  665. case 'doc':
  666. case 'dot':
  667. case 'wrd':
  668. $content_type = 'application/msword';
  669. break;
  670. case 'pdf':
  671. $content_type = 'application/pdf';
  672. break;
  673. case 'pgp':
  674. $content_type = 'application/pgp';
  675. break;
  676. case 'ps':
  677. case 'eps':
  678. case 'ai':
  679. $content_type = 'application/postscript';
  680. break;
  681. case 'ppt':
  682. $content_type = 'application/powerpoint';
  683. break;
  684. case 'rtf':
  685. $content_type = 'application/rtf';
  686. break;
  687. case 'tgz':
  688. case 'gtar':
  689. $content_type = 'application/x-gtar';
  690. break;
  691. case 'gz':
  692. $content_type = 'application/x-gzip';
  693. break;
  694. case 'php':
  695. case 'php3':
  696. case 'php4':
  697. $content_type = 'application/x-httpd-php';
  698. break;
  699. case 'js':
  700. $content_type = 'application/x-javascript';
  701. break;
  702. case 'ppd':
  703. case 'psd':
  704. $content_type = 'application/x-photoshop';
  705. break;
  706. case 'swf':
  707. case 'swc':
  708. case 'rf':
  709. $content_type = 'application/x-shockwave-flash';
  710. break;
  711. case 'tar':
  712. $content_type = 'application/x-tar';
  713. break;
  714. case 'zip':
  715. $content_type = 'application/zip';
  716. break;
  717. case 'mid':
  718. case 'midi':
  719. case 'kar':
  720. $content_type = 'audio/midi';
  721. break;
  722. case 'mp2':
  723. case 'mp3':
  724. case 'mpga':
  725. $content_type = 'audio/mpeg';
  726. break;
  727. case 'ra':
  728. $content_type = 'audio/x-realaudio';
  729. break;
  730. case 'wav':
  731. $content_type = 'audio/wav';
  732. break;
  733. case 'bmp':
  734. $content_type = 'image/bitmap';
  735. break;
  736. case 'gif':
  737. $content_type = 'image/gif';
  738. break;
  739. case 'iff':
  740. $content_type = 'image/iff';
  741. break;
  742. case 'jb2':
  743. $content_type = 'image/jb2';
  744. break;
  745. case 'jpg':
  746. case 'jpe':
  747. case 'jpeg':
  748. $content_type = 'image/jpeg';
  749. break;
  750. case 'jpx':
  751. $content_type = 'image/jpx';
  752. break;
  753. case 'png':
  754. $content_type = 'image/png';
  755. break;
  756. case 'tif':
  757. case 'tiff':
  758. $content_type = 'image/tiff';
  759. break;
  760. case 'wbmp':
  761. $content_type = 'image/vnd.wap.wbmp';
  762. break;
  763. case 'xbm':
  764. $content_type = 'image/xbm';
  765. break;
  766. case 'css':
  767. $content_type = 'text/css';
  768. break;
  769. case 'txt':
  770. $content_type = 'text/plain';
  771. break;
  772. case 'htm':
  773. case 'html':
  774. $content_type = 'text/html';
  775. break;
  776. case 'xml':
  777. $content_type = 'text/xml';
  778. break;
  779. case 'xsl':
  780. $content_type = 'text/xsl';
  781. break;
  782. case 'mpg':
  783. case 'mpe':
  784. case 'mpeg':
  785. $content_type = 'video/mpeg';
  786. break;
  787. case 'qt':
  788. case 'mov':
  789. $content_type = 'video/quicktime';
  790. break;
  791. case 'avi':
  792. $content_type = 'video/x-ms-video';
  793. break;
  794. case 'eml':
  795. $content_type = 'message/rfc822';
  796. break;
  797. default:
  798. $content_type = 'binary/octet-stream';
  799. break;
  800. }
  801. return $content_type;
  802. }
  803. /**
  804. * Register this object as stream wrapper client
  805. *
  806. * @param string $name
  807. * @return Zend_Service_Amazon_S3
  808. */
  809. public function registerAsClient($name)
  810. {
  811. self::$_wrapperClients[$name] = $this;
  812. return $this;
  813. }
  814. /**
  815. * Unregister this object as stream wrapper client
  816. *
  817. * @param string $name
  818. * @return Zend_Service_Amazon_S3
  819. */
  820. public function unregisterAsClient($name)
  821. {
  822. unset(self::$_wrapperClients[$name]);
  823. return $this;
  824. }
  825. /**
  826. * Get wrapper client for stream type
  827. *
  828. * @param string $name
  829. * @return Zend_Service_Amazon_S3
  830. */
  831. public static function getWrapperClient($name)
  832. {
  833. return self::$_wrapperClients[$name];
  834. }
  835. /**
  836. * Register this object as stream wrapper
  837. *
  838. * @param string $name
  839. * @return Zend_Service_Amazon_S3
  840. */
  841. public function registerStreamWrapper($name='s3')
  842. {
  843. stream_register_wrapper($name, 'Zend\Service\Amazon\S3\Stream');
  844. $this->registerAsClient($name);
  845. }
  846. /**
  847. * Unregister this object as stream wrapper
  848. *
  849. * @param string $name
  850. * @return Zend_Service_Amazon_S3
  851. */
  852. public function unregisterStreamWrapper($name='s3')
  853. {
  854. stream_wrapper_unregister($name);
  855. $this->unregisterAsClient($name);
  856. }
  857. }