377 foreach ( $this->versions as $version ) {
393 $semiPos = strpos( $proposed,
';' );
394 if ( $semiPos !==
false ) {
395 $proposed = substr( $proposed, 0, $semiPos );
398 $proposedFormat = $this->
getDataFormat( $version, $proposed );
399 if ( $proposedFormat ==
'unknown'
400 && $proposed !=
'multipart/mixed'
401 && $proposed !=
'multipart/x-mixed-replace'
405 if ( strval( $chunk ) ===
'' ) {
410 $chunk = substr( $chunk, 0, 255 );
415 $result = $this->
sampleData( $version, $chunk );
416 $sampleFound = $result[
'found'];
417 $counters = $result[
'counters'];
418 $binaryType = $this->checkBinaryHeaders( $version, $chunk );
419 $textType = $this->checkTextHeaders( $version, $chunk );
421 if ( $proposed ==
'text/html' && isset( $sampleFound[
'html'] ) ) {
424 if ( $proposed ==
'image/gif' && $binaryType ==
'image/gif' ) {
427 if ( ( $proposed ==
'image/pjpeg' || $proposed ==
'image/jpeg' )
428 && $binaryType ==
'image/pjpeg'
433 if ( $version >=
'ie07'
434 && ( $proposed ==
'image/x-png' || $proposed ==
'image/png' )
435 && $binaryType ==
'image/x-png'
441 if ( isset( $sampleFound[
'cdf'] ) ) {
442 return 'application/x-cdf';
447 if ( isset( $sampleFound[
'rss'] ) ) {
448 return 'application/rss+xml';
450 if ( isset( $sampleFound[
'rdf-tag'] )
451 && isset( $sampleFound[
'rdf-url'] )
452 && isset( $sampleFound[
'rdf-purl'] )
454 return 'application/rss+xml';
456 if ( isset( $sampleFound[
'atom'] ) ) {
457 return 'application/atom+xml';
460 if ( isset( $sampleFound[
'xml'] ) ) {
462 if ( strpos( $version,
'strict' ) !==
false ) {
463 if ( $proposed ==
'text/html' || $proposed ==
'text/xml' ) {
470 if ( isset( $sampleFound[
'html'] ) ) {
472 if ( strpos( $version,
'nohtml' ) !==
false ) {
473 if ( $proposed ==
'text/plain' ) {
480 if ( isset( $sampleFound[
'xbm'] ) ) {
481 return 'image/x-bitmap';
483 if ( isset( $sampleFound[
'binhex'] ) ) {
484 return 'application/macbinhex40';
486 if ( isset( $sampleFound[
'scriptlet'] ) ) {
487 if ( strpos( $version,
'strict' ) !==
false ) {
488 if ( $proposed ==
'text/plain' || $proposed ==
'text/scriptlet' ) {
489 return 'text/scriptlet';
492 return 'text/scriptlet';
498 if ( $counters[
'ctrl'] != 0 && ( $counters[
'ff'] + $counters[
'low'] )
499 < ( $counters[
'ctrl'] + $counters[
'high'] ) * 16
501 $kindOfBinary =
true;
502 $type = $binaryType ?: $textType;
503 if (
$type ===
false ) {
504 $type =
'application/octet-stream';
507 $kindOfBinary =
false;
508 $type = $textType ?: $binaryType;
509 if (
$type ===
false ) {
510 $type =
'text/plain';
517 if ( $detectedFormat !=
'ambiguous' ) {
521 if ( $proposedFormat !=
'ambiguous' ) {
523 if ( $proposedFormat ==
'text' && !$kindOfBinary ) {
526 if ( $proposedFormat ==
'binary' && $kindOfBinary ) {
529 if ( $proposedFormat ==
'html' ) {
535 $dotPos = strrpos( $fileName,
'.' );
536 if ( $dotPos ===
false ) {
539 $ext = substr( $fileName, $dotPos );
540 if ( isset( $this->registry[
$ext] ) ) {
541 return $this->registry[
$ext];
708 $rdfUrl =
'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
709 $rdfPurl =
'http://purl.org/rss/1.0/';
710 $xbmMagic1 =
'#define';
711 $xbmMagic2 =
'_width';
712 $xbmMagic3 =
'_bits';
713 $binhexMagic =
'converted with BinHex';
714 $chunkLength = strlen( $chunk );
716 for ( $offset = 0; $offset < $chunkLength; $offset++ ) {
717 $curChar = $chunk[$offset];
718 if ( $curChar ==
"\x0a" ) {
721 } elseif ( $curChar ==
"\x0d" ) {
724 } elseif ( $curChar ==
"\x0c" ) {
727 } elseif ( $curChar ==
"\t" ) {
730 } elseif ( ord( $curChar ) < 32 ) {
733 } elseif ( ord( $curChar ) >= 128 ) {
739 if ( $curChar ==
'<' ) {
741 $remainder = substr( $chunk, $offset + 1 );
742 if ( !strncasecmp( $remainder,
'?XML', 4 ) ) {
743 $nextChar = substr( $chunk, $offset + 5, 1 );
744 if ( $nextChar ==
':' || $nextChar ==
' ' || $nextChar ==
"\t" ) {
745 $found[
'xml'] =
true;
749 if ( !strncasecmp( $remainder,
'SCRIPTLET', 9 ) ) {
750 $found[
'scriptlet'] =
true;
754 foreach ( $htmlTags as $tag ) {
755 if ( !strncasecmp( $remainder, $tag, strlen( $tag ) ) ) {
756 $found[
'html'] =
true;
762 if ( $version <
'ie07' ) {
763 if ( !strncasecmp( $remainder,
'CHANNEL', 7 ) ) {
764 $found[
'cdf'] =
true;
768 if ( !strncasecmp( $remainder,
'RSS', 3 ) ) {
769 $found[
'rss'] =
true;
772 if ( !strncasecmp( $remainder,
'rdf:RDF', 7 ) ) {
773 $found[
'rdf-tag'] =
true;
776 if ( !strncasecmp( $remainder,
'FEED', 4 ) ) {
777 $found[
'atom'] =
true;
787 $remainder = substr( $chunk, $offset );
788 if ( !strncasecmp( $remainder, $rdfUrl, strlen( $rdfUrl ) ) ) {
789 $found[
'rdf-url'] =
true;
790 if ( isset( $found[
'rdf-tag'] )
791 && isset( $found[
'rdf-purl'] )
798 if ( !strncasecmp( $remainder, $rdfPurl, strlen( $rdfPurl ) ) ) {
799 if ( isset( $found[
'rdf-tag'] )
800 && isset( $found[
'rdf-url'] )
808 if ( !strncasecmp( $remainder, $xbmMagic1, strlen( $xbmMagic1 ) ) ) {
809 $found[
'xbm1'] =
true;
812 if ( $curChar ==
'_' ) {
813 if ( isset( $found[
'xbm2'] ) ) {
814 if ( !strncasecmp( $remainder, $xbmMagic3, strlen( $xbmMagic3 ) ) ) {
815 $found[
'xbm'] =
true;
818 } elseif ( isset( $found[
'xbm1'] ) ) {
819 if ( !strncasecmp( $remainder, $xbmMagic2, strlen( $xbmMagic2 ) ) ) {
820 $found[
'xbm2'] =
true;
826 if ( !strncmp( $remainder, $binhexMagic, strlen( $binhexMagic ) ) ) {
827 $found[
'binhex'] =
true;
830 return [
'found' => $found,
'counters' => $counters ];