Esempio n. 1
0
 /** Initializes the MimeMagic object. This is called by MimeMagic::singleton().
  *
  * This constructor parses the mime.types and mime.info files and build internal mappings.
  */
 function __construct()
 {
     /**
      *   --- load mime.types ---
      */
     global $wgMimeTypeFile, $IP, $wgLoadFileinfoExtension;
     $types = MM_WELL_KNOWN_MIME_TYPES;
     if ($wgMimeTypeFile == 'includes/mime.types') {
         $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");
             $types .= "\n";
             $types .= file_get_contents($wgMimeTypeFile);
         } else {
             wfDebug(__METHOD__ . ": can't load mime types from {$wgMimeTypeFile}\n");
         }
     } else {
         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);
     $types = str_replace("\t", " ", $types);
     $this->mMimeToExt = array();
     $this->mToMime = array();
     $lines = explode("\n", $types);
     foreach ($lines as $s) {
         $s = trim($s);
         if (empty($s)) {
             continue;
         }
         if (strpos($s, '#') === 0) {
             continue;
         }
         $s = strtolower($s);
         $i = strpos($s, ' ');
         if ($i === false) {
             continue;
         }
         $mime = substr($s, 0, $i);
         $ext = trim(substr($s, $i + 1));
         if (empty($ext)) {
             continue;
         }
         if (!empty($this->mMimeToExt[$mime])) {
             $this->mMimeToExt[$mime] .= ' ' . $ext;
         } else {
             $this->mMimeToExt[$mime] = $ext;
         }
         $extensions = explode(' ', $ext);
         foreach ($extensions as $e) {
             $e = trim($e);
             if (empty($e)) {
                 continue;
             }
             if (!empty($this->mExtToMime[$e])) {
                 $this->mExtToMime[$e] .= ' ' . $mime;
             } else {
                 $this->mExtToMime[$e] = $mime;
             }
         }
     }
     /**
      *   --- load mime.info ---
      */
     global $wgMimeInfoFile;
     if ($wgMimeInfoFile == 'includes/mime.info') {
         $wgMimeInfoFile = "{$IP}/{$wgMimeInfoFile}";
     }
     $info = MM_WELL_KNOWN_MIME_INFO;
     if ($wgMimeInfoFile) {
         if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
             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");
         }
     } else {
         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("\t", " ", $info);
     $this->mMimeTypeAliases = array();
     $this->mMediaTypes = array();
     $lines = explode("\n", $info);
     foreach ($lines as $s) {
         $s = trim($s);
         if (empty($s)) {
             continue;
         }
         if (strpos($s, '#') === 0) {
             continue;
         }
         $s = strtolower($s);
         $i = strpos($s, ' ');
         if ($i === false) {
             continue;
         }
         #print "processing MIME INFO line $s<br>";
         $match = array();
         if (preg_match('!\\[\\s*(\\w+)\\s*\\]!', $s, $match)) {
             $s = preg_replace('!\\[\\s*(\\w+)\\s*\\]!', '', $s);
             $mtype = trim(strtoupper($match[1]));
         } else {
             $mtype = MEDIATYPE_UNKNOWN;
         }
         $m = explode(' ', $s);
         if (!isset($this->mMediaTypes[$mtype])) {
             $this->mMediaTypes[$mtype] = array();
         }
         foreach ($m as $mime) {
             $mime = trim($mime);
             if (empty($mime)) {
                 continue;
             }
             $this->mMediaTypes[$mtype][] = $mime;
         }
         if (sizeof($m) > 1) {
             $main = $m[0];
             for ($i = 1; $i < sizeof($m); $i += 1) {
                 $mime = $m[$i];
                 $this->mMimeTypeAliases[$mime] = $main;
             }
         }
     }
 }