sfValidatorFile.class.php 31 KB

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