PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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