[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Service/Amazon/ -> S3.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1