@@ -51,340 +51,340 @@ |
||
51 | 51 | * @package OC\Files\Type |
52 | 52 | */ |
53 | 53 | class Detection implements IMimeTypeDetector { |
54 | - private const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json'; |
|
55 | - private const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json'; |
|
56 | - |
|
57 | - protected $mimetypes = []; |
|
58 | - protected $secureMimeTypes = []; |
|
59 | - |
|
60 | - protected $mimetypeIcons = []; |
|
61 | - /** @var string[] */ |
|
62 | - protected $mimeTypeAlias = []; |
|
63 | - |
|
64 | - /** @var IURLGenerator */ |
|
65 | - private $urlGenerator; |
|
66 | - |
|
67 | - /** @var ILogger */ |
|
68 | - private $logger; |
|
69 | - |
|
70 | - /** @var string */ |
|
71 | - private $customConfigDir; |
|
72 | - |
|
73 | - /** @var string */ |
|
74 | - private $defaultConfigDir; |
|
75 | - |
|
76 | - /** |
|
77 | - * @param IURLGenerator $urlGenerator |
|
78 | - * @param ILogger $logger |
|
79 | - * @param string $customConfigDir |
|
80 | - * @param string $defaultConfigDir |
|
81 | - */ |
|
82 | - public function __construct(IURLGenerator $urlGenerator, |
|
83 | - ILogger $logger, |
|
84 | - string $customConfigDir, |
|
85 | - string $defaultConfigDir) { |
|
86 | - $this->urlGenerator = $urlGenerator; |
|
87 | - $this->logger = $logger; |
|
88 | - $this->customConfigDir = $customConfigDir; |
|
89 | - $this->defaultConfigDir = $defaultConfigDir; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Add an extension -> mimetype mapping |
|
94 | - * |
|
95 | - * $mimetype is the assumed correct mime type |
|
96 | - * The optional $secureMimeType is an alternative to send to send |
|
97 | - * to avoid potential XSS. |
|
98 | - * |
|
99 | - * @param string $extension |
|
100 | - * @param string $mimetype |
|
101 | - * @param string|null $secureMimeType |
|
102 | - */ |
|
103 | - public function registerType(string $extension, |
|
104 | - string $mimetype, |
|
105 | - ?string $secureMimeType = null): void { |
|
106 | - $this->mimetypes[$extension] = [$mimetype, $secureMimeType]; |
|
107 | - $this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Add an array of extension -> mimetype mappings |
|
112 | - * |
|
113 | - * The mimetype value is in itself an array where the first index is |
|
114 | - * the assumed correct mimetype and the second is either a secure alternative |
|
115 | - * or null if the correct is considered secure. |
|
116 | - * |
|
117 | - * @param array $types |
|
118 | - */ |
|
119 | - public function registerTypeArray(array $types): void { |
|
120 | - $this->mimetypes = array_merge($this->mimetypes, $types); |
|
121 | - |
|
122 | - // Update the alternative mimetypes to avoid having to look them up each time. |
|
123 | - foreach ($this->mimetypes as $mimeType) { |
|
124 | - $this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0]; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - private function loadCustomDefinitions(string $fileName, array $definitions): array { |
|
129 | - if (file_exists($this->customConfigDir . '/' . $fileName)) { |
|
130 | - $custom = json_decode(file_get_contents($this->customConfigDir . '/' . $fileName), true); |
|
131 | - if (json_last_error() === JSON_ERROR_NONE) { |
|
132 | - $definitions = array_merge($definitions, $custom); |
|
133 | - } else { |
|
134 | - $this->logger->warning('Failed to parse ' . $fileName . ': ' . json_last_error_msg()); |
|
135 | - } |
|
136 | - } |
|
137 | - return $definitions; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Add the mimetype aliases if they are not yet present |
|
142 | - */ |
|
143 | - private function loadAliases(): void { |
|
144 | - if (!empty($this->mimeTypeAlias)) { |
|
145 | - return; |
|
146 | - } |
|
147 | - |
|
148 | - $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
|
149 | - $this->mimeTypeAlias = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEALIASES, $this->mimeTypeAlias); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @return string[] |
|
154 | - */ |
|
155 | - public function getAllAliases(): array { |
|
156 | - $this->loadAliases(); |
|
157 | - return $this->mimeTypeAlias; |
|
158 | - } |
|
159 | - |
|
160 | - public function getOnlyDefaultAliases(): array { |
|
161 | - $this->loadMappings(); |
|
162 | - $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
|
163 | - return $this->mimeTypeAlias; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Add mimetype mappings if they are not yet present |
|
168 | - */ |
|
169 | - private function loadMappings(): void { |
|
170 | - if (!empty($this->mimetypes)) { |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - $mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true); |
|
175 | - $mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimetypeMapping); |
|
176 | - |
|
177 | - $this->registerTypeArray($mimetypeMapping); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return array |
|
182 | - */ |
|
183 | - public function getAllMappings(): array { |
|
184 | - $this->loadMappings(); |
|
185 | - return $this->mimetypes; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * detect mimetype only based on filename, content of file is not used |
|
190 | - * |
|
191 | - * @param string $path |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - public function detectPath($path): string { |
|
195 | - $this->loadMappings(); |
|
196 | - |
|
197 | - $fileName = basename($path); |
|
198 | - |
|
199 | - // remove leading dot on hidden files with a file extension |
|
200 | - $fileName = ltrim($fileName, '.'); |
|
201 | - |
|
202 | - // note: leading dot doesn't qualify as extension |
|
203 | - if (strpos($fileName, '.') > 0) { |
|
204 | - |
|
205 | - // remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part |
|
206 | - $fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName); |
|
207 | - |
|
208 | - //try to guess the type by the file extension |
|
209 | - $extension = strrchr($fileName, '.'); |
|
210 | - if ($extension !== false) { |
|
211 | - $extension = strtolower($extension); |
|
212 | - $extension = substr($extension, 1); //remove leading . |
|
213 | - return $this->mimetypes[$extension][0] ?? 'application/octet-stream'; |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - return 'application/octet-stream'; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * detect mimetype only based on the content of file |
|
222 | - * @param string $path |
|
223 | - * @return string |
|
224 | - * @since 18.0.0 |
|
225 | - */ |
|
226 | - public function detectContent(string $path): string { |
|
227 | - $this->loadMappings(); |
|
228 | - |
|
229 | - if (@is_dir($path)) { |
|
230 | - // directories are easy |
|
231 | - return 'httpd/unix-directory'; |
|
232 | - } |
|
233 | - |
|
234 | - if (function_exists('finfo_open') |
|
235 | - && function_exists('finfo_file') |
|
236 | - && $finfo = finfo_open(FILEINFO_MIME)) { |
|
237 | - $info = @finfo_file($finfo, $path); |
|
238 | - finfo_close($finfo); |
|
239 | - if ($info) { |
|
240 | - $info = strtolower($info); |
|
241 | - $mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info; |
|
242 | - $mimeType = $this->getSecureMimeType($mimeType); |
|
243 | - if ($mimeType !== 'application/octet-stream') { |
|
244 | - return $mimeType; |
|
245 | - } |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) { |
|
250 | - // Is the file wrapped in a stream? |
|
251 | - return 'application/octet-stream'; |
|
252 | - } |
|
253 | - |
|
254 | - if (function_exists('mime_content_type')) { |
|
255 | - // use mime magic extension if available |
|
256 | - $mimeType = mime_content_type($path); |
|
257 | - if ($mimeType !== false) { |
|
258 | - $mimeType = $this->getSecureMimeType($mimeType); |
|
259 | - if ($mimeType !== 'application/octet-stream') { |
|
260 | - return $mimeType; |
|
261 | - } |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - if (\OC_Helper::canExecute('file')) { |
|
266 | - // it looks like we have a 'file' command, |
|
267 | - // lets see if it does have mime support |
|
268 | - $path = escapeshellarg($path); |
|
269 | - $fp = popen("test -f $path && file -b --mime-type $path", 'r'); |
|
270 | - $mimeType = fgets($fp); |
|
271 | - pclose($fp); |
|
272 | - |
|
273 | - if ($mimeType !== false) { |
|
274 | - //trim the newline |
|
275 | - $mimeType = trim($mimeType); |
|
276 | - $mimeType = $this->getSecureMimeType($mimeType); |
|
277 | - if ($mimeType !== 'application/octet-stream') { |
|
278 | - return $mimeType; |
|
279 | - } |
|
280 | - } |
|
281 | - } |
|
282 | - return 'application/octet-stream'; |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * detect mimetype based on both filename and content |
|
287 | - * |
|
288 | - * @param string $path |
|
289 | - * @return string |
|
290 | - */ |
|
291 | - public function detect($path): string { |
|
292 | - $mimeType = $this->detectPath($path); |
|
293 | - |
|
294 | - if ($mimeType !== 'application/octet-stream') { |
|
295 | - return $mimeType; |
|
296 | - } |
|
297 | - |
|
298 | - return $this->detectContent($path); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * detect mimetype based on the content of a string |
|
303 | - * |
|
304 | - * @param string $data |
|
305 | - * @return string |
|
306 | - */ |
|
307 | - public function detectString($data): string { |
|
308 | - if (function_exists('finfo_open') && function_exists('finfo_file')) { |
|
309 | - $finfo = finfo_open(FILEINFO_MIME); |
|
310 | - $info = finfo_buffer($finfo, $data); |
|
311 | - return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info; |
|
312 | - } |
|
313 | - |
|
314 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile(); |
|
315 | - $fh = fopen($tmpFile, 'wb'); |
|
316 | - fwrite($fh, $data, 8024); |
|
317 | - fclose($fh); |
|
318 | - $mime = $this->detect($tmpFile); |
|
319 | - unset($tmpFile); |
|
320 | - return $mime; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Get a secure mimetype that won't expose potential XSS. |
|
325 | - * |
|
326 | - * @param string $mimeType |
|
327 | - * @return string |
|
328 | - */ |
|
329 | - public function getSecureMimeType($mimeType): string { |
|
330 | - $this->loadMappings(); |
|
331 | - |
|
332 | - return $this->secureMimeTypes[$mimeType] ?? 'application/octet-stream'; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Get path to the icon of a file type |
|
337 | - * @param string $mimetype the MIME type |
|
338 | - * @return string the url |
|
339 | - */ |
|
340 | - public function mimeTypeIcon($mimetype): string { |
|
341 | - $this->loadAliases(); |
|
342 | - |
|
343 | - while (isset($this->mimeTypeAlias[$mimetype])) { |
|
344 | - $mimetype = $this->mimeTypeAlias[$mimetype]; |
|
345 | - } |
|
346 | - if (isset($this->mimetypeIcons[$mimetype])) { |
|
347 | - return $this->mimetypeIcons[$mimetype]; |
|
348 | - } |
|
349 | - |
|
350 | - // Replace slash and backslash with a minus |
|
351 | - $icon = str_replace(['/', '\\'], '-', $mimetype); |
|
352 | - |
|
353 | - // Is it a dir? |
|
354 | - if ($mimetype === 'dir') { |
|
355 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.svg'); |
|
356 | - return $this->mimetypeIcons[$mimetype]; |
|
357 | - } |
|
358 | - if ($mimetype === 'dir-shared') { |
|
359 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.svg'); |
|
360 | - return $this->mimetypeIcons[$mimetype]; |
|
361 | - } |
|
362 | - if ($mimetype === 'dir-external') { |
|
363 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.svg'); |
|
364 | - return $this->mimetypeIcons[$mimetype]; |
|
365 | - } |
|
366 | - |
|
367 | - // Icon exists? |
|
368 | - try { |
|
369 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.svg'); |
|
370 | - return $this->mimetypeIcons[$mimetype]; |
|
371 | - } catch (\RuntimeException $e) { |
|
372 | - // Specified image not found |
|
373 | - } |
|
374 | - |
|
375 | - // Try only the first part of the filetype |
|
376 | - |
|
377 | - if (strpos($icon, '-')) { |
|
378 | - $mimePart = substr($icon, 0, strpos($icon, '-')); |
|
379 | - try { |
|
380 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.svg'); |
|
381 | - return $this->mimetypeIcons[$mimetype]; |
|
382 | - } catch (\RuntimeException $e) { |
|
383 | - // Image for the first part of the mimetype not found |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/file.svg'); |
|
388 | - return $this->mimetypeIcons[$mimetype]; |
|
389 | - } |
|
54 | + private const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json'; |
|
55 | + private const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json'; |
|
56 | + |
|
57 | + protected $mimetypes = []; |
|
58 | + protected $secureMimeTypes = []; |
|
59 | + |
|
60 | + protected $mimetypeIcons = []; |
|
61 | + /** @var string[] */ |
|
62 | + protected $mimeTypeAlias = []; |
|
63 | + |
|
64 | + /** @var IURLGenerator */ |
|
65 | + private $urlGenerator; |
|
66 | + |
|
67 | + /** @var ILogger */ |
|
68 | + private $logger; |
|
69 | + |
|
70 | + /** @var string */ |
|
71 | + private $customConfigDir; |
|
72 | + |
|
73 | + /** @var string */ |
|
74 | + private $defaultConfigDir; |
|
75 | + |
|
76 | + /** |
|
77 | + * @param IURLGenerator $urlGenerator |
|
78 | + * @param ILogger $logger |
|
79 | + * @param string $customConfigDir |
|
80 | + * @param string $defaultConfigDir |
|
81 | + */ |
|
82 | + public function __construct(IURLGenerator $urlGenerator, |
|
83 | + ILogger $logger, |
|
84 | + string $customConfigDir, |
|
85 | + string $defaultConfigDir) { |
|
86 | + $this->urlGenerator = $urlGenerator; |
|
87 | + $this->logger = $logger; |
|
88 | + $this->customConfigDir = $customConfigDir; |
|
89 | + $this->defaultConfigDir = $defaultConfigDir; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Add an extension -> mimetype mapping |
|
94 | + * |
|
95 | + * $mimetype is the assumed correct mime type |
|
96 | + * The optional $secureMimeType is an alternative to send to send |
|
97 | + * to avoid potential XSS. |
|
98 | + * |
|
99 | + * @param string $extension |
|
100 | + * @param string $mimetype |
|
101 | + * @param string|null $secureMimeType |
|
102 | + */ |
|
103 | + public function registerType(string $extension, |
|
104 | + string $mimetype, |
|
105 | + ?string $secureMimeType = null): void { |
|
106 | + $this->mimetypes[$extension] = [$mimetype, $secureMimeType]; |
|
107 | + $this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Add an array of extension -> mimetype mappings |
|
112 | + * |
|
113 | + * The mimetype value is in itself an array where the first index is |
|
114 | + * the assumed correct mimetype and the second is either a secure alternative |
|
115 | + * or null if the correct is considered secure. |
|
116 | + * |
|
117 | + * @param array $types |
|
118 | + */ |
|
119 | + public function registerTypeArray(array $types): void { |
|
120 | + $this->mimetypes = array_merge($this->mimetypes, $types); |
|
121 | + |
|
122 | + // Update the alternative mimetypes to avoid having to look them up each time. |
|
123 | + foreach ($this->mimetypes as $mimeType) { |
|
124 | + $this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0]; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + private function loadCustomDefinitions(string $fileName, array $definitions): array { |
|
129 | + if (file_exists($this->customConfigDir . '/' . $fileName)) { |
|
130 | + $custom = json_decode(file_get_contents($this->customConfigDir . '/' . $fileName), true); |
|
131 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
132 | + $definitions = array_merge($definitions, $custom); |
|
133 | + } else { |
|
134 | + $this->logger->warning('Failed to parse ' . $fileName . ': ' . json_last_error_msg()); |
|
135 | + } |
|
136 | + } |
|
137 | + return $definitions; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Add the mimetype aliases if they are not yet present |
|
142 | + */ |
|
143 | + private function loadAliases(): void { |
|
144 | + if (!empty($this->mimeTypeAlias)) { |
|
145 | + return; |
|
146 | + } |
|
147 | + |
|
148 | + $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
|
149 | + $this->mimeTypeAlias = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEALIASES, $this->mimeTypeAlias); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @return string[] |
|
154 | + */ |
|
155 | + public function getAllAliases(): array { |
|
156 | + $this->loadAliases(); |
|
157 | + return $this->mimeTypeAlias; |
|
158 | + } |
|
159 | + |
|
160 | + public function getOnlyDefaultAliases(): array { |
|
161 | + $this->loadMappings(); |
|
162 | + $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
|
163 | + return $this->mimeTypeAlias; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Add mimetype mappings if they are not yet present |
|
168 | + */ |
|
169 | + private function loadMappings(): void { |
|
170 | + if (!empty($this->mimetypes)) { |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + $mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true); |
|
175 | + $mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimetypeMapping); |
|
176 | + |
|
177 | + $this->registerTypeArray($mimetypeMapping); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return array |
|
182 | + */ |
|
183 | + public function getAllMappings(): array { |
|
184 | + $this->loadMappings(); |
|
185 | + return $this->mimetypes; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * detect mimetype only based on filename, content of file is not used |
|
190 | + * |
|
191 | + * @param string $path |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + public function detectPath($path): string { |
|
195 | + $this->loadMappings(); |
|
196 | + |
|
197 | + $fileName = basename($path); |
|
198 | + |
|
199 | + // remove leading dot on hidden files with a file extension |
|
200 | + $fileName = ltrim($fileName, '.'); |
|
201 | + |
|
202 | + // note: leading dot doesn't qualify as extension |
|
203 | + if (strpos($fileName, '.') > 0) { |
|
204 | + |
|
205 | + // remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part |
|
206 | + $fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName); |
|
207 | + |
|
208 | + //try to guess the type by the file extension |
|
209 | + $extension = strrchr($fileName, '.'); |
|
210 | + if ($extension !== false) { |
|
211 | + $extension = strtolower($extension); |
|
212 | + $extension = substr($extension, 1); //remove leading . |
|
213 | + return $this->mimetypes[$extension][0] ?? 'application/octet-stream'; |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + return 'application/octet-stream'; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * detect mimetype only based on the content of file |
|
222 | + * @param string $path |
|
223 | + * @return string |
|
224 | + * @since 18.0.0 |
|
225 | + */ |
|
226 | + public function detectContent(string $path): string { |
|
227 | + $this->loadMappings(); |
|
228 | + |
|
229 | + if (@is_dir($path)) { |
|
230 | + // directories are easy |
|
231 | + return 'httpd/unix-directory'; |
|
232 | + } |
|
233 | + |
|
234 | + if (function_exists('finfo_open') |
|
235 | + && function_exists('finfo_file') |
|
236 | + && $finfo = finfo_open(FILEINFO_MIME)) { |
|
237 | + $info = @finfo_file($finfo, $path); |
|
238 | + finfo_close($finfo); |
|
239 | + if ($info) { |
|
240 | + $info = strtolower($info); |
|
241 | + $mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info; |
|
242 | + $mimeType = $this->getSecureMimeType($mimeType); |
|
243 | + if ($mimeType !== 'application/octet-stream') { |
|
244 | + return $mimeType; |
|
245 | + } |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) { |
|
250 | + // Is the file wrapped in a stream? |
|
251 | + return 'application/octet-stream'; |
|
252 | + } |
|
253 | + |
|
254 | + if (function_exists('mime_content_type')) { |
|
255 | + // use mime magic extension if available |
|
256 | + $mimeType = mime_content_type($path); |
|
257 | + if ($mimeType !== false) { |
|
258 | + $mimeType = $this->getSecureMimeType($mimeType); |
|
259 | + if ($mimeType !== 'application/octet-stream') { |
|
260 | + return $mimeType; |
|
261 | + } |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + if (\OC_Helper::canExecute('file')) { |
|
266 | + // it looks like we have a 'file' command, |
|
267 | + // lets see if it does have mime support |
|
268 | + $path = escapeshellarg($path); |
|
269 | + $fp = popen("test -f $path && file -b --mime-type $path", 'r'); |
|
270 | + $mimeType = fgets($fp); |
|
271 | + pclose($fp); |
|
272 | + |
|
273 | + if ($mimeType !== false) { |
|
274 | + //trim the newline |
|
275 | + $mimeType = trim($mimeType); |
|
276 | + $mimeType = $this->getSecureMimeType($mimeType); |
|
277 | + if ($mimeType !== 'application/octet-stream') { |
|
278 | + return $mimeType; |
|
279 | + } |
|
280 | + } |
|
281 | + } |
|
282 | + return 'application/octet-stream'; |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * detect mimetype based on both filename and content |
|
287 | + * |
|
288 | + * @param string $path |
|
289 | + * @return string |
|
290 | + */ |
|
291 | + public function detect($path): string { |
|
292 | + $mimeType = $this->detectPath($path); |
|
293 | + |
|
294 | + if ($mimeType !== 'application/octet-stream') { |
|
295 | + return $mimeType; |
|
296 | + } |
|
297 | + |
|
298 | + return $this->detectContent($path); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * detect mimetype based on the content of a string |
|
303 | + * |
|
304 | + * @param string $data |
|
305 | + * @return string |
|
306 | + */ |
|
307 | + public function detectString($data): string { |
|
308 | + if (function_exists('finfo_open') && function_exists('finfo_file')) { |
|
309 | + $finfo = finfo_open(FILEINFO_MIME); |
|
310 | + $info = finfo_buffer($finfo, $data); |
|
311 | + return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info; |
|
312 | + } |
|
313 | + |
|
314 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile(); |
|
315 | + $fh = fopen($tmpFile, 'wb'); |
|
316 | + fwrite($fh, $data, 8024); |
|
317 | + fclose($fh); |
|
318 | + $mime = $this->detect($tmpFile); |
|
319 | + unset($tmpFile); |
|
320 | + return $mime; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Get a secure mimetype that won't expose potential XSS. |
|
325 | + * |
|
326 | + * @param string $mimeType |
|
327 | + * @return string |
|
328 | + */ |
|
329 | + public function getSecureMimeType($mimeType): string { |
|
330 | + $this->loadMappings(); |
|
331 | + |
|
332 | + return $this->secureMimeTypes[$mimeType] ?? 'application/octet-stream'; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Get path to the icon of a file type |
|
337 | + * @param string $mimetype the MIME type |
|
338 | + * @return string the url |
|
339 | + */ |
|
340 | + public function mimeTypeIcon($mimetype): string { |
|
341 | + $this->loadAliases(); |
|
342 | + |
|
343 | + while (isset($this->mimeTypeAlias[$mimetype])) { |
|
344 | + $mimetype = $this->mimeTypeAlias[$mimetype]; |
|
345 | + } |
|
346 | + if (isset($this->mimetypeIcons[$mimetype])) { |
|
347 | + return $this->mimetypeIcons[$mimetype]; |
|
348 | + } |
|
349 | + |
|
350 | + // Replace slash and backslash with a minus |
|
351 | + $icon = str_replace(['/', '\\'], '-', $mimetype); |
|
352 | + |
|
353 | + // Is it a dir? |
|
354 | + if ($mimetype === 'dir') { |
|
355 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.svg'); |
|
356 | + return $this->mimetypeIcons[$mimetype]; |
|
357 | + } |
|
358 | + if ($mimetype === 'dir-shared') { |
|
359 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.svg'); |
|
360 | + return $this->mimetypeIcons[$mimetype]; |
|
361 | + } |
|
362 | + if ($mimetype === 'dir-external') { |
|
363 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.svg'); |
|
364 | + return $this->mimetypeIcons[$mimetype]; |
|
365 | + } |
|
366 | + |
|
367 | + // Icon exists? |
|
368 | + try { |
|
369 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.svg'); |
|
370 | + return $this->mimetypeIcons[$mimetype]; |
|
371 | + } catch (\RuntimeException $e) { |
|
372 | + // Specified image not found |
|
373 | + } |
|
374 | + |
|
375 | + // Try only the first part of the filetype |
|
376 | + |
|
377 | + if (strpos($icon, '-')) { |
|
378 | + $mimePart = substr($icon, 0, strpos($icon, '-')); |
|
379 | + try { |
|
380 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.svg'); |
|
381 | + return $this->mimetypeIcons[$mimetype]; |
|
382 | + } catch (\RuntimeException $e) { |
|
383 | + // Image for the first part of the mimetype not found |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/file.svg'); |
|
388 | + return $this->mimetypeIcons[$mimetype]; |
|
389 | + } |
|
390 | 390 | } |
@@ -126,12 +126,12 @@ discard block |
||
126 | 126 | } |
127 | 127 | |
128 | 128 | private function loadCustomDefinitions(string $fileName, array $definitions): array { |
129 | - if (file_exists($this->customConfigDir . '/' . $fileName)) { |
|
130 | - $custom = json_decode(file_get_contents($this->customConfigDir . '/' . $fileName), true); |
|
129 | + if (file_exists($this->customConfigDir.'/'.$fileName)) { |
|
130 | + $custom = json_decode(file_get_contents($this->customConfigDir.'/'.$fileName), true); |
|
131 | 131 | if (json_last_error() === JSON_ERROR_NONE) { |
132 | 132 | $definitions = array_merge($definitions, $custom); |
133 | 133 | } else { |
134 | - $this->logger->warning('Failed to parse ' . $fileName . ': ' . json_last_error_msg()); |
|
134 | + $this->logger->warning('Failed to parse '.$fileName.': '.json_last_error_msg()); |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | return $definitions; |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | return; |
146 | 146 | } |
147 | 147 | |
148 | - $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
|
148 | + $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir.'/mimetypealiases.dist.json'), true); |
|
149 | 149 | $this->mimeTypeAlias = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEALIASES, $this->mimeTypeAlias); |
150 | 150 | } |
151 | 151 | |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | |
160 | 160 | public function getOnlyDefaultAliases(): array { |
161 | 161 | $this->loadMappings(); |
162 | - $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
|
162 | + $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir.'/mimetypealiases.dist.json'), true); |
|
163 | 163 | return $this->mimeTypeAlias; |
164 | 164 | } |
165 | 165 | |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | return; |
172 | 172 | } |
173 | 173 | |
174 | - $mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true); |
|
174 | + $mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir.'/mimetypemapping.dist.json'), true); |
|
175 | 175 | $mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimetypeMapping); |
176 | 176 | |
177 | 177 | $this->registerTypeArray($mimetypeMapping); |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | |
367 | 367 | // Icon exists? |
368 | 368 | try { |
369 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.svg'); |
|
369 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/'.$icon.'.svg'); |
|
370 | 370 | return $this->mimetypeIcons[$mimetype]; |
371 | 371 | } catch (\RuntimeException $e) { |
372 | 372 | // Specified image not found |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | if (strpos($icon, '-')) { |
378 | 378 | $mimePart = substr($icon, 0, strpos($icon, '-')); |
379 | 379 | try { |
380 | - $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.svg'); |
|
380 | + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/'.$mimePart.'.svg'); |
|
381 | 381 | return $this->mimetypeIcons[$mimetype]; |
382 | 382 | } catch (\RuntimeException $e) { |
383 | 383 | // Image for the first part of the mimetype not found |