PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Service/Amazon/S3.php

http://yurishop.googlecode.com/
PHP | 955 lines | 546 code | 107 blank | 302 comment | 98 complexity | b6fc9bb861a5d34f942ed200bbd86bed 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-2010 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 23224 2010-10-22 13:45:57Z matthew $
  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-2010 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. if($location) {
  146. $data = '<CreateBucketConfiguration><LocationConstraint>'.$location.'</LocationConstraint></CreateBucketConfiguration>';
  147. }
  148. else {
  149. $data = null;
  150. }
  151. $response = $this->_makeRequest('PUT', $bucket, null, array(), $data);
  152. return ($response->getStatus() == 200);
  153. }
  154. /**
  155. * Checks if a given bucket name is available
  156. *
  157. * @param string $bucket
  158. * @return boolean
  159. */
  160. public function isBucketAvailable($bucket)
  161. {
  162. $response = $this->_makeRequest('HEAD', $bucket, array('max-keys'=>0));
  163. return ($response->getStatus() != 404);
  164. }
  165. /**
  166. * Checks if a given object exists
  167. *
  168. * @param string $object
  169. * @return boolean
  170. */
  171. public function isObjectAvailable($object)
  172. {
  173. $object = $this->_fixupObjectName($object);
  174. $response = $this->_makeRequest('HEAD', $object);
  175. return ($response->getStatus() == 200);
  176. }
  177. /**
  178. * Remove a given bucket. All objects in the bucket must be removed prior
  179. * to removing the bucket.
  180. *
  181. * @param string $bucket
  182. * @return boolean
  183. */
  184. public function removeBucket($bucket)
  185. {
  186. $response = $this->_makeRequest('DELETE', $bucket);
  187. // Look for a 204 No Content response
  188. return ($response->getStatus() == 204);
  189. }
  190. /**
  191. * Get metadata information for a given object
  192. *
  193. * @param string $object
  194. * @return array|false
  195. */
  196. public function getInfo($object)
  197. {
  198. $info = array();
  199. $object = $this->_fixupObjectName($object);
  200. $response = $this->_makeRequest('HEAD', $object);
  201. if ($response->getStatus() == 200) {
  202. $info['type'] = $response->getHeader('Content-type');
  203. $info['size'] = $response->getHeader('Content-length');
  204. $info['mtime'] = strtotime($response->getHeader('Last-modified'));
  205. $info['etag'] = $response->getHeader('ETag');
  206. }
  207. else {
  208. return false;
  209. }
  210. return $info;
  211. }
  212. /**
  213. * List the S3 buckets
  214. *
  215. * @return array|false
  216. */
  217. public function getBuckets()
  218. {
  219. $response = $this->_makeRequest('GET');
  220. if ($response->getStatus() != 200) {
  221. return false;
  222. }
  223. $xml = new SimpleXMLElement($response->getBody());
  224. $buckets = array();
  225. foreach ($xml->Buckets->Bucket as $bucket) {
  226. $buckets[] = (string)$bucket->Name;
  227. }
  228. return $buckets;
  229. }
  230. /**
  231. * Remove all objects in the bucket.
  232. *
  233. * @param string $bucket
  234. * @return boolean
  235. */
  236. public function cleanBucket($bucket)
  237. {
  238. $objects = $this->getObjectsByBucket($bucket);
  239. if (!$objects) {
  240. return false;
  241. }
  242. foreach ($objects as $object) {
  243. $this->removeObject("$bucket/$object");
  244. }
  245. return true;
  246. }
  247. /**
  248. * List the objects in a bucket.
  249. *
  250. * Provides the list of object keys that are contained in the bucket. Valid params include the following.
  251. * 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.
  252. * 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.
  253. * 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.
  254. * 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.
  255. *
  256. * @param string $bucket
  257. * @param array $params S3 GET Bucket Paramater
  258. * @return array|false
  259. */
  260. public function getObjectsByBucket($bucket, $params = array())
  261. {
  262. $response = $this->_makeRequest('GET', $bucket, $params);
  263. if ($response->getStatus() != 200) {
  264. return false;
  265. }
  266. $xml = new SimpleXMLElement($response->getBody());
  267. $objects = array();
  268. if (isset($xml->Contents)) {
  269. foreach ($xml->Contents as $contents) {
  270. foreach ($contents->Key as $object) {
  271. $objects[] = (string)$object;
  272. }
  273. }
  274. }
  275. return $objects;
  276. }
  277. /**
  278. * Make sure the object name is valid
  279. *
  280. * @param string $object
  281. * @return string
  282. */
  283. protected function _fixupObjectName($object)
  284. {
  285. $nameparts = explode('/', $object);
  286. $this->_validBucketName($nameparts[0]);
  287. $firstpart = array_shift($nameparts);
  288. if (count($nameparts) == 0) {
  289. return $firstpart;
  290. }
  291. return $firstpart.'/'.join('/', array_map('rawurlencode', $nameparts));
  292. }
  293. /**
  294. * Get an object
  295. *
  296. * @param string $object
  297. * @param bool $paidobject This is "requestor pays" object
  298. * @return string|false
  299. */
  300. public function getObject($object, $paidobject=false)
  301. {
  302. $object = $this->_fixupObjectName($object);
  303. if ($paidobject) {
  304. $response = $this->_makeRequest('GET', $object, null, array(self::S3_REQUESTPAY_HEADER => 'requester'));
  305. }
  306. else {
  307. $response = $this->_makeRequest('GET', $object);
  308. }
  309. if ($response->getStatus() != 200) {
  310. return false;
  311. }
  312. return $response->getBody();
  313. }
  314. /**
  315. * Get an object using streaming
  316. *
  317. * Can use either provided filename for storage or create a temp file if none provided.
  318. *
  319. * @param string $object Object path
  320. * @param string $streamfile File to write the stream to
  321. * @param bool $paidobject This is "requestor pays" object
  322. * @return Zend_Http_Response_Stream|false
  323. */
  324. public function getObjectStream($object, $streamfile = null, $paidobject=false)
  325. {
  326. $object = $this->_fixupObjectName($object);
  327. self::getHttpClient()->setStream($streamfile?$streamfile:true);
  328. if ($paidobject) {
  329. $response = $this->_makeRequest('GET', $object, null, array(self::S3_REQUESTPAY_HEADER => 'requester'));
  330. }
  331. else {
  332. $response = $this->_makeRequest('GET', $object);
  333. }
  334. self::getHttpClient()->setStream(null);
  335. if ($response->getStatus() != 200 || !($response instanceof Zend_Http_Response_Stream)) {
  336. return false;
  337. }
  338. return $response;
  339. }
  340. /**
  341. * Upload an object by a PHP string
  342. *
  343. * @param string $object Object name
  344. * @param string|resource $data Object data (can be string or stream)
  345. * @param array $meta Metadata
  346. * @return boolean
  347. */
  348. public function putObject($object, $data, $meta=null)
  349. {
  350. $object = $this->_fixupObjectName($object);
  351. $headers = (is_array($meta)) ? $meta : array();
  352. if(!is_resource($data)) {
  353. $headers['Content-MD5'] = base64_encode(md5($data, true));
  354. }
  355. $headers['Expect'] = '100-continue';
  356. if (!isset($headers[self::S3_CONTENT_TYPE_HEADER])) {
  357. $headers[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($object);
  358. }
  359. $response = $this->_makeRequest('PUT', $object, null, $headers, $data);
  360. // Check the MD5 Etag returned by S3 against and MD5 of the buffer
  361. if ($response->getStatus() == 200) {
  362. // It is escaped by double quotes for some reason
  363. $etag = str_replace('"', '', $response->getHeader('Etag'));
  364. if (is_resource($data) || $etag == md5($data)) {
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. /**
  371. * Put file to S3 as object
  372. *
  373. * @param string $path File name
  374. * @param string $object Object name
  375. * @param array $meta Metadata
  376. * @return boolean
  377. */
  378. public function putFile($path, $object, $meta=null)
  379. {
  380. $data = @file_get_contents($path);
  381. if ($data === false) {
  382. /**
  383. * @see Zend_Service_Amazon_S3_Exception
  384. */
  385. #require_once 'Zend/Service/Amazon/S3/Exception.php';
  386. throw new Zend_Service_Amazon_S3_Exception("Cannot read file $path");
  387. }
  388. if (!is_array($meta)) {
  389. $meta = array();
  390. }
  391. if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
  392. $meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
  393. }
  394. return $this->putObject($object, $data, $meta);
  395. }
  396. /**
  397. * Put file to S3 as object, using streaming
  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 putFileStream($path, $object, $meta=null)
  405. {
  406. $data = @fopen($path, "rb");
  407. if ($data === false) {
  408. /**
  409. * @see Zend_Service_Amazon_S3_Exception
  410. */
  411. #require_once 'Zend/Service/Amazon/S3/Exception.php';
  412. throw new Zend_Service_Amazon_S3_Exception("Cannot open file $path");
  413. }
  414. if (!is_array($meta)) {
  415. $meta = array();
  416. }
  417. if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
  418. $meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
  419. }
  420. if(!isset($meta['Content-MD5'])) {
  421. $headers['Content-MD5'] = base64_encode(md5_file($path, true));
  422. }
  423. return $this->putObject($object, $data, $meta);
  424. }
  425. /**
  426. * Remove a given object
  427. *
  428. * @param string $object
  429. * @return boolean
  430. */
  431. public function removeObject($object)
  432. {
  433. $object = $this->_fixupObjectName($object);
  434. $response = $this->_makeRequest('DELETE', $object);
  435. // Look for a 204 No Content response
  436. return ($response->getStatus() == 204);
  437. }
  438. /**
  439. * Copy an object
  440. *
  441. * @param string $sourceObject Source object name
  442. * @param string $destObject Destination object name
  443. * @param array $meta (OPTIONAL) Metadata to apply to desination object.
  444. * Set to null to copy metadata from source object.
  445. * @return boolean
  446. */
  447. public function copyObject($sourceObject, $destObject, $meta = null)
  448. {
  449. $sourceObject = $this->_fixupObjectName($sourceObject);
  450. $destObject = $this->_fixupObjectName($destObject);
  451. $headers = (is_array($meta)) ? $meta : array();
  452. $headers['x-amz-copy-source'] = $sourceObject;
  453. $headers['x-amz-metadata-directive'] = $meta === null ? 'COPY' : 'REPLACE';
  454. $response = $this->_makeRequest('PUT', $destObject, null, $headers);
  455. if ($response->getStatus() == 200 && !stristr($response->getBody(), '<Error>')) {
  456. return true;
  457. }
  458. return false;
  459. }
  460. /**
  461. * Move an object
  462. *
  463. * Performs a copy to dest + verify + remove source
  464. *
  465. * @param string $sourceObject Source object name
  466. * @param string $destObject Destination object name
  467. * @param array $meta (OPTIONAL) Metadata to apply to destination object.
  468. * Set to null to retain existing metadata.
  469. */
  470. public function moveObject($sourceObject, $destObject, $meta = null)
  471. {
  472. $sourceInfo = $this->getInfo($sourceObject);
  473. $this->copyObject($sourceObject, $destObject, $meta);
  474. $destInfo = $this->getInfo($destObject);
  475. if ($sourceInfo['etag'] === $destInfo['etag']) {
  476. return $this->removeObject($sourceObject);
  477. } else {
  478. return false;
  479. }
  480. }
  481. /**
  482. * Make a request to Amazon S3
  483. *
  484. * @param string $method Request method
  485. * @param string $path Path to requested object
  486. * @param array $params Request parameters
  487. * @param array $headers HTTP headers
  488. * @param string|resource $data Request data
  489. * @return Zend_Http_Response
  490. */
  491. public function _makeRequest($method, $path='', $params=null, $headers=array(), $data=null)
  492. {
  493. $retry_count = 0;
  494. if (!is_array($headers)) {
  495. $headers = array($headers);
  496. }
  497. $headers['Date'] = gmdate(DATE_RFC1123, time());
  498. if(is_resource($data) && $method != 'PUT') {
  499. /**
  500. * @see Zend_Service_Amazon_S3_Exception
  501. */
  502. #require_once 'Zend/Service/Amazon/S3/Exception.php';
  503. throw new Zend_Service_Amazon_S3_Exception("Only PUT request supports stream data");
  504. }
  505. // build the end point out
  506. $parts = explode('/', $path, 2);
  507. $endpoint = clone($this->_endpoint);
  508. if ($parts[0]) {
  509. // prepend bucket name to the hostname
  510. $endpoint->setHost($parts[0].'.'.$endpoint->getHost());
  511. }
  512. if (!empty($parts[1])) {
  513. $endpoint->setPath('/'.$parts[1]);
  514. }
  515. else {
  516. $endpoint->setPath('/');
  517. if ($parts[0]) {
  518. $path = $parts[0].'/';
  519. }
  520. }
  521. self::addSignature($method, $path, $headers);
  522. $client = self::getHttpClient();
  523. $client->resetParameters();
  524. $client->setUri($endpoint);
  525. $client->setAuth(false);
  526. // Work around buglet in HTTP client - it doesn't clean headers
  527. // Remove when ZHC is fixed
  528. $client->setHeaders(array('Content-MD5' => null,
  529. 'Content-Encoding' => null,
  530. 'Expect' => null,
  531. 'Range' => null,
  532. 'x-amz-acl' => null,
  533. 'x-amz-copy-source' => null,
  534. 'x-amz-metadata-directive' => null));
  535. $client->setHeaders($headers);
  536. if (is_array($params)) {
  537. foreach ($params as $name=>$value) {
  538. $client->setParameterGet($name, $value);
  539. }
  540. }
  541. if (($method == 'PUT') && ($data !== null)) {
  542. if (!isset($headers['Content-type'])) {
  543. $headers['Content-type'] = self::getMimeType($path);
  544. }
  545. $client->setRawData($data, $headers['Content-type']);
  546. }
  547. do {
  548. $retry = false;
  549. $response = $client->request($method);
  550. $response_code = $response->getStatus();
  551. // Some 5xx errors are expected, so retry automatically
  552. if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
  553. $retry = true;
  554. $retry_count++;
  555. sleep($retry_count / 4 * $retry_count);
  556. }
  557. else if ($response_code == 307) {
  558. // Need to redirect, new S3 endpoint given
  559. // This should never happen as Zend_Http_Client will redirect automatically
  560. }
  561. else if ($response_code == 100) {
  562. // echo 'OK to Continue';
  563. }
  564. } while ($retry);
  565. return $response;
  566. }
  567. /**
  568. * Add the S3 Authorization signature to the request headers
  569. *
  570. * @param string $method
  571. * @param string $path
  572. * @param array &$headers
  573. * @return string
  574. */
  575. protected function addSignature($method, $path, &$headers)
  576. {
  577. if (!is_array($headers)) {
  578. $headers = array($headers);
  579. }
  580. $type = $md5 = $date = '';
  581. // Search for the Content-type, Content-MD5 and Date headers
  582. foreach ($headers as $key=>$val) {
  583. if (strcasecmp($key, 'content-type') == 0) {
  584. $type = $val;
  585. }
  586. else if (strcasecmp($key, 'content-md5') == 0) {
  587. $md5 = $val;
  588. }
  589. else if (strcasecmp($key, 'date') == 0) {
  590. $date = $val;
  591. }
  592. }
  593. // If we have an x-amz-date header, use that instead of the normal Date
  594. if (isset($headers['x-amz-date']) && isset($date)) {
  595. $date = '';
  596. }
  597. $sig_str = "$method\n$md5\n$type\n$date\n";
  598. // For x-amz- headers, combine like keys, lowercase them, sort them
  599. // alphabetically and remove excess spaces around values
  600. $amz_headers = array();
  601. foreach ($headers as $key=>$val) {
  602. $key = strtolower($key);
  603. if (substr($key, 0, 6) == 'x-amz-') {
  604. if (is_array($val)) {
  605. $amz_headers[$key] = $val;
  606. }
  607. else {
  608. $amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
  609. }
  610. }
  611. }
  612. if (!empty($amz_headers)) {
  613. ksort($amz_headers);
  614. foreach ($amz_headers as $key=>$val) {
  615. $sig_str .= $key.':'.implode(',', $val)."\n";
  616. }
  617. }
  618. $sig_str .= '/'.parse_url($path, PHP_URL_PATH);
  619. if (strpos($path, '?location') !== false) {
  620. $sig_str .= '?location';
  621. }
  622. else if (strpos($path, '?acl') !== false) {
  623. $sig_str .= '?acl';
  624. }
  625. else if (strpos($path, '?torrent') !== false) {
  626. $sig_str .= '?torrent';
  627. }
  628. $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
  629. $headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;
  630. return $sig_str;
  631. }
  632. /**
  633. * Attempt to get the content-type of a file based on the extension
  634. *
  635. * @param string $path
  636. * @return string
  637. */
  638. public static function getMimeType($path)
  639. {
  640. $ext = substr(strrchr($path, '.'), 1);
  641. if(!$ext) {
  642. // shortcut
  643. return 'binary/octet-stream';
  644. }
  645. switch (strtolower($ext)) {
  646. case 'xls':
  647. $content_type = 'application/excel';
  648. break;
  649. case 'hqx':
  650. $content_type = 'application/macbinhex40';
  651. break;
  652. case 'doc':
  653. case 'dot':
  654. case 'wrd':
  655. $content_type = 'application/msword';
  656. break;
  657. case 'pdf':
  658. $content_type = 'application/pdf';
  659. break;
  660. case 'pgp':
  661. $content_type = 'application/pgp';
  662. break;
  663. case 'ps':
  664. case 'eps':
  665. case 'ai':
  666. $content_type = 'application/postscript';
  667. break;
  668. case 'ppt':
  669. $content_type = 'application/powerpoint';
  670. break;
  671. case 'rtf':
  672. $content_type = 'application/rtf';
  673. break;
  674. case 'tgz':
  675. case 'gtar':
  676. $content_type = 'application/x-gtar';
  677. break;
  678. case 'gz':
  679. $content_type = 'application/x-gzip';
  680. break;
  681. case 'php':
  682. case 'php3':
  683. case 'php4':
  684. $content_type = 'application/x-httpd-php';
  685. break;
  686. case 'js':
  687. $content_type = 'application/x-javascript';
  688. break;
  689. case 'ppd':
  690. case 'psd':
  691. $content_type = 'application/x-photoshop';
  692. break;
  693. case 'swf':
  694. case 'swc':
  695. case 'rf':
  696. $content_type = 'application/x-shockwave-flash';
  697. break;
  698. case 'tar':
  699. $content_type = 'application/x-tar';
  700. break;
  701. case 'zip':
  702. $content_type = 'application/zip';
  703. break;
  704. case 'mid':
  705. case 'midi':
  706. case 'kar':
  707. $content_type = 'audio/midi';
  708. break;
  709. case 'mp2':
  710. case 'mp3':
  711. case 'mpga':
  712. $content_type = 'audio/mpeg';
  713. break;
  714. case 'ra':
  715. $content_type = 'audio/x-realaudio';
  716. break;
  717. case 'wav':
  718. $content_type = 'audio/wav';
  719. break;
  720. case 'bmp':
  721. $content_type = 'image/bitmap';
  722. break;
  723. case 'gif':
  724. $content_type = 'image/gif';
  725. break;
  726. case 'iff':
  727. $content_type = 'image/iff';
  728. break;
  729. case 'jb2':
  730. $content_type = 'image/jb2';
  731. break;
  732. case 'jpg':
  733. case 'jpe':
  734. case 'jpeg':
  735. $content_type = 'image/jpeg';
  736. break;
  737. case 'jpx':
  738. $content_type = 'image/jpx';
  739. break;
  740. case 'png':
  741. $content_type = 'image/png';
  742. break;
  743. case 'tif':
  744. case 'tiff':
  745. $content_type = 'image/tiff';
  746. break;
  747. case 'wbmp':
  748. $content_type = 'image/vnd.wap.wbmp';
  749. break;
  750. case 'xbm':
  751. $content_type = 'image/xbm';
  752. break;
  753. case 'css':
  754. $content_type = 'text/css';
  755. break;
  756. case 'txt':
  757. $content_type = 'text/plain';
  758. break;
  759. case 'htm':
  760. case 'html':
  761. $content_type = 'text/html';
  762. break;
  763. case 'xml':
  764. $content_type = 'text/xml';
  765. break;
  766. case 'xsl':
  767. $content_type = 'text/xsl';
  768. break;
  769. case 'mpg':
  770. case 'mpe':
  771. case 'mpeg':
  772. $content_type = 'video/mpeg';
  773. break;
  774. case 'qt':
  775. case 'mov':
  776. $content_type = 'video/quicktime';
  777. break;
  778. case 'avi':
  779. $content_type = 'video/x-ms-video';
  780. break;
  781. case 'eml':
  782. $content_type = 'message/rfc822';
  783. break;
  784. default:
  785. $content_type = 'binary/octet-stream';
  786. break;
  787. }
  788. return $content_type;
  789. }
  790. /**
  791. * Register this object as stream wrapper client
  792. *
  793. * @param string $name
  794. * @return Zend_Service_Amazon_S3
  795. */
  796. public function registerAsClient($name)
  797. {
  798. self::$_wrapperClients[$name] = $this;
  799. return $this;
  800. }
  801. /**
  802. * Unregister this object as stream wrapper client
  803. *
  804. * @param string $name
  805. * @return Zend_Service_Amazon_S3
  806. */
  807. public function unregisterAsClient($name)
  808. {
  809. unset(self::$_wrapperClients[$name]);
  810. return $this;
  811. }
  812. /**
  813. * Get wrapper client for stream type
  814. *
  815. * @param string $name
  816. * @return Zend_Service_Amazon_S3
  817. */
  818. public static function getWrapperClient($name)
  819. {
  820. return self::$_wrapperClients[$name];
  821. }
  822. /**
  823. * Register this object as stream wrapper
  824. *
  825. * @param string $name
  826. * @return Zend_Service_Amazon_S3
  827. */
  828. public function registerStreamWrapper($name='s3')
  829. {
  830. /**
  831. * @see Zend_Service_Amazon_S3_Stream
  832. */
  833. #require_once 'Zend/Service/Amazon/S3/Stream.php';
  834. stream_register_wrapper($name, 'Zend_Service_Amazon_S3_Stream');
  835. $this->registerAsClient($name);
  836. }
  837. /**
  838. * Unregister this object as stream wrapper
  839. *
  840. * @param string $name
  841. * @return Zend_Service_Amazon_S3
  842. */
  843. public function unregisterStreamWrapper($name='s3')
  844. {
  845. stream_wrapper_unregister($name);
  846. $this->unregisterAsClient($name);
  847. }
  848. }