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