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.

Encoding.php 14KB

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 Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Storage\Wrapper;
  29. use OC\Cache\CappedMemoryCache;
  30. use OCP\Files\Storage\IStorage;
  31. use OCP\ICache;
  32. /**
  33. * Encoding wrapper that deals with file names that use unsupported encodings like NFD.
  34. *
  35. * When applied and a UTF-8 path name was given, the wrapper will first attempt to access
  36. * the actual given name and then try its NFD form.
  37. */
  38. class Encoding extends Wrapper {
  39. /**
  40. * @var ICache
  41. */
  42. private $namesCache;
  43. /**
  44. * @param array $parameters
  45. */
  46. public function __construct($parameters) {
  47. $this->storage = $parameters['storage'];
  48. $this->namesCache = new CappedMemoryCache();
  49. }
  50. /**
  51. * Returns whether the given string is only made of ASCII characters
  52. *
  53. * @param string $str string
  54. *
  55. * @return bool true if the string is all ASCII, false otherwise
  56. */
  57. private function isAscii($str) {
  58. return (bool) !preg_match('/[\\x80-\\xff]+/', $str);
  59. }
  60. /**
  61. * Checks whether the given path exists in NFC or NFD form after checking
  62. * each form for each path section and returns the correct form.
  63. * If no existing path found, returns the path as it was given.
  64. *
  65. * @param string $fullPath path to check
  66. *
  67. * @return string original or converted path
  68. */
  69. private function findPathToUse($fullPath) {
  70. $cachedPath = $this->namesCache[$fullPath];
  71. if ($cachedPath !== null) {
  72. return $cachedPath;
  73. }
  74. $sections = explode('/', $fullPath);
  75. $path = '';
  76. foreach ($sections as $section) {
  77. $convertedPath = $this->findPathToUseLastSection($path, $section);
  78. if ($convertedPath === null) {
  79. // no point in continuing if the section was not found, use original path
  80. return $fullPath;
  81. }
  82. $path = $convertedPath . '/';
  83. }
  84. $path = rtrim($path, '/');
  85. return $path;
  86. }
  87. /**
  88. * Checks whether the last path section of the given path exists in NFC or NFD form
  89. * and returns the correct form. If no existing path found, returns null.
  90. *
  91. * @param string $basePath base path to check
  92. * @param string $lastSection last section of the path to check for NFD/NFC variations
  93. *
  94. * @return string|null original or converted path, or null if none of the forms was found
  95. */
  96. private function findPathToUseLastSection($basePath, $lastSection) {
  97. $fullPath = $basePath . $lastSection;
  98. if ($lastSection === '' || $this->isAscii($lastSection) || $this->storage->file_exists($fullPath)) {
  99. $this->namesCache[$fullPath] = $fullPath;
  100. return $fullPath;
  101. }
  102. // swap encoding
  103. if (\Normalizer::isNormalized($lastSection, \Normalizer::FORM_C)) {
  104. $otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_D);
  105. } else {
  106. $otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_C);
  107. }
  108. $otherFullPath = $basePath . $otherFormPath;
  109. if ($this->storage->file_exists($otherFullPath)) {
  110. $this->namesCache[$fullPath] = $otherFullPath;
  111. return $otherFullPath;
  112. }
  113. // return original path, file did not exist at all
  114. $this->namesCache[$fullPath] = $fullPath;
  115. return null;
  116. }
  117. /**
  118. * see https://www.php.net/manual/en/function.mkdir.php
  119. *
  120. * @param string $path
  121. * @return bool
  122. */
  123. public function mkdir($path) {
  124. // note: no conversion here, method should not be called with non-NFC names!
  125. $result = $this->storage->mkdir($path);
  126. if ($result) {
  127. $this->namesCache[$path] = $path;
  128. }
  129. return $result;
  130. }
  131. /**
  132. * see https://www.php.net/manual/en/function.rmdir.php
  133. *
  134. * @param string $path
  135. * @return bool
  136. */
  137. public function rmdir($path) {
  138. $result = $this->storage->rmdir($this->findPathToUse($path));
  139. if ($result) {
  140. unset($this->namesCache[$path]);
  141. }
  142. return $result;
  143. }
  144. /**
  145. * see https://www.php.net/manual/en/function.opendir.php
  146. *
  147. * @param string $path
  148. * @return resource|bool
  149. */
  150. public function opendir($path) {
  151. return $this->storage->opendir($this->findPathToUse($path));
  152. }
  153. /**
  154. * see https://www.php.net/manual/en/function.is_dir.php
  155. *
  156. * @param string $path
  157. * @return bool
  158. */
  159. public function is_dir($path) {
  160. return $this->storage->is_dir($this->findPathToUse($path));
  161. }
  162. /**
  163. * see https://www.php.net/manual/en/function.is_file.php
  164. *
  165. * @param string $path
  166. * @return bool
  167. */
  168. public function is_file($path) {
  169. return $this->storage->is_file($this->findPathToUse($path));
  170. }
  171. /**
  172. * see https://www.php.net/manual/en/function.stat.php
  173. * only the following keys are required in the result: size and mtime
  174. *
  175. * @param string $path
  176. * @return array|bool
  177. */
  178. public function stat($path) {
  179. return $this->storage->stat($this->findPathToUse($path));
  180. }
  181. /**
  182. * see https://www.php.net/manual/en/function.filetype.php
  183. *
  184. * @param string $path
  185. * @return string|bool
  186. */
  187. public function filetype($path) {
  188. return $this->storage->filetype($this->findPathToUse($path));
  189. }
  190. /**
  191. * see https://www.php.net/manual/en/function.filesize.php
  192. * The result for filesize when called on a folder is required to be 0
  193. *
  194. * @param string $path
  195. * @return int|bool
  196. */
  197. public function filesize($path) {
  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|bool
  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|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 $path1
  309. * @param string $path2
  310. * @return bool
  311. */
  312. public function rename($path1, $path2) {
  313. // second name always NFC
  314. return $this->storage->rename($this->findPathToUse($path1), $this->findPathToUse($path2));
  315. }
  316. /**
  317. * see https://www.php.net/manual/en/function.copy.php
  318. *
  319. * @param string $path1
  320. * @param string $path2
  321. * @return bool
  322. */
  323. public function copy($path1, $path2) {
  324. return $this->storage->copy($this->findPathToUse($path1), $this->findPathToUse($path2));
  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|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|bool
  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|bool
  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. /**
  487. * @param string $path
  488. * @return array
  489. */
  490. public function getMetaData($path) {
  491. return $this->storage->getMetaData($this->findPathToUse($path));
  492. }
  493. public function getDirectoryContent($directory): \Traversable {
  494. return $this->storage->getDirectoryContent($this->findPathToUse($directory));
  495. }
  496. }