Encoding.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage\Wrapper;
  8. use OC\Files\Filesystem;
  9. use OCP\Cache\CappedMemoryCache;
  10. use OCP\Files\Cache\IScanner;
  11. use OCP\Files\Storage\IStorage;
  12. use OCP\ICache;
  13. /**
  14. * Encoding wrapper that deals with file names that use unsupported encodings like NFD.
  15. *
  16. * When applied and a UTF-8 path name was given, the wrapper will first attempt to access
  17. * the actual given name and then try its NFD form.
  18. */
  19. class Encoding extends Wrapper {
  20. /**
  21. * @var ICache
  22. */
  23. private $namesCache;
  24. /**
  25. * @param array $parameters
  26. */
  27. public function __construct(array $parameters) {
  28. $this->storage = $parameters['storage'];
  29. $this->namesCache = new CappedMemoryCache();
  30. }
  31. /**
  32. * Returns whether the given string is only made of ASCII characters
  33. */
  34. private function isAscii(string $str): bool {
  35. return !preg_match('/[\\x80-\\xff]+/', $str);
  36. }
  37. /**
  38. * Checks whether the given path exists in NFC or NFD form after checking
  39. * each form for each path section and returns the correct form.
  40. * If no existing path found, returns the path as it was given.
  41. *
  42. * @return string original or converted path
  43. */
  44. private function findPathToUse(string $fullPath): string {
  45. $cachedPath = $this->namesCache[$fullPath];
  46. if ($cachedPath !== null) {
  47. return $cachedPath;
  48. }
  49. $sections = explode('/', $fullPath);
  50. $path = '';
  51. foreach ($sections as $section) {
  52. $convertedPath = $this->findPathToUseLastSection($path, $section);
  53. if ($convertedPath === null) {
  54. // no point in continuing if the section was not found, use original path
  55. return $fullPath;
  56. }
  57. $path = $convertedPath . '/';
  58. }
  59. $path = rtrim($path, '/');
  60. return $path;
  61. }
  62. /**
  63. * Checks whether the last path section of the given path exists in NFC or NFD form
  64. * and returns the correct form. If no existing path found, returns null.
  65. *
  66. * @param string $lastSection last section of the path to check for NFD/NFC variations
  67. *
  68. * @return string|null original or converted path, or null if none of the forms was found
  69. */
  70. private function findPathToUseLastSection(string $basePath, string $lastSection): ?string {
  71. $fullPath = $basePath . $lastSection;
  72. if ($lastSection === '' || $this->isAscii($lastSection) || $this->storage->file_exists($fullPath)) {
  73. $this->namesCache[$fullPath] = $fullPath;
  74. return $fullPath;
  75. }
  76. // swap encoding
  77. if (\Normalizer::isNormalized($lastSection, \Normalizer::FORM_C)) {
  78. $otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_D);
  79. } else {
  80. $otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_C);
  81. }
  82. $otherFullPath = $basePath . $otherFormPath;
  83. if ($this->storage->file_exists($otherFullPath)) {
  84. $this->namesCache[$fullPath] = $otherFullPath;
  85. return $otherFullPath;
  86. }
  87. // return original path, file did not exist at all
  88. $this->namesCache[$fullPath] = $fullPath;
  89. return null;
  90. }
  91. public function mkdir(string $path): bool {
  92. // note: no conversion here, method should not be called with non-NFC names!
  93. $result = $this->storage->mkdir($path);
  94. if ($result) {
  95. $this->namesCache[$path] = $path;
  96. }
  97. return $result;
  98. }
  99. public function rmdir(string $path): bool {
  100. $result = $this->storage->rmdir($this->findPathToUse($path));
  101. if ($result) {
  102. unset($this->namesCache[$path]);
  103. }
  104. return $result;
  105. }
  106. public function opendir(string $path) {
  107. $handle = $this->storage->opendir($this->findPathToUse($path));
  108. return EncodingDirectoryWrapper::wrap($handle);
  109. }
  110. public function is_dir(string $path): bool {
  111. return $this->storage->is_dir($this->findPathToUse($path));
  112. }
  113. public function is_file(string $path): bool {
  114. return $this->storage->is_file($this->findPathToUse($path));
  115. }
  116. public function stat(string $path): array|false {
  117. return $this->storage->stat($this->findPathToUse($path));
  118. }
  119. public function filetype(string $path): string|false {
  120. return $this->storage->filetype($this->findPathToUse($path));
  121. }
  122. public function filesize(string $path): int|float|false {
  123. return $this->storage->filesize($this->findPathToUse($path));
  124. }
  125. public function isCreatable(string $path): bool {
  126. return $this->storage->isCreatable($this->findPathToUse($path));
  127. }
  128. public function isReadable(string $path): bool {
  129. return $this->storage->isReadable($this->findPathToUse($path));
  130. }
  131. public function isUpdatable(string $path): bool {
  132. return $this->storage->isUpdatable($this->findPathToUse($path));
  133. }
  134. public function isDeletable(string $path): bool {
  135. return $this->storage->isDeletable($this->findPathToUse($path));
  136. }
  137. public function isSharable(string $path): bool {
  138. return $this->storage->isSharable($this->findPathToUse($path));
  139. }
  140. public function getPermissions(string $path): int {
  141. return $this->storage->getPermissions($this->findPathToUse($path));
  142. }
  143. public function file_exists(string $path): bool {
  144. return $this->storage->file_exists($this->findPathToUse($path));
  145. }
  146. public function filemtime(string $path): int|false {
  147. return $this->storage->filemtime($this->findPathToUse($path));
  148. }
  149. public function file_get_contents(string $path): string|false {
  150. return $this->storage->file_get_contents($this->findPathToUse($path));
  151. }
  152. public function file_put_contents(string $path, mixed $data): int|float|false {
  153. return $this->storage->file_put_contents($this->findPathToUse($path), $data);
  154. }
  155. public function unlink(string $path): bool {
  156. $result = $this->storage->unlink($this->findPathToUse($path));
  157. if ($result) {
  158. unset($this->namesCache[$path]);
  159. }
  160. return $result;
  161. }
  162. public function rename(string $source, string $target): bool {
  163. // second name always NFC
  164. return $this->storage->rename($this->findPathToUse($source), $this->findPathToUse($target));
  165. }
  166. public function copy(string $source, string $target): bool {
  167. return $this->storage->copy($this->findPathToUse($source), $this->findPathToUse($target));
  168. }
  169. public function fopen(string $path, string $mode) {
  170. $result = $this->storage->fopen($this->findPathToUse($path), $mode);
  171. if ($result && $mode !== 'r' && $mode !== 'rb') {
  172. unset($this->namesCache[$path]);
  173. }
  174. return $result;
  175. }
  176. public function getMimeType(string $path): string|false {
  177. return $this->storage->getMimeType($this->findPathToUse($path));
  178. }
  179. public function hash(string $type, string $path, bool $raw = false): string|false {
  180. return $this->storage->hash($type, $this->findPathToUse($path), $raw);
  181. }
  182. public function free_space(string $path): int|float|false {
  183. return $this->storage->free_space($this->findPathToUse($path));
  184. }
  185. public function touch(string $path, ?int $mtime = null): bool {
  186. return $this->storage->touch($this->findPathToUse($path), $mtime);
  187. }
  188. public function getLocalFile(string $path): string|false {
  189. return $this->storage->getLocalFile($this->findPathToUse($path));
  190. }
  191. public function hasUpdated(string $path, int $time): bool {
  192. return $this->storage->hasUpdated($this->findPathToUse($path), $time);
  193. }
  194. public function getCache(string $path = '', ?IStorage $storage = null): \OCP\Files\Cache\ICache {
  195. if (!$storage) {
  196. $storage = $this;
  197. }
  198. return $this->storage->getCache($this->findPathToUse($path), $storage);
  199. }
  200. public function getScanner(string $path = '', ?IStorage $storage = null): IScanner {
  201. if (!$storage) {
  202. $storage = $this;
  203. }
  204. return $this->storage->getScanner($this->findPathToUse($path), $storage);
  205. }
  206. public function getETag(string $path): string|false {
  207. return $this->storage->getETag($this->findPathToUse($path));
  208. }
  209. public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  210. if ($sourceStorage === $this) {
  211. return $this->copy($sourceInternalPath, $this->findPathToUse($targetInternalPath));
  212. }
  213. $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
  214. if ($result) {
  215. unset($this->namesCache[$targetInternalPath]);
  216. }
  217. return $result;
  218. }
  219. public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  220. if ($sourceStorage === $this) {
  221. $result = $this->rename($sourceInternalPath, $this->findPathToUse($targetInternalPath));
  222. if ($result) {
  223. unset($this->namesCache[$sourceInternalPath]);
  224. unset($this->namesCache[$targetInternalPath]);
  225. }
  226. return $result;
  227. }
  228. $result = $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
  229. if ($result) {
  230. unset($this->namesCache[$sourceInternalPath]);
  231. unset($this->namesCache[$targetInternalPath]);
  232. }
  233. return $result;
  234. }
  235. public function getMetaData(string $path): ?array {
  236. $entry = $this->storage->getMetaData($this->findPathToUse($path));
  237. $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
  238. return $entry;
  239. }
  240. public function getDirectoryContent(string $directory): \Traversable {
  241. $entries = $this->storage->getDirectoryContent($this->findPathToUse($directory));
  242. foreach ($entries as $entry) {
  243. $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
  244. yield $entry;
  245. }
  246. }
  247. }