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