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));
// 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
}
}
}
}
- 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)