378 foreach ( $this->versions as $version ) {
394 $semiPos = strpos( $proposed,
';' );
395 if ( $semiPos !==
false ) {
396 $proposed = substr( $proposed, 0, $semiPos );
399 $proposedFormat = $this->
getDataFormat( $version, $proposed );
400 if ( $proposedFormat ==
'unknown'
401 && $proposed !=
'multipart/mixed'
402 && $proposed !=
'multipart/x-mixed-replace'
406 if ( strval( $chunk ) ===
'' ) {
411 $chunk = substr( $chunk, 0, 255 );
416 $result = $this->
sampleData( $version, $chunk );
417 $sampleFound = $result[
'found'];
418 $counters = $result[
'counters'];
422 if ( $proposed ==
'text/html' && isset( $sampleFound[
'html'] ) ) {
425 if ( $proposed ==
'image/gif' && $binaryType ==
'image/gif' ) {
428 if ( ( $proposed ==
'image/pjpeg' || $proposed ==
'image/jpeg' )
429 && $binaryType ==
'image/pjpeg'
434 if ( $version >=
'ie07'
435 && ( $proposed ==
'image/x-png' || $proposed ==
'image/png' )
436 && $binaryType ==
'image/x-png'
442 if ( isset( $sampleFound[
'cdf'] ) ) {
443 return 'application/x-cdf';
448 if ( isset( $sampleFound[
'rss'] ) ) {
449 return 'application/rss+xml';
451 if ( isset( $sampleFound[
'rdf-tag'] )
452 && isset( $sampleFound[
'rdf-url'] )
453 && isset( $sampleFound[
'rdf-purl'] )
455 return 'application/rss+xml';
457 if ( isset( $sampleFound[
'atom'] ) ) {
458 return 'application/atom+xml';
461 if ( isset( $sampleFound[
'xml'] ) ) {
463 if ( strpos( $version,
'strict' ) !==
false ) {
464 if ( $proposed ==
'text/html' || $proposed ==
'text/xml' ) {
471 if ( isset( $sampleFound[
'html'] ) ) {
473 if ( strpos( $version,
'nohtml' ) !==
false ) {
474 if ( $proposed ==
'text/plain' ) {
481 if ( isset( $sampleFound[
'xbm'] ) ) {
482 return 'image/x-bitmap';
484 if ( isset( $sampleFound[
'binhex'] ) ) {
485 return 'application/macbinhex40';
487 if ( isset( $sampleFound[
'scriptlet'] ) ) {
488 if ( strpos( $version,
'strict' ) !==
false ) {
489 if ( $proposed ==
'text/plain' || $proposed ==
'text/scriptlet' ) {
490 return 'text/scriptlet';
493 return 'text/scriptlet';
499 if ( $counters[
'ctrl'] != 0 && ( $counters[
'ff'] + $counters[
'low'] )
500 < ( $counters[
'ctrl'] + $counters[
'high'] ) * 16
502 $kindOfBinary =
true;
503 $type = $binaryType ? $binaryType : $textType;
504 if (
$type ===
false ) {
505 $type =
'application/octet-stream';
508 $kindOfBinary =
false;
509 $type = $textType ? $textType : $binaryType;
510 if (
$type ===
false ) {
511 $type =
'text/plain';
518 if ( $detectedFormat !=
'ambiguous' ) {
522 if ( $proposedFormat !=
'ambiguous' ) {
524 if ( $proposedFormat ==
'text' && !$kindOfBinary ) {
527 if ( $proposedFormat ==
'binary' && $kindOfBinary ) {
530 if ( $proposedFormat ==
'html' ) {
536 $dotPos = strrpos( $fileName,
'.' );
537 if ( $dotPos ===
false ) {
540 $ext = substr( $fileName, $dotPos );
541 if ( isset( $this->registry[
$ext] ) ) {
542 return $this->registry[
$ext];
561 $chunk2 = substr( $chunk, 0, 2 );
562 $chunk4 = substr( $chunk, 0, 4 );
563 $chunk5 = substr( $chunk, 0, 5 );
564 if ( $chunk4 ==
'%PDF' ) {
565 return 'application/pdf';
567 if ( $chunk2 ==
'%!' ) {
568 return 'application/postscript';
570 if ( $chunk5 ==
'{\\rtf' ) {
571 return 'text/richtext';
573 if ( $chunk5 ==
'begin' ) {
574 return 'application/base64';
587 $chunk2 = substr( $chunk, 0, 2 );
588 $chunk3 = substr( $chunk, 0, 3 );
589 $chunk4 = substr( $chunk, 0, 4 );
590 $chunk5 = substr( $chunk, 0, 5 );
591 $chunk5uc = strtoupper( $chunk5 );
592 $chunk8 = substr( $chunk, 0, 8 );
593 if ( $chunk5uc ==
'GIF87' || $chunk5uc ==
'GIF89' ) {
596 if ( $chunk2 ==
"\xff\xd8" ) {
597 return 'image/pjpeg';
601 && substr( $chunk, 6, 2 ) ==
"\000\000"
602 && substr( $chunk, 8, 2 ) ==
"\000\000"
606 if ( $chunk4 ==
'RIFF'
607 && substr( $chunk, 8, 4 ) ==
'WAVE'
613 if ( $chunk4 ==
".sd\000"
615 || $chunk4 ==
"\000ds."
618 return 'audio/basic';
620 if ( $chunk3 ==
"MM\000" ) {
623 if ( $chunk2 ==
'MZ' ) {
624 return 'application/x-msdownload';
626 if ( $chunk8 ==
"\x89PNG\x0d\x0a\x1a\x0a" ) {
627 return 'image/x-png';
629 if ( strlen( $chunk ) >= 5 ) {
630 $byte2 = ord( $chunk[2] );
631 $byte4 = ord( $chunk[4] );
632 if ( $byte2 >= 3 && $byte2 <= 31 && $byte4 == 0 && $chunk2 ==
'JG' ) {
637 if ( $chunk4 ==
'MROF' ) {
638 return 'audio/x-aiff';
640 $chunk4_8 = substr( $chunk, 8, 4 );
641 if ( $chunk4 ==
'FORM' && ( $chunk4_8 ==
'AIFF' || $chunk4_8 ==
'AIFC' ) ) {
642 return 'audio/x-aiff';
644 if ( $chunk4 ==
'RIFF' && $chunk4_8 ==
'AVI ' ) {
647 if ( $chunk4 ==
"\x00\x00\x01\xb3" || $chunk4 ==
"\x00\x00\x01\xba" ) {
650 if ( $chunk4 ==
"\001\000\000\000"
651 && substr( $chunk, 40, 4 ) ==
' EMF'
653 return 'image/x-emf';
655 if ( $chunk4 ==
"\xd7\xcd\xc6\x9a" ) {
656 return 'image/x-wmf';
658 if ( $chunk4 ==
"\xca\xfe\xba\xbe" ) {
659 return 'application/java';
661 if ( $chunk2 ==
'PK' ) {
662 return 'application/x-zip-compressed';
664 if ( $chunk2 ==
"\x1f\x9d" ) {
665 return 'application/x-compressed';
667 if ( $chunk2 ==
"\x1f\x8b" ) {
668 return 'application/x-gzip-compressed';
671 if ( $chunk5 ==
"MThd\000" ) {
674 if ( $chunk4 ==
'%PDF' ) {
675 return 'application/pdf';
709 $rdfUrl =
'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
710 $rdfPurl =
'http://purl.org/rss/1.0/';
711 $xbmMagic1 =
'#define';
712 $xbmMagic2 =
'_width';
713 $xbmMagic3 =
'_bits';
714 $binhexMagic =
'converted with BinHex';
715 $chunkLength = strlen( $chunk );
717 for ( $offset = 0; $offset < $chunkLength; $offset++ ) {
718 $curChar = $chunk[$offset];
719 if ( $curChar ==
"\x0a" ) {
722 } elseif ( $curChar ==
"\x0d" ) {
725 } elseif ( $curChar ==
"\x0c" ) {
728 } elseif ( $curChar ==
"\t" ) {
731 } elseif ( ord( $curChar ) < 32 ) {
734 } elseif ( ord( $curChar ) >= 128 ) {
740 if ( $curChar ==
'<' ) {
742 $remainder = substr( $chunk, $offset + 1 );
743 if ( !strncasecmp( $remainder,
'?XML', 4 ) ) {
744 $nextChar = substr( $chunk, $offset + 5, 1 );
745 if ( $nextChar ==
':' || $nextChar ==
' ' || $nextChar ==
"\t" ) {
746 $found[
'xml'] =
true;
750 if ( !strncasecmp( $remainder,
'SCRIPTLET', 9 ) ) {
751 $found[
'scriptlet'] =
true;
755 foreach ( $htmlTags as $tag ) {
756 if ( !strncasecmp( $remainder, $tag, strlen( $tag ) ) ) {
757 $found[
'html'] =
true;
763 if ( $version <
'ie07' ) {
764 if ( !strncasecmp( $remainder,
'CHANNEL', 7 ) ) {
765 $found[
'cdf'] =
true;
769 if ( !strncasecmp( $remainder,
'RSS', 3 ) ) {
770 $found[
'rss'] =
true;
773 if ( !strncasecmp( $remainder,
'rdf:RDF', 7 ) ) {
774 $found[
'rdf-tag'] =
true;
777 if ( !strncasecmp( $remainder,
'FEED', 4 ) ) {
778 $found[
'atom'] =
true;
788 $remainder = substr( $chunk, $offset );
789 if ( !strncasecmp( $remainder, $rdfUrl, strlen( $rdfUrl ) ) ) {
790 $found[
'rdf-url'] =
true;
791 if ( isset( $found[
'rdf-tag'] )
792 && isset( $found[
'rdf-purl'] )
799 if ( !strncasecmp( $remainder, $rdfPurl, strlen( $rdfPurl ) ) ) {
800 if ( isset( $found[
'rdf-tag'] )
801 && isset( $found[
'rdf-url'] )
809 if ( !strncasecmp( $remainder, $xbmMagic1, strlen( $xbmMagic1 ) ) ) {
810 $found[
'xbm1'] =
true;
813 if ( $curChar ==
'_' ) {
814 if ( isset( $found[
'xbm2'] ) ) {
815 if ( !strncasecmp( $remainder, $xbmMagic3, strlen( $xbmMagic3 ) ) ) {
816 $found[
'xbm'] =
true;
819 } elseif ( isset( $found[
'xbm1'] ) ) {
820 if ( !strncasecmp( $remainder, $xbmMagic2, strlen( $xbmMagic2 ) ) ) {
821 $found[
'xbm2'] =
true;
827 if ( !strncmp( $remainder, $binhexMagic, strlen( $binhexMagic ) ) ) {
828 $found[
'binhex'] =
true;
831 return [
'found' => $found,
'counters' => $counters ];