Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
MimeTypeDetector.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Filesystem;
3 
8 
9  const DEFAULT_TYPE = 'application/octet-stream';
10 
14  public $strategies = [
15  [__CLASS__, 'tryFinfo'],
16  [__CLASS__, 'tryMimeContentType'],
17  [__CLASS__, 'tryFile'],
18  [__CLASS__, 'tryGetimagesize'],
19  ];
20 
24  public $use_extension = true;
25 
29  public $extensions = array(
30  'txt' => 'text/plain',
31  'htm' => 'text/html',
32  'html' => 'text/html',
33  'php' => 'text/html',
34  'css' => 'text/css',
35  'js' => 'application/javascript',
36  'json' => 'application/json',
37  'xml' => 'application/xml',
38  'swf' => 'application/x-shockwave-flash',
39  'flv' => 'video/x-flv',
40 
41  // images
42  'png' => 'image/png',
43  'jpe' => 'image/jpeg',
44  'jpeg' => 'image/jpeg',
45  'jpg' => 'image/jpeg',
46  'gif' => 'image/gif',
47  'bmp' => 'image/bmp',
48  'ico' => 'image/vnd.microsoft.icon',
49  'tiff' => 'image/tiff',
50  'tif' => 'image/tiff',
51  'svg' => 'image/svg+xml',
52  'svgz' => 'image/svg+xml',
53 
54  // archives
55  'zip' => 'application/zip',
56  'rar' => 'application/x-rar-compressed',
57  'exe' => 'application/x-msdownload',
58  'msi' => 'application/x-msdownload',
59  'cab' => 'application/vnd.ms-cab-compressed',
60 
61  // audio/video
62  'mp3' => 'audio/mpeg',
63  'qt' => 'video/quicktime',
64  'mov' => 'video/quicktime',
65 
66  // adobe
67  'pdf' => 'application/pdf',
68  'psd' => 'image/vnd.adobe.photoshop',
69  'ai' => 'application/postscript',
70  'eps' => 'application/postscript',
71  'ps' => 'application/postscript',
72 
73  // open office
74  'odt' => 'application/vnd.oasis.opendocument.text',
75  'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
76 
77  // office
78  'doc' => 'application/msword',
79  'rtf' => 'application/rtf',
80  'xls' => 'application/vnd.ms-excel',
81  'ppt' => 'application/vnd.ms-powerpoint',
82  'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
83  'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
84  'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
85  'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
86  'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
87  'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
88  'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
89  'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
90  'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
91  'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
92  'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
93  'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
94  'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
95  'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
96  'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
97  'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
98  'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
99  'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
100  'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
101  'one' => 'application/msonenote',
102  'onetoc2' => 'application/msonenote',
103  'onetmp' => 'application/msonenote',
104  'onepkg' => 'application/msonenote',
105  'thmx' => 'application/vnd.ms-officetheme',
106  );
107 
115  public function getType($file, $default = self::DEFAULT_TYPE) {
116  // Check only existing files
117  if (!is_file($file) || !is_readable($file)) {
118  return $default;
119  }
120 
121  $ext = pathinfo($file, PATHINFO_EXTENSION);
122 
123  $type = $this->tryStrategies($file);
124 
125  if ($type) {
126  return $this->fixDetectionErrors($type, $ext);
127  }
128 
129  if ($this->use_extension && isset($this->extensions[$ext])) {
130  return $this->extensions[$ext];
131  }
132 
133  return $default;
134  }
135 
142  public function tryStrategies($file) {
143  $type = '';
144  foreach ($this->strategies as $strategy) {
145  $type = call_user_func($strategy, $file);
146  if ($type) {
147  break;
148  }
149  }
150  return $type;
151  }
152 
160  public function fixDetectionErrors($type, $extension) {
161  if ($type === 'application/zip') {
162  // hack for Microsoft zipped formats
163  switch ($extension) {
164  case 'docx':
165  return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
166  case 'xlsx':
167  return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
168  case 'pptx':
169  return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
170  }
171  }
172 
173  // check for bad ppt detection
174  if ($type === "application/vnd.ms-office" && $extension === "ppt") {
175  return "application/vnd.ms-powerpoint";
176  }
177 
178  // try extension detection as a fallback for octet-stream
179  if ($type === "application/octet-stream" && $this->use_extension && isset($this->extensions[$extension])) {
180  return $this->extensions[$extension];
181  }
182 
183  return $type;
184  }
185 
192  public static function tryFinfo($file) {
193  if (!function_exists('finfo_open')) {
194  return '';
195  }
196 
197  $finfo = finfo_open(FILEINFO_MIME);
198  $type = finfo_file($finfo, $file);
199  finfo_close($finfo);
200  // Mimetype can come in text/plain; charset=us-ascii form
201  if (strpos($type, ';')) {
202  list($type,) = explode(';', $type);
203  }
204  return $type;
205  }
206 
213  public static function tryMimeContentType($file) {
214  return function_exists('mime_content_type') ? mime_content_type($file) : '';
215  }
216 
223  public static function tryFile($file) {
224  if (DIRECTORY_SEPARATOR !== '/' || !function_exists('exec')) {
225  return '';
226  }
227  $type = @exec("file -b --mime-type " . escapeshellarg($file));
228  return $type ? $type : '';
229  }
230 
237  public static function tryGetimagesize($file) {
238  $data = @getimagesize($file);
239  return empty($data['mime']) ? '' : $data['mime'];
240  }
241 }
static tryGetimagesize($file)
Detect MIME type.
if(!array_key_exists($filename, $text_files)) $file
list extensions
Definition: conf.py:31
tryStrategies($file)
Detect MIME type using various strategies.
static tryFinfo($file)
Detect MIME type using finfo_open.
$data
Definition: opendd.php:13
$default
Definition: checkbox.php:35
getType($file, $default=self::DEFAULT_TYPE)
Sniff the MIME type.
fixDetectionErrors($type, $extension)
Fix common type detection errors.
static tryMimeContentType($file)
Detect MIME type using mime_content_type.
Detect the MIME type of a file.
static tryFile($file)
Detect MIME type using file(1)
$extension
Definition: default.php:23
if(!$display_name) $type
Definition: delete.php:27