PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/CoreVersions/0.2.0/Frameworks/Versions/SabreDAV.1.5.7-stable/lib/Sabre/DAV/Browser/GuessContentType.php

http://github.com/jeromeschneider/Baikal
PHP | 97 lines | 29 code | 19 blank | 49 comment | 3 complexity | 185a5c9c05f8ba76113785054e0711e9 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * GuessContentType plugin
  4. *
  5. * A lot of the built-in File objects just return application/octet-stream
  6. * as a content-type by default. This is a problem for some clients, because
  7. * they expect a correct contenttype.
  8. *
  9. * There's really no accurate, fast and portable way to determine the contenttype
  10. * so this extension does what the rest of the world does, and guesses it based
  11. * on the file extension.
  12. *
  13. * @package Sabre
  14. * @subpackage DAV
  15. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  16. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  17. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  18. */
  19. class Sabre_DAV_Browser_GuessContentType extends Sabre_DAV_ServerPlugin {
  20. /**
  21. * List of recognized file extensions
  22. *
  23. * Feel free to add more
  24. *
  25. * @var array
  26. */
  27. public $extensionMap = array(
  28. // images
  29. 'jpg' => 'image/jpeg',
  30. 'gif' => 'image/gif',
  31. 'png' => 'image/png',
  32. // groupware
  33. 'ics' => 'text/calendar',
  34. 'vcf' => 'text/x-vcard',
  35. // text
  36. 'txt' => 'text/plain',
  37. );
  38. /**
  39. * Initializes the plugin
  40. *
  41. * @param Sabre_DAV_Server $server
  42. * @return void
  43. */
  44. public function initialize(Sabre_DAV_Server $server) {
  45. // Using a relatively low priority (200) to allow other extensions
  46. // to set the content-type first.
  47. $server->subscribeEvent('afterGetProperties',array($this,'afterGetProperties'),200);
  48. }
  49. /**
  50. * Handler for teh afterGetProperties event
  51. *
  52. * @param string $path
  53. * @param array $properties
  54. * @return void
  55. */
  56. public function afterGetProperties($path, &$properties) {
  57. if (array_key_exists('{DAV:}getcontenttype', $properties[404])) {
  58. list(, $fileName) = Sabre_DAV_URLUtil::splitPath($path);
  59. $contentType = $this->getContentType($fileName);
  60. if ($contentType) {
  61. $properties[200]['{DAV:}getcontenttype'] = $contentType;
  62. unset($properties[404]['{DAV:}getcontenttype']);
  63. }
  64. }
  65. }
  66. /**
  67. * Simple method to return the contenttype
  68. *
  69. * @param string $fileName
  70. * @return string
  71. */
  72. protected function getContentType($fileName) {
  73. // Just grabbing the extension
  74. $extension = strtolower(substr($fileName,strrpos($fileName,'.')+1));
  75. if (isset($this->extensionMap[$extension]))
  76. return $this->extensionMap[$extension];
  77. }
  78. }