示例#1
0
 public function testGetExtensionFail()
 {
     $this->assertType('PEAR_Error', $this->mte->getExtension(null));
     $this->assertType('PEAR_Error', $this->mte->getExtension(''));
     $this->assertType('PEAR_Error', $this->mte->getExtension('n'));
     $this->assertType('PEAR_Error', $this->mte->getExtension('n/n'));
 }
示例#2
0
文件: File.php 项目: Br3nda/StatusNet
 static function filename($profile, $basename, $mimetype)
 {
     require_once 'MIME/Type/Extension.php';
     $mte = new MIME_Type_Extension();
     try {
         $ext = $mte->getExtension($mimetype);
     } catch (Exception $e) {
         $ext = strtolower(preg_replace('/\\W/', '', $mimetype));
     }
     $nickname = $profile->nickname;
     $datestamp = strftime('%Y%m%dT%H%M%S', time());
     $random = strtolower(common_confirmation_code(32));
     return "{$nickname}-{$datestamp}-{$random}.{$ext}";
 }
 /**
  * Autodetect a file's MIME-type
  *
  * @param string $file   Path to the file to get the type of
  * @param bool   $params Append MIME parameters if true
  *
  * @return string $file's MIME-type on success, PEAR_Error otherwise
  *
  * @since 1.3.0
  *
  * @internal Tries to use fileinfo extension at first. If that
  *  does not work, mime_magic is used. If this is also not available
  *  or does not succeed, "file" command is tried to be executed with
  *  System_Command. When that fails, too, then we use our in-built
  *  extension-to-mimetype-mapping list.
  */
 function _autoDetect($file, $params = false)
 {
     // Sanity checks
     if (!file_exists($file)) {
         return PEAR::raiseError("File \"{$file}\" doesn't exist");
     }
     if (!is_readable($file)) {
         return PEAR::raiseError("File \"{$file}\" is not readable");
     }
     if ($this->useFinfo && function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME, $this->magicFile);
         if ($finfo) {
             $type = finfo_file($finfo, $file);
             finfo_close($finfo);
             if ($type !== false && $type !== '') {
                 return MIME_Type::_handleDetection($type, $params);
             }
         }
     }
     if ($this->useMimeContentType && function_exists('mime_content_type')) {
         $type = mime_content_type($file);
         if ($type !== false && $type !== '') {
             return MIME_Type::_handleDetection($type, $params);
         }
     }
     if ($this->useFileCmd) {
         @(include_once 'System/Command.php');
         if (class_exists('System_Command')) {
             $type = MIME_Type::_fileAutoDetect($file);
             if ($type !== false && $type !== '') {
                 return MIME_Type::_handleDetection($type, $params);
             }
         }
     }
     if ($this->useExtension) {
         include_once 'MIME/Type/Extension.php';
         $mte = new MIME_Type_Extension();
         return $mte->getMIMEType($file);
     }
     return PEAR::raiseError("Sorry, couldn't determine file type.");
 }
示例#4
0
文件: File.php 项目: Grasia/bolotweet
 static function filename($profile, $basename, $mimetype)
 {
     require_once 'MIME/Type/Extension.php';
     // We have to temporarily disable auto handling of PEAR errors...
     PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
     $mte = new MIME_Type_Extension();
     $ext = $mte->getExtension($mimetype);
     if (PEAR::isError($ext)) {
         $ext = strtolower(preg_replace('/\\W/', '', $mimetype));
     }
     // Restore error handling.
     PEAR::staticPopErrorHandling();
     $nickname = $profile->nickname;
     $datestamp = strftime('%Y%m%dT%H%M%S', time());
     $random = strtolower(common_confirmation_code(32));
     return "{$nickname}-{$datestamp}-{$random}.{$ext}";
 }
示例#5
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);
 }
示例#6
0
 /**
  * Autodetect a file's MIME-type
  *
  * This function may be called staticly.
  *
  * @internal Tries to use fileinfo extension at first. If that
  *  does not work, mime_magic is used. If this is also not available
  *  or does not succeed, "file" command is tried to be executed with
  *  System_Command. When that fails, too, then we use our in-built
  *  extension-to-mimetype-mapping list.
  *
  * @param string $file   Path to the file to get the type of
  * @param bool   $params Append MIME parameters if true
  *
  * @return string $file's MIME-type on success, PEAR_Error otherwise
  *
  * @since 1.0.0beta1
  * @static
  */
 function autoDetect($file, $params = false)
 {
     // Sanity checks
     if (!file_exists($file)) {
         return PEAR::raiseError("File \"{$file}\" doesn't exist");
     }
     if (!is_readable($file)) {
         return PEAR::raiseError("File \"{$file}\" is not readable");
     }
     // modifications by brusch for pimcore
     require_once 'MIME/Type/Extension.php';
     $mte = new MIME_Type_Extension();
     if ($type = $mte->getMIMEType($file)) {
         if (!PEAR::isError($type)) {
             return $type;
         }
     }
     if (function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME);
         $type = finfo_file($finfo, $file);
         finfo_close($finfo);
         if ($type !== false && $type !== '') {
             return MIME_Type::_handleDetection($type, $params);
         }
     }
     if (function_exists('mime_content_type')) {
         $type = mime_content_type($file);
         if ($type !== false && $type !== '') {
             return MIME_Type::_handleDetection($type, $params);
         }
     }
     @(include_once 'System/Command.php');
     if (class_exists('System_Command')) {
         return MIME_Type::_handleDetection(MIME_Type::_fileAutoDetect($file), $params);
     }
 }
示例#7
0
文件: File.php 项目: stof/phorkie
 /**
  * @return string Mime type of file, NULL if no type detected
  */
 public function getMimeType()
 {
     $ext = $this->getExt();
     if (isset($GLOBALS['phorkie']['languages'][$ext])) {
         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
     }
     $mte = new \MIME_Type_Extension();
     $type = $mte->getMIMEType($this->getFilename());
     if (!\PEAR::isError($type)) {
         return $type;
     }
     return null;
 }