Suppress warnings
[lhc/web/wiklou.git] / includes / MimeMagic.php
index 5e28ebe..18fc9e7 100644 (file)
@@ -9,7 +9,7 @@
  * the file mime.types in the includes directory.
  */
 define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
-application/ogg ogg ogm ogv
+application/ogg ogx ogg ogm ogv oga spx
 application/pdf pdf
 application/vnd.oasis.opendocument.chart odc
 application/vnd.oasis.opendocument.chart-template otc
@@ -33,7 +33,7 @@ audio/midi mid midi kar
 audio/mpeg mpga mpa mp2 mp3
 audio/x-aiff aif aiff aifc
 audio/x-wav wav
-audio/ogg ogg
+audio/ogg oga spx ogg
 image/x-bmp bmp
 image/gif gif
 image/jpeg jpeg jpg jpe
@@ -45,7 +45,7 @@ image/x-portable-pixmap ppm
 image/x-xcf xcf
 text/plain txt
 text/html html htm
-video/ogg ogm ogg ogv
+video/ogg ogv ogm ogg
 video/mpeg mpg mpeg
 END_STRING
 );
@@ -80,7 +80,7 @@ audio/x-aiff [AUDIO]
 audio/x-wav [AUDIO]
 audio/mp3 audio/mpeg [AUDIO]
 application/ogg audio/ogg video/ogg [MULTIMEDIA]
-image/x-bmp image/bmp [BITMAP]
+image/x-bmp image/x-ms-bmp image/bmp [BITMAP]
 image/gif [BITMAP]
 image/jpeg [BITMAP]
 image/png [BITMAP]
@@ -102,7 +102,7 @@ END_STRING
 global $wgLoadFileinfoExtension;
 
 if ($wgLoadFileinfoExtension) {
-       if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
+       wfDl( 'fileinfo' );
 }
 
 /**
@@ -118,19 +118,19 @@ class MimeMagic {
        * Mapping of media types to arrays of mime types.
        * This is used by findMediaType and getMediaType, respectively
        */
-       var $mMediaTypes= NULL;
+       var $mMediaTypes= null;
 
        /** Map of mime type aliases
        */
-       var $mMimeTypeAliases= NULL;
+       var $mMimeTypeAliases= null;
 
        /** map of mime types to file extensions (as a space seprarated list)
        */
-       var $mMimeToExt= NULL;
+       var $mMimeToExt= null;
 
        /** map of file extensions types to mime types (as a space seprarated list)
        */
-       var $mExtToMime= NULL;
+       var $mExtToMime= null;
 
        /** IEContentAnalyzer instance
         */
@@ -328,7 +328,7 @@ class MimeMagic {
        */
        function guessTypesForExtension( $ext ) {
                $m = $this->getTypesForExtension( $ext );
-               if ( is_null( $m ) ) return NULL;
+               if ( is_null( $m ) ) return null;
 
                $m = trim( $m );
                $m = preg_replace( '/\s.*$/', '', $m );
@@ -345,7 +345,7 @@ class MimeMagic {
                $ext = $this->getExtensionsForType( $mime );
 
                if ( !$ext ) {
-                       return NULL;  //unknown
+                       return null;  //unknown
                }
 
                $ext = explode( ' ', $ext );
@@ -396,7 +396,7 @@ class MimeMagic {
                        'xbm',
 
                        // Formats we recognize magic numbers for
-                       'djvu', 'ogg', 'ogv', 'mid', 'pdf', 'wmf', 'xcf',
+                       'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 'mid', 'pdf', 'wmf', 'xcf',
 
                        // XML formats we sure hope we recognize reliably
                        'svg',
@@ -409,9 +409,9 @@ class MimeMagic {
        * but applies additional checks to determine some well known file formats that may be missed
        * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
        *
-       * @param string $file The file to check
-       * @param mixed $ext The file extension, or true to extract it from the filename.
-       *                   Set it to false to ignore the extension.
+       * @param $file String: the file to check
+       * @param $ext Mixed: the file extension, or true to extract it from the filename.
+       *             Set it to false to ignore the extension.
        *
        * @return string the mime type of $file
        */
@@ -508,7 +508,7 @@ class MimeMagic {
                /*
                 * look for shell scripts
                 */
-               $script_type = NULL;
+               $script_type = null;
 
                # detect by shebang
                if ( substr( $head, 0, 2) == "#!" ) {
@@ -548,7 +548,7 @@ class MimeMagic {
                // Check for ZIP (before getimagesize)
                if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
                        wfDebug( __METHOD__.": ZIP header present at end of $file\n" );
-                       return $this->detectZipType( $head );
+                       return $this->detectZipType( $head, $ext );
                }
 
                wfSuppressWarnings();
@@ -576,10 +576,14 @@ class MimeMagic {
         * header data.  Currently works for OpenDocument types...
         * If can't tell, returns 'application/zip'.
         *
-        * @param string $header Some reasonably-sized chunk of file header
+        * @param $header String: some reasonably-sized chunk of file header
+        * @param $ext Mixed: the file extension, or true to extract it from the filename.
+        *             Set it to false to ignore the extension.
+        *
         * @return string
         */
-       function detectZipType( $header ) {
+       function detectZipType( $header, $ext = false ) {
+               $mime = 'application/zip';
                $opendocTypes = array(
                        'chart-template',
                        'chart',
@@ -602,15 +606,33 @@ class MimeMagic {
                $types = '(?:' . implode( '|', $opendocTypes ) . ')';
                $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
                wfDebug( __METHOD__.": $opendocRegex\n" );
+
+               $openxmlRegex = "/^\[Content_Types\].xml/";
                
                if( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
                        $mime = $matches[1];
                        wfDebug( __METHOD__.": detected $mime from ZIP archive\n" );
-                       return $mime;
+               } elseif( preg_match( $openxmlRegex, substr( $header, 30 ), $matches ) ) {
+                       $mime = "application/x-opc+zip";
+                       if( $ext !== true && $ext !== false ) {
+                               /** This is the mode used by getPropsFromPath
+                               * These mime's are stored in the database, where we don't really want
+                               * x-opc+zip, because we use it only for internal purposes
+                               */
+                               if( $this->isMatchingExtension( $ext, $mime) ) {
+                                       /* A known file extension for an OPC file,
+                                       * find the proper mime type for that file extension */
+                                       $mime = $this->guessTypesForExtension( $ext );
+                               } else {
+                                       $mime = 'application/zip';
+                               }
+                               
+                       }
+                       wfDebug( __METHOD__.": detected an Open Packaging Conventions archive: $mime\n" );
                } else {
                        wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" );
-                       return 'application/zip';
                }
+               return $mime;
        }
 
        /** Internal mime type detection, please use guessMimeType() for application code instead.
@@ -621,9 +643,9 @@ class MimeMagic {
        * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
        * If no mime type can be determined, this function returns "unknown/unknown".
        *
-       * @param string $file The file to check
-       * @param mixed $ext The file extension, or true to extract it from the filename.
-       *                   Set it to false to ignore the extension.
+       * @param $file String: the file to check
+       * @param $ext Mixed: the file extension, or true to extract it from the filename.
+       *             Set it to false to ignore the extension.
        *
        * @return string the mime type of $file
        * @access private
@@ -631,7 +653,7 @@ class MimeMagic {
        function detectMimeType( $file, $ext = true ) {
                global $wgMimeDetectorCommand;
 
-               $m = NULL;
+               $m = null;
                if ( $wgMimeDetectorCommand ) {
                        $fn = wfEscapeShellArg( $file );
                        $m = `$wgMimeDetectorCommand $fn`;
@@ -678,7 +700,7 @@ class MimeMagic {
                        $m = strtolower( $m );
 
                        if ( strpos( $m, 'unknown' ) !== false ) {
-                               $m = NULL;
+                               $m = null;
                        } else {
                                wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
                                return $m;
@@ -717,13 +739,13 @@ class MimeMagic {
        * @todo analyse file if need be
        * @todo look at multiple extension, separately and together.
        *
-       * @param string $path full path to the image file, in case we have to look at the contents
+       * @param $path String: full path to the image file, in case we have to look at the contents
        *        (if null, only the mime type is used to determine the media type code).
-       * @param string $mime mime type. If null it will be guessed using guessMimeType.
+       * @param $mime String: mime type. If null it will be guessed using guessMimeType.
        *
        * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
        */
-       function getMediaType( $path = NULL, $mime = NULL ) {
+       function getMediaType( $path = null, $mime = null ) {
                if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
 
                # If mime type is unknown, guess it
@@ -756,7 +778,7 @@ class MimeMagic {
                }
 
                # Check for entry for file extension
-               $e = NULL;
+               $e = null;
                if ( $path ) {
                        $i = strrpos( $path, '.' );
                        $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
@@ -819,9 +841,9 @@ class MimeMagic {
         * Get the MIME types that various versions of Internet Explorer would 
         * detect from a chunk of the content.
         *
-        * @param string $fileName The file name (unused at present)
-        * @param string $chunk The first 256 bytes of the file
-        * @param string $proposed The MIME type proposed by the server
+        * @param $fileName String: the file name (unused at present)
+        * @param $chunk String: the first 256 bytes of the file
+        * @param $proposed String: the MIME type proposed by the server
         */
        public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
                $ca = $this->getIEContentAnalyzer();