From 639f1a01e0fb944b3081a30c4a29169a17751708 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sat, 8 Mar 2014 03:51:47 +0100 Subject: [PATCH] File class no longer depends on MIME + minor tweaks to MediaFile --- classes/File.php | 17 +++++------------ lib/mediafile.php | 6 ++++-- lib/util.php | 15 ++++++++++++++- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/classes/File.php b/classes/File.php index db80159d3b..5f03e8bbd3 100644 --- a/classes/File.php +++ b/classes/File.php @@ -265,20 +265,13 @@ class File extends Managed_DataObject 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)); + try { + $ext = common_supported_mime_to_ext($mimetype); + } catch (Exception $e) { + // We don't support this mimetype, but let's guess the extension + $ext = substr(strrchr($mimetype, '/'), 1); } - // Restore error handling. - PEAR::staticPopErrorHandling(); - $nickname = $profile->nickname; $datestamp = strftime('%Y%m%dT%H%M%S', time()); $random = strtolower(common_confirmation_code(32)); diff --git a/lib/mediafile.php b/lib/mediafile.php index c2da82caa8..07d5c2dd26 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -298,9 +298,11 @@ class MediaFile // If we didn't match, or it is an unclear match if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) { - $type = common_supported_ext_to_mime($originalFilename); - if (!empty($type)) { + try { + $type = common_supported_ext_to_mime($originalFilename); return $type; + } catch (Exception $e) { + // Extension not found, so $mimetype is our best guess } } diff --git a/lib/util.php b/lib/util.php index 5f9c69dde2..269089db8b 100644 --- a/lib/util.php +++ b/lib/util.php @@ -1812,7 +1812,20 @@ function common_supported_ext_to_mime($fileext) } } - return false; + throw new ServerException('Unsupported file extension'); +} + +// Match by our supported mime types +function common_supported_mime_to_ext($mimetype) +{ + $supported = common_config('attachments', 'supported'); + foreach($supported as $type => $ext) { + if ($mimetype === $type) { + return $ext; + } + } + + throw new ServerException('Unsupported MIME type'); } // The MIME "media" is the part before the slash (video in video/webm)