You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileMimeType.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Check;
  22. use OCA\WorkflowEngine\Entity\File;
  23. use OCP\Files\IMimeTypeDetector;
  24. use OCP\Files\Storage\IStorage;
  25. use OCP\IL10N;
  26. use OCP\IRequest;
  27. use OCP\WorkflowEngine\IFileCheck;
  28. class FileMimeType extends AbstractStringCheck implements IFileCheck {
  29. use TFileCheck {
  30. setFileInfo as _setFileInfo;
  31. }
  32. /** @var array */
  33. protected $mimeType;
  34. /** @var IRequest */
  35. protected $request;
  36. /** @var IMimeTypeDetector */
  37. protected $mimeTypeDetector;
  38. /**
  39. * @param IL10N $l
  40. * @param IRequest $request
  41. * @param IMimeTypeDetector $mimeTypeDetector
  42. */
  43. public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
  44. parent::__construct($l);
  45. $this->request = $request;
  46. $this->mimeTypeDetector = $mimeTypeDetector;
  47. }
  48. /**
  49. * @param IStorage $storage
  50. * @param string $path
  51. * @param bool $isDir
  52. */
  53. public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
  54. $this->_setFileInfo($storage, $path, $isDir);
  55. if (!isset($this->mimeType[$this->storage->getId()][$this->path])
  56. || $this->mimeType[$this->storage->getId()][$this->path] === '') {
  57. if ($isDir) {
  58. $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory';
  59. } else {
  60. $this->mimeType[$this->storage->getId()][$this->path] = null;
  61. }
  62. }
  63. }
  64. /**
  65. * The mimetype is only cached if the file exists. Otherwise files access
  66. * control will cache "application/octet-stream" for all the target node on:
  67. * rename, move, copy and all other methods which create a new item
  68. *
  69. * To check this:
  70. * 1. Add an automated tagging rule which tags on mimetype NOT "httpd/unix-directory"
  71. * 2. Add an access control rule which checks for any mimetype
  72. * 3. Create a folder and rename it, the folder should not be tagged, but it is
  73. *
  74. * @param string $storageId
  75. * @param string|null $path
  76. * @param string $mimeType
  77. * @return string
  78. */
  79. protected function cacheAndReturnMimeType(string $storageId, ?string $path, string $mimeType): string {
  80. if ($path !== null && $this->storage->file_exists($path)) {
  81. $this->mimeType[$storageId][$path] = $mimeType;
  82. }
  83. return $mimeType;
  84. }
  85. /**
  86. * Make sure that even though the content based check returns an application/octet-stream can still be checked based on mimetypemappings of their extension
  87. *
  88. * @param string $operator
  89. * @param string $value
  90. * @return bool
  91. */
  92. public function executeCheck($operator, $value) {
  93. $actualValue = $this->getActualValue();
  94. $plainMimetypeResult = $this->executeStringCheck($operator, $value, $actualValue);
  95. if ($actualValue === 'httpd/unix-directory') {
  96. return $plainMimetypeResult;
  97. }
  98. $detectMimetypeBasedOnFilenameResult = $this->executeStringCheck($operator, $value, $this->mimeTypeDetector->detectPath($this->path));
  99. return $plainMimetypeResult || $detectMimetypeBasedOnFilenameResult;
  100. }
  101. /**
  102. * @return string
  103. */
  104. protected function getActualValue() {
  105. if ($this->mimeType[$this->storage->getId()][$this->path] !== null) {
  106. return $this->mimeType[$this->storage->getId()][$this->path];
  107. }
  108. if ($this->storage->is_dir($this->path)) {
  109. return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
  110. }
  111. if ($this->storage->file_exists($this->path)) {
  112. $path = $this->storage->getLocalFile($this->path);
  113. $mimeType = $this->mimeTypeDetector->detectContent($path);
  114. return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
  115. }
  116. if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) {
  117. // Creating a folder
  118. if ($this->request->getMethod() === 'MKCOL') {
  119. return 'httpd/unix-directory';
  120. }
  121. }
  122. // We do not cache this, as the file did not exist yet.
  123. // In case it does in the future, we will check with detectContent()
  124. // again to get the real mimetype of the content, rather than
  125. // guessing it from the path.
  126. return $this->mimeTypeDetector->detectPath($this->path);
  127. }
  128. /**
  129. * @return bool
  130. */
  131. protected function isWebDAVRequest() {
  132. return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
  133. $this->request->getPathInfo() === '/webdav' ||
  134. strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
  135. $this->request->getPathInfo() === '/dav/files' ||
  136. strpos($this->request->getPathInfo(), '/dav/files/') === 0 ||
  137. $this->request->getPathInfo() === '/dav/uploads' ||
  138. strpos($this->request->getPathInfo(), '/dav/uploads/') === 0
  139. );
  140. }
  141. /**
  142. * @return bool
  143. */
  144. protected function isPublicWebDAVRequest() {
  145. return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && (
  146. $this->request->getPathInfo() === '/webdav' ||
  147. strpos($this->request->getPathInfo(), '/webdav/') === 0
  148. );
  149. }
  150. public function supportedEntities(): array {
  151. return [ File::class ];
  152. }
  153. public function isAvailableForScope(int $scope): bool {
  154. return true;
  155. }
  156. }