Merge "Give a 200 response for valid user pages"
[lhc/web/wiklou.git] / includes / MimeMagic.php
index 65a2a6f..e787fbd 100644 (file)
@@ -39,7 +39,7 @@
  * appending MM_WELL_KNOWN_MIME_TYPES behind $wgMimeTypeFile, but who knows
  * what will break? In practice this probably isn't a problem anyway -- Bryan)
  */
-define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
+define('MM_WELL_KNOWN_MIME_TYPES', <<<END_STRING
 application/ogg ogx ogg ogm ogv oga spx
 application/pdf pdf
 application/vnd.oasis.opendocument.chart odc
@@ -197,14 +197,14 @@ class MimeMagic {
 
                if ( $wgMimeTypeFile ) {
                        if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
-                               wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" );
+                               wfDebug( __METHOD__ . ": loading mime types from $wgMimeTypeFile\n" );
                                $types .= "\n";
                                $types .= file_get_contents( $wgMimeTypeFile );
                        } else {
-                               wfDebug( __METHOD__.": can't load mime types from $wgMimeTypeFile\n" );
+                               wfDebug( __METHOD__ . ": can't load mime types from $wgMimeTypeFile\n" );
                        }
                } else {
-                       wfDebug( __METHOD__.": no mime types file defined, using build-ins only.\n" );
+                       wfDebug( __METHOD__ . ": no mime types file defined, using build-ins only.\n" );
                }
 
                $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
@@ -213,7 +213,7 @@ class MimeMagic {
                $this->mMimeToExt = array();
                $this->mToMime = array();
 
-               $lines = explode( "\n",$types );
+               $lines = explode( "\n", $types );
                foreach ( $lines as $s ) {
                        $s = trim( $s );
                        if ( empty( $s ) ) {
@@ -231,7 +231,7 @@ class MimeMagic {
                        }
 
                        $mime = substr( $s, 0, $i );
-                       $ext = trim( substr($s, $i+1 ) );
+                       $ext = trim( substr( $s, $i+1 ) );
 
                        if ( empty( $ext ) ) {
                                continue;
@@ -272,17 +272,17 @@ class MimeMagic {
 
                if ( $wgMimeInfoFile ) {
                        if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
-                               wfDebug( __METHOD__.": loading mime info from $wgMimeInfoFile\n" );
+                               wfDebug( __METHOD__ . ": loading mime info from $wgMimeInfoFile\n" );
                                $info .= "\n";
                                $info .= file_get_contents( $wgMimeInfoFile );
                        } else {
-                               wfDebug(__METHOD__.": can't load mime info from $wgMimeInfoFile\n");
+                               wfDebug( __METHOD__ . ": can't load mime info from $wgMimeInfoFile\n" );
                        }
                } else {
-                       wfDebug(__METHOD__.": no mime info file defined, using build-ins only.\n");
+                       wfDebug( __METHOD__ . ": no mime info file defined, using build-ins only.\n" );
                }
 
-               $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info);
+               $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info );
                $info = str_replace( "\t", " ", $info );
 
                $this->mMimeTypeAliases = array();
@@ -330,9 +330,9 @@ class MimeMagic {
                                $this->mMediaTypes[$mtype][] = $mime;
                        }
 
-                       if ( sizeof( $m ) > 1 ) {
+                       if ( count( $m ) > 1 ) {
                                $main = $m[0];
-                               for ( $i=1; $i<sizeof($m); $i += 1 ) {
+                               for ( $i = 1; $i < count( $m ); $i += 1 ) {
                                        $mime = $m[$i];
                                        $this->mMimeTypeAliases[$mime] = $main;
                                }
@@ -511,7 +511,7 @@ class MimeMagic {
        public function improveTypeFromExtension( $mime, $ext ) {
                if ( $mime === 'unknown/unknown' ) {
                        if ( $this->isRecognizableExtension( $ext ) ) {
-                               wfDebug( __METHOD__. ': refusing to guess mime type for .' .
+                               wfDebug( __METHOD__ . ': refusing to guess mime type for .' .
                                        "$ext file, we should have recognized it\n" );
                        } else {
                                // Not something we can detect, so simply
@@ -525,7 +525,7 @@ class MimeMagic {
                                // find the proper mime type for that file extension
                                $mime = $this->guessTypesForExtension( $ext );
                        } else {
-                               wfDebug( __METHOD__. ": refusing to guess better type for $mime file, " .
+                               wfDebug( __METHOD__ . ": refusing to guess better type for $mime file, " .
                                        ".$ext is not a known OPC extension.\n" );
                                $mime = 'application/zip';
                        }
@@ -535,7 +535,7 @@ class MimeMagic {
                        $mime = $this->mMimeTypeAliases[$mime];
                }
 
-               wfDebug(__METHOD__.": improved mime type for .$ext: $mime\n");
+               wfDebug( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
                return $mime;
        }
 
@@ -555,14 +555,14 @@ class MimeMagic {
         */
        public function guessMimeType( $file, $ext = true ) {
                if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
-                       wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " .
+                       wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
                                "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
                }
 
                $mime = $this->doGuessMimeType( $file, $ext );
 
                if( !$mime ) {
-                       wfDebug( __METHOD__.": internal type detection failed for $file (.$ext)...\n" );
+                       wfDebug( __METHOD__ . ": internal type detection failed for $file (.$ext)...\n" );
                        $mime = $this->detectMimeType( $file, $ext );
                }
 
@@ -570,7 +570,7 @@ class MimeMagic {
                        $mime = $this->mMimeTypeAliases[$mime];
                }
 
-               wfDebug(__METHOD__.": guessed mime type of $file: $mime\n");
+               wfDebug( __METHOD__ . ": guessed mime type of $file: $mime\n" );
                return $mime;
        }
 
@@ -629,7 +629,7 @@ class MimeMagic {
                        $doctype = strpos( $head, "\x42\x82" );
                        if ( $doctype ) {
                                // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers)
-                               $data = substr($head, $doctype+3, 8);
+                               $data = substr( $head, $doctype+3, 8 );
                                if ( strncmp( $data, "matroska", 8 ) == 0 ) {
                                        wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" );
                                        return "video/x-matroska";
@@ -661,12 +661,11 @@ class MimeMagic {
                 * strings like "<? ", but should it be axed completely?
                 */
                if ( ( strpos( $head, '<?php' ) !== false ) ||
-
-                   ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
-                   ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
-                   ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
-                   ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
-                   ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
+                       ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
+                       ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
+                       ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
+                       ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
+                       ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
 
                        wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
                        return 'application/x-php';
@@ -698,11 +697,11 @@ class MimeMagic {
                } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
                        $script_type = "UTF-16BE";
                } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
-                       $script_type= "UTF-16LE";
+                       $script_type = "UTF-16LE";
                }
 
                if ( $script_type ) {
-                       if ( $script_type !== "UTF-8" && $script_type !== "ASCII") {
+                       if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
                                // Quick and dirty fold down to ASCII!
                                $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
                                $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
@@ -720,14 +719,14 @@ class MimeMagic {
 
                        if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
                                $mime = "application/x-{$match[2]}";
-                               wfDebug( __METHOD__.": shell script recognized as $mime\n" );
+                               wfDebug( __METHOD__ . ": shell script recognized as $mime\n" );
                                return $mime;
                        }
                }
 
                // Check for ZIP variants (before getimagesize)
                if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
-                       wfDebug( __METHOD__.": ZIP header present in $file\n" );
+                       wfDebug( __METHOD__ . ": ZIP header present in $file\n" );
                        return $this->detectZipType( $head, $tail, $ext );
                }
 
@@ -737,14 +736,14 @@ class MimeMagic {
 
                if( $gis && isset( $gis['mime'] ) ) {
                        $mime = $gis['mime'];
-                       wfDebug( __METHOD__.": getimagesize detected $file as $mime\n" );
+                       wfDebug( __METHOD__ . ": getimagesize detected $file as $mime\n" );
                        return $mime;
                }
 
                // Also test DjVu
                $deja = new DjVuImage( $file );
                if( $deja->isValid() ) {
-                       wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" );
+                       wfDebug( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
                        return 'image/vnd.djvu';
                }
 
@@ -766,7 +765,7 @@ class MimeMagic {
         */
        function detectZipType( $header, $tail = null, $ext = false ) {
                if( $ext ) { # TODO: remove $ext param
-                       wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " .
+                       wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
                                "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
                }
 
@@ -797,7 +796,7 @@ class MimeMagic {
 
                if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
                        $mime = $matches[1];
-                       wfDebug( __METHOD__.": detected $mime from ZIP archive\n" );
+                       wfDebug( __METHOD__ . ": detected $mime from ZIP archive\n" );
                } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
                        $mime = "application/x-opc+zip";
                        # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
@@ -814,7 +813,7 @@ class MimeMagic {
                                        $mime = "application/zip";
                                }
                        }
-                       wfDebug( __METHOD__.": detected an Open Packaging Conventions archive: $mime\n" );
+                       wfDebug( __METHOD__ . ": detected an Open Packaging Conventions archive: $mime\n" );
                } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
                                ($headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
                                preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
@@ -843,9 +842,9 @@ class MimeMagic {
                                        break;
                        }
 
-                       wfDebug( __METHOD__.": detected a MS Office document with OPC trailer\n");
+                       wfDebug( __METHOD__ . ": detected a MS Office document with OPC trailer\n" );
                } else {
-                       wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" );
+                       wfDebug( __METHOD__ . ": unable to identify type of ZIP archive\n" );
                }
                return $mime;
        }
@@ -872,7 +871,7 @@ class MimeMagic {
                global $wgMimeDetectorCommand;
 
                if ( $ext ) { # TODO:  make $ext default to false. Or better, remove it.
-                       wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
+                       wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
                }
 
                $m = null;
@@ -898,7 +897,7 @@ class MimeMagic {
                                $m = finfo_file( $mime_magic_resource, $file );
                                finfo_close( $mime_magic_resource );
                        } else {
-                               wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" );
+                               wfDebug( __METHOD__ . ": finfo_open failed on ".FILEINFO_MIME."!\n" );
                        }
                } elseif ( function_exists( "mime_content_type" ) ) {
 
@@ -911,9 +910,9 @@ class MimeMagic {
                        # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
                        # see http://www.php.net/manual/en/ref.mime-magic.php for details.
 
-                       $m = mime_content_type($file);
+                       $m = mime_content_type( $file );
                } else {
-                       wfDebug( __METHOD__.": no magic mime detector found!\n" );
+                       wfDebug( __METHOD__ . ": no magic mime detector found!\n" );
                }
 
                if ( $m ) {
@@ -925,7 +924,7 @@ class MimeMagic {
                        if ( strpos( $m, 'unknown' ) !== false ) {
                                $m = null;
                        } else {
-                               wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
+                               wfDebug( __METHOD__ . ": magic mime type of $file: $m\n" );
                                return $m;
                        }
                }
@@ -937,11 +936,11 @@ class MimeMagic {
                }
                if ( $ext ) {
                        if( $this->isRecognizableExtension( $ext ) ) {
-                               wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
+                               wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
                        } else {
                                $m = $this->guessTypesForExtension( $ext );
                                if ( $m ) {
-                                       wfDebug( __METHOD__.": extension mime type of $file: $m\n" );
+                                       wfDebug( __METHOD__ . ": extension mime type of $file: $m\n" );
                                        return $m;
                                }
                        }
@@ -1066,7 +1065,7 @@ class MimeMagic {
 
                foreach ( $m as $mime ) {
                        foreach ( $this->mMediaTypes as $type => $codes ) {
-                               if ( in_array($mime, $codes, true ) ) {
+                               if ( in_array( $mime, $codes, true ) ) {
                                        return $type;
                                }
                        }