Detection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Andreas Fischer <bantu@owncloud.com>
  7. * @author bladewing <lukas@ifflaender-family.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Hendrik Leppelsack <hendrik@leppelsack.de>
  11. * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch>
  12. * @author Joas Schilling <coding@schilljs.com>
  13. * @author Julius Härtl <jus@bitgrid.net>
  14. * @author lui87kw <lukas.ifflaender@uni-wuerzburg.de>
  15. * @author Lukas Reschke <lukas@statuscode.ch>
  16. * @author Magnus Walbeck <mw@mwalbeck.org>
  17. * @author Morris Jobke <hey@morrisjobke.de>
  18. * @author Robin Appelman <robin@icewind.nl>
  19. * @author Robin McCorkell <robin@mccorkell.me.uk>
  20. * @author Roeland Jago Douma <roeland@famdouma.nl>
  21. * @author Thomas Tanghus <thomas@tanghus.net>
  22. * @author Vincent Petry <vincent@nextcloud.com>
  23. * @author Xheni Myrtaj <myrtajxheni@gmail.com>
  24. *
  25. * @license AGPL-3.0
  26. *
  27. * This code is free software: you can redistribute it and/or modify
  28. * it under the terms of the GNU Affero General Public License, version 3,
  29. * as published by the Free Software Foundation.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Affero General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Affero General Public License, version 3,
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>
  38. *
  39. */
  40. namespace OC\Files\Type;
  41. use OCP\Files\IMimeTypeDetector;
  42. use OCP\IURLGenerator;
  43. use Psr\Log\LoggerInterface;
  44. /**
  45. * Class Detection
  46. *
  47. * Mimetype detection
  48. *
  49. * @package OC\Files\Type
  50. */
  51. class Detection implements IMimeTypeDetector {
  52. private const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
  53. private const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
  54. protected $mimetypes = [];
  55. protected $secureMimeTypes = [];
  56. protected $mimetypeIcons = [];
  57. /** @var string[] */
  58. protected $mimeTypeAlias = [];
  59. /** @var IURLGenerator */
  60. private $urlGenerator;
  61. private LoggerInterface $logger;
  62. /** @var string */
  63. private $customConfigDir;
  64. /** @var string */
  65. private $defaultConfigDir;
  66. public function __construct(IURLGenerator $urlGenerator,
  67. LoggerInterface $logger,
  68. string $customConfigDir,
  69. string $defaultConfigDir) {
  70. $this->urlGenerator = $urlGenerator;
  71. $this->logger = $logger;
  72. $this->customConfigDir = $customConfigDir;
  73. $this->defaultConfigDir = $defaultConfigDir;
  74. }
  75. /**
  76. * Add an extension -> mimetype mapping
  77. *
  78. * $mimetype is the assumed correct mime type
  79. * The optional $secureMimeType is an alternative to send to send
  80. * to avoid potential XSS.
  81. *
  82. * @param string $extension
  83. * @param string $mimetype
  84. * @param string|null $secureMimeType
  85. */
  86. public function registerType(string $extension,
  87. string $mimetype,
  88. ?string $secureMimeType = null): void {
  89. $this->mimetypes[$extension] = [$mimetype, $secureMimeType];
  90. $this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
  91. }
  92. /**
  93. * Add an array of extension -> mimetype mappings
  94. *
  95. * The mimetype value is in itself an array where the first index is
  96. * the assumed correct mimetype and the second is either a secure alternative
  97. * or null if the correct is considered secure.
  98. *
  99. * @param array $types
  100. */
  101. public function registerTypeArray(array $types): void {
  102. $this->mimetypes = array_merge($this->mimetypes, $types);
  103. // Update the alternative mimetypes to avoid having to look them up each time.
  104. foreach ($this->mimetypes as $extension => $mimeType) {
  105. if (str_starts_with($extension, '_comment')) {
  106. continue;
  107. }
  108. $this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0];
  109. if (isset($mimeType[1])) {
  110. $this->secureMimeTypes[$mimeType[1]] = $mimeType[1];
  111. }
  112. }
  113. }
  114. private function loadCustomDefinitions(string $fileName, array $definitions): array {
  115. if (file_exists($this->customConfigDir . '/' . $fileName)) {
  116. $custom = json_decode(file_get_contents($this->customConfigDir . '/' . $fileName), true);
  117. if (json_last_error() === JSON_ERROR_NONE) {
  118. $definitions = array_merge($definitions, $custom);
  119. } else {
  120. $this->logger->warning('Failed to parse ' . $fileName . ': ' . json_last_error_msg());
  121. }
  122. }
  123. return $definitions;
  124. }
  125. /**
  126. * Add the mimetype aliases if they are not yet present
  127. */
  128. private function loadAliases(): void {
  129. if (!empty($this->mimeTypeAlias)) {
  130. return;
  131. }
  132. $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
  133. $this->mimeTypeAlias = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEALIASES, $this->mimeTypeAlias);
  134. }
  135. /**
  136. * @return string[]
  137. */
  138. public function getAllAliases(): array {
  139. $this->loadAliases();
  140. return $this->mimeTypeAlias;
  141. }
  142. public function getOnlyDefaultAliases(): array {
  143. $this->loadMappings();
  144. $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
  145. return $this->mimeTypeAlias;
  146. }
  147. /**
  148. * Add mimetype mappings if they are not yet present
  149. */
  150. private function loadMappings(): void {
  151. if (!empty($this->mimetypes)) {
  152. return;
  153. }
  154. $mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
  155. $mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimetypeMapping);
  156. $this->registerTypeArray($mimetypeMapping);
  157. }
  158. /**
  159. * @return array
  160. */
  161. public function getAllMappings(): array {
  162. $this->loadMappings();
  163. return $this->mimetypes;
  164. }
  165. /**
  166. * detect mimetype only based on filename, content of file is not used
  167. *
  168. * @param string $path
  169. * @return string
  170. */
  171. public function detectPath($path): string {
  172. $this->loadMappings();
  173. $fileName = basename($path);
  174. // remove leading dot on hidden files with a file extension
  175. $fileName = ltrim($fileName, '.');
  176. // note: leading dot doesn't qualify as extension
  177. if (strpos($fileName, '.') > 0) {
  178. // remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part
  179. $fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName);
  180. //try to guess the type by the file extension
  181. $extension = strrchr($fileName, '.');
  182. if ($extension !== false) {
  183. $extension = strtolower($extension);
  184. $extension = substr($extension, 1); //remove leading .
  185. return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
  186. }
  187. }
  188. return 'application/octet-stream';
  189. }
  190. /**
  191. * detect mimetype only based on the content of file
  192. * @param string $path
  193. * @return string
  194. * @since 18.0.0
  195. */
  196. public function detectContent(string $path): string {
  197. $this->loadMappings();
  198. if (@is_dir($path)) {
  199. // directories are easy
  200. return 'httpd/unix-directory';
  201. }
  202. if (function_exists('finfo_open')
  203. && function_exists('finfo_file')
  204. && $finfo = finfo_open(FILEINFO_MIME)) {
  205. $info = @finfo_file($finfo, $path);
  206. finfo_close($finfo);
  207. if ($info) {
  208. $info = strtolower($info);
  209. $mimeType = str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info;
  210. $mimeType = $this->getSecureMimeType($mimeType);
  211. if ($mimeType !== 'application/octet-stream') {
  212. return $mimeType;
  213. }
  214. }
  215. }
  216. if (str_starts_with($path, 'file://')) {
  217. // Is the file wrapped in a stream?
  218. return 'application/octet-stream';
  219. }
  220. if (function_exists('mime_content_type')) {
  221. // use mime magic extension if available
  222. $mimeType = mime_content_type($path);
  223. if ($mimeType !== false) {
  224. $mimeType = $this->getSecureMimeType($mimeType);
  225. if ($mimeType !== 'application/octet-stream') {
  226. return $mimeType;
  227. }
  228. }
  229. }
  230. if (\OC_Helper::canExecute('file')) {
  231. // it looks like we have a 'file' command,
  232. // lets see if it does have mime support
  233. $path = escapeshellarg($path);
  234. $fp = popen("test -f $path && file -b --mime-type $path", 'r');
  235. $mimeType = fgets($fp);
  236. pclose($fp);
  237. if ($mimeType !== false) {
  238. //trim the newline
  239. $mimeType = trim($mimeType);
  240. $mimeType = $this->getSecureMimeType($mimeType);
  241. if ($mimeType !== 'application/octet-stream') {
  242. return $mimeType;
  243. }
  244. }
  245. }
  246. return 'application/octet-stream';
  247. }
  248. /**
  249. * detect mimetype based on both filename and content
  250. *
  251. * @param string $path
  252. * @return string
  253. */
  254. public function detect($path): string {
  255. $mimeType = $this->detectPath($path);
  256. if ($mimeType !== 'application/octet-stream') {
  257. return $mimeType;
  258. }
  259. return $this->detectContent($path);
  260. }
  261. /**
  262. * detect mimetype based on the content of a string
  263. *
  264. * @param string $data
  265. * @return string
  266. */
  267. public function detectString($data): string {
  268. if (function_exists('finfo_open') && function_exists('finfo_file')) {
  269. $finfo = finfo_open(FILEINFO_MIME);
  270. $info = finfo_buffer($finfo, $data);
  271. return str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info;
  272. }
  273. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
  274. $fh = fopen($tmpFile, 'wb');
  275. fwrite($fh, $data, 8024);
  276. fclose($fh);
  277. $mime = $this->detect($tmpFile);
  278. unset($tmpFile);
  279. return $mime;
  280. }
  281. /**
  282. * Get a secure mimetype that won't expose potential XSS.
  283. *
  284. * @param string $mimeType
  285. * @return string
  286. */
  287. public function getSecureMimeType($mimeType): string {
  288. $this->loadMappings();
  289. return $this->secureMimeTypes[$mimeType] ?? 'application/octet-stream';
  290. }
  291. /**
  292. * Get path to the icon of a file type
  293. * @param string $mimetype the MIME type
  294. * @return string the url
  295. */
  296. public function mimeTypeIcon($mimetype): string {
  297. $this->loadAliases();
  298. while (isset($this->mimeTypeAlias[$mimetype])) {
  299. $mimetype = $this->mimeTypeAlias[$mimetype];
  300. }
  301. if (isset($this->mimetypeIcons[$mimetype])) {
  302. return $this->mimetypeIcons[$mimetype];
  303. }
  304. // Replace slash and backslash with a minus
  305. $icon = str_replace(['/', '\\'], '-', $mimetype);
  306. // Is it a dir?
  307. if ($mimetype === 'dir') {
  308. $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.svg');
  309. return $this->mimetypeIcons[$mimetype];
  310. }
  311. if ($mimetype === 'dir-shared') {
  312. $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.svg');
  313. return $this->mimetypeIcons[$mimetype];
  314. }
  315. if ($mimetype === 'dir-external') {
  316. $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.svg');
  317. return $this->mimetypeIcons[$mimetype];
  318. }
  319. // Icon exists?
  320. try {
  321. $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.svg');
  322. return $this->mimetypeIcons[$mimetype];
  323. } catch (\RuntimeException $e) {
  324. // Specified image not found
  325. }
  326. // Try only the first part of the filetype
  327. if (strpos($icon, '-')) {
  328. $mimePart = substr($icon, 0, strpos($icon, '-'));
  329. try {
  330. $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.svg');
  331. return $this->mimetypeIcons[$mimetype];
  332. } catch (\RuntimeException $e) {
  333. // Image for the first part of the mimetype not found
  334. }
  335. }
  336. $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/file.svg');
  337. return $this->mimetypeIcons[$mimetype];
  338. }
  339. }