IEContentAnalyzer.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <?php
  2. /**
  3. * This class simulates Microsoft Internet Explorer's terribly broken and
  4. * insecure MIME type detection algorithm. It can be used to check web uploads
  5. * with an apparently safe type, to see if IE will reinterpret them to produce
  6. * something dangerous.
  7. *
  8. * It is full of bugs and strange design choices should not under any
  9. * circumstances be used to determine a MIME type to present to a user or
  10. * client. (Apple Safari developers, this means you too.)
  11. *
  12. * This class is based on a disassembly of IE 5.0, 6.0 and 7.0. Although I have
  13. * attempted to ensure that this code works in exactly the same way as Internet
  14. * Explorer, it does not share any source code, or creative choices such as
  15. * variable names, thus I (Tim Starling) claim copyright on it.
  16. *
  17. * It may be redistributed without restriction. To aid reuse, this class does
  18. * not depend on any MediaWiki module.
  19. */
  20. class IEContentAnalyzer {
  21. /**
  22. * Relevant data taken from the type table in IE 5
  23. */
  24. protected $baseTypeTable = array(
  25. 'ambiguous' /*1*/ => array(
  26. 'text/plain',
  27. 'application/octet-stream',
  28. 'application/x-netcdf', // [sic]
  29. ),
  30. 'text' /*3*/ => array(
  31. 'text/richtext', 'image/x-bitmap', 'application/postscript', 'application/base64',
  32. 'application/macbinhex40', 'application/x-cdf', 'text/scriptlet'
  33. ),
  34. 'binary' /*4*/ => array(
  35. 'application/pdf', 'audio/x-aiff', 'audio/basic', 'audio/wav', 'image/gif',
  36. 'image/pjpeg', 'image/jpeg', 'image/tiff', 'image/x-png', 'image/png', 'image/bmp',
  37. 'image/x-jg', 'image/x-art', 'image/x-emf', 'image/x-wmf', 'video/avi',
  38. 'video/x-msvideo', 'video/mpeg', 'application/x-compressed',
  39. 'application/x-zip-compressed', 'application/x-gzip-compressed', 'application/java',
  40. 'application/x-msdownload'
  41. ),
  42. 'html' /*5*/ => array( 'text/html' ),
  43. );
  44. /**
  45. * Changes to the type table in later versions of IE
  46. */
  47. protected $addedTypes = array(
  48. 'ie07' => array(
  49. 'text' => array( 'text/xml', 'application/xml' )
  50. ),
  51. );
  52. /**
  53. * An approximation of the "Content Type" values in HKEY_CLASSES_ROOT in a
  54. * typical Windows installation.
  55. *
  56. * Used for extension to MIME type mapping if detection fails.
  57. */
  58. protected $registry = array(
  59. '.323' => 'text/h323',
  60. '.3g2' => 'video/3gpp2',
  61. '.3gp' => 'video/3gpp',
  62. '.3gp2' => 'video/3gpp2',
  63. '.3gpp' => 'video/3gpp',
  64. '.aac' => 'audio/aac',
  65. '.ac3' => 'audio/ac3',
  66. '.accda' => 'application/msaccess',
  67. '.accdb' => 'application/msaccess',
  68. '.accdc' => 'application/msaccess',
  69. '.accde' => 'application/msaccess',
  70. '.accdr' => 'application/msaccess',
  71. '.accdt' => 'application/msaccess',
  72. '.ade' => 'application/msaccess',
  73. '.adp' => 'application/msaccess',
  74. '.adts' => 'audio/aac',
  75. '.ai' => 'application/postscript',
  76. '.aif' => 'audio/aiff',
  77. '.aifc' => 'audio/aiff',
  78. '.aiff' => 'audio/aiff',
  79. '.amc' => 'application/x-mpeg',
  80. '.application' => 'application/x-ms-application',
  81. '.asf' => 'video/x-ms-asf',
  82. '.asx' => 'video/x-ms-asf',
  83. '.au' => 'audio/basic',
  84. '.avi' => 'video/avi',
  85. '.bmp' => 'image/bmp',
  86. '.caf' => 'audio/x-caf',
  87. '.cat' => 'application/vnd.ms-pki.seccat',
  88. '.cbo' => 'application/sha',
  89. '.cdda' => 'audio/aiff',
  90. '.cer' => 'application/x-x509-ca-cert',
  91. '.conf' => 'text/plain',
  92. '.crl' => 'application/pkix-crl',
  93. '.crt' => 'application/x-x509-ca-cert',
  94. '.css' => 'text/css',
  95. '.csv' => 'application/vnd.ms-excel',
  96. '.der' => 'application/x-x509-ca-cert',
  97. '.dib' => 'image/bmp',
  98. '.dif' => 'video/x-dv',
  99. '.dll' => 'application/x-msdownload',
  100. '.doc' => 'application/msword',
  101. '.docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  102. '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  103. '.dot' => 'application/msword',
  104. '.dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  105. '.dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  106. '.dv' => 'video/x-dv',
  107. '.dwfx' => 'model/vnd.dwfx+xps',
  108. '.edn' => 'application/vnd.adobe.edn',
  109. '.eml' => 'message/rfc822',
  110. '.eps' => 'application/postscript',
  111. '.etd' => 'application/x-ebx',
  112. '.exe' => 'application/x-msdownload',
  113. '.fdf' => 'application/vnd.fdf',
  114. '.fif' => 'application/fractals',
  115. '.gif' => 'image/gif',
  116. '.gsm' => 'audio/x-gsm',
  117. '.hqx' => 'application/mac-binhex40',
  118. '.hta' => 'application/hta',
  119. '.htc' => 'text/x-component',
  120. '.htm' => 'text/html',
  121. '.html' => 'text/html',
  122. '.htt' => 'text/webviewhtml',
  123. '.hxa' => 'application/xml',
  124. '.hxc' => 'application/xml',
  125. '.hxd' => 'application/octet-stream',
  126. '.hxe' => 'application/xml',
  127. '.hxf' => 'application/xml',
  128. '.hxh' => 'application/octet-stream',
  129. '.hxi' => 'application/octet-stream',
  130. '.hxk' => 'application/xml',
  131. '.hxq' => 'application/octet-stream',
  132. '.hxr' => 'application/octet-stream',
  133. '.hxs' => 'application/octet-stream',
  134. '.hxt' => 'application/xml',
  135. '.hxv' => 'application/xml',
  136. '.hxw' => 'application/octet-stream',
  137. '.ico' => 'image/x-icon',
  138. '.iii' => 'application/x-iphone',
  139. '.ins' => 'application/x-internet-signup',
  140. '.iqy' => 'text/x-ms-iqy',
  141. '.isp' => 'application/x-internet-signup',
  142. '.jfif' => 'image/jpeg',
  143. '.jnlp' => 'application/x-java-jnlp-file',
  144. '.jpe' => 'image/jpeg',
  145. '.jpeg' => 'image/jpeg',
  146. '.jpg' => 'image/jpeg',
  147. '.jtx' => 'application/x-jtx+xps',
  148. '.latex' => 'application/x-latex',
  149. '.log' => 'text/plain',
  150. '.m1v' => 'video/mpeg',
  151. '.m2v' => 'video/mpeg',
  152. '.m3u' => 'audio/x-mpegurl',
  153. '.mac' => 'image/x-macpaint',
  154. '.man' => 'application/x-troff-man',
  155. '.mda' => 'application/msaccess',
  156. '.mdb' => 'application/msaccess',
  157. '.mde' => 'application/msaccess',
  158. '.mfp' => 'application/x-shockwave-flash',
  159. '.mht' => 'message/rfc822',
  160. '.mhtml' => 'message/rfc822',
  161. '.mid' => 'audio/mid',
  162. '.midi' => 'audio/mid',
  163. '.mod' => 'video/mpeg',
  164. '.mov' => 'video/quicktime',
  165. '.mp2' => 'video/mpeg',
  166. '.mp2v' => 'video/mpeg',
  167. '.mp3' => 'audio/mpeg',
  168. '.mp4' => 'video/mp4',
  169. '.mpa' => 'video/mpeg',
  170. '.mpe' => 'video/mpeg',
  171. '.mpeg' => 'video/mpeg',
  172. '.mpf' => 'application/vnd.ms-mediapackage',
  173. '.mpg' => 'video/mpeg',
  174. '.mpv2' => 'video/mpeg',
  175. '.mqv' => 'video/quicktime',
  176. '.NMW' => 'application/nmwb',
  177. '.nws' => 'message/rfc822',
  178. '.odc' => 'text/x-ms-odc',
  179. '.ols' => 'application/vnd.ms-publisher',
  180. '.p10' => 'application/pkcs10',
  181. '.p12' => 'application/x-pkcs12',
  182. '.p7b' => 'application/x-pkcs7-certificates',
  183. '.p7c' => 'application/pkcs7-mime',
  184. '.p7m' => 'application/pkcs7-mime',
  185. '.p7r' => 'application/x-pkcs7-certreqresp',
  186. '.p7s' => 'application/pkcs7-signature',
  187. '.pct' => 'image/pict',
  188. '.pdf' => 'application/pdf',
  189. '.pdx' => 'application/vnd.adobe.pdx',
  190. '.pfx' => 'application/x-pkcs12',
  191. '.pic' => 'image/pict',
  192. '.pict' => 'image/pict',
  193. '.pinstall' => 'application/x-picasa-detect',
  194. '.pko' => 'application/vnd.ms-pki.pko',
  195. '.png' => 'image/png',
  196. '.pnt' => 'image/x-macpaint',
  197. '.pntg' => 'image/x-macpaint',
  198. '.pot' => 'application/vnd.ms-powerpoint',
  199. '.potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  200. '.potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  201. '.ppa' => 'application/vnd.ms-powerpoint',
  202. '.ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  203. '.pps' => 'application/vnd.ms-powerpoint',
  204. '.ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  205. '.ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  206. '.ppt' => 'application/vnd.ms-powerpoint',
  207. '.pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  208. '.pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  209. '.prf' => 'application/pics-rules',
  210. '.ps' => 'application/postscript',
  211. '.pub' => 'application/vnd.ms-publisher',
  212. '.pwz' => 'application/vnd.ms-powerpoint',
  213. '.py' => 'text/plain',
  214. '.pyw' => 'text/plain',
  215. '.qht' => 'text/x-html-insertion',
  216. '.qhtm' => 'text/x-html-insertion',
  217. '.qt' => 'video/quicktime',
  218. '.qti' => 'image/x-quicktime',
  219. '.qtif' => 'image/x-quicktime',
  220. '.qtl' => 'application/x-quicktimeplayer',
  221. '.rat' => 'application/rat-file',
  222. '.rmf' => 'application/vnd.adobe.rmf',
  223. '.rmi' => 'audio/mid',
  224. '.rqy' => 'text/x-ms-rqy',
  225. '.rtf' => 'application/msword',
  226. '.sct' => 'text/scriptlet',
  227. '.sd2' => 'audio/x-sd2',
  228. '.sdp' => 'application/sdp',
  229. '.shtml' => 'text/html',
  230. '.sit' => 'application/x-stuffit',
  231. '.sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  232. '.sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  233. '.slk' => 'application/vnd.ms-excel',
  234. '.snd' => 'audio/basic',
  235. '.so' => 'application/x-apachemodule',
  236. '.sol' => 'text/plain',
  237. '.sor' => 'text/plain',
  238. '.spc' => 'application/x-pkcs7-certificates',
  239. '.spl' => 'application/futuresplash',
  240. '.sst' => 'application/vnd.ms-pki.certstore',
  241. '.stl' => 'application/vnd.ms-pki.stl',
  242. '.swf' => 'application/x-shockwave-flash',
  243. '.thmx' => 'application/vnd.ms-officetheme',
  244. '.tif' => 'image/tiff',
  245. '.tiff' => 'image/tiff',
  246. '.txt' => 'text/plain',
  247. '.uls' => 'text/iuls',
  248. '.vcf' => 'text/x-vcard',
  249. '.vdx' => 'application/vnd.ms-visio.viewer',
  250. '.vsd' => 'application/vnd.ms-visio.viewer',
  251. '.vss' => 'application/vnd.ms-visio.viewer',
  252. '.vst' => 'application/vnd.ms-visio.viewer',
  253. '.vsx' => 'application/vnd.ms-visio.viewer',
  254. '.vtx' => 'application/vnd.ms-visio.viewer',
  255. '.wav' => 'audio/wav',
  256. '.wax' => 'audio/x-ms-wax',
  257. '.wbk' => 'application/msword',
  258. '.wdp' => 'image/vnd.ms-photo',
  259. '.wiz' => 'application/msword',
  260. '.wm' => 'video/x-ms-wm',
  261. '.wma' => 'audio/x-ms-wma',
  262. '.wmd' => 'application/x-ms-wmd',
  263. '.wmv' => 'video/x-ms-wmv',
  264. '.wmx' => 'video/x-ms-wmx',
  265. '.wmz' => 'application/x-ms-wmz',
  266. '.wpl' => 'application/vnd.ms-wpl',
  267. '.wsc' => 'text/scriptlet',
  268. '.wvx' => 'video/x-ms-wvx',
  269. '.xaml' => 'application/xaml+xml',
  270. '.xbap' => 'application/x-ms-xbap',
  271. '.xdp' => 'application/vnd.adobe.xdp+xml',
  272. '.xfdf' => 'application/vnd.adobe.xfdf',
  273. '.xht' => 'application/xhtml+xml',
  274. '.xhtml' => 'application/xhtml+xml',
  275. '.xla' => 'application/vnd.ms-excel',
  276. '.xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  277. '.xlk' => 'application/vnd.ms-excel',
  278. '.xll' => 'application/vnd.ms-excel',
  279. '.xlm' => 'application/vnd.ms-excel',
  280. '.xls' => 'application/vnd.ms-excel',
  281. '.xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  282. '.xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  283. '.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  284. '.xlt' => 'application/vnd.ms-excel',
  285. '.xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  286. '.xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  287. '.xlw' => 'application/vnd.ms-excel',
  288. '.xml' => 'text/xml',
  289. '.xps' => 'application/vnd.ms-xpsdocument',
  290. '.xsl' => 'text/xml',
  291. );
  292. /**
  293. * IE versions which have been analysed to bring you this class, and for
  294. * which some substantive difference exists. These will appear as keys
  295. * in the return value of getRealMimesFromData(). The names are chosen to sort correctly.
  296. */
  297. protected $versions = array( 'ie05', 'ie06', 'ie07', 'ie07.strict', 'ie07.nohtml' );
  298. /**
  299. * Type table with versions expanded
  300. */
  301. protected $typeTable = array();
  302. /** constructor */
  303. function __construct() {
  304. // Construct versioned type arrays from the base type array plus additions
  305. $types = $this->baseTypeTable;
  306. foreach ( $this->versions as $version ) {
  307. if ( isset( $this->addedTypes[$version] ) ) {
  308. foreach ( $this->addedTypes[$version] as $format => $addedTypes ) {
  309. $types[$format] = array_merge( $types[$format], $addedTypes );
  310. }
  311. }
  312. $this->typeTable[$version] = $types;
  313. }
  314. }
  315. /**
  316. * Get the MIME types from getMimesFromData(), but convert the result from IE's
  317. * idiosyncratic private types into something other apps will understand.
  318. *
  319. * @param string $fileName The file name (unused at present)
  320. * @param string $chunk The first 256 bytes of the file
  321. * @param string $proposed The MIME type proposed by the server
  322. *
  323. * @return array Map of IE version to detected mime type
  324. */
  325. public function getRealMimesFromData( $fileName, $chunk, $proposed ) {
  326. $types = $this->getMimesFromData( $fileName, $chunk, $proposed );
  327. $types = array_map( array( $this, 'translateMimeType' ), $types );
  328. return $types;
  329. }
  330. /**
  331. * Translate a MIME type from IE's idiosyncratic private types into
  332. * more commonly understood type strings
  333. */
  334. public function translateMimeType( $type ) {
  335. static $table = array(
  336. 'image/pjpeg' => 'image/jpeg',
  337. 'image/x-png' => 'image/png',
  338. 'image/x-wmf' => 'application/x-msmetafile',
  339. 'image/bmp' => 'image/x-bmp',
  340. 'application/x-zip-compressed' => 'application/zip',
  341. 'application/x-compressed' => 'application/x-compress',
  342. 'application/x-gzip-compressed' => 'application/x-gzip',
  343. 'audio/mid' => 'audio/midi',
  344. );
  345. if ( isset( $table[$type] ) ) {
  346. $type = $table[$type];
  347. }
  348. return $type;
  349. }
  350. /**
  351. * Get the untranslated MIME types for all known versions
  352. *
  353. * @param string $fileName The file name (unused at present)
  354. * @param string $chunk The first 256 bytes of the file
  355. * @param string $proposed The MIME type proposed by the server
  356. *
  357. * @return array Map of IE version to detected mime type
  358. */
  359. public function getMimesFromData( $fileName, $chunk, $proposed ) {
  360. $types = array();
  361. foreach ( $this->versions as $version ) {
  362. $types[$version] = $this->getMimeTypeForVersion( $version, $fileName, $chunk, $proposed );
  363. }
  364. return $types;
  365. }
  366. /**
  367. * Get the MIME type for a given named version
  368. */
  369. protected function getMimeTypeForVersion( $version, $fileName, $chunk, $proposed ) {
  370. // Strip text after a semicolon
  371. $semiPos = strpos( $proposed, ';' );
  372. if ( $semiPos !== false ) {
  373. $proposed = substr( $proposed, 0, $semiPos );
  374. }
  375. $proposedFormat = $this->getDataFormat( $version, $proposed );
  376. if ( $proposedFormat == 'unknown'
  377. && $proposed != 'multipart/mixed'
  378. && $proposed != 'multipart/x-mixed-replace' )
  379. {
  380. return $proposed;
  381. }
  382. if ( strval( $chunk ) === '' ) {
  383. return $proposed;
  384. }
  385. // Truncate chunk at 255 bytes
  386. $chunk = substr( $chunk, 0, 255 );
  387. // IE does the Check*Headers() calls last, and instead does the following image
  388. // type checks by directly looking for the magic numbers. What I do here should
  389. // have the same effect since the magic number checks are identical in both cases.
  390. $result = $this->sampleData( $version, $chunk );
  391. $sampleFound = $result['found'];
  392. $counters = $result['counters'];
  393. $binaryType = $this->checkBinaryHeaders( $version, $chunk );
  394. $textType = $this->checkTextHeaders( $version, $chunk );
  395. if ( $proposed == 'text/html' && isset( $sampleFound['html'] ) ) {
  396. return 'text/html';
  397. }
  398. if ( $proposed == 'image/gif' && $binaryType == 'image/gif' ) {
  399. return 'image/gif';
  400. }
  401. if ( ( $proposed == 'image/pjpeg' || $proposed == 'image/jpeg' )
  402. && $binaryType == 'image/pjpeg' )
  403. {
  404. return $proposed;
  405. }
  406. // PNG check added in IE 7
  407. if ( $version >= 'ie07'
  408. && ( $proposed == 'image/x-png' || $proposed == 'image/png' )
  409. && $binaryType == 'image/x-png' )
  410. {
  411. return $proposed;
  412. }
  413. // CDF was removed in IE 7 so it won't be in $sampleFound for later versions
  414. if ( isset( $sampleFound['cdf'] ) ) {
  415. return 'application/x-cdf';
  416. }
  417. // RSS and Atom were added in IE 7 so they won't be in $sampleFound for
  418. // previous versions
  419. if ( isset( $sampleFound['rss'] ) ) {
  420. return 'application/rss+xml';
  421. }
  422. if ( isset( $sampleFound['rdf-tag'] )
  423. && isset( $sampleFound['rdf-url'] )
  424. && isset( $sampleFound['rdf-purl'] ) )
  425. {
  426. return 'application/rss+xml';
  427. }
  428. if ( isset( $sampleFound['atom'] ) ) {
  429. return 'application/atom+xml';
  430. }
  431. if ( isset( $sampleFound['xml'] ) ) {
  432. // TODO: I'm not sure under what circumstances this flag is enabled
  433. if ( strpos( $version, 'strict' ) !== false ) {
  434. if ( $proposed == 'text/html' || $proposed == 'text/xml' ) {
  435. return 'text/xml';
  436. }
  437. } else {
  438. return 'text/xml';
  439. }
  440. }
  441. if ( isset( $sampleFound['html'] ) ) {
  442. // TODO: I'm not sure under what circumstances this flag is enabled
  443. if ( strpos( $version, 'nohtml' ) !== false ) {
  444. if ( $proposed == 'text/plain' ) {
  445. return 'text/html';
  446. }
  447. } else {
  448. return 'text/html';
  449. }
  450. }
  451. if ( isset( $sampleFound['xbm'] ) ) {
  452. return 'image/x-bitmap';
  453. }
  454. if ( isset( $sampleFound['binhex'] ) ) {
  455. return 'application/macbinhex40';
  456. }
  457. if ( isset( $sampleFound['scriptlet'] ) ) {
  458. if ( strpos( $version, 'strict' ) !== false ) {
  459. if ( $proposed == 'text/plain' || $proposed == 'text/scriptlet' ) {
  460. return 'text/scriptlet';
  461. }
  462. } else {
  463. return 'text/scriptlet';
  464. }
  465. }
  466. // Freaky heuristics to determine if the data is text or binary
  467. // The heuristic is of course broken for non-ASCII text
  468. if ( $counters['ctrl'] != 0 && ( $counters['ff'] + $counters['low'] )
  469. < ( $counters['ctrl'] + $counters['high'] ) * 16 )
  470. {
  471. $kindOfBinary = true;
  472. $type = $binaryType ? $binaryType : $textType;
  473. if ( $type === false ) {
  474. $type = 'application/octet-stream';
  475. }
  476. } else {
  477. $kindOfBinary = false;
  478. $type = $textType ? $textType : $binaryType;
  479. if ( $type === false ) {
  480. $type = 'text/plain';
  481. }
  482. }
  483. // Check if the output format is ambiguous
  484. // This generally means that detection failed, real types aren't ambiguous
  485. $detectedFormat = $this->getDataFormat( $version, $type );
  486. if ( $detectedFormat != 'ambiguous' ) {
  487. return $type;
  488. }
  489. if ( $proposedFormat != 'ambiguous' ) {
  490. // FormatAgreesWithData()
  491. if ( $proposedFormat == 'text' && !$kindOfBinary ) {
  492. return $proposed;
  493. }
  494. if ( $proposedFormat == 'binary' && $kindOfBinary ) {
  495. return $proposed;
  496. }
  497. if ( $proposedFormat == 'html' ) {
  498. return $proposed;
  499. }
  500. }
  501. // Find a MIME type by searching the registry for the file extension.
  502. $dotPos = strrpos( $fileName, '.' );
  503. if ( $dotPos === false ) {
  504. return $type;
  505. }
  506. $ext = substr( $fileName, $dotPos );
  507. if ( isset( $this->registry[$ext] ) ) {
  508. return $this->registry[$ext];
  509. }
  510. // TODO: If the extension has an application registered to it, IE will return
  511. // application/octet-stream. We'll skip that, so we could erroneously
  512. // return text/plain or application/x-netcdf where application/octet-stream
  513. // would be correct.
  514. return $type;
  515. }
  516. /**
  517. * Check for text headers at the start of the chunk
  518. * Confirmed same in 5 and 7.
  519. */
  520. private function checkTextHeaders( $version, $chunk ) {
  521. $chunk2 = substr( $chunk, 0, 2 );
  522. $chunk4 = substr( $chunk, 0, 4 );
  523. $chunk5 = substr( $chunk, 0, 5 );
  524. if ( $chunk4 == '%PDF' ) {
  525. return 'application/pdf';
  526. }
  527. if ( $chunk2 == '%!' ) {
  528. return 'application/postscript';
  529. }
  530. if ( $chunk5 == '{\\rtf' ) {
  531. return 'text/richtext';
  532. }
  533. if ( $chunk5 == 'begin' ) {
  534. return 'application/base64';
  535. }
  536. return false;
  537. }
  538. /**
  539. * Check for binary headers at the start of the chunk
  540. * Confirmed same in 5 and 7.
  541. */
  542. private function checkBinaryHeaders( $version, $chunk ) {
  543. $chunk2 = substr( $chunk, 0, 2 );
  544. $chunk3 = substr( $chunk, 0, 3 );
  545. $chunk4 = substr( $chunk, 0, 4 );
  546. $chunk5 = substr( $chunk, 0, 5 );
  547. $chunk5uc = strtoupper( $chunk5 );
  548. $chunk8 = substr( $chunk, 0, 8 );
  549. if ( $chunk5uc == 'GIF87' || $chunk5uc == 'GIF89' ) {
  550. return 'image/gif';
  551. }
  552. if ( $chunk2 == "\xff\xd8" ) {
  553. return 'image/pjpeg'; // actually plain JPEG but this is what IE returns
  554. }
  555. if ( $chunk2 == 'BM'
  556. && substr( $chunk, 6, 2 ) == "\000\000"
  557. && substr( $chunk, 8, 2 ) == "\000\000" )
  558. {
  559. return 'image/bmp'; // another non-standard MIME
  560. }
  561. if ( $chunk4 == 'RIFF'
  562. && substr( $chunk, 8, 4 ) == 'WAVE' )
  563. {
  564. return 'audio/wav';
  565. }
  566. // These were integer literals in IE
  567. // Perhaps the author was not sure what the target endianness was
  568. if ( $chunk4 == ".sd\000"
  569. || $chunk4 == ".snd"
  570. || $chunk4 == "\000ds."
  571. || $chunk4 == "dns." )
  572. {
  573. return 'audio/basic';
  574. }
  575. if ( $chunk3 == "MM\000" ) {
  576. return 'image/tiff';
  577. }
  578. if ( $chunk2 == 'MZ' ) {
  579. return 'application/x-msdownload';
  580. }
  581. if ( $chunk8 == "\x89PNG\x0d\x0a\x1a\x0a" ) {
  582. return 'image/x-png'; // [sic]
  583. }
  584. if ( strlen( $chunk ) >= 5 ) {
  585. $byte2 = ord( $chunk[2] );
  586. $byte4 = ord( $chunk[4] );
  587. if ( $byte2 >= 3 && $byte2 <= 31 && $byte4 == 0 && $chunk2 == 'JG' ) {
  588. return 'image/x-jg';
  589. }
  590. }
  591. // More endian confusion?
  592. if ( $chunk4 == 'MROF' ) {
  593. return 'audio/x-aiff';
  594. }
  595. $chunk4_8 = substr( $chunk, 8, 4 );
  596. if ( $chunk4 == 'FORM' && ( $chunk4_8 == 'AIFF' || $chunk4_8 == 'AIFC' ) ) {
  597. return 'audio/x-aiff';
  598. }
  599. if ( $chunk4 == 'RIFF' && $chunk4_8 == 'AVI ' ) {
  600. return 'video/avi';
  601. }
  602. if ( $chunk4 == "\x00\x00\x01\xb3" || $chunk4 == "\x00\x00\x01\xba" ) {
  603. return 'video/mpeg';
  604. }
  605. if ( $chunk4 == "\001\000\000\000"
  606. && substr( $chunk, 40, 4 ) == ' EMF' )
  607. {
  608. return 'image/x-emf';
  609. }
  610. if ( $chunk4 == "\xd7\xcd\xc6\x9a" ) {
  611. return 'image/x-wmf';
  612. }
  613. if ( $chunk4 == "\xca\xfe\xba\xbe" ) {
  614. return 'application/java';
  615. }
  616. if ( $chunk2 == 'PK' ) {
  617. return 'application/x-zip-compressed';
  618. }
  619. if ( $chunk2 == "\x1f\x9d" ) {
  620. return 'application/x-compressed';
  621. }
  622. if ( $chunk2 == "\x1f\x8b" ) {
  623. return 'application/x-gzip-compressed';
  624. }
  625. // Skip redundant check for ZIP
  626. if ( $chunk5 == "MThd\000" ) {
  627. return 'audio/mid';
  628. }
  629. if ( $chunk4 == '%PDF' ) {
  630. return 'application/pdf';
  631. }
  632. return false;
  633. }
  634. /**
  635. * Do heuristic checks on the bulk of the data sample.
  636. * Search for HTML tags.
  637. */
  638. protected function sampleData( $version, $chunk ) {
  639. $found = array();
  640. $counters = array(
  641. 'ctrl' => 0,
  642. 'high' => 0,
  643. 'low' => 0,
  644. 'lf' => 0,
  645. 'cr' => 0,
  646. 'ff' => 0
  647. );
  648. $htmlTags = array(
  649. 'html',
  650. 'head',
  651. 'title',
  652. 'body',
  653. 'script',
  654. 'a href',
  655. 'pre',
  656. 'img',
  657. 'plaintext',
  658. 'table'
  659. );
  660. $rdfUrl = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  661. $rdfPurl = 'http://purl.org/rss/1.0/';
  662. $xbmMagic1 = '#define';
  663. $xbmMagic2 = '_width';
  664. $xbmMagic3 = '_bits';
  665. $binhexMagic = 'converted with BinHex';
  666. for ( $offset = 0; $offset < strlen( $chunk ); $offset++ ) {
  667. $curChar = $chunk[$offset];
  668. if ( $curChar == "\x0a" ) {
  669. $counters['lf']++;
  670. continue;
  671. } elseif ( $curChar == "\x0d" ) {
  672. $counters['cr']++;
  673. continue;
  674. } elseif ( $curChar == "\x0c" ) {
  675. $counters['ff']++;
  676. continue;
  677. } elseif ( $curChar == "\t" ) {
  678. $counters['low']++;
  679. continue;
  680. } elseif ( ord( $curChar ) < 32 ) {
  681. $counters['ctrl']++;
  682. continue;
  683. } elseif ( ord( $curChar ) >= 128 ) {
  684. $counters['high']++;
  685. continue;
  686. }
  687. $counters['low']++;
  688. if ( $curChar == '<' ) {
  689. // XML
  690. $remainder = substr( $chunk, $offset + 1 );
  691. if ( !strncasecmp( $remainder, '?XML', 4 ) ) {
  692. $nextChar = substr( $chunk, $offset + 5, 1 );
  693. if ( $nextChar == ':' || $nextChar == ' ' || $nextChar == "\t" ) {
  694. $found['xml'] = true;
  695. }
  696. }
  697. // Scriptlet (JSP)
  698. if ( !strncasecmp( $remainder, 'SCRIPTLET', 9 ) ) {
  699. $found['scriptlet'] = true;
  700. break;
  701. }
  702. // HTML
  703. foreach ( $htmlTags as $tag ) {
  704. if ( !strncasecmp( $remainder, $tag, strlen( $tag ) ) ) {
  705. $found['html'] = true;
  706. }
  707. }
  708. // Skip broken check for additional tags (HR etc.)
  709. // CHANNEL replaced by RSS, RDF and FEED in IE 7
  710. if ( $version < 'ie07' ) {
  711. if ( !strncasecmp( $remainder, 'CHANNEL', 7 ) ) {
  712. $found['cdf'] = true;
  713. }
  714. } else {
  715. // RSS
  716. if ( !strncasecmp( $remainder, 'RSS', 3 ) ) {
  717. $found['rss'] = true;
  718. break; // return from SampleData
  719. }
  720. if ( !strncasecmp( $remainder, 'rdf:RDF', 7 ) ) {
  721. $found['rdf-tag'] = true;
  722. // no break
  723. }
  724. if ( !strncasecmp( $remainder, 'FEED', 4 ) ) {
  725. $found['atom'] = true;
  726. break;
  727. }
  728. }
  729. continue;
  730. }
  731. // Skip broken check for -->
  732. // RSS URL checks
  733. // For some reason both URLs must appear before it is recognised
  734. $remainder = substr( $chunk, $offset );
  735. if ( !strncasecmp( $remainder, $rdfUrl, strlen( $rdfUrl ) ) ) {
  736. $found['rdf-url'] = true;
  737. if ( isset( $found['rdf-tag'] )
  738. && isset( $found['rdf-purl'] ) ) // [sic]
  739. {
  740. break;
  741. }
  742. continue;
  743. }
  744. if ( !strncasecmp( $remainder, $rdfPurl, strlen( $rdfPurl ) ) ) {
  745. if ( isset( $found['rdf-tag'] )
  746. && isset( $found['rdf-url'] ) ) // [sic]
  747. {
  748. break;
  749. }
  750. continue;
  751. }
  752. // XBM checks
  753. if ( !strncasecmp( $remainder, $xbmMagic1, strlen( $xbmMagic1 ) ) ) {
  754. $found['xbm1'] = true;
  755. continue;
  756. }
  757. if ( $curChar == '_' ) {
  758. if ( isset( $found['xbm2'] ) ) {
  759. if ( !strncasecmp( $remainder, $xbmMagic3, strlen( $xbmMagic3 ) ) ) {
  760. $found['xbm'] = true;
  761. break;
  762. }
  763. } elseif ( isset( $found['xbm1'] ) ) {
  764. if ( !strncasecmp( $remainder, $xbmMagic2, strlen( $xbmMagic2 ) ) ) {
  765. $found['xbm2'] = true;
  766. }
  767. }
  768. }
  769. // BinHex
  770. if ( !strncmp( $remainder, $binhexMagic, strlen( $binhexMagic ) ) ) {
  771. $found['binhex'] = true;
  772. }
  773. }
  774. return array( 'found' => $found, 'counters' => $counters );
  775. }
  776. protected function getDataFormat( $version, $type ) {
  777. $types = $this->typeTable[$version];
  778. if ( $type == '(null)' || strval( $type ) === '' ) {
  779. return 'ambiguous';
  780. }
  781. foreach ( $types as $format => $list ) {
  782. if ( in_array( $type, $list ) ) {
  783. return $format;
  784. }
  785. }
  786. return 'unknown';
  787. }
  788. }