| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\Mime; |
| 22 |
|
| 23 |
use Psr\Log\LoggerAwareInterface; |
| 24 |
use Psr\Log\LoggerInterface; |
| 25 |
use Psr\Log\NullLogger; |
| 26 |
use UnexpectedValueException; |
| 27 |
use Wikimedia\AtEase\AtEase; |
| 28 |
use ZipDirectoryReader; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class MimeAnalyzer implements LoggerAwareInterface { |
| 43 |
|
| 44 |
protected $typeFile; |
| 45 |
|
| 46 |
protected $infoFile; |
| 47 |
|
| 48 |
protected $xmlTypes; |
| 49 |
|
| 50 |
protected $initCallback; |
| 51 |
|
| 52 |
protected $detectCallback; |
| 53 |
|
| 54 |
protected $guessCallback; |
| 55 |
|
| 56 |
protected $extCallback; |
| 57 |
|
| 58 |
protected $mediaTypes = null; |
| 59 |
|
| 60 |
protected $mimeTypeAliases = null; |
| 61 |
|
| 62 |
protected $mimeToExts = []; |
| 63 |
|
| 64 |
protected $extToMimes = []; |
| 65 |
|
| 66 |
|
| 67 |
public $mExtToMime = []; |
| 68 |
|
| 69 |
|
| 70 |
private $extraTypes = ''; |
| 71 |
|
| 72 |
private $extraInfo = ''; |
| 73 |
|
| 74 |
|
| 75 |
private $logger; |
| 76 |
|
| 77 |
|
| 78 |
public const USE_INTERNAL = 'internal'; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function __construct( array $params ) { |
| 96 |
$this->typeFile = $params['typeFile']; |
| 97 |
$this->infoFile = $params['infoFile']; |
| 98 |
$this->xmlTypes = $params['xmlTypes']; |
| 99 |
$this->initCallback = $params['initCallback'] ?? null; |
| 100 |
$this->detectCallback = $params['detectCallback'] ?? null; |
| 101 |
$this->guessCallback = $params['guessCallback'] ?? null; |
| 102 |
$this->extCallback = $params['extCallback'] ?? null; |
| 103 |
$this->logger = $params['logger'] ?? new NullLogger(); |
| 104 |
|
| 105 |
$this->loadFiles(); |
| 106 |
} |
| 107 |
|
| 108 |
protected function loadFiles(): void { |
| 109 |
|
| 110 |
if ( $this->initCallback ) { |
| 111 |
call_user_func( $this->initCallback, $this ); |
| 112 |
} |
| 113 |
|
| 114 |
$rawTypes = $this->extraTypes; |
| 115 |
if ( $this->typeFile === self::USE_INTERNAL ) { |
| 116 |
$this->mimeToExts = MimeMap::MIME_EXTENSIONS; |
| 117 |
} else { |
| 118 |
$this->mimeToExts = MimeMapMinimal::MIME_EXTENSIONS; |
| 119 |
if ( $this->typeFile ) { |
| 120 |
$rawTypes = file_get_contents( $this->typeFile ) . "\n" . $this->extraTypes; |
| 121 |
} |
| 122 |
} |
| 123 |
if ( $rawTypes ) { |
| 124 |
$this->parseMimeTypes( $rawTypes ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
foreach ( $this->mimeToExts as $mime => $exts ) { |
| 129 |
foreach ( $exts as $ext ) { |
| 130 |
$this->extToMimes[$ext][] = $mime; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
foreach ( $this->mExtToMime as $ext => $mimes ) { |
| 137 |
foreach ( explode( ' ', $mimes ) as $mime ) { |
| 138 |
$this->extToMimes[$ext][] = $mime; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$rawInfo = $this->extraInfo; |
| 143 |
if ( $this->infoFile === self::USE_INTERNAL ) { |
| 144 |
$this->mimeTypeAliases = MimeMap::MIME_TYPE_ALIASES; |
| 145 |
$this->mediaTypes = MimeMap::MEDIA_TYPES; |
| 146 |
} else { |
| 147 |
$this->mimeTypeAliases = MimeMapMinimal::MIME_TYPE_ALIASES; |
| 148 |
$this->mediaTypes = MimeMapMinimal::MEDIA_TYPES; |
| 149 |
if ( $this->infoFile ) { |
| 150 |
$rawInfo = file_get_contents( $this->infoFile ) . "\n" . $this->extraInfo; |
| 151 |
} |
| 152 |
} |
| 153 |
if ( $rawInfo ) { |
| 154 |
$this->parseMimeInfo( $rawInfo ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
protected function parseMimeTypes( string $rawMimeTypes ): void { |
| 159 |
$rawMimeTypes = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeTypes ); |
| 160 |
$rawMimeTypes = str_replace( "\t", " ", $rawMimeTypes ); |
| 161 |
|
| 162 |
$lines = explode( "\n", $rawMimeTypes ); |
| 163 |
foreach ( $lines as $s ) { |
| 164 |
$s = trim( $s ); |
| 165 |
if ( $s === '' || str_starts_with( $s, '#' ) ) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
$s = strtolower( $s ); |
| 170 |
$i = strpos( $s, ' ' ); |
| 171 |
|
| 172 |
if ( $i === false ) { |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
$ext = trim( substr( $s, $i + 1 ) ); |
| 177 |
|
| 178 |
if ( !$ext ) { |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
$tokens = preg_split( '/\s+/', $s, -1, PREG_SPLIT_NO_EMPTY ); |
| 183 |
if ( count( $tokens ) > 1 ) { |
| 184 |
$mime = array_shift( $tokens ); |
| 185 |
$this->mimeToExts[$mime] = array_values( array_unique( |
| 186 |
array_merge( $this->mimeToExts[$mime] ?? [], $tokens ) ) ); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
protected function parseMimeInfo( string $rawMimeInfo ): void { |
| 192 |
$rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo ); |
| 193 |
$rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo ); |
| 194 |
|
| 195 |
$lines = explode( "\n", $rawMimeInfo ); |
| 196 |
foreach ( $lines as $s ) { |
| 197 |
$s = trim( $s ); |
| 198 |
if ( $s === '' || str_starts_with( $s, '#' ) ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
$s = strtolower( $s ); |
| 203 |
$i = strpos( $s, ' ' ); |
| 204 |
|
| 205 |
if ( $i === false ) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 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 ( !$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 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public function addExtraTypes( string $types ): void { |
| 258 |
$this->extraTypes .= "\n" . $types; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function addExtraInfo( string $info ): void { |
| 270 |
$this->extraInfo .= "\n" . $info; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function getExtensionsForType( $mime ) { |
| 283 |
$exts = $this->getExtensionsFromMimeType( $mime ); |
| 284 |
return $exts ? implode( ' ', $exts ) : null; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public function getMimeTypesFromExtension( string $ext ): array { |
| 314 |
$ext = strtolower( $ext ); |
| 315 |
return $this->extToMimes[$ext] ?? []; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function getMimeTypeFromExtensionOrNull( string $ext ): ?string { |
| 327 |
$types = $this->getMimeTypesFromExtension( $ext ); |
| 328 |
return $types[0] ?? null; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function guessTypesForExtension( $ext ) { |
| 340 |
return $this->getMimeTypeFromExtensionOrNull( $ext ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function getTypesForExtension( $ext ) { |
| 352 |
$types = $this->getMimeTypesFromExtension( $ext ); |
| 353 |
return $types ? implode( ' ', $types ) : null; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public function getExtensionFromMimeTypeOrNull( string $mime ): ?string { |
| 365 |
$exts = $this->getExtensionsFromMimeType( $mime ); |
| 366 |
return $exts[0] ?? null; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public function isMatchingExtension( string $extension, string $mime ): ?bool { |
| 379 |
$exts = $this->getExtensionsFromMimeType( $mime ); |
| 380 |
|
| 381 |
if ( !$exts ) { |
| 382 |
return null; |
| 383 |
} |
| 384 |
|
| 385 |
return in_array( strtolower( $extension ), $exts ); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public function isPHPImageType( string $mime ): bool { |
| 397 |
wfDeprecated( __METHOD__, '1.40' ); |
| 398 |
|
| 399 |
static $types = [ |
| 400 |
'image/gif', 'image/jpeg', 'image/png', |
| 401 |
'image/x-bmp', 'image/xbm', 'image/tiff', |
| 402 |
'image/jp2', 'image/jpeg2000', 'image/iff', |
| 403 |
'image/xbm', 'image/x-xbitmap', |
| 404 |
'image/vnd.wap.wbmp', 'image/vnd.xiff', |
| 405 |
'image/x-photoshop', |
| 406 |
'application/x-shockwave-flash', |
| 407 |
]; |
| 408 |
|
| 409 |
return in_array( $mime, $types ); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public function isRecognizableExtension( string $extension ): bool { |
| 425 |
static $types = [ |
| 426 |
|
| 427 |
'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', |
| 428 |
'bmp', 'tiff', 'tif', 'jpc', 'jp2', |
| 429 |
'jpx', 'jb2', 'swc', 'iff', 'wbmp', |
| 430 |
'xbm', |
| 431 |
|
| 432 |
|
| 433 |
'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 'opus', |
| 434 |
'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka', |
| 435 |
'webp', 'mp3', |
| 436 |
|
| 437 |
|
| 438 |
'svg', |
| 439 |
|
| 440 |
|
| 441 |
'stl', 'glb', |
| 442 |
]; |
| 443 |
return in_array( strtolower( $extension ), $types ); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public function improveTypeFromExtension( string $mime, string $ext ): ?string { |
| 460 |
if ( $mime === 'unknown/unknown' ) { |
| 461 |
if ( $this->isRecognizableExtension( $ext ) ) { |
| 462 |
$this->logger->info( __METHOD__ . ': refusing to guess mime type for .' . |
| 463 |
"$ext file, we should have recognized it" ); |
| 464 |
} else { |
| 465 |
|
| 466 |
|
| 467 |
$mime = $this->getMimeTypeFromExtensionOrNull( $ext ); |
| 468 |
} |
| 469 |
} elseif ( $mime === 'application/x-opc+zip' |
| 470 |
|| $mime === 'application/vnd.oasis.opendocument' |
| 471 |
) { |
| 472 |
if ( $this->isMatchingExtension( $ext, $mime ) ) { |
| 473 |
|
| 474 |
|
| 475 |
$mime = $this->getMimeTypeFromExtensionOrNull( $ext ); |
| 476 |
} else { |
| 477 |
$this->logger->info( __METHOD__ . |
| 478 |
": refusing to guess better type for $mime file, " . |
| 479 |
".$ext is not a known OPC/ODF extension." ); |
| 480 |
$mime = 'application/zip'; |
| 481 |
} |
| 482 |
} elseif ( $mime === 'text/plain' && $this->findMediaType( ".$ext" ) === MEDIATYPE_TEXT ) { |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
$mime = $this->getMimeTypeFromExtensionOrNull( $ext ); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
$callback = $this->extCallback; |
| 492 |
if ( $callback ) { |
| 493 |
$callback( $this, $ext, $mime ); |
| 494 |
} |
| 495 |
|
| 496 |
if ( $mime !== null && isset( $this->mimeTypeAliases[$mime] ) ) { |
| 497 |
$mime = $this->mimeTypeAliases[$mime]; |
| 498 |
} |
| 499 |
|
| 500 |
$this->logger->info( __METHOD__ . ": improved mime type for .$ext: $mime" ); |
| 501 |
return $mime; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
public function guessMimeType( string $file, $ext = true ): string { |
| 518 |
if ( $ext ) { |
| 519 |
$this->logger->info( __METHOD__ . |
| 520 |
": WARNING: use of the \$ext parameter is deprecated. " . |
| 521 |
"Use improveTypeFromExtension(\$mime, \$ext) instead." ); |
| 522 |
} |
| 523 |
|
| 524 |
$mime = $this->doGuessMimeType( $file ); |
| 525 |
|
| 526 |
if ( !$mime ) { |
| 527 |
$this->logger->info( __METHOD__ . |
| 528 |
": internal type detection failed for $file (.$ext)..." ); |
| 529 |
$mime = $this->detectMimeType( $file, $ext ); |
| 530 |
} |
| 531 |
|
| 532 |
if ( isset( $this->mimeTypeAliases[$mime] ) ) { |
| 533 |
$mime = $this->mimeTypeAliases[$mime]; |
| 534 |
} |
| 535 |
|
| 536 |
$this->logger->info( __METHOD__ . ": guessed mime type of $file: $mime" ); |
| 537 |
return $mime; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
private function doGuessMimeType( string $file ) { |
| 548 |
|
| 549 |
AtEase::suppressWarnings(); |
| 550 |
$f = fopen( $file, 'rb' ); |
| 551 |
AtEase::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 |
$head16k = $head . fread( $f, 16384 - 1024 ); |
| 564 |
$tailLength = min( 65558, $fsize ); |
| 565 |
if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) { |
| 566 |
throw new UnexpectedValueException( |
| 567 |
"Seeking $tailLength bytes from EOF failed in " . __METHOD__ ); |
| 568 |
} |
| 569 |
$tail = $tailLength ? fread( $f, $tailLength ) : ''; |
| 570 |
|
| 571 |
$this->logger->info( __METHOD__ . |
| 572 |
": analyzing head and tail of $file for magic numbers." ); |
| 573 |
|
| 574 |
|
| 575 |
$headers = [ |
| 576 |
|
| 577 |
'MThd' => 'audio/midi', |
| 578 |
'OggS' => 'application/ogg', |
| 579 |
'ID3' => 'audio/mpeg', |
| 580 |
"\xff\xfb" => 'audio/mpeg', |
| 581 |
"\xff\xf3" => 'audio/mpeg', |
| 582 |
"\xff\xe3" => 'audio/mpeg', |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
"\x01\x00\x09\x00" => 'application/x-msmetafile', |
| 587 |
"\xd7\xcd\xc6\x9a" => 'application/x-msmetafile', |
| 588 |
'%PDF' => 'application/pdf', |
| 589 |
'gimp xcf' => 'image/x-xcf', |
| 590 |
|
| 591 |
|
| 592 |
"glTF" => 'model/gltf-binary', |
| 593 |
|
| 594 |
|
| 595 |
'MZ' => 'application/octet-stream', |
| 596 |
"\xca\xfe\xba\xbe" => 'application/octet-stream', |
| 597 |
"\x7fELF" => 'application/octet-stream', |
| 598 |
]; |
| 599 |
|
| 600 |
foreach ( $headers as $magic => $candidate ) { |
| 601 |
if ( str_starts_with( $head, $magic ) ) { |
| 602 |
$this->logger->info( __METHOD__ . |
| 603 |
": magic header in $file recognized as $candidate" ); |
| 604 |
return $candidate; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
if ( str_starts_with( $head16k, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ) ) ) { |
| 610 |
$doctype = strpos( $head16k, "\x42\x82" ); |
| 611 |
if ( $doctype ) { |
| 612 |
|
| 613 |
$data = substr( $head16k, $doctype + 3, 8 ); |
| 614 |
if ( str_starts_with( $data, "matroska" ) ) { |
| 615 |
$this->logger->info( __METHOD__ . ": recognized file as video/matroska" ); |
| 616 |
return "video/matroska"; |
| 617 |
} |
| 618 |
|
| 619 |
if ( str_starts_with( $data, "webm" ) ) { |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
$videotrack = str_contains( $head16k, "\x86\x85V_VP8" ) || |
| 630 |
str_contains( $head16k, "\x86\x85V_VP9" ) || |
| 631 |
str_contains( $head16k, "\x86\x85V_AV1" ) || |
| 632 |
str_contains( $head16k, "\x86\x86V_VP8\x0" ) || |
| 633 |
str_contains( $head16k, "\x86\x86V_VP9\x0" ) || |
| 634 |
str_contains( $head16k, "\x86\x86V_AV1\x0" ); |
| 635 |
|
| 636 |
if ( $videotrack ) { |
| 637 |
|
| 638 |
$this->logger->info( __METHOD__ . ": recognized file as video/webm" ); |
| 639 |
return "video/webm"; |
| 640 |
} |
| 641 |
|
| 642 |
$this->logger->info( __METHOD__ . ": recognized file as audio/webm" ); |
| 643 |
return "audio/webm"; |
| 644 |
} |
| 645 |
} |
| 646 |
$this->logger->info( __METHOD__ . ": unknown EBML file" ); |
| 647 |
return "unknown/unknown"; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
if ( str_starts_with( $head, "RIFF" ) && substr( $head, 8, 7 ) === "WEBPVP8" ) { |
| 652 |
$this->logger->info( __METHOD__ . ": recognized file as image/webp" ); |
| 653 |
return "image/webp"; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
if ( str_starts_with( $head, "\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a" ) ) { |
| 658 |
$this->logger->info( __METHOD__ . ": recognized as JPEG2000" ); |
| 659 |
|
| 660 |
if ( substr( $head, 16, 8 ) === "ftypjp2 " ) { |
| 661 |
$this->logger->info( __METHOD__ . ": recognized file as image/jp2" ); |
| 662 |
return 'image/jp2'; |
| 663 |
} elseif ( substr( $head, 16, 8 ) === "ftypjpx " ) { |
| 664 |
$this->logger->info( __METHOD__ . ": recognized file as image/jpx" ); |
| 665 |
return 'image/jpx'; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
if ( str_starts_with( $head, "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" ) ) { |
| 671 |
$this->logger->info( __METHOD__ . ': recognized MS CFB (OLE) file' ); |
| 672 |
return $this->detectMicrosoftBinaryType( $f ); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
if ( ( strpos( $head, '<?php' ) !== false ) || |
| 688 |
( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) || |
| 689 |
( strpos( $head, "<\x00?\x00 " ) !== false ) || |
| 690 |
( strpos( $head, "<\x00?\x00\n" ) !== false ) || |
| 691 |
( strpos( $head, "<\x00?\x00\t" ) !== false ) || |
| 692 |
( strpos( $head, "<\x00?\x00=" ) !== false ) |
| 693 |
) { |
| 694 |
$this->logger->info( __METHOD__ . ": recognized $file as application/x-php" ); |
| 695 |
return 'application/x-php'; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
AtEase::suppressWarnings(); |
| 702 |
$xml = new XmlTypeCheck( $file ); |
| 703 |
AtEase::restoreWarnings(); |
| 704 |
if ( $xml->wellFormed ) { |
| 705 |
$xmlTypes = $this->xmlTypes; |
| 706 |
|
| 707 |
return $xmlTypes[$xml->getRootElement()] ?? 'application/xml'; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
$script_type = null; |
| 714 |
|
| 715 |
|
| 716 |
if ( str_starts_with( $head, "#!" ) ) { |
| 717 |
$script_type = "ASCII"; |
| 718 |
} elseif ( str_starts_with( $head, "\xef\xbb\xbf#!" ) ) { |
| 719 |
$script_type = "UTF-8"; |
| 720 |
} elseif ( str_starts_with( $head, "\xfe\xff\x00#\x00!" ) ) { |
| 721 |
$script_type = "UTF-16BE"; |
| 722 |
} elseif ( str_starts_with( $head, "\xff\xfe#\x00!" ) ) { |
| 723 |
$script_type = "UTF-16LE"; |
| 724 |
} |
| 725 |
|
| 726 |
if ( $script_type ) { |
| 727 |
if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) { |
| 728 |
|
| 729 |
$pack = [ 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' ]; |
| 730 |
$chars = unpack( $pack[$script_type], substr( $head, 2 ) ); |
| 731 |
$head = ''; |
| 732 |
foreach ( $chars as $codepoint ) { |
| 733 |
if ( $codepoint < 128 ) { |
| 734 |
$head .= chr( $codepoint ); |
| 735 |
} else { |
| 736 |
$head .= '?'; |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
$match = []; |
| 742 |
|
| 743 |
if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) { |
| 744 |
$mime = "application/x-{$match[2]}"; |
| 745 |
$this->logger->info( __METHOD__ . ": shell script recognized as $mime" ); |
| 746 |
return $mime; |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
$eocdrPos = strpos( $tail, "PK\x05\x06" ); |
| 752 |
if ( $eocdrPos !== false && $eocdrPos <= strlen( $tail ) - 22 ) { |
| 753 |
$this->logger->info( __METHOD__ . ": ZIP signature present in $file" ); |
| 754 |
|
| 755 |
$commentLength = unpack( "n", substr( $tail, $eocdrPos + 20 ) )[1]; |
| 756 |
if ( $eocdrPos + 22 + $commentLength !== strlen( $tail ) ) { |
| 757 |
$this->logger->info( __METHOD__ . ": ZIP EOCDR not at end. Not a ZIP file." ); |
| 758 |
} else { |
| 759 |
return $this->detectZipTypeFromFile( $f ); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
if ( $fsize >= 15 && |
| 766 |
stripos( $head, 'SOLID ' ) === 0 && |
| 767 |
preg_match( '/\RENDSOLID .*$/i', $tail ) ) { |
| 768 |
|
| 769 |
return 'application/sla'; |
| 770 |
} elseif ( $fsize > 84 ) { |
| 771 |
|
| 772 |
$triangles = substr( $head, 80, 4 ); |
| 773 |
$triangles = unpack( 'V', $triangles ); |
| 774 |
$triangles = reset( $triangles ); |
| 775 |
if ( $triangles !== false && $fsize === 84 + ( $triangles * 50 ) ) { |
| 776 |
return 'application/sla'; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
AtEase::suppressWarnings(); |
| 781 |
$gis = getimagesize( $file ); |
| 782 |
AtEase::restoreWarnings(); |
| 783 |
|
| 784 |
if ( $gis && isset( $gis['mime'] ) ) { |
| 785 |
$mime = $gis['mime']; |
| 786 |
$this->logger->info( __METHOD__ . ": getimagesize detected $file as $mime" ); |
| 787 |
return $mime; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
$mime = false; |
| 794 |
|
| 795 |
$callback = $this->guessCallback; |
| 796 |
if ( $callback ) { |
| 797 |
$callback( $this, $head, $tail, $file, $mime ); |
| 798 |
} |
| 799 |
|
| 800 |
return $mime; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public function detectZipTypeFromFile( $handle ) { |
| 812 |
$types = []; |
| 813 |
$status = ZipDirectoryReader::readHandle( |
| 814 |
$handle, |
| 815 |
static function ( $entry ) use ( &$types ) { |
| 816 |
$name = $entry['name']; |
| 817 |
$names = [ $name ]; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
$nullPos = strpos( $entry['name'], "\000" ); |
| 825 |
if ( $nullPos !== false ) { |
| 826 |
$names[] = substr( $entry['name'], 0, $nullPos ); |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if ( preg_grep( '!\.class/?$!', $names ) ) { |
| 832 |
$types[] = 'application/java'; |
| 833 |
} |
| 834 |
|
| 835 |
if ( $name === '[Content_Types].xml' ) { |
| 836 |
$types[] = 'application/x-opc+zip'; |
| 837 |
} elseif ( $name === 'mimetype' ) { |
| 838 |
$types[] = 'application/vnd.oasis.opendocument'; |
| 839 |
} |
| 840 |
} |
| 841 |
); |
| 842 |
if ( !$status->isOK() ) { |
| 843 |
$this->logger->info( "Error reading zip file: " . (string)$status ); |
| 844 |
|
| 845 |
return 'application/zip'; |
| 846 |
} |
| 847 |
if ( in_array( 'application/java', $types ) ) { |
| 848 |
|
| 849 |
return 'application/java'; |
| 850 |
} elseif ( count( $types ) ) { |
| 851 |
return $types[0]; |
| 852 |
} else { |
| 853 |
return 'application/zip'; |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
private function detectMicrosoftBinaryType( $handle ): string { |
| 865 |
$info = MSCompoundFileReader::readHandle( $handle ); |
| 866 |
if ( !$info['valid'] ) { |
| 867 |
$this->logger->info( __METHOD__ . ': invalid file format' ); |
| 868 |
return 'unknown/unknown'; |
| 869 |
} |
| 870 |
if ( !$info['mime'] ) { |
| 871 |
$this->logger->info( __METHOD__ . ": unrecognised document subtype" ); |
| 872 |
return 'unknown/unknown'; |
| 873 |
} |
| 874 |
return $info['mime']; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
private function detectMimeType( string $file, $ext = true ): string { |
| 894 |
|
| 895 |
if ( $ext ) { |
| 896 |
$this->logger->info( __METHOD__ . |
| 897 |
": WARNING: use of the \$ext parameter is deprecated. " |
| 898 |
. "Use improveTypeFromExtension(\$mime, \$ext) instead." ); |
| 899 |
} |
| 900 |
|
| 901 |
$callback = $this->detectCallback; |
| 902 |
if ( $callback ) { |
| 903 |
$m = $callback( $file ); |
| 904 |
} else { |
| 905 |
$m = mime_content_type( $file ); |
| 906 |
} |
| 907 |
|
| 908 |
if ( $m ) { |
| 909 |
|
| 910 |
$m = preg_replace( '![;, ].*$!', '', $m ); |
| 911 |
$m = trim( $m ); |
| 912 |
$m = strtolower( $m ); |
| 913 |
|
| 914 |
if ( !str_contains( $m, 'unknown' ) ) { |
| 915 |
$this->logger->info( __METHOD__ . ": magic mime type of $file: $m" ); |
| 916 |
return $m; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
if ( $ext === true ) { |
| 922 |
$i = strrpos( $file, '.' ); |
| 923 |
$ext = strtolower( $i ? substr( $file, $i + 1 ) : '' ); |
| 924 |
} |
| 925 |
if ( $ext ) { |
| 926 |
if ( $this->isRecognizableExtension( $ext ) ) { |
| 927 |
$this->logger->info( __METHOD__ . ": refusing to guess mime type for .$ext file, " |
| 928 |
. "we should have recognized it" ); |
| 929 |
} else { |
| 930 |
$m = $this->getMimeTypeFromExtensionOrNull( $ext ); |
| 931 |
if ( $m ) { |
| 932 |
$this->logger->info( __METHOD__ . ": extension mime type of $file: $m" ); |
| 933 |
return $m; |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
$this->logger->info( __METHOD__ . ": failed to guess mime type for $file!" ); |
| 940 |
return 'unknown/unknown'; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
public function getMediaType( ?string $path = null, ?string $mime = null ): string { |
| 959 |
if ( !$mime && !$path ) { |
| 960 |
return MEDIATYPE_UNKNOWN; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
if ( !$mime ) { |
| 965 |
|
| 966 |
$mime = $this->guessMimeType( $path, false ); |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
if ( $mime == 'application/ogg' && is_string( $path ) && file_exists( $path ) ) { |
| 972 |
|
| 973 |
$f = fopen( $path, "rt" ); |
| 974 |
if ( !$f ) { |
| 975 |
return MEDIATYPE_UNKNOWN; |
| 976 |
} |
| 977 |
$head = fread( $f, 256 ); |
| 978 |
fclose( $f ); |
| 979 |
|
| 980 |
$head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) ); |
| 981 |
|
| 982 |
|
| 983 |
if ( strpos( $head, 'theora' ) !== false ) { |
| 984 |
return MEDIATYPE_VIDEO; |
| 985 |
} elseif ( strpos( $head, 'vorbis' ) !== false ) { |
| 986 |
return MEDIATYPE_AUDIO; |
| 987 |
} elseif ( strpos( $head, 'flac' ) !== false ) { |
| 988 |
return MEDIATYPE_AUDIO; |
| 989 |
} elseif ( strpos( $head, 'speex' ) !== false ) { |
| 990 |
return MEDIATYPE_AUDIO; |
| 991 |
} elseif ( strpos( $head, 'opus' ) !== false ) { |
| 992 |
return MEDIATYPE_AUDIO; |
| 993 |
} else { |
| 994 |
return MEDIATYPE_MULTIMEDIA; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
$type = null; |
| 999 |
|
| 1000 |
if ( $mime ) { |
| 1001 |
$type = $this->findMediaType( $mime ); |
| 1002 |
if ( $type !== MEDIATYPE_UNKNOWN ) { |
| 1003 |
return $type; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
if ( $path ) { |
| 1009 |
$i = strrpos( $path, '.' ); |
| 1010 |
$e = strtolower( $i ? substr( $path, $i + 1 ) : '' ); |
| 1011 |
|
| 1012 |
|
| 1013 |
$type = $this->findMediaType( '.' . $e ); |
| 1014 |
if ( $type !== MEDIATYPE_UNKNOWN ) { |
| 1015 |
return $type; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
if ( $mime ) { |
| 1021 |
$i = strpos( $mime, '/' ); |
| 1022 |
if ( $i !== false ) { |
| 1023 |
$major = substr( $mime, 0, $i ); |
| 1024 |
$type = $this->findMediaType( $major ); |
| 1025 |
if ( $type !== MEDIATYPE_UNKNOWN ) { |
| 1026 |
return $type; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
if ( !$type ) { |
| 1032 |
$type = MEDIATYPE_UNKNOWN; |
| 1033 |
} |
| 1034 |
|
| 1035 |
return $type; |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
private function findMediaType( string $extMime ) { |
| 1048 |
if ( strpos( $extMime, '.' ) === 0 ) { |
| 1049 |
|
| 1050 |
$m = $this->getMimeTypesFromExtension( substr( $extMime, 1 ) ); |
| 1051 |
if ( !$m ) { |
| 1052 |
return MEDIATYPE_UNKNOWN; |
| 1053 |
} |
| 1054 |
} else { |
| 1055 |
|
| 1056 |
if ( isset( $this->mimeTypeAliases[$extMime] ) ) { |
| 1057 |
$extMime = $this->mimeTypeAliases[$extMime]; |
| 1058 |
} |
| 1059 |
|
| 1060 |
$m = [ $extMime ]; |
| 1061 |
} |
| 1062 |
|
| 1063 |
foreach ( $m as $mime ) { |
| 1064 |
foreach ( $this->mediaTypes as $type => $codes ) { |
| 1065 |
if ( in_array( $mime, $codes, true ) ) { |
| 1066 |
return $type; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
return MEDIATYPE_UNKNOWN; |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
public function getMediaTypes(): array { |
| 1080 |
return array_keys( $this->mediaTypes ); |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
public function isValidMajorMimeType( string $type ): bool { |
| 1092 |
|
| 1093 |
$types = [ |
| 1094 |
'unknown', |
| 1095 |
'application', |
| 1096 |
'audio', |
| 1097 |
'image', |
| 1098 |
'text', |
| 1099 |
'video', |
| 1100 |
'message', |
| 1101 |
'model', |
| 1102 |
'multipart', |
| 1103 |
'chemical', |
| 1104 |
]; |
| 1105 |
|
| 1106 |
return in_array( $type, $types ); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
class_alias( MimeAnalyzer::class, 'MimeAnalyzer' ); |