Mimes.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php namespace Config;
  2. /*
  3. | -------------------------------------------------------------------
  4. | MIME TYPES
  5. | -------------------------------------------------------------------
  6. | This file contains an array of mime types. It is used by the
  7. | Upload class to help identify allowed file types.
  8. |
  9. | When more than one variation for an extension exist (like jpg, jpeg, etc)
  10. | the most common one should be first in the array to aid the guess*
  11. | methods. The same applies when more than one mime-type exists for a
  12. | single extension.
  13. |
  14. */
  15. class Mimes
  16. {
  17. /**
  18. * Map of extensions to mime types.
  19. *
  20. * @var array
  21. */
  22. public static $mimes = [
  23. 'hqx' => [
  24. 'application/mac-binhex40',
  25. 'application/mac-binhex',
  26. 'application/x-binhex40',
  27. 'application/x-mac-binhex40',
  28. ],
  29. 'cpt' => 'application/mac-compactpro',
  30. 'csv' => [
  31. 'text/csv',
  32. 'text/x-comma-separated-values',
  33. 'text/comma-separated-values',
  34. 'application/octet-stream',
  35. 'application/vnd.ms-excel',
  36. 'application/x-csv',
  37. 'text/x-csv',
  38. 'application/csv',
  39. 'application/excel',
  40. 'application/vnd.msexcel',
  41. 'text/plain',
  42. ],
  43. 'bin' => [
  44. 'application/macbinary',
  45. 'application/mac-binary',
  46. 'application/octet-stream',
  47. 'application/x-binary',
  48. 'application/x-macbinary',
  49. ],
  50. 'dms' => 'application/octet-stream',
  51. 'lha' => 'application/octet-stream',
  52. 'lzh' => 'application/octet-stream',
  53. 'exe' => [
  54. 'application/octet-stream',
  55. 'application/x-msdownload',
  56. ],
  57. 'class' => 'application/octet-stream',
  58. 'psd' => [
  59. 'application/x-photoshop',
  60. 'image/vnd.adobe.photoshop',
  61. ],
  62. 'so' => 'application/octet-stream',
  63. 'sea' => 'application/octet-stream',
  64. 'dll' => 'application/octet-stream',
  65. 'oda' => 'application/oda',
  66. 'pdf' => [
  67. 'application/pdf',
  68. 'application/force-download',
  69. 'application/x-download',
  70. 'binary/octet-stream',
  71. ],
  72. 'ai' => [
  73. 'application/pdf',
  74. 'application/postscript',
  75. ],
  76. 'eps' => 'application/postscript',
  77. 'ps' => 'application/postscript',
  78. 'smi' => 'application/smil',
  79. 'smil' => 'application/smil',
  80. 'mif' => 'application/vnd.mif',
  81. 'xls' => [
  82. 'application/vnd.ms-excel',
  83. 'application/msexcel',
  84. 'application/x-msexcel',
  85. 'application/x-ms-excel',
  86. 'application/x-excel',
  87. 'application/x-dos_ms_excel',
  88. 'application/xls',
  89. 'application/x-xls',
  90. 'application/excel',
  91. 'application/download',
  92. 'application/vnd.ms-office',
  93. 'application/msword',
  94. ],
  95. 'ppt' => [
  96. 'application/vnd.ms-powerpoint',
  97. 'application/powerpoint',
  98. 'application/vnd.ms-office',
  99. 'application/msword',
  100. ],
  101. 'pptx' => [
  102. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  103. 'application/x-zip',
  104. 'application/zip',
  105. ],
  106. 'wbxml' => 'application/wbxml',
  107. 'wmlc' => 'application/wmlc',
  108. 'dcr' => 'application/x-director',
  109. 'dir' => 'application/x-director',
  110. 'dxr' => 'application/x-director',
  111. 'dvi' => 'application/x-dvi',
  112. 'gtar' => 'application/x-gtar',
  113. 'gz' => 'application/x-gzip',
  114. 'gzip' => 'application/x-gzip',
  115. 'php' => [
  116. 'application/x-php',
  117. 'application/x-httpd-php',
  118. 'application/php',
  119. 'text/php',
  120. 'text/x-php',
  121. 'application/x-httpd-php-source',
  122. ],
  123. 'php4' => 'application/x-httpd-php',
  124. 'php3' => 'application/x-httpd-php',
  125. 'phtml' => 'application/x-httpd-php',
  126. 'phps' => 'application/x-httpd-php-source',
  127. 'js' => [
  128. 'application/x-javascript',
  129. 'text/plain',
  130. ],
  131. 'swf' => 'application/x-shockwave-flash',
  132. 'sit' => 'application/x-stuffit',
  133. 'tar' => 'application/x-tar',
  134. 'tgz' => [
  135. 'application/x-tar',
  136. 'application/x-gzip-compressed',
  137. ],
  138. 'z' => 'application/x-compress',
  139. 'xhtml' => 'application/xhtml+xml',
  140. 'xht' => 'application/xhtml+xml',
  141. 'zip' => [
  142. 'application/x-zip',
  143. 'application/zip',
  144. 'application/x-zip-compressed',
  145. 'application/s-compressed',
  146. 'multipart/x-zip',
  147. ],
  148. 'rar' => [
  149. 'application/x-rar',
  150. 'application/rar',
  151. 'application/x-rar-compressed',
  152. ],
  153. 'mid' => 'audio/midi',
  154. 'midi' => 'audio/midi',
  155. 'mpga' => 'audio/mpeg',
  156. 'mp2' => 'audio/mpeg',
  157. 'mp3' => [
  158. 'audio/mpeg',
  159. 'audio/mpg',
  160. 'audio/mpeg3',
  161. 'audio/mp3',
  162. ],
  163. 'aif' => [
  164. 'audio/x-aiff',
  165. 'audio/aiff',
  166. ],
  167. 'aiff' => [
  168. 'audio/x-aiff',
  169. 'audio/aiff',
  170. ],
  171. 'aifc' => 'audio/x-aiff',
  172. 'ram' => 'audio/x-pn-realaudio',
  173. 'rm' => 'audio/x-pn-realaudio',
  174. 'rpm' => 'audio/x-pn-realaudio-plugin',
  175. 'ra' => 'audio/x-realaudio',
  176. 'rv' => 'video/vnd.rn-realvideo',
  177. 'wav' => [
  178. 'audio/x-wav',
  179. 'audio/wave',
  180. 'audio/wav',
  181. ],
  182. 'bmp' => [
  183. 'image/bmp',
  184. 'image/x-bmp',
  185. 'image/x-bitmap',
  186. 'image/x-xbitmap',
  187. 'image/x-win-bitmap',
  188. 'image/x-windows-bmp',
  189. 'image/ms-bmp',
  190. 'image/x-ms-bmp',
  191. 'application/bmp',
  192. 'application/x-bmp',
  193. 'application/x-win-bitmap',
  194. ],
  195. 'gif' => 'image/gif',
  196. 'jpg' => [
  197. 'image/jpeg',
  198. 'image/pjpeg',
  199. ],
  200. 'jpeg' => [
  201. 'image/jpeg',
  202. 'image/pjpeg',
  203. ],
  204. 'jpe' => [
  205. 'image/jpeg',
  206. 'image/pjpeg',
  207. ],
  208. 'jp2' => [
  209. 'image/jp2',
  210. 'video/mj2',
  211. 'image/jpx',
  212. 'image/jpm',
  213. ],
  214. 'j2k' => [
  215. 'image/jp2',
  216. 'video/mj2',
  217. 'image/jpx',
  218. 'image/jpm',
  219. ],
  220. 'jpf' => [
  221. 'image/jp2',
  222. 'video/mj2',
  223. 'image/jpx',
  224. 'image/jpm',
  225. ],
  226. 'jpg2' => [
  227. 'image/jp2',
  228. 'video/mj2',
  229. 'image/jpx',
  230. 'image/jpm',
  231. ],
  232. 'jpx' => [
  233. 'image/jp2',
  234. 'video/mj2',
  235. 'image/jpx',
  236. 'image/jpm',
  237. ],
  238. 'jpm' => [
  239. 'image/jp2',
  240. 'video/mj2',
  241. 'image/jpx',
  242. 'image/jpm',
  243. ],
  244. 'mj2' => [
  245. 'image/jp2',
  246. 'video/mj2',
  247. 'image/jpx',
  248. 'image/jpm',
  249. ],
  250. 'mjp2' => [
  251. 'image/jp2',
  252. 'video/mj2',
  253. 'image/jpx',
  254. 'image/jpm',
  255. ],
  256. 'png' => [
  257. 'image/png',
  258. 'image/x-png',
  259. ],
  260. 'tif' => 'image/tiff',
  261. 'tiff' => 'image/tiff',
  262. 'css' => [
  263. 'text/css',
  264. 'text/plain',
  265. ],
  266. 'html' => [
  267. 'text/html',
  268. 'text/plain',
  269. ],
  270. 'htm' => [
  271. 'text/html',
  272. 'text/plain',
  273. ],
  274. 'shtml' => [
  275. 'text/html',
  276. 'text/plain',
  277. ],
  278. 'txt' => 'text/plain',
  279. 'text' => 'text/plain',
  280. 'log' => [
  281. 'text/plain',
  282. 'text/x-log',
  283. ],
  284. 'rtx' => 'text/richtext',
  285. 'rtf' => 'text/rtf',
  286. 'xml' => [
  287. 'application/xml',
  288. 'text/xml',
  289. 'text/plain',
  290. ],
  291. 'xsl' => [
  292. 'application/xml',
  293. 'text/xsl',
  294. 'text/xml',
  295. ],
  296. 'mpeg' => 'video/mpeg',
  297. 'mpg' => 'video/mpeg',
  298. 'mpe' => 'video/mpeg',
  299. 'qt' => 'video/quicktime',
  300. 'mov' => 'video/quicktime',
  301. 'avi' => [
  302. 'video/x-msvideo',
  303. 'video/msvideo',
  304. 'video/avi',
  305. 'application/x-troff-msvideo',
  306. ],
  307. 'movie' => 'video/x-sgi-movie',
  308. 'doc' => [
  309. 'application/msword',
  310. 'application/vnd.ms-office',
  311. ],
  312. 'docx' => [
  313. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  314. 'application/zip',
  315. 'application/msword',
  316. 'application/x-zip',
  317. ],
  318. 'dot' => [
  319. 'application/msword',
  320. 'application/vnd.ms-office',
  321. ],
  322. 'dotx' => [
  323. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  324. 'application/zip',
  325. 'application/msword',
  326. ],
  327. 'xlsx' => [
  328. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  329. 'application/zip',
  330. 'application/vnd.ms-excel',
  331. 'application/msword',
  332. 'application/x-zip',
  333. ],
  334. 'word' => [
  335. 'application/msword',
  336. 'application/octet-stream',
  337. ],
  338. 'xl' => 'application/excel',
  339. 'eml' => 'message/rfc822',
  340. 'json' => [
  341. 'application/json',
  342. 'text/json',
  343. ],
  344. 'pem' => [
  345. 'application/x-x509-user-cert',
  346. 'application/x-pem-file',
  347. 'application/octet-stream',
  348. ],
  349. 'p10' => [
  350. 'application/x-pkcs10',
  351. 'application/pkcs10',
  352. ],
  353. 'p12' => 'application/x-pkcs12',
  354. 'p7a' => 'application/x-pkcs7-signature',
  355. 'p7c' => [
  356. 'application/pkcs7-mime',
  357. 'application/x-pkcs7-mime',
  358. ],
  359. 'p7m' => [
  360. 'application/pkcs7-mime',
  361. 'application/x-pkcs7-mime',
  362. ],
  363. 'p7r' => 'application/x-pkcs7-certreqresp',
  364. 'p7s' => 'application/pkcs7-signature',
  365. 'crt' => [
  366. 'application/x-x509-ca-cert',
  367. 'application/x-x509-user-cert',
  368. 'application/pkix-cert',
  369. ],
  370. 'crl' => [
  371. 'application/pkix-crl',
  372. 'application/pkcs-crl',
  373. ],
  374. 'der' => 'application/x-x509-ca-cert',
  375. 'kdb' => 'application/octet-stream',
  376. 'pgp' => 'application/pgp',
  377. 'gpg' => 'application/gpg-keys',
  378. 'sst' => 'application/octet-stream',
  379. 'csr' => 'application/octet-stream',
  380. 'rsa' => 'application/x-pkcs7',
  381. 'cer' => [
  382. 'application/pkix-cert',
  383. 'application/x-x509-ca-cert',
  384. ],
  385. '3g2' => 'video/3gpp2',
  386. '3gp' => [
  387. 'video/3gp',
  388. 'video/3gpp',
  389. ],
  390. 'mp4' => 'video/mp4',
  391. 'm4a' => 'audio/x-m4a',
  392. 'f4v' => [
  393. 'video/mp4',
  394. 'video/x-f4v',
  395. ],
  396. 'flv' => 'video/x-flv',
  397. 'webm' => 'video/webm',
  398. 'aac' => 'audio/x-acc',
  399. 'm4u' => 'application/vnd.mpegurl',
  400. 'm3u' => 'text/plain',
  401. 'xspf' => 'application/xspf+xml',
  402. 'vlc' => 'application/videolan',
  403. 'wmv' => [
  404. 'video/x-ms-wmv',
  405. 'video/x-ms-asf',
  406. ],
  407. 'au' => 'audio/x-au',
  408. 'ac3' => 'audio/ac3',
  409. 'flac' => 'audio/x-flac',
  410. 'ogg' => [
  411. 'audio/ogg',
  412. 'video/ogg',
  413. 'application/ogg',
  414. ],
  415. 'kmz' => [
  416. 'application/vnd.google-earth.kmz',
  417. 'application/zip',
  418. 'application/x-zip',
  419. ],
  420. 'kml' => [
  421. 'application/vnd.google-earth.kml+xml',
  422. 'application/xml',
  423. 'text/xml',
  424. ],
  425. 'ics' => 'text/calendar',
  426. 'ical' => 'text/calendar',
  427. 'zsh' => 'text/x-scriptzsh',
  428. '7zip' => [
  429. 'application/x-compressed',
  430. 'application/x-zip-compressed',
  431. 'application/zip',
  432. 'multipart/x-zip',
  433. ],
  434. 'cdr' => [
  435. 'application/cdr',
  436. 'application/coreldraw',
  437. 'application/x-cdr',
  438. 'application/x-coreldraw',
  439. 'image/cdr',
  440. 'image/x-cdr',
  441. 'zz-application/zz-winassoc-cdr',
  442. ],
  443. 'wma' => [
  444. 'audio/x-ms-wma',
  445. 'video/x-ms-asf',
  446. ],
  447. 'jar' => [
  448. 'application/java-archive',
  449. 'application/x-java-application',
  450. 'application/x-jar',
  451. 'application/x-compressed',
  452. ],
  453. 'svg' => [
  454. 'image/svg+xml',
  455. 'application/xml',
  456. 'text/xml',
  457. ],
  458. 'vcf' => 'text/x-vcard',
  459. 'srt' => [
  460. 'text/srt',
  461. 'text/plain',
  462. ],
  463. 'vtt' => [
  464. 'text/vtt',
  465. 'text/plain',
  466. ],
  467. 'ico' => [
  468. 'image/x-icon',
  469. 'image/x-ico',
  470. 'image/vnd.microsoft.icon',
  471. ],
  472. ];
  473. //--------------------------------------------------------------------
  474. /**
  475. * Attempts to determine the best mime type for the given file extension.
  476. *
  477. * @param string $extension
  478. *
  479. * @return string|null The mime type found, or none if unable to determine.
  480. */
  481. public static function guessTypeFromExtension(string $extension)
  482. {
  483. $extension = trim(strtolower($extension), '. ');
  484. if (! array_key_exists($extension, static::$mimes))
  485. {
  486. return null;
  487. }
  488. return is_array(static::$mimes[$extension]) ? static::$mimes[$extension][0] : static::$mimes[$extension];
  489. }
  490. //--------------------------------------------------------------------
  491. /**
  492. * Attempts to determine the best file extension for a given mime type.
  493. *
  494. * @param string $type
  495. * @param string $proposed_extension - default extension (in case there is more than one with the same mime type)
  496. *
  497. * @return string|null The extension determined, or null if unable to match.
  498. */
  499. public static function guessExtensionFromType(string $type, ?string $proposed_extension = null)
  500. {
  501. $type = trim(strtolower($type), '. ');
  502. $proposed_extension = trim(strtolower($proposed_extension));
  503. if (! is_null($proposed_extension) && array_key_exists($proposed_extension, static::$mimes) && in_array($type, is_string(static::$mimes[$proposed_extension]) ? [static::$mimes[$proposed_extension]] : static::$mimes[$proposed_extension]))
  504. {
  505. return $proposed_extension;
  506. }
  507. foreach (static::$mimes as $ext => $types)
  508. {
  509. if ((is_string($types) && $types === $type) || (is_array($types) && in_array($type, $types)))
  510. {
  511. return $ext;
  512. }
  513. }
  514. return null;
  515. }
  516. //--------------------------------------------------------------------
  517. }