"else if" to "elseif"
[lhc/web/wiklou.git] / includes / MimeMagic.php
index 159a658..6232523 100644 (file)
@@ -117,14 +117,6 @@ unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
 END_STRING
 );
 
-// Note: because this file is possibly included by a function,
-// we need to access the global scope explicitely!
-global $wgLoadFileinfoExtension;
-
-if ( $wgLoadFileinfoExtension ) {
-       wfDl( 'fileinfo' );
-}
-
 /**
  * Implements functions related to mime types such as detection and mapping to
  * file extension.
@@ -160,16 +152,20 @@ class MimeMagic {
         */
        private static $instance;
 
+       /** True if the fileinfo extension has been loaded
+        */
+       private static $extensionLoaded = false;
+
        /** Initializes the MimeMagic object. This is called by MimeMagic::singleton().
-       *
-       * This constructor parses the mime.types and mime.info files and build internal mappings.
-       */
+        *
+        * This constructor parses the mime.types and mime.info files and build internal mappings.
+        */
        function __construct() {
-               /*
+               /**
                *   --- load mime.types ---
                */
 
-               global $wgMimeTypeFile, $IP;
+               global $wgMimeTypeFile, $IP, $wgLoadFileinfoExtension;
 
                $types = MM_WELL_KNOWN_MIME_TYPES;
 
@@ -177,6 +173,11 @@ class MimeMagic {
                        $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
                }
 
+               if ( $wgLoadFileinfoExtension && !self::$extensionLoaded ) {
+                       self::$extensionLoaded = true;
+                       wfDl( 'fileinfo' );
+               }
+
                if ( $wgMimeTypeFile ) {
                        if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
                                wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" );
@@ -198,20 +199,28 @@ class MimeMagic {
                $lines = explode( "\n",$types );
                foreach ( $lines as $s ) {
                        $s = trim( $s );
-                       if ( empty( $s ) ) continue;
-                       if ( strpos( $s, '#' ) === 0 ) continue;
+                       if ( empty( $s ) ) {
+                               continue;
+                       }
+                       if ( strpos( $s, '#' ) === 0 ) {
+                               continue;
+                       }
 
                        $s = strtolower( $s );
                        $i = strpos( $s, ' ' );
 
-                       if ( $i === false ) continue;
+                       if ( $i === false ) {
+                               continue;
+                       }
 
                        #print "processing MIME line $s<br>";
 
                        $mime = substr( $s, 0, $i );
                        $ext = trim( substr($s, $i+1 ) );
 
-                       if ( empty( $ext ) ) continue;
+                       if ( empty( $ext ) ) {
+                               continue;
+                       }
 
                        if ( !empty( $this->mMimeToExt[$mime] ) ) {
                                $this->mMimeToExt[$mime] .= ' ' . $ext;
@@ -223,7 +232,9 @@ class MimeMagic {
 
                        foreach ( $extensions as $e ) {
                                $e = trim( $e );
-                               if ( empty( $e ) ) continue;
+                               if ( empty( $e ) ) {
+                                       continue;
+                               }
 
                                if ( !empty( $this->mExtToMime[$e] ) ) {
                                        $this->mExtToMime[$e] .= ' ' . $mime;
@@ -233,9 +244,9 @@ class MimeMagic {
                        }
                }
 
-               /*
-               *   --- load mime.info ---
-               */
+               /**
+                *   --- load mime.info ---
+                */
 
                global $wgMimeInfoFile;
                if ( $wgMimeInfoFile == 'includes/mime.info' ) {
@@ -265,13 +276,19 @@ class MimeMagic {
                $lines = explode( "\n", $info );
                foreach ( $lines as $s ) {
                        $s = trim( $s );
-                       if ( empty( $s ) ) continue;
-                       if ( strpos( $s, '#' ) === 0 ) continue;
+                       if ( empty( $s ) ) {
+                               continue;
+                       }
+                       if ( strpos( $s, '#' ) === 0 ) {
+                               continue;
+                       }
 
                        $s = strtolower( $s );
                        $i = strpos( $s, ' ' );
 
-                       if ( $i === false ) continue;
+                       if ( $i === false ) {
+                               continue;
+                       }
 
                        #print "processing MIME INFO line $s<br>";
 
@@ -291,7 +308,9 @@ class MimeMagic {
 
                        foreach ( $m as $mime ) {
                                $mime = trim( $mime );
-                               if ( empty( $mime ) ) continue;
+                               if ( empty( $mime ) ) {
+                                       continue;
+                               }
 
                                $this->mMediaTypes[$mtype][] = $mime;
                        }
@@ -405,6 +424,10 @@ class MimeMagic {
        /** 
         * Returns true if the mime type is known to represent an image format 
         * supported by the PHP GD library.
+        *
+        * @param $mime string
+        * 
+        * @return bool
         */
        public function isPHPImageType( $mime ) {
                // As defined by imagegetsize and image_type_to_mime
@@ -545,7 +568,8 @@ class MimeMagic {
        private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
                // Read a chunk of the file
                wfSuppressWarnings();
-               $f = fopen( $file, 'rt' ); // FIXME: Shouldn't this be rb?
+               // @todo FIXME: Shouldn't this be rb?
+               $f = fopen( $file, 'rt' );
                wfRestoreWarnings();
                
                if( !$f ) {
@@ -608,12 +632,12 @@ class MimeMagic {
                        return "image/webp";
                }
 
-               /*
+               /**
                 * Look for PHP.  Check for this before HTML/XML...  Warning: this is a
                 * heuristic, and won't match a file with a lot of non-PHP before.  It
                 * will also match text files which could be PHP. :)
                 *
-                * FIXME: For this reason, the check is probably useless -- an attacker
+                * @todo FIXME: For this reason, the check is probably useless -- an attacker
                 * could almost certainly just pad the file with a lot of nonsense to
                 * circumvent the check in any case where it would be a security
                 * problem.  On the other hand, it causes harmful false positives (bug
@@ -632,7 +656,7 @@ class MimeMagic {
                        return 'application/x-php';
                }
 
-               /*
+               /**
                 * look for XML formats (XHTML and SVG)
                 */
                $xml = new XmlTypeCheck( $file );
@@ -645,7 +669,7 @@ class MimeMagic {
                        }
                }
 
-               /*
+               /**
                 * look for shell scripts
                 */
                $script_type = null;
@@ -837,7 +861,7 @@ class MimeMagic {
 
                $m = null;
                if ( $wgMimeDetectorCommand ) {
-                       // FIXME: Use wfShellExec
+                       // @todo FIXME: Use wfShellExec
                        $fn = wfEscapeShellArg( $file );
                        $m = `$wgMimeDetectorCommand $fn`;
                } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
@@ -1049,6 +1073,8 @@ class MimeMagic {
 
        /**
         * Get a cached instance of IEContentAnalyzer
+        *
+        * @return IEContentAnalyzer
         */
        protected function getIEContentAnalyzer() {
                if ( is_null( $this->mIEAnalyzer ) ) {