SimplePie_Content_Type_Sniffer

Content-type sniffing.

Defined (1)

The class is defined in the following location(s).

/wp-includes/SimplePie/Content/Type/Sniffer.php  
  1. class SimplePie_Content_Type_Sniffer 
  2. /** 
  3. * File object 
  4. * @var SimplePie_File 
  5. */ 
  6. var $file; 
  7.  
  8. /** 
  9. * Create an instance of the class with the input file 
  10. * @param SimplePie_Content_Type_Sniffer $file Input file 
  11. */ 
  12. public function __construct($file) 
  13. $this->file = $file; 
  14.  
  15. /** 
  16. * Get the Content-Type of the specified file 
  17. * @return string Actual Content-Type 
  18. */ 
  19. public function get_type() 
  20. if (isset($this->file->headers['content-type'])) 
  21. if (!isset($this->file->headers['content-encoding']) 
  22. && ($this->file->headers['content-type'] === 'text/plain' 
  23. || $this->file->headers['content-type'] === 'text/plain; charset=ISO-8859-1' 
  24. || $this->file->headers['content-type'] === 'text/plain; charset=iso-8859-1' 
  25. || $this->file->headers['content-type'] === 'text/plain; charset=UTF-8')) 
  26. return $this->text_or_binary(); 
  27.  
  28. if (($pos = strpos($this->file->headers['content-type'], ';')) !== false) 
  29. $official = substr($this->file->headers['content-type'], 0, $pos); 
  30. else 
  31. $official = $this->file->headers['content-type']; 
  32. $official = trim(strtolower($official)); 
  33.  
  34. if ($official === 'unknown/unknown' 
  35. || $official === 'application/unknown') 
  36. return $this->unknown(); 
  37. elseif (substr($official, -4) === '+xml' 
  38. || $official === 'text/xml' 
  39. || $official === 'application/xml') 
  40. return $official; 
  41. elseif (substr($official, 0, 6) === 'image/') 
  42. if ($return = $this->image()) 
  43. return $return; 
  44. else 
  45. return $official; 
  46. elseif ($official === 'text/html') 
  47. return $this->feed_or_html(); 
  48. else 
  49. return $official; 
  50. else 
  51. return $this->unknown(); 
  52.  
  53. /** 
  54. * Sniff text or binary 
  55. * @return string Actual Content-Type 
  56. */ 
  57. public function text_or_binary() 
  58. if (substr($this->file->body, 0, 2) === "\xFE\xFF" 
  59. || substr($this->file->body, 0, 2) === "\xFF\xFE" 
  60. || substr($this->file->body, 0, 4) === "\x00\x00\xFE\xFF" 
  61. || substr($this->file->body, 0, 3) === "\xEF\xBB\xBF") 
  62. return 'text/plain'; 
  63. elseif (preg_match('/[\x00-\x08\x0E-\x1A\x1C-\x1F]/', $this->file->body)) 
  64. return 'application/octect-stream'; 
  65. else 
  66. return 'text/plain'; 
  67.  
  68. /** 
  69. * Sniff unknown 
  70. * @return string Actual Content-Type 
  71. */ 
  72. public function unknown() 
  73. $ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20"); 
  74. if (strtolower(substr($this->file->body, $ws, 14)) === '<!doctype html' 
  75. || strtolower(substr($this->file->body, $ws, 5)) === '<html' 
  76. || strtolower(substr($this->file->body, $ws, 7)) === '<script') 
  77. return 'text/html'; 
  78. elseif (substr($this->file->body, 0, 5) === '%PDF-') 
  79. return 'application/pdf'; 
  80. elseif (substr($this->file->body, 0, 11) === '%!PS-Adobe-') 
  81. return 'application/postscript'; 
  82. elseif (substr($this->file->body, 0, 6) === 'GIF87a' 
  83. || substr($this->file->body, 0, 6) === 'GIF89a') 
  84. return 'image/gif'; 
  85. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") 
  86. return 'image/png'; 
  87. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF") 
  88. return 'image/jpeg'; 
  89. elseif (substr($this->file->body, 0, 2) === "\x42\x4D") 
  90. return 'image/bmp'; 
  91. elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00") 
  92. return 'image/vnd.microsoft.icon'; 
  93. else 
  94. return $this->text_or_binary(); 
  95.  
  96. /** 
  97. * Sniff images 
  98. * @return string Actual Content-Type 
  99. */ 
  100. public function image() 
  101. if (substr($this->file->body, 0, 6) === 'GIF87a' 
  102. || substr($this->file->body, 0, 6) === 'GIF89a') 
  103. return 'image/gif'; 
  104. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") 
  105. return 'image/png'; 
  106. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF") 
  107. return 'image/jpeg'; 
  108. elseif (substr($this->file->body, 0, 2) === "\x42\x4D") 
  109. return 'image/bmp'; 
  110. elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00") 
  111. return 'image/vnd.microsoft.icon'; 
  112. else 
  113. return false; 
  114.  
  115. /** 
  116. * Sniff HTML 
  117. * @return string Actual Content-Type 
  118. */ 
  119. public function feed_or_html() 
  120. $len = strlen($this->file->body); 
  121. $pos = strspn($this->file->body, "\x09\x0A\x0D\x20"); 
  122.  
  123. while ($pos < $len) 
  124. switch ($this->file->body[$pos]) 
  125. case "\x09": 
  126. case "\x0A": 
  127. case "\x0D": 
  128. case "\x20": 
  129. $pos += strspn($this->file->body, "\x09\x0A\x0D\x20", $pos); 
  130. continue 2; 
  131.  
  132. case '<': 
  133. $pos++; 
  134. break; 
  135.  
  136. default: 
  137. return 'text/html'; 
  138.  
  139. if (substr($this->file->body, $pos, 3) === '!--') 
  140. $pos += 3; 
  141. if ($pos < $len && ($pos = strpos($this->file->body, '-->', $pos)) !== false) 
  142. $pos += 3; 
  143. else 
  144. return 'text/html'; 
  145. elseif (substr($this->file->body, $pos, 1) === '!') 
  146. if ($pos < $len && ($pos = strpos($this->file->body, '>', $pos)) !== false) 
  147. $pos++; 
  148. else 
  149. return 'text/html'; 
  150. elseif (substr($this->file->body, $pos, 1) === '?') 
  151. if ($pos < $len && ($pos = strpos($this->file->body, '?>', $pos)) !== false) 
  152. $pos += 2; 
  153. else 
  154. return 'text/html'; 
  155. elseif (substr($this->file->body, $pos, 3) === 'rss' 
  156. || substr($this->file->body, $pos, 7) === 'rdf:RDF') 
  157. return 'application/rss+xml'; 
  158. elseif (substr($this->file->body, $pos, 4) === 'feed') 
  159. return 'application/atom+xml'; 
  160. else 
  161. return 'text/html'; 
  162.  
  163. return 'text/html';