Elgg  Version 5.1
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 
117  public function getType($file, $default = self::DEFAULT_TYPE) {
118  // Check only existing files
119  if (!is_file($file) || !is_readable($file)) {
120  return $default;
121  }
122 
123  $ext = pathinfo($file, PATHINFO_EXTENSION);
124 
125  $type = $this->tryStrategies($file);
126 
127  if ($type) {
128  return $this->fixDetectionErrors($type, $ext);
129  }
130 
131  if ($this->use_extension && isset($this->extensions[$ext])) {
132  return $this->extensions[$ext];
133  }
134 
135  return $default;
136  }
137 
145  public function tryStrategies($file) {
146  foreach ($this->strategies as $strategy) {
147  $type = call_user_func($strategy, $file);
148  if (!empty($type)) {
149  return $type;
150  }
151  }
152 
153  return '';
154  }
155 
164  public function fixDetectionErrors($type, $extension) {
165  if ($type === 'application/zip') {
166  // hack for Microsoft zipped formats
167  switch ($extension) {
168  case 'docx':
169  return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
170  case 'xlsx':
171  return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
172  case 'pptx':
173  return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
174  }
175  }
176 
177  // check for bad ppt detection
178  if ($type === 'application/vnd.ms-office' && $extension === 'ppt') {
179  return 'application/vnd.ms-powerpoint';
180  }
181 
182  // try extension detection as a fallback for octet-stream
183  if ($type === 'application/octet-stream' && $this->use_extension && isset($this->extensions[$extension])) {
184  return $this->extensions[$extension];
185  }
186 
187  return $type;
188  }
189 
197  public static function tryFinfo($file) {
198  if (!function_exists('finfo_open')) {
199  return '';
200  }
201 
202  $finfo = finfo_open(FILEINFO_MIME);
203  $type = finfo_file($finfo, $file);
204  finfo_close($finfo);
205  // Mimetype can come in text/plain; charset=us-ascii form
206  if (strpos($type, ';')) {
207  list($type,) = explode(';', $type);
208  }
209 
210  return $type;
211  }
212 
220  public static function tryMimeContentType($file) {
221  return function_exists('mime_content_type') ? mime_content_type($file) : '';
222  }
223 
231  public static function tryFile($file) {
232  if (DIRECTORY_SEPARATOR !== '/' || !function_exists('exec')) {
233  return '';
234  }
235 
236  $type = @exec('file -b --mime-type ' . escapeshellarg($file));
237  return $type ?: '';
238  }
239 
247  public static function tryGetimagesize($file) {
248  $data = @getimagesize($file);
249  return empty($data['mime']) ? '' : $data['mime'];
250  }
251 }
$default
Definition: checkbox.php:30
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.
$type
Definition: delete.php:22
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
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