Some mimetype madness!

This commit is contained in:
Mikael Nordfeldth 2016-07-06 08:59:16 +02:00
parent 3987cad9b7
commit b4a0bff740
4 changed files with 39 additions and 7 deletions

View File

@ -304,13 +304,12 @@ class File extends Managed_DataObject
$ext = common_supported_mime_to_ext($mimetype);
// we do, so use it!
return $ext;
} catch (Exception $e) { // FIXME: Make this exception more specific to "unknown mime=>ext relation"
} catch (UnknownMimeExtensionException $e) {
// We don't know the extension for this mimetype, but let's guess.
// If we are very liberal with uploads ($config['attachments']['supported'] === true)
// then we try to do some guessing based on the filename, if it was supplied.
if (!is_null($filename) && common_config('attachments', 'supported')===true
&& preg_match('/^.+\.([A-Za-z0-9]+)$/', $filename, $matches)) {
// If we can't recognize the extension from the MIME, we try
// to guess based on filename, if one was supplied.
if (!is_null($filename) && preg_match('/^.+\.([A-Za-z0-9]+)$/', $filename, $matches)) {
// we matched on a file extension, so let's see if it means something.
$ext = mb_strtolower($matches[1]);
@ -330,6 +329,8 @@ class File extends Managed_DataObject
// the attachment extension based on its filename was not blacklisted so it's ok to use it
return $ext;
}
} catch (Exception $e) {
common_log(LOG_INFO, 'Problem when figuring out extension for mimetype: '._ve($e));
}
// If nothing else has given us a result, try to extract it from

View File

@ -249,6 +249,7 @@ $default =
'application/zip' => 'zip',
'application/x-go-sgf' => 'sgf',
'application/xml' => 'xml',
'application/gpx+xml' => 'gpx',
'image/png' => 'png',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
@ -273,7 +274,7 @@ $default =
'show_thumbs' => true, // show thumbnails in notice lists for uploaded images, and photos and videos linked remotely that provide oEmbed info
'process_links' => true, // check linked resources for embeddable photos and videos; this will hit referenced external web sites when processing new messages.
'extblacklist' => [
'php' => 'phps',
'php' => 'phps', // this turns .php into .phps
'exe' => false, // this would deny any uploads to keep the "exe" file extension
],
),

View File

@ -0,0 +1,30 @@
<?php
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Class for unknown MIME extension exception
*
* Thrown when we don't know the file extension for a given MIME type.
* This generally means that all files are accepted since if we have
* a list of known MIMEs then they have extensions coupled to them.
*
* @category Exception
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @license https://www.gnu.org/licenses/agpl-3.0.html
* @link https://gnu.io/social
*/
class UnknownMimeExtensionException extends ServerException
{
public function __construct($msg=null)
{
if ($msg === null) {
// TRANS: We accept the file type (we probably just accept all files)
// TRANS: but don't know the file extension for it.
$msg = _('Supported mimetype but unknown extension relation.');
}
parent::__construct($msg);
}
}

View File

@ -2016,7 +2016,7 @@ function common_supported_mime_to_ext($mimetype)
{
$supported = common_config('attachments', 'supported');
if ($supported === true) {
throw new ServerException('Supported mimetype but unknown extension relation.');
throw new UnknownMimeExtensionException();
}
foreach($supported as $type => $ext) {
if ($mimetype === $type) {