MediaWiki  master
MimeAnalyzer.php
Go to the documentation of this file.
1 <?php
22 use Psr\Log\LoggerAwareInterface;
23 use Psr\Log\LoggerInterface;
24 use Psr\Log\NullLogger;
27 
33 class MimeAnalyzer implements LoggerAwareInterface {
35  protected $typeFile;
37  protected $infoFile;
39  protected $xmlTypes;
41  protected $initCallback;
43  protected $detectCallback;
45  protected $guessCallback;
47  protected $extCallback;
49  protected $mediaTypes = null;
51  protected $mimeTypeAliases = null;
53  protected $mimeToExts = [];
55  protected $extToMimes = [];
56 
58  public $mExtToMime = []; // legacy name; field accessed by hooks
59 
61  protected $IEAnalyzer;
62 
64  private $extraTypes = '';
66  private $extraInfo = '';
67 
69  private $logger;
70 
72  public const USE_INTERNAL = 'internal';
73 
89  public function __construct( array $params ) {
90  $this->typeFile = $params['typeFile'];
91  $this->infoFile = $params['infoFile'];
92  $this->xmlTypes = $params['xmlTypes'];
93  $this->initCallback = $params['initCallback'] ?? null;
94  $this->detectCallback = $params['detectCallback'] ?? null;
95  $this->guessCallback = $params['guessCallback'] ?? null;
96  $this->extCallback = $params['extCallback'] ?? null;
97  $this->logger = $params['logger'] ?? new NullLogger();
98 
99  $this->loadFiles();
100  }
101 
102  protected function loadFiles() : void {
103  # Allow media handling extensions adding MIME-types and MIME-info
104  if ( $this->initCallback ) {
105  call_user_func( $this->initCallback, $this );
106  }
107 
108  $rawTypes = $this->extraTypes;
109  if ( $this->typeFile === self::USE_INTERNAL ) {
110  $this->mimeToExts = MimeMap::MIME_EXTENSIONS;
111  } else {
112  $this->mimeToExts = MimeMapMinimal::MIME_EXTENSIONS;
113  if ( $this->typeFile ) {
114  $rawTypes = file_get_contents( $this->typeFile ) . "\n" . $this->extraTypes;
115  }
116  }
117  if ( $rawTypes ) {
118  $this->parseMimeTypes( $rawTypes );
119  }
120 
121  // Build the reverse mapping (extension => MIME type).
122  foreach ( $this->mimeToExts as $mime => $exts ) {
123  foreach ( $exts as $ext ) {
124  $this->extToMimes[$ext][] = $mime;
125  }
126  }
127 
128  // Migrate items from the legacy $this->mExtToMime field.
129  // TODO: Remove this when mExtToMime is finally removed.
130  foreach ( $this->mExtToMime as $ext => $mimes ) {
131  foreach ( explode( ' ', $mimes ) as $mime ) {
132  $this->extToMimes[$ext][] = $mime;
133  }
134  }
135 
136  $rawInfo = $this->extraInfo;
137  if ( $this->infoFile === self::USE_INTERNAL ) {
138  $this->mimeTypeAliases = MimeMap::MIME_TYPE_ALIASES;
139  $this->mediaTypes = MimeMap::MEDIA_TYPES;
140  } else {
141  $this->mimeTypeAliases = MimeMapMinimal::MIME_TYPE_ALIASES;
142  $this->mediaTypes = MimeMapMinimal::MEDIA_TYPES;
143  if ( $this->infoFile ) {
144  $rawInfo = file_get_contents( $this->infoFile ) . "\n" . $this->extraInfo;
145  }
146  }
147  if ( $rawInfo ) {
148  $this->parseMimeInfo( $rawInfo );
149  }
150  }
151 
152  protected function parseMimeTypes( string $rawMimeTypes ) : void {
153  $rawMimeTypes = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeTypes );
154  $rawMimeTypes = str_replace( "\t", " ", $rawMimeTypes );
155 
156  $lines = explode( "\n", $rawMimeTypes );
157  foreach ( $lines as $s ) {
158  $s = trim( $s );
159  if ( empty( $s ) ) {
160  continue;
161  }
162  if ( strpos( $s, '#' ) === 0 ) {
163  continue;
164  }
165 
166  $s = strtolower( $s );
167  $i = strpos( $s, ' ' );
168 
169  if ( $i === false ) {
170  continue;
171  }
172 
173  $mime = substr( $s, 0, $i );
174  $ext = trim( substr( $s, $i + 1 ) );
175 
176  if ( empty( $ext ) ) {
177  continue;
178  }
179 
180  $tokens = preg_split( '/\s+/', $s, -1, PREG_SPLIT_NO_EMPTY );
181  if ( count( $tokens ) > 1 ) {
182  $mime = array_shift( $tokens );
183  $this->mimeToExts[$mime] = array_values( array_unique(
184  array_merge( $this->mimeToExts[$mime] ?? [], $tokens ) ) );
185  }
186  }
187  }
188 
189  protected function parseMimeInfo( string $rawMimeInfo ) : void {
190  $rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo );
191  $rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo );
192 
193  $lines = explode( "\n", $rawMimeInfo );
194  foreach ( $lines as $s ) {
195  $s = trim( $s );
196  if ( empty( $s ) ) {
197  continue;
198  }
199  if ( strpos( $s, '#' ) === 0 ) {
200  continue;
201  }
202 
203  $s = strtolower( $s );
204  $i = strpos( $s, ' ' );
205 
206  if ( $i === false ) {
207  continue;
208  }
209 
210  # print "processing MIME INFO line $s<br>";
211 
212  $match = [];
213  if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
214  $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
215  $mtype = trim( strtoupper( $match[1] ) );
216  } else {
217  $mtype = MEDIATYPE_UNKNOWN;
218  }
219 
220  $m = preg_split( '/\s+/', $s, -1, PREG_SPLIT_NO_EMPTY );
221 
222  if ( !isset( $this->mediaTypes[$mtype] ) ) {
223  $this->mediaTypes[$mtype] = [];
224  }
225 
226  foreach ( $m as $mime ) {
227  $mime = trim( $mime );
228  if ( empty( $mime ) ) {
229  continue;
230  }
231 
232  $this->mediaTypes[$mtype][] = $mime;
233  }
234 
235  if ( count( $m ) > 1 ) {
236  $main = $m[0];
237  $mCount = count( $m );
238  for ( $i = 1; $i < $mCount; $i += 1 ) {
239  $mime = $m[$i];
240  $this->mimeTypeAliases[$mime] = $main;
241  }
242  }
243  }
244  }
245 
246  public function setLogger( LoggerInterface $logger ) {
247  $this->logger = $logger;
248  }
249 
258  public function addExtraTypes( string $types ) : void {
259  $this->extraTypes .= "\n" . $types;
260  }
261 
270  public function addExtraInfo( string $info ) : void {
271  $this->extraInfo .= "\n" . $info;
272  }
273 
283  public function getExtensionsForType( $mime ) {
284  $exts = $this->getExtensionsFromMimeType( $mime );
285  return $exts ? implode( ' ', $exts ) : null;
286  }
287 
297  public function getExtensionsFromMimeType( string $mime ) : array {
298  $mime = strtolower( $mime );
299  if ( !isset( $this->mimeToExts[$mime] ) && isset( $this->mimeTypeAliases[$mime] ) ) {
300  $mime = $this->mimeTypeAliases[$mime];
301  }
302  return $this->mimeToExts[$mime] ?? [];
303  }
304 
314  public function getMimeTypesFromExtension( string $ext ) : array {
315  $ext = strtolower( $ext );
316  return $this->extToMimes[$ext] ?? [];
317  }
318 
327  public function getMimeTypeFromExtensionOrNull( string $ext ) : ?string {
328  $types = $this->getMimeTypesFromExtension( $ext );
329  return $types[0] ?? null;
330  }
331 
340  public function guessTypesForExtension( $ext ) {
341  return $this->getMimeTypeFromExtensionOrNull( $ext );
342  }
343 
352  public function getTypesForExtension( $ext ) {
353  $types = $this->getMimeTypesFromExtension( $ext );
354  return $types ? implode( ' ', $types ) : null;
355  }
356 
365  public function getExtensionFromMimeTypeOrNull( string $mime ) : ?string {
366  $exts = $this->getExtensionsFromMimeType( $mime );
367  return $exts[0] ?? null;
368  }
369 
379  public function isMatchingExtension( string $extension, string $mime ) : ?bool {
380  $exts = $this->getExtensionsFromMimeType( $mime );
381 
382  if ( !$exts ) {
383  return null; // Unknown MIME type
384  }
385 
386  return in_array( strtolower( $extension ), $exts );
387  }
388 
396  public function isPHPImageType( string $mime ) : bool {
397  // As defined by imagegetsize and image_type_to_mime
398  static $types = [
399  'image/gif', 'image/jpeg', 'image/png',
400  'image/x-bmp', 'image/xbm', 'image/tiff',
401  'image/jp2', 'image/jpeg2000', 'image/iff',
402  'image/xbm', 'image/x-xbitmap',
403  'image/vnd.wap.wbmp', 'image/vnd.xiff',
404  'image/x-photoshop',
405  'application/x-shockwave-flash',
406  ];
407 
408  return in_array( $mime, $types );
409  }
410 
423  public function isRecognizableExtension( string $extension ) : bool {
424  static $types = [
425  // Types recognized by getimagesize()
426  'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
427  'bmp', 'tiff', 'tif', 'jpc', 'jp2',
428  'jpx', 'jb2', 'swc', 'iff', 'wbmp',
429  'xbm',
430 
431  // Formats we recognize magic numbers for
432  'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 'opus',
433  'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
434  'webp', 'mp3',
435 
436  // XML formats we sure hope we recognize reliably
437  'svg',
438 
439  // 3D formats
440  'stl',
441  ];
442  return in_array( strtolower( $extension ), $types );
443  }
444 
458  public function improveTypeFromExtension( string $mime, string $ext ) : ?string {
459  if ( $mime === 'unknown/unknown' ) {
460  if ( $this->isRecognizableExtension( $ext ) ) {
461  $this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
462  "$ext file, we should have recognized it\n" );
463  } else {
464  // Not something we can detect, so simply
465  // trust the file extension
466  $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
467  }
468  } elseif ( $mime === 'application/x-opc+zip' ) {
469  if ( $this->isMatchingExtension( $ext, $mime ) ) {
470  // A known file extension for an OPC file,
471  // find the proper MIME type for that file extension
472  $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
473  } else {
474  $this->logger->info( __METHOD__ .
475  ": refusing to guess better type for $mime file, " .
476  ".$ext is not a known OPC extension.\n" );
477  $mime = 'application/zip';
478  }
479  } elseif ( $mime === 'text/plain' && $this->findMediaType( ".$ext" ) === MEDIATYPE_TEXT ) {
480  // Textual types are sometimes not recognized properly.
481  // If detected as text/plain, and has an extension which is textual
482  // improve to the extension's type. For example, csv and json are often
483  // misdetected as text/plain.
484  $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
485  }
486 
487  # Media handling extensions can improve the MIME detected
488  $callback = $this->extCallback;
489  if ( $callback ) {
490  $callback( $this, $ext, $mime /* by reference */ );
491  }
492 
493  if ( isset( $this->mimeTypeAliases[$mime] ) ) {
494  $mime = $this->mimeTypeAliases[$mime];
495  }
496 
497  $this->logger->info( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
498  return $mime;
499  }
500 
514  public function guessMimeType( string $file, $ext = true ) : string {
515  if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
516  $this->logger->info( __METHOD__ .
517  ": WARNING: use of the \$ext parameter is deprecated. " .
518  "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
519  }
520 
521  $mime = $this->doGuessMimeType( $file, $ext );
522 
523  if ( !$mime ) {
524  $this->logger->info( __METHOD__ .
525  ": internal type detection failed for $file (.$ext)...\n" );
526  $mime = $this->detectMimeType( $file, $ext );
527  }
528 
529  if ( isset( $this->mimeTypeAliases[$mime] ) ) {
530  $mime = $this->mimeTypeAliases[$mime];
531  }
532 
533  $this->logger->info( __METHOD__ . ": guessed mime type of $file: $mime\n" );
534  return $mime;
535  }
536 
547  private function doGuessMimeType( string $file, $ext ) {
548  // Read a chunk of the file
549  Wikimedia\suppressWarnings();
550  $f = fopen( $file, 'rb' );
551  Wikimedia\restoreWarnings();
552 
553  if ( !$f ) {
554  return 'unknown/unknown';
555  }
556 
557  $fsize = filesize( $file );
558  if ( $fsize === false ) {
559  return 'unknown/unknown';
560  }
561 
562  $head = fread( $f, 1024 );
563  $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
564  if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
565  throw new UnexpectedValueException(
566  "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
567  }
568  $tail = $tailLength ? fread( $f, $tailLength ) : '';
569 
570  $this->logger->info( __METHOD__ .
571  ": analyzing head and tail of $file for magic numbers.\n" );
572 
573  // Hardcode a few magic number checks...
574  $headers = [
575  // Multimedia...
576  'MThd' => 'audio/midi',
577  'OggS' => 'application/ogg',
578  'ID3' => 'audio/mpeg',
579  "\xff\xfb" => 'audio/mpeg', // MPEG-1 layer 3
580  "\xff\xf3" => 'audio/mpeg', // MPEG-2 layer 3 (lower sample rates)
581  "\xff\xe3" => 'audio/mpeg', // MPEG-2.5 layer 3 (very low sample rates)
582 
583  // Image formats...
584  // Note that WMF may have a bare header, no magic number.
585  "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
586  "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
587  '%PDF' => 'application/pdf',
588  'gimp xcf' => 'image/x-xcf',
589 
590  // Some forbidden fruit...
591  'MZ' => 'application/octet-stream', // DOS/Windows executable
592  "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
593  "\x7fELF" => 'application/octet-stream', // ELF binary
594  ];
595 
596  foreach ( $headers as $magic => $candidate ) {
597  if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
598  $this->logger->info( __METHOD__ .
599  ": magic header in $file recognized as $candidate\n" );
600  return $candidate;
601  }
602  }
603 
604  /* Look for WebM and Matroska files */
605  if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
606  $doctype = strpos( $head, "\x42\x82" );
607  if ( $doctype ) {
608  // Next byte is datasize, then data (sizes larger than 1 byte are stupid muxers)
609  $data = substr( $head, $doctype + 3, 8 );
610  if ( strncmp( $data, "matroska", 8 ) == 0 ) {
611  $this->logger->info( __METHOD__ . ": recognized file as video/x-matroska\n" );
612  return "video/x-matroska";
613  } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
614  // XXX HACK look for a video track, if we don't find it, this is an audio file
615  $videotrack = strpos( $head, "\x86\x85V_VP" );
616 
617  if ( $videotrack ) {
618  // There is a video track, so this is a video file.
619  $this->logger->info( __METHOD__ . ": recognized file as video/webm\n" );
620  return "video/webm";
621  }
622 
623  $this->logger->info( __METHOD__ . ": recognized file as audio/webm\n" );
624  return "audio/webm";
625  }
626  }
627  $this->logger->info( __METHOD__ . ": unknown EBML file\n" );
628  return "unknown/unknown";
629  }
630 
631  /* Look for WebP */
632  if ( strncmp( $head, "RIFF", 4 ) == 0 &&
633  strncmp( substr( $head, 8, 7 ), "WEBPVP8", 7 ) == 0
634  ) {
635  $this->logger->info( __METHOD__ . ": recognized file as image/webp\n" );
636  return "image/webp";
637  }
638 
639  /* Look for JPEG2000 */
640  if ( strncmp( $head, "\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a", 12 ) == 0 ) {
641  $this->logger->info( __METHOD__ . ": recognized as JPEG2000\n" );
642  // we skip 4 bytes
643  if ( strncmp( substr( $head, 16, 8 ), "ftypjp2 ", 8 ) == 0 ) {
644  $this->logger->info( __METHOD__ . ": recognized file as image/jp2\n" );
645  return 'image/jp2';
646  } elseif ( strncmp( substr( $head, 16, 8 ), "ftypjpx ", 8 ) == 0 ) {
647  $this->logger->info( __METHOD__ . ": recognized file as image/jpx\n" );
648  return 'image/jpx';
649  }
650  }
651 
652  /* Look for MS Compound Binary (OLE) files */
653  if ( strncmp( $head, "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1", 8 ) == 0 ) {
654  $this->logger->info( __METHOD__ . ': recognized MS CFB (OLE) file' );
655  return $this->detectMicrosoftBinaryType( $f );
656  }
657 
670  if ( ( strpos( $head, '<?php' ) !== false ) ||
671  ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
672  ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
673  ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
674  ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
675  ( strpos( $head, "<\x00?\x00=" ) !== false )
676  ) {
677  $this->logger->info( __METHOD__ . ": recognized $file as application/x-php\n" );
678  return 'application/x-php';
679  }
680 
684  Wikimedia\suppressWarnings();
685  $xml = new XmlTypeCheck( $file );
686  Wikimedia\restoreWarnings();
687  if ( $xml->wellFormed ) {
689  return $xmlTypes[$xml->getRootElement()] ?? 'application/xml';
690  }
691 
695  $script_type = null;
696 
697  # detect by shebang
698  if ( substr( $head, 0, 2 ) == "#!" ) {
699  $script_type = "ASCII";
700  } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) {
701  $script_type = "UTF-8";
702  } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) {
703  $script_type = "UTF-16BE";
704  } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
705  $script_type = "UTF-16LE";
706  }
707 
708  if ( $script_type ) {
709  if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
710  // Quick and dirty fold down to ASCII!
711  $pack = [ 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' ];
712  $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
713  $head = '';
714  foreach ( $chars as $codepoint ) {
715  if ( $codepoint < 128 ) {
716  $head .= chr( $codepoint );
717  } else {
718  $head .= '?';
719  }
720  }
721  }
722 
723  $match = [];
724 
725  if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
726  $mime = "application/x-{$match[2]}";
727  $this->logger->info( __METHOD__ . ": shell script recognized as $mime\n" );
728  return $mime;
729  }
730  }
731 
732  // Check for ZIP variants (before getimagesize)
733  $eocdrPos = strpos( $tail, "PK\x05\x06" );
734  if ( $eocdrPos !== false && $eocdrPos <= strlen( $tail ) - 22 ) {
735  $this->logger->info( __METHOD__ . ": ZIP signature present in $file\n" );
736  // Check if it really is a ZIP file, make sure the EOCDR is at the end (T40432)
737  $commentLength = unpack( "n", substr( $tail, $eocdrPos + 20 ) )[1];
738  if ( $eocdrPos + 22 + $commentLength !== strlen( $tail ) ) {
739  $this->logger->info( __METHOD__ . ": ZIP EOCDR not at end. Not a ZIP file." );
740  } else {
741  return $this->detectZipType( $head, $tail, $ext );
742  }
743  }
744 
745  // Check for STL (3D) files
746  // @see https://en.wikipedia.org/wiki/STL_(file_format)
747  if ( $fsize >= 15 &&
748  stripos( $head, 'SOLID ' ) === 0 &&
749  preg_match( '/\RENDSOLID .*$/i', $tail ) ) {
750  // ASCII STL file
751  return 'application/sla';
752  } elseif ( $fsize > 84 ) {
753  // binary STL file
754  $triangles = substr( $head, 80, 4 );
755  $triangles = unpack( 'V', $triangles );
756  $triangles = reset( $triangles );
757  if ( $triangles !== false && $fsize === 84 + ( $triangles * 50 ) ) {
758  return 'application/sla';
759  }
760  }
761 
762  Wikimedia\suppressWarnings();
763  $gis = getimagesize( $file );
764  Wikimedia\restoreWarnings();
765 
766  if ( $gis && isset( $gis['mime'] ) ) {
767  $mime = $gis['mime'];
768  $this->logger->info( __METHOD__ . ": getimagesize detected $file as $mime\n" );
769  return $mime;
770  }
771 
772  # Media handling extensions can guess the MIME by content
773  # It's intentionally here so that if core is wrong about a type (false positive),
774  # people will hopefully nag and submit patches :)
775  $mime = false;
776  # Some strings by reference for performance - assuming well-behaved hooks
777  $callback = $this->guessCallback;
778  if ( $callback ) {
779  $callback( $this, $head, $tail, $file, $mime /* by reference */ );
780  }
781 
782  return $mime;
783  }
784 
797  public function detectZipType( string $header, ?string $tail = null, $ext = false ) : string {
798  if ( $ext ) { # TODO: remove $ext param
799  $this->logger->info( __METHOD__ .
800  ": WARNING: use of the \$ext parameter is deprecated. " .
801  "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
802  }
803 
804  $mime = 'application/zip';
805  $opendocTypes = [
806  # In OASIS Open Document Format v1.2, Database front end document
807  # has a recommended MIME type of:
808  # application/vnd.oasis.opendocument.base
809  # Despite the type registered at the IANA being 'database' which is
810  # supposed to be normative.
811  # T35515
812  'base',
813 
814  'chart-template',
815  'chart',
816  'formula-template',
817  'formula',
818  'graphics-template',
819  'graphics',
820  'image-template',
821  'image',
822  'presentation-template',
823  'presentation',
824  'spreadsheet-template',
825  'spreadsheet',
826  'text-template',
827  'text-master',
828  'text-web',
829  'text' ];
830 
831  // The list of document types is available in OASIS Open Document
832  // Format version 1.2 under Appendix C. It is not normative though,
833  // supposedly types registered at the IANA should be.
834  // http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html
835  $types = '(?:' . implode( '|', $opendocTypes ) . ')';
836  $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
837 
838  $openxmlRegex = "/^\[Content_Types\].xml/";
839 
840  if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
841  $mime = $matches[1];
842  $this->logger->info( __METHOD__ . ": detected $mime from ZIP archive\n" );
843  } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
844  $mime = "application/x-opc+zip";
845  # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
846  if ( $ext !== true && $ext !== false ) {
851  if ( $this->isMatchingExtension( $ext, $mime ) ) {
852  /* A known file extension for an OPC file,
853  * find the proper mime type for that file extension
854  */
856  } else {
857  $mime = "application/zip";
858  }
859  }
860  $this->logger->info( __METHOD__ .
861  ": detected an Open Packaging Conventions archive: $mime\n" );
862  } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
863  ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
864  preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
865  if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) {
866  $mime = "application/msword";
867  }
868  switch ( substr( $header, 512, 6 ) ) {
869  case "\xEC\xA5\xC1\x00\x0E\x00":
870  case "\xEC\xA5\xC1\x00\x1C\x00":
871  case "\xEC\xA5\xC1\x00\x43\x00":
872  $mime = "application/vnd.ms-powerpoint";
873  break;
874  case "\xFD\xFF\xFF\xFF\x10\x00":
875  case "\xFD\xFF\xFF\xFF\x1F\x00":
876  case "\xFD\xFF\xFF\xFF\x22\x00":
877  case "\xFD\xFF\xFF\xFF\x23\x00":
878  case "\xFD\xFF\xFF\xFF\x28\x00":
879  case "\xFD\xFF\xFF\xFF\x29\x00":
880  case "\xFD\xFF\xFF\xFF\x10\x02":
881  case "\xFD\xFF\xFF\xFF\x1F\x02":
882  case "\xFD\xFF\xFF\xFF\x22\x02":
883  case "\xFD\xFF\xFF\xFF\x23\x02":
884  case "\xFD\xFF\xFF\xFF\x28\x02":
885  case "\xFD\xFF\xFF\xFF\x29\x02":
886  $mime = "application/vnd.msexcel";
887  break;
888  }
889 
890  $this->logger->info( __METHOD__ .
891  ": detected a MS Office document with OPC trailer\n" );
892  } else {
893  $this->logger->info( __METHOD__ . ": unable to identify type of ZIP archive\n" );
894  }
895  return $mime;
896  }
897 
905  private function detectMicrosoftBinaryType( $handle ) : string {
906  $info = MSCompoundFileReader::readHandle( $handle );
907  if ( !$info['valid'] ) {
908  $this->logger->info( __METHOD__ . ': invalid file format' );
909  return 'unknown/unknown';
910  }
911  if ( !$info['mime'] ) {
912  $this->logger->info( __METHOD__ . ": unrecognised document subtype" );
913  return 'unknown/unknown';
914  }
915  return $info['mime'];
916  }
917 
934  private function detectMimeType( string $file, $ext = true ) : string {
936  if ( $ext ) {
937  $this->logger->info( __METHOD__ .
938  ": WARNING: use of the \$ext parameter is deprecated. "
939  . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
940  }
941 
942  $callback = $this->detectCallback;
943  $m = null;
944  if ( $callback ) {
945  $m = $callback( $file );
946  } else {
947  $m = mime_content_type( $file );
948  }
949 
950  if ( $m ) {
951  # normalize
952  $m = preg_replace( '![;, ].*$!', '', $m ); # strip charset, etc
953  $m = trim( $m );
954  $m = strtolower( $m );
955 
956  if ( strpos( $m, 'unknown' ) !== false ) {
957  $m = null;
958  } else {
959  $this->logger->info( __METHOD__ . ": magic mime type of $file: $m\n" );
960  return $m;
961  }
962  }
963 
964  // If desired, look at extension as a fallback.
965  if ( $ext === true ) {
966  $i = strrpos( $file, '.' );
967  $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
968  }
969  if ( $ext ) {
970  if ( $this->isRecognizableExtension( $ext ) ) {
971  $this->logger->info( __METHOD__ . ": refusing to guess mime type for .$ext file, "
972  . "we should have recognized it\n" );
973  } else {
974  $m = $this->getMimeTypeFromExtensionOrNull( $ext );
975  if ( $m ) {
976  $this->logger->info( __METHOD__ . ": extension mime type of $file: $m\n" );
977  return $m;
978  }
979  }
980  }
981 
982  // Unknown type
983  $this->logger->info( __METHOD__ . ": failed to guess mime type for $file!\n" );
984  return 'unknown/unknown';
985  }
986 
1002  public function getMediaType( string $path = null, string $mime = null ) : string {
1003  if ( !$mime && !$path ) {
1004  return MEDIATYPE_UNKNOWN;
1005  }
1006 
1007  // If MIME type is unknown, guess it
1008  if ( !$mime ) {
1009  $mime = $this->guessMimeType( $path, false );
1010  }
1011 
1012  // Special code for ogg - detect if it's video (theora),
1013  // else label it as sound.
1014  if ( $mime == 'application/ogg' && is_string( $path ) && file_exists( $path ) ) {
1015  // Read a chunk of the file
1016  $f = fopen( $path, "rt" );
1017  if ( !$f ) {
1018  return MEDIATYPE_UNKNOWN;
1019  }
1020  $head = fread( $f, 256 );
1021  fclose( $f );
1022 
1023  $head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) );
1024 
1025  // This is an UGLY HACK, file should be parsed correctly
1026  if ( strpos( $head, 'theora' ) !== false ) {
1027  return MEDIATYPE_VIDEO;
1028  } elseif ( strpos( $head, 'vorbis' ) !== false ) {
1029  return MEDIATYPE_AUDIO;
1030  } elseif ( strpos( $head, 'flac' ) !== false ) {
1031  return MEDIATYPE_AUDIO;
1032  } elseif ( strpos( $head, 'speex' ) !== false ) {
1033  return MEDIATYPE_AUDIO;
1034  } elseif ( strpos( $head, 'opus' ) !== false ) {
1035  return MEDIATYPE_AUDIO;
1036  } else {
1037  return MEDIATYPE_MULTIMEDIA;
1038  }
1039  }
1040 
1041  $type = null;
1042  // Check for entry for full MIME type
1043  if ( $mime ) {
1044  $type = $this->findMediaType( $mime );
1045  if ( $type !== MEDIATYPE_UNKNOWN ) {
1046  return $type;
1047  }
1048  }
1049 
1050  // Check for entry for file extension
1051  if ( $path ) {
1052  $i = strrpos( $path, '.' );
1053  $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
1054 
1055  // TODO: look at multi-extension if this fails, parse from full path
1056  $type = $this->findMediaType( '.' . $e );
1057  if ( $type !== MEDIATYPE_UNKNOWN ) {
1058  return $type;
1059  }
1060  }
1061 
1062  // Check major MIME type
1063  if ( $mime ) {
1064  $i = strpos( $mime, '/' );
1065  if ( $i !== false ) {
1066  $major = substr( $mime, 0, $i );
1067  $type = $this->findMediaType( $major );
1068  if ( $type !== MEDIATYPE_UNKNOWN ) {
1069  return $type;
1070  }
1071  }
1072  }
1073 
1074  if ( !$type ) {
1076  }
1077 
1078  return $type;
1079  }
1080 
1090  private function findMediaType( string $extMime ) {
1091  if ( strpos( $extMime, '.' ) === 0 ) {
1092  // If it's an extension, look up the MIME types
1093  $m = $this->getMimeTypesFromExtension( substr( $extMime, 1 ) );
1094  if ( !$m ) {
1095  return MEDIATYPE_UNKNOWN;
1096  }
1097  } else {
1098  // Normalize MIME type
1099  if ( isset( $this->mimeTypeAliases[$extMime] ) ) {
1100  $extMime = $this->mimeTypeAliases[$extMime];
1101  }
1102 
1103  $m = [ $extMime ];
1104  }
1105 
1106  foreach ( $m as $mime ) {
1107  foreach ( $this->mediaTypes as $type => $codes ) {
1108  if ( in_array( $mime, $codes, true ) ) {
1109  return $type;
1110  }
1111  }
1112  }
1113 
1114  return MEDIATYPE_UNKNOWN;
1115  }
1116 
1122  public function getMediaTypes() : array {
1123  return array_keys( $this->mediaTypes );
1124  }
1125 
1135  public function getIEMimeTypes( string $fileName, string $chunk, string $proposed ) : array {
1136  $ca = $this->getIEContentAnalyzer();
1137  return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
1138  }
1139 
1145  protected function getIEContentAnalyzer() : IEContentAnalyzer {
1146  if ( $this->IEAnalyzer === null ) {
1147  $this->IEAnalyzer = new IEContentAnalyzer;
1148  }
1149  return $this->IEAnalyzer;
1150  }
1151 }
MimeAnalyzer\$guessCallback
callable $guessCallback
Definition: MimeAnalyzer.php:45
MimeAnalyzer\getIEContentAnalyzer
getIEContentAnalyzer()
Get a cached instance of IEContentAnalyzer.
Definition: MimeAnalyzer.php:1145
MimeAnalyzer\guessTypesForExtension
guessTypesForExtension( $ext)
Returns a single file extension for a given MIME type or null if unknown.
Definition: MimeAnalyzer.php:340
MimeAnalyzer\$mimeToExts
array< string, string[]> $mimeToExts
Map of MIME types to file extensions.
Definition: MimeAnalyzer.php:53
MimeAnalyzer\parseMimeInfo
parseMimeInfo(string $rawMimeInfo)
Definition: MimeAnalyzer.php:189
Wikimedia\Mime\MimeMapMinimal
MimeMapMinimal defines a core set of MIME types that cannot be overridden by configuration.
Definition: MimeMapMinimal.php:32
MimeAnalyzer\getExtensionFromMimeTypeOrNull
getExtensionFromMimeTypeOrNull(string $mime)
Returns a single file extension for a given MIME type or null if unknown.
Definition: MimeAnalyzer.php:365
MEDIATYPE_AUDIO
const MEDIATYPE_AUDIO
Definition: defines.php:32
MimeAnalyzer\getExtensionsForType
getExtensionsForType( $mime)
Returns a list of file extensions for a given MIME type as a space separated string or null if the MI...
Definition: MimeAnalyzer.php:283
MimeAnalyzer\isRecognizableExtension
isRecognizableExtension(string $extension)
Returns true if the extension represents a type which can be reliably detected from its content.
Definition: MimeAnalyzer.php:423
MimeAnalyzer\$initCallback
callable $initCallback
Definition: MimeAnalyzer.php:41
MimeAnalyzer
Implements functions related to MIME types such as detection and mapping to file extension.
Definition: MimeAnalyzer.php:33
MimeAnalyzer\$mExtToMime
array $mExtToMime
Map of file extensions types to MIME types (as a space separated list)
Definition: MimeAnalyzer.php:58
MimeAnalyzer\getMimeTypeFromExtensionOrNull
getMimeTypeFromExtensionOrNull(string $ext)
Returns a single MIME type for a given file extension or null if unknown.
Definition: MimeAnalyzer.php:327
MimeAnalyzer\$infoFile
string $infoFile
Definition: MimeAnalyzer.php:37
MimeAnalyzer\guessMimeType
guessMimeType(string $file, $ext=true)
MIME type detection.
Definition: MimeAnalyzer.php:514
MimeAnalyzer\__construct
__construct(array $params)
Definition: MimeAnalyzer.php:89
MimeAnalyzer\getTypesForExtension
getTypesForExtension( $ext)
Returns a list of MIME types for a given file extension as a space separated string or null if the ex...
Definition: MimeAnalyzer.php:352
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
MEDIATYPE_UNKNOWN
const MEDIATYPE_UNKNOWN
Definition: defines.php:26
MimeAnalyzer\$mediaTypes
array $mediaTypes
Mapping of media types to arrays of MIME types.
Definition: MimeAnalyzer.php:49
MimeAnalyzer\setLogger
setLogger(LoggerInterface $logger)
Definition: MimeAnalyzer.php:246
MimeAnalyzer\getMimeTypesFromExtension
getMimeTypesFromExtension(string $ext)
Returns an array of MIME types associated with a given file extension.
Definition: MimeAnalyzer.php:314
MimeAnalyzer\$IEAnalyzer
IEContentAnalyzer $IEAnalyzer
Definition: MimeAnalyzer.php:61
MimeAnalyzer\detectMimeType
detectMimeType(string $file, $ext=true)
Internal MIME type detection.
Definition: MimeAnalyzer.php:934
MimeAnalyzer\getIEMimeTypes
getIEMimeTypes(string $fileName, string $chunk, string $proposed)
Get the MIME types that various versions of Internet Explorer would detect from a chunk of the conten...
Definition: MimeAnalyzer.php:1135
MSCompoundFileReader\readHandle
static readHandle( $fileHandle)
Read from an open seekable handle.
Definition: MSCompoundFileReader.php:101
MimeAnalyzer\$typeFile
string $typeFile
Definition: MimeAnalyzer.php:35
$matches
$matches
Definition: NoLocalSettings.php:24
MimeAnalyzer\detectZipType
detectZipType(string $header, ?string $tail=null, $ext=false)
Detect application-specific file type of a given ZIP file from its header data.
Definition: MimeAnalyzer.php:797
IEContentAnalyzer
This class simulates Microsoft Internet Explorer's terribly broken and insecure MIME type detection a...
Definition: IEContentAnalyzer.php:27
MEDIATYPE_MULTIMEDIA
const MEDIATYPE_MULTIMEDIA
Definition: defines.php:37
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
MimeAnalyzer\getMediaType
getMediaType(string $path=null, string $mime=null)
Determine the media type code for a file, using its MIME type, name and possibly its contents.
Definition: MimeAnalyzer.php:1002
$header
$header
Definition: updateCredits.php:37
MimeAnalyzer\getMediaTypes
getMediaTypes()
Returns an array of media types (MEDIATYPE_xxx constants)
Definition: MimeAnalyzer.php:1122
MimeAnalyzer\$logger
LoggerInterface $logger
Definition: MimeAnalyzer.php:69
MimeAnalyzer\$extraInfo
string $extraInfo
Extra MIME info, set for example by media handling extensions.
Definition: MimeAnalyzer.php:66
MimeAnalyzer\$mimeTypeAliases
array $mimeTypeAliases
Map of MIME type aliases.
Definition: MimeAnalyzer.php:51
MimeAnalyzer\getExtensionsFromMimeType
getExtensionsFromMimeType(string $mime)
Returns an array of file extensions associated with a given MIME type.
Definition: MimeAnalyzer.php:297
XmlTypeCheck
Definition: XmlTypeCheck.php:28
MimeAnalyzer\loadFiles
loadFiles()
Definition: MimeAnalyzer.php:102
$lines
if(!file_exists( $CREDITS)) $lines
Definition: updateCredits.php:45
MimeAnalyzer\doGuessMimeType
doGuessMimeType(string $file, $ext)
Guess the MIME type from the file contents.
Definition: MimeAnalyzer.php:547
MEDIATYPE_TEXT
const MEDIATYPE_TEXT
Definition: defines.php:41
MimeAnalyzer\$extToMimes
array< string, string[]> $extToMimes
Map of file extensions to MIME types.
Definition: MimeAnalyzer.php:55
MEDIATYPE_VIDEO
const MEDIATYPE_VIDEO
Definition: defines.php:35
MimeAnalyzer\$extCallback
callable $extCallback
Definition: MimeAnalyzer.php:47
$path
$path
Definition: NoLocalSettings.php:25
MimeAnalyzer\findMediaType
findMediaType(string $extMime)
Returns a media code matching the given MIME type or file extension.
Definition: MimeAnalyzer.php:1090
MimeAnalyzer\isPHPImageType
isPHPImageType(string $mime)
Returns true if the MIME type is known to represent an image format supported by the PHP GD library.
Definition: MimeAnalyzer.php:396
MimeAnalyzer\isMatchingExtension
isMatchingExtension(string $extension, string $mime)
Tests if the extension matches the given MIME type.
Definition: MimeAnalyzer.php:379
MimeAnalyzer\$extraTypes
string $extraTypes
Extra MIME types, set for example by media handling extensions.
Definition: MimeAnalyzer.php:64
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
$mime
$mime
Definition: router.php:60
MimeAnalyzer\parseMimeTypes
parseMimeTypes(string $rawMimeTypes)
Definition: MimeAnalyzer.php:152
MimeAnalyzer\addExtraInfo
addExtraInfo(string $info)
Adds to the list mapping MIME to media type.
Definition: MimeAnalyzer.php:270
MimeAnalyzer\$xmlTypes
string $xmlTypes
Definition: MimeAnalyzer.php:39
MimeAnalyzer\detectMicrosoftBinaryType
detectMicrosoftBinaryType( $handle)
Detect the type of a Microsoft Compound Binary a.k.a.
Definition: MimeAnalyzer.php:905
Wikimedia\Mime\MimeMap
MimeMap defines the mapping of MIME types to file extensions and media types.
Definition: MimeMap.php:29
MimeAnalyzer\$detectCallback
callable $detectCallback
Definition: MimeAnalyzer.php:43
MimeAnalyzer\addExtraTypes
addExtraTypes(string $types)
Adds to the list mapping MIME to file extensions.
Definition: MimeAnalyzer.php:258
MimeAnalyzer\improveTypeFromExtension
improveTypeFromExtension(string $mime, string $ext)
Improves a MIME type using the file extension.
Definition: MimeAnalyzer.php:458
$type
$type
Definition: testCompression.php:52