sfValidatorFile.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. // only in PHP 5.2
  10. if (!defined('UPLOAD_ERR_EXTENSION'))
  11. {
  12. define('UPLOAD_ERR_EXTENSION', 8);
  13. }
  14. /**
  15. * sfValidatorFile validates an uploaded file.
  16. *
  17. * @package symfony
  18. * @subpackage validator
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. * @version SVN: $Id: sfValidatorFile.class.php 18164 2009-05-12 13:12:59Z fabien $
  21. */
  22. class sfValidatorFile extends sfValidatorBase
  23. {
  24. /**
  25. * Configures the current validator.
  26. *
  27. * Available options:
  28. *
  29. * * max_size: The maximum file size
  30. * * mime_types: Allowed mime types array or category (available categories: web_images)
  31. * * mime_type_guessers: An array of mime type guesser PHP callables (must return the mime type or null)
  32. * * mime_categories: An array of mime type categories (web_images is defined by default)
  33. * * validated_file_class: Name of the class that manages the cleaned uploaded file (optional)
  34. *
  35. * There are 3 built-in mime type guessers:
  36. *
  37. * * guessFromFileinfo: Uses the finfo_open() function (from the Fileinfo PECL extension)
  38. * * guessFromMimeContentType: Uses the mime_content_type() function (deprecated)
  39. * * guessFromFileBinary: Uses the file binary (only works on *nix system)
  40. *
  41. * Available error codes:
  42. *
  43. * * max_size
  44. * * mime_types
  45. * * partial
  46. * * no_tmp_dir
  47. * * cant_write
  48. * * extension
  49. *
  50. * @param array $options An array of options
  51. * @param array $messages An array of error messages
  52. *
  53. * @see sfValidatorBase
  54. */
  55. protected function configure($options = array(), $messages = array())
  56. {
  57. $this->addOption('max_size');
  58. $this->addOption('mime_types');
  59. $this->addOption('mime_type_guessers', array(
  60. array($this, 'guessFromFileinfo'),
  61. array($this, 'guessFromMimeContentType'),
  62. array($this, 'guessFromFileBinary'),
  63. ));
  64. $this->addOption('mime_categories', array(
  65. 'web_images' => array(
  66. 'image/jpeg',
  67. 'image/pjpeg',
  68. 'image/png',
  69. 'image/x-png',
  70. 'image/gif',
  71. )));
  72. $this->addOption('validated_file_class', 'sfValidatedFile');
  73. $this->addMessage('max_size', 'File is too large (maximum is %max_size% bytes).');
  74. $this->addMessage('mime_types', 'Invalid mime type (%mime_type%).');
  75. $this->addMessage('partial', 'The uploaded file was only partially uploaded.');
  76. $this->addMessage('no_tmp_dir', 'Missing a temporary folder.');
  77. $this->addMessage('cant_write', 'Failed to write file to disk.');
  78. $this->addMessage('extension', 'File upload stopped by extension.');
  79. }
  80. /**
  81. * This validator always returns a sfValidatedFile object.
  82. *
  83. * The input value must be an array with the following keys:
  84. *
  85. * * tmp_name: The absolute temporary path to the file
  86. * * name: The original file name (optional)
  87. * * type: The file content type (optional)
  88. * * error: The error code (optional)
  89. * * size: The file size in bytes (optional)
  90. *
  91. * @see sfValidatorBase
  92. */
  93. protected function doClean($value)
  94. {
  95. if (!is_array($value) || !isset($value['tmp_name']))
  96. {
  97. throw new sfValidatorError($this, 'invalid', array('value' => (string) $value));
  98. }
  99. if (!isset($value['name']))
  100. {
  101. $value['name'] = '';
  102. }
  103. if (!isset($value['error']))
  104. {
  105. $value['error'] = UPLOAD_ERR_OK;
  106. }
  107. if (!isset($value['size']))
  108. {
  109. $value['size'] = filesize($value['tmp_name']);
  110. }
  111. if (!isset($value['type']))
  112. {
  113. $value['type'] = 'application/octet-stream';
  114. }
  115. switch ($value['error'])
  116. {
  117. case UPLOAD_ERR_INI_SIZE:
  118. $max = ini_get('upload_max_filesize');
  119. if ($this->getOption('max_size'))
  120. {
  121. $max = min($max, $this->getOption('max_size'));
  122. }
  123. throw new sfValidatorError($this, 'max_size', array('max_size' => $max, 'size' => (int) $value['size']));
  124. case UPLOAD_ERR_FORM_SIZE:
  125. throw new sfValidatorError($this, 'max_size', array('max_size' => 0, 'size' => (int) $value['size']));
  126. case UPLOAD_ERR_PARTIAL:
  127. throw new sfValidatorError($this, 'partial');
  128. case UPLOAD_ERR_NO_TMP_DIR:
  129. throw new sfValidatorError($this, 'no_tmp_dir');
  130. case UPLOAD_ERR_CANT_WRITE:
  131. throw new sfValidatorError($this, 'cant_write');
  132. case UPLOAD_ERR_EXTENSION:
  133. throw new sfValidatorError($this, 'extension');
  134. }
  135. // check file size
  136. if ($this->hasOption('max_size') && $this->getOption('max_size') < (int) $value['size'])
  137. {
  138. throw new sfValidatorError($this, 'max_size', array('max_size' => $this->getOption('max_size'), 'size' => (int) $value['size']));
  139. }
  140. $mimeType = $this->getMimeType((string) $value['tmp_name'], (string) $value['type']);
  141. // check mime type
  142. if ($this->hasOption('mime_types'))
  143. {
  144. $mimeTypes = is_array($this->getOption('mime_types')) ? $this->getOption('mime_types') : $this->getMimeTypesFromCategory($this->getOption('mime_types'));
  145. if (!in_array($mimeType, $mimeTypes))
  146. {
  147. throw new sfValidatorError($this, 'mime_types', array('mime_types' => $mimeTypes, 'mime_type' => $mimeType));
  148. }
  149. }
  150. $class = $this->getOption('validated_file_class');
  151. return new $class($value['name'], $mimeType, $value['tmp_name'], $value['size']);
  152. }
  153. /**
  154. * Returns the mime type of a file.
  155. *
  156. * This methods call each mime_type_guessers option callables to
  157. * guess the mime type.
  158. *
  159. * @param string $file The absolute path of a file
  160. * @param string $fallback The default mime type to return if not guessable
  161. *
  162. * @return string The mime type of the file (fallback is returned if not guessable)
  163. */
  164. protected function getMimeType($file, $fallback)
  165. {
  166. foreach ($this->getOption('mime_type_guessers') as $method)
  167. {
  168. $type = call_user_func($method, $file);
  169. if (!is_null($type) && $type !== false)
  170. {
  171. return $type;
  172. }
  173. }
  174. return $fallback;
  175. }
  176. /**
  177. * Guess the file mime type with PECL Fileinfo extension
  178. *
  179. * @param string $file The absolute path of a file
  180. *
  181. * @return string The mime type of the file (null if not guessable)
  182. */
  183. protected function guessFromFileinfo($file)
  184. {
  185. if (!function_exists('finfo_open'))
  186. {
  187. return null;
  188. }
  189. if (!$finfo = new finfo(FILEINFO_MIME))
  190. {
  191. return null;
  192. }
  193. $type = $finfo->file($file);
  194. return $type;
  195. }
  196. /**
  197. * Guess the file mime type with mime_content_type function (deprecated)
  198. *
  199. * @param string $file The absolute path of a file
  200. *
  201. * @return string The mime type of the file (null if not guessable)
  202. */
  203. protected function guessFromMimeContentType($file)
  204. {
  205. if (!function_exists('mime_content_type'))
  206. {
  207. return null;
  208. }
  209. return mime_content_type($file);
  210. }
  211. /**
  212. * Guess the file mime type with the file binary (only available on *nix)
  213. *
  214. * @param string $file The absolute path of a file
  215. *
  216. * @return string The mime type of the file (null if not guessable)
  217. */
  218. protected function guessFromFileBinary($file)
  219. {
  220. ob_start();
  221. passthru(sprintf('file -bi %s 2>/dev/null', escapeshellarg($file)), $return);
  222. if ($return > 0)
  223. {
  224. ob_end_clean();
  225. return null;
  226. }
  227. $type = trim(ob_get_clean());
  228. if (!preg_match('#^([a-z0-9\-]+/[a-z0-9\-]+)#i', $type, $match))
  229. {
  230. // it's not a type, but an error message
  231. return null;
  232. }
  233. return $match[1];
  234. }
  235. protected function getMimeTypesFromCategory($category)
  236. {
  237. $categories = $this->getOption('mime_categories');
  238. if (!isset($categories[$category]))
  239. {
  240. throw new InvalidArgumentException(sprintf('Invalid mime type category "%s".', $category));
  241. }
  242. return $categories[$category];
  243. }
  244. /**
  245. * @see sfValidatorBase
  246. */
  247. protected function isEmpty($value)
  248. {
  249. // empty if the value is not an array
  250. // or if the value comes from PHP with an error of UPLOAD_ERR_NO_FILE
  251. return
  252. (!is_array($value))
  253. ||
  254. (is_array($value) && isset($value['error']) && UPLOAD_ERR_NO_FILE === $value['error']);
  255. }
  256. }
  257. /**
  258. * sfValidatedFile represents a validated uploaded file.
  259. *
  260. * @package symfony
  261. * @subpackage validator
  262. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  263. * @version SVN: $Id: sfValidatorFile.class.php 18164 2009-05-12 13:12:59Z fabien $
  264. */
  265. class sfValidatedFile
  266. {
  267. protected
  268. $originalName = '',
  269. $tempName = '',
  270. $savedName = null,
  271. $type = '',
  272. $size = 0;
  273. /**
  274. * Constructor.
  275. *
  276. * @param string $originalName The original file name
  277. * @param string $type The file content type
  278. * @param string $tempName The absolute temporary path to the file
  279. * @param int $size The file size (in bytes)
  280. */
  281. public function __construct($originalName, $type, $tempName, $size)
  282. {
  283. $this->originalName = $originalName;
  284. $this->tempName = $tempName;
  285. $this->type = $type;
  286. $this->size = $size;
  287. }
  288. /**
  289. * Returns the name of the saved file.
  290. */
  291. public function __toString()
  292. {
  293. return is_null($this->savedName) ? '' : $this->savedName;
  294. }
  295. /**
  296. * Saves the uploaded file.
  297. *
  298. * This method can throw exceptions if there is a problem when saving the file.
  299. *
  300. * @param string $file The absolute file path to save the file
  301. * @param int $fileMode The octal mode to use for the new file
  302. * @param bool $create Indicates that we should make the directory before moving the file
  303. * @param int $dirMode The octal mode to use when creating the directory
  304. *
  305. * @return bool true, if the file was saved, otherwise false
  306. *
  307. * @throws Exception
  308. */
  309. public function save($file, $fileMode = 0666, $create = true, $dirMode = 0777)
  310. {
  311. // get our directory path from the destination filename
  312. $directory = dirname($file);
  313. if (!is_readable($directory))
  314. {
  315. if ($create && !mkdir($directory, $dirMode, true))
  316. {
  317. // failed to create the directory
  318. throw new Exception(sprintf('Failed to create file upload directory "%s".', $directory));
  319. }
  320. // chmod the directory since it doesn't seem to work on recursive paths
  321. chmod($directory, $dirMode);
  322. }
  323. if (!is_dir($directory))
  324. {
  325. // the directory path exists but it's not a directory
  326. throw new Exception(sprintf('File upload path "%s" exists, but is not a directory.', $directory));
  327. }
  328. if (!is_writable($directory))
  329. {
  330. // the directory isn't writable
  331. throw new Exception(sprintf('File upload path "%s" is not writable.', $directory));
  332. }
  333. // copy the temp file to the destination file
  334. copy($this->getTempName(), $file);
  335. // chmod our file
  336. chmod($file, $fileMode);
  337. $this->savedName = $file;
  338. return true;
  339. }
  340. /**
  341. * Returns the file extension, based on the content type of the file.
  342. *
  343. * @param string $default The default extension to return if none was given
  344. *
  345. * @return string The extension (with the dot)
  346. */
  347. public function getExtension($default = '')
  348. {
  349. return $this->getExtensionFromType($this->type, $default);
  350. }
  351. /**
  352. * Returns the original uploaded file name extension.
  353. *
  354. * @param string $default The default extension to return if none was given
  355. *
  356. * @return string The extension of the uploaded name (with the dot)
  357. */
  358. public function getOriginalExtension($default = '')
  359. {
  360. return (false === $pos = strrpos($this->getOriginalName(), '.')) ? $default : substr($this->getOriginalName(), $pos);
  361. }
  362. /**
  363. * Returns true if the file has already been saved.
  364. *
  365. * @return Boolean true if the file has already been saved, false otherwise
  366. */
  367. public function isSaved()
  368. {
  369. return !is_null($this->savedName);
  370. }
  371. /**
  372. * Returns the path where the file has been saved
  373. *
  374. * @return string The path where the file has been saved
  375. */
  376. public function getSavedName()
  377. {
  378. return $this->savedName;
  379. }
  380. /**
  381. * Returns the original file name.
  382. *
  383. * @return string The file name
  384. */
  385. public function getOriginalName()
  386. {
  387. return $this->originalName;
  388. }
  389. /**
  390. * Returns the absolute temporary path to the uploaded file.
  391. *
  392. * @return string The temporary path
  393. */
  394. public function getTempName()
  395. {
  396. return $this->tempName;
  397. }
  398. /**
  399. * Returns the file content type.
  400. *
  401. * @return string The content type
  402. */
  403. public function getType()
  404. {
  405. return $this->type;
  406. }
  407. /**
  408. * Returns the size of the uploaded file.
  409. *
  410. * @return int The file size
  411. */
  412. public function getSize()
  413. {
  414. return $this->size;
  415. }
  416. /**
  417. * Returns the extension associated with the given content type.
  418. *
  419. * @param string $type The content type
  420. * @param string $default The default extension to use
  421. *
  422. * @return string The extension (with the dot)
  423. */
  424. protected function getExtensionFromType($type, $default = '')
  425. {
  426. static $extensions = array(
  427. 'application/andrew-inset' => 'ez',
  428. 'application/appledouble' => 'base64',
  429. 'application/applefile' => 'base64',
  430. 'application/commonground' => 'dp',
  431. 'application/cprplayer' => 'pqi',
  432. 'application/dsptype' => 'tsp',
  433. 'application/excel' => 'xls',
  434. 'application/font-tdpfr' => 'pfr',
  435. 'application/futuresplash' => 'spl',
  436. 'application/hstu' => 'stk',
  437. 'application/hyperstudio' => 'stk',
  438. 'application/javascript' => 'js',
  439. 'application/mac-binhex40' => 'hqx',
  440. 'application/mac-compactpro' => 'cpt',
  441. 'application/mbed' => 'mbd',
  442. 'application/mirage' => 'mfp',
  443. 'application/msword' => 'doc',
  444. 'application/ocsp-request' => 'orq',
  445. 'application/ocsp-response' => 'ors',
  446. 'application/octet-stream' => 'bin',
  447. 'application/oda' => 'oda',
  448. 'application/ogg' => 'ogg',
  449. 'application/pdf' => 'pdf',
  450. 'application/x-pdf' => 'pdf',
  451. 'application/pgp-encrypted' => '7bit',
  452. 'application/pgp-keys' => '7bit',
  453. 'application/pgp-signature' => 'sig',
  454. 'application/pkcs10' => 'p10',
  455. 'application/pkcs7-mime' => 'p7m',
  456. 'application/pkcs7-signature' => 'p7s',
  457. 'application/pkix-cert' => 'cer',
  458. 'application/pkix-crl' => 'crl',
  459. 'application/pkix-pkipath' => 'pkipath',
  460. 'application/pkixcmp' => 'pki',
  461. 'application/postscript' => 'ps',
  462. 'application/presentations' => 'shw',
  463. 'application/prs.cww' => 'cw',
  464. 'application/prs.nprend' => 'rnd',
  465. 'application/quest' => 'qrt',
  466. 'application/rtf' => 'rtf',
  467. 'application/sgml-open-catalog' => 'soc',
  468. 'application/sieve' => 'siv',
  469. 'application/smil' => 'smi',
  470. 'application/toolbook' => 'tbk',
  471. 'application/vnd.3gpp.pic-bw-large' => 'plb',
  472. 'application/vnd.3gpp.pic-bw-small' => 'psb',
  473. 'application/vnd.3gpp.pic-bw-var' => 'pvb',
  474. 'application/vnd.3gpp.sms' => 'sms',
  475. 'application/vnd.acucorp' => 'atc',
  476. 'application/vnd.adobe.xfdf' => 'xfdf',
  477. 'application/vnd.amiga.amu' => 'ami',
  478. 'application/vnd.blueice.multipass' => 'mpm',
  479. 'application/vnd.cinderella' => 'cdy',
  480. 'application/vnd.cosmocaller' => 'cmc',
  481. 'application/vnd.criticaltools.wbs+xml' => 'wbs',
  482. 'application/vnd.curl' => 'curl',
  483. 'application/vnd.data-vision.rdz' => 'rdz',
  484. 'application/vnd.dreamfactory' => 'dfac',
  485. 'application/vnd.fsc.weblauch' => 'fsc',
  486. 'application/vnd.genomatix.tuxedo' => 'txd',
  487. 'application/vnd.hbci' => 'hbci',
  488. 'application/vnd.hhe.lesson-player' => 'les',
  489. 'application/vnd.hp-hpgl' => 'plt',
  490. 'application/vnd.ibm.electronic-media' => 'emm',
  491. 'application/vnd.ibm.rights-management' => 'irm',
  492. 'application/vnd.ibm.secure-container' => 'sc',
  493. 'application/vnd.ipunplugged.rcprofile' => 'rcprofile',
  494. 'application/vnd.irepository.package+xml' => 'irp',
  495. 'application/vnd.jisp' => 'jisp',
  496. 'application/vnd.kde.karbon' => 'karbon',
  497. 'application/vnd.kde.kchart' => 'chrt',
  498. 'application/vnd.kde.kformula' => 'kfo',
  499. 'application/vnd.kde.kivio' => 'flw',
  500. 'application/vnd.kde.kontour' => 'kon',
  501. 'application/vnd.kde.kpresenter' => 'kpr',
  502. 'application/vnd.kde.kspread' => 'ksp',
  503. 'application/vnd.kde.kword' => 'kwd',
  504. 'application/vnd.kenameapp' => 'htke',
  505. 'application/vnd.kidspiration' => 'kia',
  506. 'application/vnd.kinar' => 'kne',
  507. 'application/vnd.llamagraphics.life-balance.desktop' => 'lbd',
  508. 'application/vnd.llamagraphics.life-balance.exchange+xml' => 'lbe',
  509. 'application/vnd.lotus-1-2-3' => 'wks',
  510. 'application/vnd.mcd' => 'mcd',
  511. 'application/vnd.mfmp' => 'mfm',
  512. 'application/vnd.micrografx.flo' => 'flo',
  513. 'application/vnd.micrografx.igx' => 'igx',
  514. 'application/vnd.mif' => 'mif',
  515. 'application/vnd.mophun.application' => 'mpn',
  516. 'application/vnd.mophun.certificate' => 'mpc',
  517. 'application/vnd.mozilla.xul+xml' => 'xul',
  518. 'application/vnd.ms-artgalry' => 'cil',
  519. 'application/vnd.ms-asf' => 'asf',
  520. 'application/vnd.ms-excel' => 'xls',
  521. 'application/vnd.ms-lrm' => 'lrm',
  522. 'application/vnd.ms-powerpoint' => 'ppt',
  523. 'application/vnd.ms-project' => 'mpp',
  524. 'application/vnd.ms-tnef' => 'base64',
  525. 'application/vnd.ms-works' => 'base64',
  526. 'application/vnd.ms-wpl' => 'wpl',
  527. 'application/vnd.mseq' => 'mseq',
  528. 'application/vnd.nervana' => 'ent',
  529. 'application/vnd.nokia.radio-preset' => 'rpst',
  530. 'application/vnd.nokia.radio-presets' => 'rpss',
  531. 'application/vnd.oasis.opendocument.text' => 'odt',
  532. 'application/vnd.oasis.opendocument.text-template' => 'ott',
  533. 'application/vnd.oasis.opendocument.text-web' => 'oth',
  534. 'application/vnd.oasis.opendocument.text-master' => 'odm',
  535. 'application/vnd.oasis.opendocument.graphics' => 'odg',
  536. 'application/vnd.oasis.opendocument.graphics-template' => 'otg',
  537. 'application/vnd.oasis.opendocument.presentation' => 'odp',
  538. 'application/vnd.oasis.opendocument.presentation-template' => 'otp',
  539. 'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
  540. 'application/vnd.oasis.opendocument.spreadsheet-template' => 'ots',
  541. 'application/vnd.oasis.opendocument.chart' => 'odc',
  542. 'application/vnd.oasis.opendocument.formula' => 'odf',
  543. 'application/vnd.oasis.opendocument.database' => 'odb',
  544. 'application/vnd.oasis.opendocument.image' => 'odi',
  545. 'application/vnd.palm' => 'prc',
  546. 'application/vnd.picsel' => 'efif',
  547. 'application/vnd.pvi.ptid1' => 'pti',
  548. 'application/vnd.quark.quarkxpress' => 'qxd',
  549. 'application/vnd.sealed.doc' => 'sdoc',
  550. 'application/vnd.sealed.eml' => 'seml',
  551. 'application/vnd.sealed.mht' => 'smht',
  552. 'application/vnd.sealed.ppt' => 'sppt',
  553. 'application/vnd.sealed.xls' => 'sxls',
  554. 'application/vnd.sealedmedia.softseal.html' => 'stml',
  555. 'application/vnd.sealedmedia.softseal.pdf' => 'spdf',
  556. 'application/vnd.seemail' => 'see',
  557. 'application/vnd.smaf' => 'mmf',
  558. 'application/vnd.sun.xml.calc' => 'sxc',
  559. 'application/vnd.sun.xml.calc.template' => 'stc',
  560. 'application/vnd.sun.xml.draw' => 'sxd',
  561. 'application/vnd.sun.xml.draw.template' => 'std',
  562. 'application/vnd.sun.xml.impress' => 'sxi',
  563. 'application/vnd.sun.xml.impress.template' => 'sti',
  564. 'application/vnd.sun.xml.math' => 'sxm',
  565. 'application/vnd.sun.xml.writer' => 'sxw',
  566. 'application/vnd.sun.xml.writer.global' => 'sxg',
  567. 'application/vnd.sun.xml.writer.template' => 'stw',
  568. 'application/vnd.sus-calendar' => 'sus',
  569. 'application/vnd.vidsoft.vidconference' => 'vsc',
  570. 'application/vnd.visio' => 'vsd',
  571. 'application/vnd.visionary' => 'vis',
  572. 'application/vnd.wap.sic' => 'sic',
  573. 'application/vnd.wap.slc' => 'slc',
  574. 'application/vnd.wap.wbxml' => 'wbxml',
  575. 'application/vnd.wap.wmlc' => 'wmlc',
  576. 'application/vnd.wap.wmlscriptc' => 'wmlsc',
  577. 'application/vnd.webturbo' => 'wtb',
  578. 'application/vnd.wordperfect' => 'wpd',
  579. 'application/vnd.wqd' => 'wqd',
  580. 'application/vnd.wv.csp+wbxml' => 'wv',
  581. 'application/vnd.wv.csp+xml' => '8bit',
  582. 'application/vnd.wv.ssp+xml' => '8bit',
  583. 'application/vnd.yamaha.hv-dic' => 'hvd',
  584. 'application/vnd.yamaha.hv-script' => 'hvs',
  585. 'application/vnd.yamaha.hv-voice' => 'hvp',
  586. 'application/vnd.yamaha.smaf-audio' => 'saf',
  587. 'application/vnd.yamaha.smaf-phrase' => 'spf',
  588. 'application/vocaltec-media-desc' => 'vmd',
  589. 'application/vocaltec-media-file' => 'vmf',
  590. 'application/vocaltec-talker' => 'vtk',
  591. 'application/watcherinfo+xml' => 'wif',
  592. 'application/wordperfect5.1' => 'wp5',
  593. 'application/x-123' => 'wk',
  594. 'application/x-7th_level_event' => '7ls',
  595. 'application/x-authorware-bin' => 'aab',
  596. 'application/x-authorware-map' => 'aam',
  597. 'application/x-authorware-seg' => 'aas',
  598. 'application/x-bcpio' => 'bcpio',
  599. 'application/x-bleeper' => 'bleep',
  600. 'application/x-bzip2' => 'bz2',
  601. 'application/x-cdlink' => 'vcd',
  602. 'application/x-chat' => 'chat',
  603. 'application/x-chess-pgn' => 'pgn',
  604. 'application/x-compress' => 'z',
  605. 'application/x-cpio' => 'cpio',
  606. 'application/x-cprplayer' => 'pqf',
  607. 'application/x-csh' => 'csh',
  608. 'application/x-cu-seeme' => 'csm',
  609. 'application/x-cult3d-object' => 'co',
  610. 'application/x-debian-package' => 'deb',
  611. 'application/x-director' => 'dcr',
  612. 'application/x-dvi' => 'dvi',
  613. 'application/x-envoy' => 'evy',
  614. 'application/x-futuresplash' => 'spl',
  615. 'application/x-gtar' => 'gtar',
  616. 'application/x-gzip' => 'gz',
  617. 'application/x-hdf' => 'hdf',
  618. 'application/x-hep' => 'hep',
  619. 'application/x-html+ruby' => 'rhtml',
  620. 'application/x-httpd-miva' => 'mv',
  621. 'application/x-httpd-php' => 'phtml',
  622. 'application/x-ica' => 'ica',
  623. 'application/x-imagemap' => 'imagemap',
  624. 'application/x-ipix' => 'ipx',
  625. 'application/x-ipscript' => 'ips',
  626. 'application/x-java-archive' => 'jar',
  627. 'application/x-java-jnlp-file' => 'jnlp',
  628. 'application/x-java-serialized-object' => 'ser',
  629. 'application/x-java-vm' => 'class',
  630. 'application/x-javascript' => 'js',
  631. 'application/x-koan' => 'skp',
  632. 'application/x-latex' => 'latex',
  633. 'application/x-mac-compactpro' => 'cpt',
  634. 'application/x-maker' => 'frm',
  635. 'application/x-mathcad' => 'mcd',
  636. 'application/x-midi' => 'mid',
  637. 'application/x-mif' => 'mif',
  638. 'application/x-msaccess' => 'mda',
  639. 'application/x-msdos-program' => 'com',
  640. 'application/x-msdownload' => 'base64',
  641. 'application/x-msexcel' => 'xls',
  642. 'application/x-msword' => 'doc',
  643. 'application/x-netcdf' => 'nc',
  644. 'application/x-ns-proxy-autoconfig' => 'pac',
  645. 'application/x-pagemaker' => 'pm5',
  646. 'application/x-perl' => 'pl',
  647. 'application/x-pn-realmedia' => 'rp',
  648. 'application/x-python' => 'py',
  649. 'application/x-quicktimeplayer' => 'qtl',
  650. 'application/x-rar-compressed' => 'rar',
  651. 'application/x-ruby' => 'rb',
  652. 'application/x-sh' => 'sh',
  653. 'application/x-shar' => 'shar',
  654. 'application/x-shockwave-flash' => 'swf',
  655. 'application/x-sprite' => 'spr',
  656. 'application/x-spss' => 'sav',
  657. 'application/x-spt' => 'spt',
  658. 'application/x-stuffit' => 'sit',
  659. 'application/x-sv4cpio' => 'sv4cpio',
  660. 'application/x-sv4crc' => 'sv4crc',
  661. 'application/x-tar' => 'tar',
  662. 'application/x-tcl' => 'tcl',
  663. 'application/x-tex' => 'tex',
  664. 'application/x-texinfo' => 'texinfo',
  665. 'application/x-troff' => 't',
  666. 'application/x-troff-man' => 'man',
  667. 'application/x-troff-me' => 'me',
  668. 'application/x-troff-ms' => 'ms',
  669. 'application/x-twinvq' => 'vqf',
  670. 'application/x-twinvq-plugin' => 'vqe',
  671. 'application/x-ustar' => 'ustar',
  672. 'application/x-vmsbackup' => 'bck',
  673. 'application/x-wais-source' => 'src',
  674. 'application/x-wingz' => 'wz',
  675. 'application/x-word' => 'base64',
  676. 'application/x-wordperfect6.1' => 'wp6',
  677. 'application/x-x509-ca-cert' => 'crt',
  678. 'application/x-zip-compressed' => 'zip',
  679. 'application/xhtml+xml' => 'xhtml',
  680. 'application/zip' => 'zip',
  681. 'audio/3gpp' => '3gpp',
  682. 'audio/amr' => 'amr',
  683. 'audio/amr-wb' => 'awb',
  684. 'audio/basic' => 'au',
  685. 'audio/evrc' => 'evc',
  686. 'audio/l16' => 'l16',
  687. 'audio/midi' => 'mid',
  688. 'audio/mpeg' => 'mp3',
  689. 'audio/prs.sid' => 'sid',
  690. 'audio/qcelp' => 'qcp',
  691. 'audio/smv' => 'smv',
  692. 'audio/vnd.audiokoz' => 'koz',
  693. 'audio/vnd.digital-winds' => 'eol',
  694. 'audio/vnd.everad.plj' => 'plj',
  695. 'audio/vnd.lucent.voice' => 'lvp',
  696. 'audio/vnd.nokia.mobile-xmf' => 'mxmf',
  697. 'audio/vnd.nortel.vbk' => 'vbk',
  698. 'audio/vnd.nuera.ecelp4800' => 'ecelp4800',
  699. 'audio/vnd.nuera.ecelp7470' => 'ecelp7470',
  700. 'audio/vnd.nuera.ecelp9600' => 'ecelp9600',
  701. 'audio/vnd.sealedmedia.softseal.mpeg' => 'smp3',
  702. 'audio/voxware' => 'vox',
  703. 'audio/x-aiff' => 'aif',
  704. 'audio/x-mid' => 'mid',
  705. 'audio/x-midi' => 'mid',
  706. 'audio/x-mpeg' => 'mp2',
  707. 'audio/x-mpegurl' => 'mpu',
  708. 'audio/x-pn-realaudio' => 'rm',
  709. 'audio/x-pn-realaudio-plugin' => 'rpm',
  710. 'audio/x-realaudio' => 'ra',
  711. 'audio/x-wav' => 'wav',
  712. 'chemical/x-csml' => 'csm',
  713. 'chemical/x-embl-dl-nucleotide' => 'emb',
  714. 'chemical/x-gaussian-cube' => 'cube',
  715. 'chemical/x-gaussian-input' => 'gau',
  716. 'chemical/x-jcamp-dx' => 'jdx',
  717. 'chemical/x-mdl-molfile' => 'mol',
  718. 'chemical/x-mdl-rxnfile' => 'rxn',
  719. 'chemical/x-mdl-tgf' => 'tgf',
  720. 'chemical/x-mopac-input' => 'mop',
  721. 'chemical/x-pdb' => 'pdb',
  722. 'chemical/x-rasmol' => 'scr',
  723. 'chemical/x-xyz' => 'xyz',
  724. 'drawing/dwf' => 'dwf',
  725. 'drawing/x-dwf' => 'dwf',
  726. 'i-world/i-vrml' => 'ivr',
  727. 'image/bmp' => 'bmp',
  728. 'image/cewavelet' => 'wif',
  729. 'image/cis-cod' => 'cod',
  730. 'image/fif' => 'fif',
  731. 'image/gif' => 'gif',
  732. 'image/ief' => 'ief',
  733. 'image/jp2' => 'jp2',
  734. 'image/jpeg' => 'jpg',
  735. 'image/jpm' => 'jpm',
  736. 'image/jpx' => 'jpf',
  737. 'image/pict' => 'pic',
  738. 'image/pjpeg' => 'jpg',
  739. 'image/png' => 'png',
  740. 'image/targa' => 'tga',
  741. 'image/tiff' => 'tif',
  742. 'image/vn-svf' => 'svf',
  743. 'image/vnd.dgn' => 'dgn',
  744. 'image/vnd.djvu' => 'djvu',
  745. 'image/vnd.dwg' => 'dwg',
  746. 'image/vnd.glocalgraphics.pgb' => 'pgb',
  747. 'image/vnd.microsoft.icon' => 'ico',
  748. 'image/vnd.ms-modi' => 'mdi',
  749. 'image/vnd.sealed.png' => 'spng',
  750. 'image/vnd.sealedmedia.softseal.gif' => 'sgif',
  751. 'image/vnd.sealedmedia.softseal.jpg' => 'sjpg',
  752. 'image/vnd.wap.wbmp' => 'wbmp',
  753. 'image/x-bmp' => 'bmp',
  754. 'image/x-cmu-raster' => 'ras',
  755. 'image/x-freehand' => 'fh4',
  756. 'image/x-png' => 'png',
  757. 'image/x-portable-anymap' => 'pnm',
  758. 'image/x-portable-bitmap' => 'pbm',
  759. 'image/x-portable-graymap' => 'pgm',
  760. 'image/x-portable-pixmap' => 'ppm',
  761. 'image/x-rgb' => 'rgb',
  762. 'image/x-xbitmap' => 'xbm',
  763. 'image/x-xpixmap' => 'xpm',
  764. 'image/x-xwindowdump' => 'xwd',
  765. 'message/external-body' => '8bit',
  766. 'message/news' => '8bit',
  767. 'message/partial' => '8bit',
  768. 'message/rfc822' => '8bit',
  769. 'model/iges' => 'igs',
  770. 'model/mesh' => 'msh',
  771. 'model/vnd.parasolid.transmit.binary' => 'x_b',
  772. 'model/vnd.parasolid.transmit.text' => 'x_t',
  773. 'model/vrml' => 'wrl',
  774. 'multipart/alternative' => '8bit',
  775. 'multipart/appledouble' => '8bit',
  776. 'multipart/digest' => '8bit',
  777. 'multipart/mixed' => '8bit',
  778. 'multipart/parallel' => '8bit',
  779. 'text/comma-separated-values' => 'csv',
  780. 'text/css' => 'css',
  781. 'text/html' => 'html',
  782. 'text/plain' => 'txt',
  783. 'text/prs.fallenstein.rst' => 'rst',
  784. 'text/richtext' => 'rtx',
  785. 'text/rtf' => 'rtf',
  786. 'text/sgml' => 'sgml',
  787. 'text/tab-separated-values' => 'tsv',
  788. 'text/vnd.net2phone.commcenter.command' => 'ccc',
  789. 'text/vnd.sun.j2me.app-descriptor' => 'jad',
  790. 'text/vnd.wap.si' => 'si',
  791. 'text/vnd.wap.sl' => 'sl',
  792. 'text/vnd.wap.wml' => 'wml',
  793. 'text/vnd.wap.wmlscript' => 'wmls',
  794. 'text/x-hdml' => 'hdml',
  795. 'text/x-setext' => 'etx',
  796. 'text/x-sgml' => 'sgml',
  797. 'text/x-speech' => 'talk',
  798. 'text/x-vcalendar' => 'vcs',
  799. 'text/x-vcard' => 'vcf',
  800. 'text/xml' => 'xml',
  801. 'ulead/vrml' => 'uvr',
  802. 'video/3gpp' => '3gp',
  803. 'video/dl' => 'dl',
  804. 'video/gl' => 'gl',
  805. 'video/mj2' => 'mj2',
  806. 'video/mpeg' => 'mpeg',
  807. 'video/quicktime' => 'mov',
  808. 'video/vdo' => 'vdo',
  809. 'video/vivo' => 'viv',
  810. 'video/vnd.fvt' => 'fvt',
  811. 'video/vnd.mpegurl' => 'mxu',
  812. 'video/vnd.nokia.interleaved-multimedia' => 'nim',
  813. 'video/vnd.objectvideo' => 'mp4',
  814. 'video/vnd.sealed.mpeg1' => 's11',
  815. 'video/vnd.sealed.mpeg4' => 'smpg',
  816. 'video/vnd.sealed.swf' => 'sswf',
  817. 'video/vnd.sealedmedia.softseal.mov' => 'smov',
  818. 'video/vnd.vivo' => 'vivo',
  819. 'video/x-fli' => 'fli',
  820. 'video/x-ms-asf' => 'asf',
  821. 'video/x-ms-wmv' => 'wmv',
  822. 'video/x-msvideo' => 'avi',
  823. 'video/x-sgi-movie' => 'movie',
  824. 'x-chemical/x-pdb' => 'pdb',
  825. 'x-chemical/x-xyz' => 'xyz',
  826. 'x-conference/x-cooltalk' => 'ice',
  827. 'x-drawing/dwf' => 'dwf',
  828. 'x-world/x-d96' => 'd',
  829. 'x-world/x-svr' => 'svr',
  830. 'x-world/x-vream' => 'vrw',
  831. 'x-world/x-vrml' => 'wrl',
  832. );
  833. return !$type ? $default : (isset($extensions[$type]) ? '.'.$extensions[$type] : $default);
  834. }
  835. }