Encoding.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author J0WI <J0WI@users.noreply.github.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Storage\Wrapper;
  30. use OCP\Cache\CappedMemoryCache;
  31. use OC\Files\Filesystem;
  32. use OCP\Files\Storage\IStorage;
  33. use OCP\ICache;
  34. /**
  35. * Encoding wrapper that deals with file names that use unsupported encodings like NFD.
  36. *
  37. * When applied and a UTF-8 path name was given, the wrapper will first attempt to access
  38. * the actual given name and then try its NFD form.
  39. */
  40. class Encoding extends Wrapper {
  41. /**
  42. * @var ICache
  43. */
  44. private $namesCache;
  45. /**
  46. * @param array $parameters
  47. */
  48. public function __construct($parameters) {
  49. $this->storage = $parameters['storage'];
  50. $this->namesCache = new CappedMemoryCache();
  51. }
  52. /**
  53. * Returns whether the given string is only made of ASCII characters
  54. *
  55. * @param string $str string
  56. *
  57. * @return bool true if the string is all ASCII, false otherwise
  58. */
  59. private function isAscii($str) {
  60. return !preg_match('/[\\x80-\\xff]+/', $str);
  61. }
  62. /**
  63. * Checks whether the given path exists in NFC or NFD form after checking
  64. * each form for each path section and returns the correct form.
  65. * If no existing path found, returns the path as it was given.
  66. *
  67. * @param string $fullPath path to check
  68. *
  69. * @return string original or converted path
  70. */
  71. private function findPathToUse($fullPath) {
  72. $cachedPath = $this->namesCache[$fullPath];
  73. if ($cachedPath !== null) {
  74. return $cachedPath;
  75. }
  76. $sections = explode('/', $fullPath);
  77. $path = '';
  78. foreach ($sections as $section) {
  79. $convertedPath = $this->findPathToUseLastSection($path, $section);
  80. if ($convertedPath === null) {
  81. // no point in continuing if the section was not found, use original path
  82. return $fullPath;
  83. }
  84. $path = $convertedPath . '/';
  85. }
  86. $path = rtrim($path, '/');
  87. return $path;
  88. }
  89. /**
  90. * Checks whether the last path section of the given path exists in NFC or NFD form
  91. * and returns the correct form. If no existing path found, returns null.
  92. *
  93. * @param string $basePath base path to check
  94. * @param string $lastSection last section of the path to check for NFD/NFC variations
  95. *
  96. * @return string|null original or converted path, or null if none of the forms was found
  97. */
  98. private function findPathToUseLastSection($basePath, $lastSection) {
  99. $fullPath = $basePath . $lastSection;
  100. if ($lastSection === '' || $this->isAscii($lastSection) || $this->storage->file_exists($fullPath)) {
  101. $this->namesCache[$fullPath] = $fullPath;
  102. return $fullPath;
  103. }
  104. // swap encoding
  105. if (\Normalizer::isNormalized($lastSection, \Normalizer::FORM_C)) {
  106. $otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_D);
  107. } else {
  108. $otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_C);
  109. }
  110. $otherFullPath = $basePath . $otherFormPath;
  111. if ($this->storage->file_exists($otherFullPath)) {
  112. $this->namesCache[$fullPath] = $otherFullPath;
  113. return $otherFullPath;
  114. }
  115. // return original path, file did not exist at all
  116. $this->namesCache[$fullPath] = $fullPath;
  117. return null;
  118. }
  119. /**
  120. * see https://www.php.net/manual/en/function.mkdir.php
  121. *
  122. * @param string $path
  123. * @return bool
  124. */
  125. public function mkdir($path) {
  126. // note: no conversion here, method should not be called with non-NFC names!
  127. $result = $this->storage->mkdir($path);
  128. if ($result) {
  129. $this->namesCache[$path] = $path;
  130. }
  131. return $result;
  132. }
  133. /**
  134. * see https://www.php.net/manual/en/function.rmdir.php
  135. *
  136. * @param string $path
  137. * @return bool
  138. */
  139. public function rmdir($path) {
  140. $result = $this->storage->rmdir($this->findPathToUse($path));
  141. if ($result) {
  142. unset($this->namesCache[$path]);
  143. }
  144. return $result;
  145. }
  146. /**
  147. * see https://www.php.net/manual/en/function.opendir.php
  148. *
  149. * @param string $path
  150. * @return resource|false
  151. */
  152. public function opendir($path) {
  153. $handle = $this->storage->opendir($this->findPathToUse($path));
  154. return EncodingDirectoryWrapper::wrap($handle);
  155. }
  156. /**
  157. * see https://www.php.net/manual/en/function.is_dir.php
  158. *
  159. * @param string $path
  160. * @return bool
  161. */
  162. public function is_dir($path) {
  163. return $this->storage->is_dir($this->findPathToUse($path));
  164. }
  165. /**
  166. * see https://www.php.net/manual/en/function.is_file.php
  167. *
  168. * @param string $path
  169. * @return bool
  170. */
  171. public function is_file($path) {
  172. return $this->storage->is_file($this->findPathToUse($path));
  173. }
  174. /**
  175. * see https://www.php.net/manual/en/function.stat.php
  176. * only the following keys are required in the result: size and mtime
  177. *
  178. * @param string $path
  179. * @return array|bool
  180. */
  181. public function stat($path) {
  182. return $this->storage->stat($this->findPathToUse($path));
  183. }
  184. /**
  185. * see https://www.php.net/manual/en/function.filetype.php
  186. *
  187. * @param string $path
  188. * @return string|bool
  189. */
  190. public function filetype($path) {
  191. return $this->storage->filetype($this->findPathToUse($path));
  192. }
  193. /**
  194. * see https://www.php.net/manual/en/function.filesize.php
  195. * The result for filesize when called on a folder is required to be 0
  196. */
  197. public function filesize($path): false|int|float {
  198. return $this->storage->filesize($this->findPathToUse($path));
  199. }
  200. /**
  201. * check if a file can be created in $path
  202. *
  203. * @param string $path
  204. * @return bool
  205. */
  206. public function isCreatable($path) {
  207. return $this->storage->isCreatable($this->findPathToUse($path));
  208. }
  209. /**
  210. * check if a file can be read
  211. *
  212. * @param string $path
  213. * @return bool
  214. */
  215. public function isReadable($path) {
  216. return $this->storage->isReadable($this->findPathToUse($path));
  217. }
  218. /**
  219. * check if a file can be written to
  220. *
  221. * @param string $path
  222. * @return bool
  223. */
  224. public function isUpdatable($path) {
  225. return $this->storage->isUpdatable($this->findPathToUse($path));
  226. }
  227. /**
  228. * check if a file can be deleted
  229. *
  230. * @param string $path
  231. * @return bool
  232. */
  233. public function isDeletable($path) {
  234. return $this->storage->isDeletable($this->findPathToUse($path));
  235. }
  236. /**
  237. * check if a file can be shared
  238. *
  239. * @param string $path
  240. * @return bool
  241. */
  242. public function isSharable($path) {
  243. return $this->storage->isSharable($this->findPathToUse($path));
  244. }
  245. /**
  246. * get the full permissions of a path.
  247. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  248. *
  249. * @param string $path
  250. * @return int
  251. */
  252. public function getPermissions($path) {
  253. return $this->storage->getPermissions($this->findPathToUse($path));
  254. }
  255. /**
  256. * see https://www.php.net/manual/en/function.file_exists.php
  257. *
  258. * @param string $path
  259. * @return bool
  260. */
  261. public function file_exists($path) {
  262. return $this->storage->file_exists($this->findPathToUse($path));
  263. }
  264. /**
  265. * see https://www.php.net/manual/en/function.filemtime.php
  266. *
  267. * @param string $path
  268. * @return int|bool
  269. */
  270. public function filemtime($path) {
  271. return $this->storage->filemtime($this->findPathToUse($path));
  272. }
  273. /**
  274. * see https://www.php.net/manual/en/function.file_get_contents.php
  275. *
  276. * @param string $path
  277. * @return string|false
  278. */
  279. public function file_get_contents($path) {
  280. return $this->storage->file_get_contents($this->findPathToUse($path));
  281. }
  282. /**
  283. * see https://www.php.net/manual/en/function.file_put_contents.php
  284. *
  285. * @param string $path
  286. * @param mixed $data
  287. * @return int|float|false
  288. */
  289. public function file_put_contents($path, $data) {
  290. return $this->storage->file_put_contents($this->findPathToUse($path), $data);
  291. }
  292. /**
  293. * see https://www.php.net/manual/en/function.unlink.php
  294. *
  295. * @param string $path
  296. * @return bool
  297. */
  298. public function unlink($path) {
  299. $result = $this->storage->unlink($this->findPathToUse($path));
  300. if ($result) {
  301. unset($this->namesCache[$path]);
  302. }
  303. return $result;
  304. }
  305. /**
  306. * see https://www.php.net/manual/en/function.rename.php
  307. *
  308. * @param string $source
  309. * @param string $target
  310. * @return bool
  311. */
  312. public function rename($source, $target) {
  313. // second name always NFC
  314. return $this->storage->rename($this->findPathToUse($source), $this->findPathToUse($target));
  315. }
  316. /**
  317. * see https://www.php.net/manual/en/function.copy.php
  318. *
  319. * @param string $source
  320. * @param string $target
  321. * @return bool
  322. */
  323. public function copy($source, $target) {
  324. return $this->storage->copy($this->findPathToUse($source), $this->findPathToUse($target));
  325. }
  326. /**
  327. * see https://www.php.net/manual/en/function.fopen.php
  328. *
  329. * @param string $path
  330. * @param string $mode
  331. * @return resource|bool
  332. */
  333. public function fopen($path, $mode) {
  334. $result = $this->storage->fopen($this->findPathToUse($path), $mode);
  335. if ($result && $mode !== 'r' && $mode !== 'rb') {
  336. unset($this->namesCache[$path]);
  337. }
  338. return $result;
  339. }
  340. /**
  341. * get the mimetype for a file or folder
  342. * The mimetype for a folder is required to be "httpd/unix-directory"
  343. *
  344. * @param string $path
  345. * @return string|bool
  346. */
  347. public function getMimeType($path) {
  348. return $this->storage->getMimeType($this->findPathToUse($path));
  349. }
  350. /**
  351. * see https://www.php.net/manual/en/function.hash.php
  352. *
  353. * @param string $type
  354. * @param string $path
  355. * @param bool $raw
  356. * @return string|bool
  357. */
  358. public function hash($type, $path, $raw = false) {
  359. return $this->storage->hash($type, $this->findPathToUse($path), $raw);
  360. }
  361. /**
  362. * see https://www.php.net/manual/en/function.free_space.php
  363. *
  364. * @param string $path
  365. * @return int|float|bool
  366. */
  367. public function free_space($path) {
  368. return $this->storage->free_space($this->findPathToUse($path));
  369. }
  370. /**
  371. * search for occurrences of $query in file names
  372. *
  373. * @param string $query
  374. * @return array|bool
  375. */
  376. public function search($query) {
  377. return $this->storage->search($query);
  378. }
  379. /**
  380. * see https://www.php.net/manual/en/function.touch.php
  381. * If the backend does not support the operation, false should be returned
  382. *
  383. * @param string $path
  384. * @param int $mtime
  385. * @return bool
  386. */
  387. public function touch($path, $mtime = null) {
  388. return $this->storage->touch($this->findPathToUse($path), $mtime);
  389. }
  390. /**
  391. * get the path to a local version of the file.
  392. * The local version of the file can be temporary and doesn't have to be persistent across requests
  393. *
  394. * @param string $path
  395. * @return string|false
  396. */
  397. public function getLocalFile($path) {
  398. return $this->storage->getLocalFile($this->findPathToUse($path));
  399. }
  400. /**
  401. * check if a file or folder has been updated since $time
  402. *
  403. * @param string $path
  404. * @param int $time
  405. * @return bool
  406. *
  407. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  408. * returning true for other changes in the folder is optional
  409. */
  410. public function hasUpdated($path, $time) {
  411. return $this->storage->hasUpdated($this->findPathToUse($path), $time);
  412. }
  413. /**
  414. * get a cache instance for the storage
  415. *
  416. * @param string $path
  417. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
  418. * @return \OC\Files\Cache\Cache
  419. */
  420. public function getCache($path = '', $storage = null) {
  421. if (!$storage) {
  422. $storage = $this;
  423. }
  424. return $this->storage->getCache($this->findPathToUse($path), $storage);
  425. }
  426. /**
  427. * get a scanner instance for the storage
  428. *
  429. * @param string $path
  430. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
  431. * @return \OC\Files\Cache\Scanner
  432. */
  433. public function getScanner($path = '', $storage = null) {
  434. if (!$storage) {
  435. $storage = $this;
  436. }
  437. return $this->storage->getScanner($this->findPathToUse($path), $storage);
  438. }
  439. /**
  440. * get the ETag for a file or folder
  441. *
  442. * @param string $path
  443. * @return string|false
  444. */
  445. public function getETag($path) {
  446. return $this->storage->getETag($this->findPathToUse($path));
  447. }
  448. /**
  449. * @param IStorage $sourceStorage
  450. * @param string $sourceInternalPath
  451. * @param string $targetInternalPath
  452. * @return bool
  453. */
  454. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  455. if ($sourceStorage === $this) {
  456. return $this->copy($sourceInternalPath, $this->findPathToUse($targetInternalPath));
  457. }
  458. $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
  459. if ($result) {
  460. unset($this->namesCache[$targetInternalPath]);
  461. }
  462. return $result;
  463. }
  464. /**
  465. * @param IStorage $sourceStorage
  466. * @param string $sourceInternalPath
  467. * @param string $targetInternalPath
  468. * @return bool
  469. */
  470. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  471. if ($sourceStorage === $this) {
  472. $result = $this->rename($sourceInternalPath, $this->findPathToUse($targetInternalPath));
  473. if ($result) {
  474. unset($this->namesCache[$sourceInternalPath]);
  475. unset($this->namesCache[$targetInternalPath]);
  476. }
  477. return $result;
  478. }
  479. $result = $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
  480. if ($result) {
  481. unset($this->namesCache[$sourceInternalPath]);
  482. unset($this->namesCache[$targetInternalPath]);
  483. }
  484. return $result;
  485. }
  486. public function getMetaData($path) {
  487. $entry = $this->storage->getMetaData($this->findPathToUse($path));
  488. $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
  489. return $entry;
  490. }
  491. public function getDirectoryContent($directory): \Traversable {
  492. $entries = $this->storage->getDirectoryContent($this->findPathToUse($directory));
  493. foreach ($entries as $entry) {
  494. $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
  495. yield $entry;
  496. }
  497. }
  498. }