PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Zend/Service/Amazon/S3.php

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