]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
If the file is text/plain, see if we accept the extension
authorMikael Nordfeldth <mmn@hethane.se>
Wed, 6 Jul 2016 07:34:09 +0000 (09:34 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Wed, 6 Jul 2016 07:34:09 +0000 (09:34 +0200)
lib/mediafile.php
lib/util.php

index 54d00b4acfefff266b7e1a600340181ff45be172..803cbe0a4cd6cece1f9d1e83540138d8912ab2b8 100644 (file)
@@ -355,6 +355,7 @@ class MediaFile
         $unclearTypes = array('application/octet-stream',
                               'application/vnd.ms-office',
                               'application/zip',
+                              'text/plain',
                               'text/html',  // Ironically, Wikimedia Commons' SVG_logo.svg is identified as text/html
                               // TODO: for XML we could do better content-based sniffing too
                               'text/xml');
@@ -364,10 +365,12 @@ class MediaFile
         // If we didn't match, or it is an unclear match
         if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
             try {
-                $type = common_supported_ext_to_mime($originalFilename);
+                $type = common_supported_filename_to_mime($originalFilename);
                 return $type;
+            } catch (UnknownExtensionMimeException $e) {
+                // FIXME: I think we should keep the file extension here (supported should be === true here)
             } catch (Exception $e) {
-                // Extension not found, so $mimetype is our best guess
+                // Extension parsed but no connected mimetype, so $mimetype is our best guess
             }
         }
 
index 846605bc7fae42de9ac1bee38770c16d250c5c3f..985b3773df38a4a63e4659339a14f114e3f7af80 100644 (file)
@@ -1991,15 +1991,22 @@ function common_accept_to_prefs($accept, $def = '*/*')
 }
 
 // Match by our supported file extensions
-function common_supported_ext_to_mime($fileext)
+function common_supported_filename_to_mime($filename)
 {
     // Accept a filename and take out the extension
-    if (strpos($fileext, '.') !== false) {
-        $fileext = substr(strrchr($fileext, '.'), 1);
+    if (strpos($filename, '.') === false) {
+        throw new ServerException(sprintf('No extension on filename: %1$s', _ve($filename)));
     }
 
+    $fileext = substr(strrchr($filename, '.'), 1);
+    return common_supported_ext_to_mime($fileext);
+}
+
+function common_supported_ext_to_mime($fileext)
+{
     $supported = common_config('attachments', 'supported');
     if ($supported === true) {
+        // FIXME: Should we just accept the extension straight off when supported === true?
         throw new UnknownExtensionMimeException($fileext);
     }
     foreach($supported as $type => $ext) {
@@ -2015,16 +2022,15 @@ function common_supported_ext_to_mime($fileext)
 function common_supported_mime_to_ext($mimetype)
 {
     $supported = common_config('attachments', 'supported');
-    if ($supported === true) {
-        throw new UnknownMimeExtensionException($mimetype);
-    }
-    foreach($supported as $type => $ext) {
-        if ($mimetype === $type) {
-            return $ext;
+    if (is_array($supported)) {
+        foreach($supported as $type => $ext) {
+            if ($mimetype === $type) {
+                return $ext;
+            }
         }
     }
 
-    throw new ServerException('Unsupported MIME type');
+    throw new UnknownMimeExtensionException($mimetype);
 }
 
 // The MIME "media" is the part before the slash (video in video/webm)