Exemplo n.º 1
0
 /**
  * Return default MIME-type for the specified extension.
  *
  * @param string $type MIME-type
  *
  * @return string A file extension without leading period.
  */
 function getExtension($type)
 {
     include_once 'MIME/Type.php';
     // Strip parameters and comments.
     $type = MIME_Type::getMedia($type) . '/' . MIME_Type::getSubType($type);
     $extension = array_search($type, $this->extensionToType);
     if ($extension === false) {
         return PEAR::raiseError("Sorry, couldn't determine extension.");
     }
     return $extension;
 }
Exemplo n.º 2
0
 /**
  * Perform a wildcard match on a MIME type
  *
  * Example:
  * MIME_Type::wildcardMatch('image/*', 'image/png')
  *
  * @param  string  $card Wildcard to check against
  * @param  string  $type MIME type to check
  * @return boolean true if there was a match, false otherwise
  */
 function wildcardMatch($card, $type)
 {
     if (!MIME_Type::isWildcard($card)) {
         return false;
     }
     if ($card == '*/*') {
         return true;
     }
     if (MIME_Type::getMedia($card) == MIME_Type::getMedia($type)) {
         return true;
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  *
  */
 public function testGetMedia()
 {
     $this->assertEquals('text', MIME_Type::getMedia('text/plain'));
     $this->assertEquals('application', MIME_Type::getMedia('application/ogg'));
     $this->assertEquals('*', MIME_Type::getMedia('*/*'));
 }
Exemplo n.º 4
0
 /**
  * Attempt to identify the content type of a given file.
  * 
  * @param mixed $f file handle resource, or filesystem path as string
  * @param string $originalFilename (optional) for extension-based detection
  * @return string
  * 
  * @fixme is this an internal or public method? It's called from GetFileAction
  * @fixme this seems to tie a front-end error message in, kinda confusing
  * @fixme this looks like it could return a PEAR_Error in some cases, if
  *        type can't be identified and $config['attachments']['supported'] is true
  * 
  * @throws ClientException if type is known, but not supported for local uploads
  */
 static function getUploadedFileType($f, $originalFilename = false)
 {
     require_once 'MIME/Type.php';
     require_once 'MIME/Type/Extension.php';
     // We have to disable auto handling of PEAR errors
     PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
     $mte = new MIME_Type_Extension();
     $cmd =& PEAR::getStaticProperty('MIME_Type', 'fileCmd');
     $cmd = common_config('attachments', 'filecommand');
     $filetype = null;
     // If we couldn't get a clear type from the file extension,
     // we'll go ahead and try checking the content. Content checks
     // are unambiguous for most image files, but nearly useless
     // for office document formats.
     if (is_string($f)) {
         // assuming a filename
         $filetype = MIME_Type::autoDetect($f);
     } else {
         // assuming a filehandle
         $stream = stream_get_meta_data($f);
         $filetype = MIME_Type::autoDetect($stream['uri']);
     }
     // The content-based sources for MIME_Type::autoDetect()
     // are wildly unreliable for office-type documents. If we've
     // gotten an unclear reponse back or just couldn't identify it,
     // we'll try detecting a type from its extension...
     $unclearTypes = array('application/octet-stream', 'application/vnd.ms-office', 'application/zip', 'text/xml');
     if ($originalFilename && (!$filetype || in_array($filetype, $unclearTypes))) {
         $type = $mte->getMIMEType($originalFilename);
         if (is_string($type)) {
             $filetype = $type;
         }
     }
     $supported = common_config('attachments', 'supported');
     if (is_array($supported)) {
         // Normalize extensions to mime types
         foreach ($supported as $i => $entry) {
             if (strpos($entry, '/') === false) {
                 common_log(LOG_INFO, "sample.{$entry}");
                 $supported[$i] = $mte->getMIMEType("sample.{$entry}");
             }
         }
     }
     if ($supported === true || in_array($filetype, $supported)) {
         // Restore PEAR error handlers for our DB code...
         PEAR::staticPopErrorHandling();
         return $filetype;
     }
     $media = MIME_Type::getMedia($filetype);
     if ('application' !== $media) {
         // TRANS: Client exception thrown trying to upload a forbidden MIME type.
         // TRANS: %1$s is the file type that was denied, %2$s is the application part of
         // TRANS: the MIME type that was denied.
         $hint = sprintf(_('"%1$s" is not a supported file type on this server. ' . 'Try using another %2$s format.'), $filetype, $media);
     } else {
         // TRANS: Client exception thrown trying to upload a forbidden MIME type.
         // TRANS: %s is the file type that was denied.
         $hint = sprintf(_('"%s" is not a supported file type on this server.'), $filetype);
     }
     // Restore PEAR error handlers for our DB code...
     PEAR::staticPopErrorHandling();
     throw new ClientException($hint);
 }
 static function getUploadedFileType($f)
 {
     require_once 'MIME/Type.php';
     $cmd =& PEAR::getStaticProperty('MIME_Type', 'fileCmd');
     $cmd = common_config('attachments', 'filecommand');
     $filetype = null;
     if (is_string($f)) {
         // assuming a filename
         $filetype = MIME_Type::autoDetect($f);
     } else {
         // assuming a filehandle
         $stream = stream_get_meta_data($f);
         $filetype = MIME_Type::autoDetect($stream['uri']);
     }
     if (common_config('attachments', 'supported') === true || in_array($filetype, common_config('attachments', 'supported'))) {
         return $filetype;
     }
     $media = MIME_Type::getMedia($filetype);
     if ('application' !== $media) {
         $hint = sprintf(_(' Try using another %s format.'), $media);
     } else {
         $hint = '';
     }
     throw new ClientException(sprintf(_('%s is not a supported file type on this server.'), $filetype) . $hint);
 }