PageRenderTime 55ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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