/includes/qcodo/_core/framework/QMimeType.class.php

https://github.com/quinta/qcodo · PHP · 158 lines · 100 code · 17 blank · 41 comment · 14 complexity · 68b9b4e0dfd527dcef54da2c9059844d MD5 · raw file

  1. <?php
  2. abstract class QMimeType {
  3. // Constants for Mime Types
  4. const _Default = 'application/octet-stream';
  5. const Executable = 'application/octet-stream';
  6. const Gif = 'image/gif';
  7. const Gzip = 'application/x-gzip';
  8. const Html = 'text/html';
  9. const Jpeg = 'image/jpeg';
  10. const Mp3 = 'audio/mpeg';
  11. const MpegVideo = 'video/mpeg';
  12. const MsExcel = 'application/vnd.ms-excel';
  13. const MsPowerpoint = 'application/vnd.ms-powerpoint';
  14. const MsWord = 'application/vnd.ms-word';
  15. const OoXmlWordProcessing = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  16. const OoXmlPresentation = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
  17. const OoXmlSpreadsheet = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  18. const Pdf = 'application/pdf';
  19. const PlainText = 'text/plain';
  20. const Png = 'image/png';
  21. const RichText = 'text/rtf';
  22. const Quicktime = 'video/quicktime';
  23. const WavAudio = 'audio/x-wav';
  24. const Xml = 'text/xml';
  25. const Zip = 'application/x-zip';
  26. /**
  27. * MimeTypeFor array is used in conjunction with GetMimeTypeForFilename()
  28. * @var string[]
  29. */
  30. public static $MimeTypeFor = array(
  31. 'doc' => QMimeType::MsWord,
  32. 'docx' => QMimeType::OoXmlWordProcessing,
  33. 'exe' => QMimeType::Executable,
  34. 'gif' => QMimeType::Gif,
  35. 'gz' => QMimeType::Gzip,
  36. 'htm' => QMimeType::Html,
  37. 'html' => QMimeType::Html,
  38. 'jpeg' => QMimeType::Jpeg,
  39. 'jpg' => QMimeType::Jpeg,
  40. 'mov' => QMimeType::Quicktime,
  41. 'mp3' => QMimeType::Mp3,
  42. 'mpeg' => QMimeType::MpegVideo,
  43. 'mpg' => QMimeType::MpegVideo,
  44. 'pdf' => QMimeType::Pdf,
  45. 'php' => QMimeType::PlainText,
  46. 'png' => QMimeType::Png,
  47. 'ppt' => QMimeType::MsPowerpoint,
  48. 'pptx' => QMimeType::OoXmlPresentation,
  49. 'rtf' => QMimeType::RichText,
  50. 'sql' => QMimeType::PlainText,
  51. 'txt' => QMimeType::PlainText,
  52. 'wav' => QMimeType::WavAudio,
  53. 'xls' => QMimeType::MsExcel,
  54. 'xlsx' => QMimeType::OoXmlSpreadsheet,
  55. 'xml' => QMimeType::Xml,
  56. 'zip' => QMimeType::Zip
  57. );
  58. /**
  59. * the absolute file path of the MIME Magic Database file
  60. * @var string
  61. */
  62. public static $MagicDatabaseFilePath = null;
  63. /**
  64. * Returns the suggested MIME type for an actual file. Using file-based heuristics
  65. * (data points in the ACTUAL file), it will utilize either the PECL FileInfo extension
  66. * OR the Magic MIME extension (if either are available) to determine the MIME type. If all
  67. * else fails, it will fall back to the basic GetMimeTypeForFilename() method.
  68. *
  69. * @param string $strFilePath the absolute file path of the ACTUAL file
  70. * @return string
  71. */
  72. public static function GetMimeTypeForFile($strFilePath) {
  73. // Clean up the File Path and pull out the filename
  74. $strRealPath = realpath($strFilePath);
  75. if (!is_file($strRealPath))
  76. throw new QCallerException('File Not Found: ' . $strFilePath);
  77. $strFilename = basename($strRealPath);
  78. $strToReturn = null;
  79. // First attempt using the PECL FileInfo extension
  80. if (class_exists('finfo')) {
  81. if (QMimeType::$MagicDatabaseFilePath)
  82. $objFileInfo = new finfo(FILEINFO_MIME, QMimeType::$MagicDatabaseFilePath);
  83. else
  84. $objFileInfo = new finfo(FILEINFO_MIME);
  85. $strToReturn = $objFileInfo->file($strRealPath);
  86. }
  87. // Next, attempt using the legacy MIME Magic extension
  88. if ((!$strToReturn) && (function_exists('mime_content_type'))) {
  89. $strToReturn = mime_content_type($strRealPath);
  90. }
  91. // Finally, use Qcodo's owns method for determining MIME type
  92. if (!$strToReturn)
  93. $strToReturn = QMimeType::GetMimeTypeForFilename($strFilename);
  94. if ($strToReturn)
  95. return $strToReturn;
  96. else
  97. return QMimeType::_Default;
  98. }
  99. /**
  100. * Returns the suggested MIME type for a filename by stripping
  101. * out the extension and looking it up from QMimeType::$MimeTypeFor
  102. *
  103. * @param string $strFilename
  104. * @return string
  105. */
  106. public static function GetMimeTypeForFilename($strFilename) {
  107. if (($intPosition = strrpos($strFilename, '.')) !== false) {
  108. $strExtension = trim(strtolower(substr($strFilename, $intPosition + 1)));
  109. if (array_key_exists($strExtension, QMimeType::$MimeTypeFor))
  110. return QMimeType::$MimeTypeFor[$strExtension];
  111. }
  112. return QMimeType::_Default;
  113. }
  114. /**
  115. * To more easily process a file repository based on Mime Types, it's sometimes
  116. * easier to tokenize a mimetype and process using the tokens (e.g. if you have a
  117. * directory of image icons that you want to map back to a mime type or a
  118. * collection of mime types, a tokenized-version of the mime type would be more
  119. * appropriate).
  120. *
  121. * Given a string-based mime type, this will return a "tokenized" version
  122. * of the mime type, which only consists of lower case characters and underscores (_).
  123. * @param string $strMimeType
  124. * @return string
  125. */
  126. public static function GetTokenForMimeType($strMimeType) {
  127. $strMimeType = strtolower($strMimeType);
  128. $strToReturn = '';
  129. $intLength = strlen($strMimeType);
  130. for ($intIndex = 0; $intIndex < $intLength; $intIndex++) {
  131. $strCharacter = $strMimeType[$intIndex];
  132. if ((ord($strCharacter) >= ord('a')) &&
  133. (ord($strCharacter) <= ord('z')))
  134. $strToReturn .= $strCharacter;
  135. else if ($strCharacter == '/')
  136. $strToReturn .= '_';
  137. }
  138. return $strToReturn;
  139. }
  140. }
  141. ?>