Elgg  Version 4.3
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 = [
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  'webp' => 'image/webp',
54 
55  // archives
56  'zip' => 'application/zip',
57  'rar' => 'application/x-rar-compressed',
58  'exe' => 'application/x-msdownload',
59  'msi' => 'application/x-msdownload',
60  'cab' => 'application/vnd.ms-cab-compressed',
61 
62  // audio/video
63  'mp3' => 'audio/mpeg',
64  'qt' => 'video/quicktime',
65  'mov' => 'video/quicktime',
66 
67  // adobe
68  'pdf' => 'application/pdf',
69  'psd' => 'image/vnd.adobe.photoshop',
70  'ai' => 'application/postscript',
71  'eps' => 'application/postscript',
72  'ps' => 'application/postscript',
73 
74  // open office
75  'odt' => 'application/vnd.oasis.opendocument.text',
76  'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
77 
78  // office
79  'doc' => 'application/msword',
80  'rtf' => 'application/rtf',
81  'xls' => 'application/vnd.ms-excel',
82  'ppt' => 'application/vnd.ms-powerpoint',
83  'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
84  'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
85  'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
86  'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
87  'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
88  'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
89  'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
90  'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
91  'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
92  'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
93  'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
94  'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
95  'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
96  'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
97  'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
98  'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
99  'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
100  'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
101  'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
102  'one' => 'application/msonenote',
103  'onetoc2' => 'application/msonenote',
104  'onetmp' => 'application/msonenote',
105  'onepkg' => 'application/msonenote',
106  'thmx' => 'application/vnd.ms-officetheme',
107  ];
108 
116  public function getType($file, $default = self::DEFAULT_TYPE) {
117  // Check only existing files
118  if (!is_file($file) || !is_readable($file)) {
119  return $default;
120  }
121 
122  $ext = pathinfo($file, PATHINFO_EXTENSION);
123 
124  $type = $this->tryStrategies($file);
125 
126  if ($type) {
127  return $this->fixDetectionErrors($type, $ext);
128  }
129 
130  if ($this->use_extension && isset($this->extensions[$ext])) {
131  return $this->extensions[$ext];
132  }
133 
134  return $default;
135  }
136 
143  public function tryStrategies($file) {
144  $type = '';
145  foreach ($this->strategies as $strategy) {
146  $type = call_user_func($strategy, $file);
147  if ($type) {
148  break;
149  }
150  }
151  return $type;
152  }
153 
161  public function fixDetectionErrors($type, $extension) {
162  if ($type === 'application/zip') {
163  // hack for Microsoft zipped formats
164  switch ($extension) {
165  case 'docx':
166  return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
167  case 'xlsx':
168  return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
169  case 'pptx':
170  return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
171  }
172  }
173 
174  // check for bad ppt detection
175  if ($type === "application/vnd.ms-office" && $extension === "ppt") {
176  return "application/vnd.ms-powerpoint";
177  }
178 
179  // try extension detection as a fallback for octet-stream
180  if ($type === "application/octet-stream" && $this->use_extension && isset($this->extensions[$extension])) {
181  return $this->extensions[$extension];
182  }
183 
184  return $type;
185  }
186 
193  public static function tryFinfo($file) {
194  if (!function_exists('finfo_open')) {
195  return '';
196  }
197 
198  $finfo = finfo_open(FILEINFO_MIME);
199  $type = finfo_file($finfo, $file);
200  finfo_close($finfo);
201  // Mimetype can come in text/plain; charset=us-ascii form
202  if (strpos($type, ';')) {
203  list($type,) = explode(';', $type);
204  }
205  return $type;
206  }
207 
214  public static function tryMimeContentType($file) {
215  return function_exists('mime_content_type') ? mime_content_type($file) : '';
216  }
217 
224  public static function tryFile($file) {
225  if (DIRECTORY_SEPARATOR !== '/' || !function_exists('exec')) {
226  return '';
227  }
228  $type = @exec("file -b --mime-type " . escapeshellarg($file));
229  return $type ? $type : '';
230  }
231 
238  public static function tryGetimagesize($file) {
239  $data = @getimagesize($file);
240  return empty($data['mime']) ? '' : $data['mime'];
241  }
242 }
$default
Definition: checkbox.php:31
static tryGetimagesize($file)
Detect MIME type.
list extensions
Definition: conf.py:31
tryStrategies($file)
Detect MIME type using various strategies.
static tryFinfo($file)
Detect MIME type using finfo_open.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
$type
Definition: delete.php:21
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:25