Encoding.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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|bool
  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. * @param string $path
  198. * @return int|bool
  199. */
  200. public function filesize($path) {
  201. return $this->storage->filesize($this->findPathToUse($path));
  202. }
  203. /**
  204. * check if a file can be created in $path
  205. *
  206. * @param string $path
  207. * @return bool
  208. */
  209. public function isCreatable($path) {
  210. return $this->storage->isCreatable($this->findPathToUse($path));
  211. }
  212. /**
  213. * check if a file can be read
  214. *
  215. * @param string $path
  216. * @return bool
  217. */
  218. public function isReadable($path) {
  219. return $this->storage->isReadable($this->findPathToUse($path));
  220. }
  221. /**
  222. * check if a file can be written to
  223. *
  224. * @param string $path
  225. * @return bool
  226. */
  227. public function isUpdatable($path) {
  228. return $this->storage->isUpdatable($this->findPathToUse($path));
  229. }
  230. /**
  231. * check if a file can be deleted
  232. *
  233. * @param string $path
  234. * @return bool
  235. */
  236. public function isDeletable($path) {
  237. return $this->storage->isDeletable($this->findPathToUse($path));
  238. }
  239. /**
  240. * check if a file can be shared
  241. *
  242. * @param string $path
  243. * @return bool
  244. */
  245. public function isSharable($path) {
  246. return $this->storage->isSharable($this->findPathToUse($path));
  247. }
  248. /**
  249. * get the full permissions of a path.
  250. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  251. *
  252. * @param string $path
  253. * @return int
  254. */
  255. public function getPermissions($path) {
  256. return $this->storage->getPermissions($this->findPathToUse($path));
  257. }
  258. /**
  259. * see https://www.php.net/manual/en/function.file_exists.php
  260. *
  261. * @param string $path
  262. * @return bool
  263. */
  264. public function file_exists($path) {
  265. return $this->storage->file_exists($this->findPathToUse($path));
  266. }
  267. /**
  268. * see https://www.php.net/manual/en/function.filemtime.php
  269. *
  270. * @param string $path
  271. * @return int|bool
  272. */
  273. public function filemtime($path) {
  274. return $this->storage->filemtime($this->findPathToUse($path));
  275. }
  276. /**
  277. * see https://www.php.net/manual/en/function.file_get_contents.php
  278. *
  279. * @param string $path
  280. * @return string|bool
  281. */
  282. public function file_get_contents($path) {
  283. return $this->storage->file_get_contents($this->findPathToUse($path));
  284. }
  285. /**
  286. * see https://www.php.net/manual/en/function.file_put_contents.php
  287. *
  288. * @param string $path
  289. * @param mixed $data
  290. * @return int|false
  291. */
  292. public function file_put_contents($path, $data) {
  293. return $this->storage->file_put_contents($this->findPathToUse($path), $data);
  294. }
  295. /**
  296. * see https://www.php.net/manual/en/function.unlink.php
  297. *
  298. * @param string $path
  299. * @return bool
  300. */
  301. public function unlink($path) {
  302. $result = $this->storage->unlink($this->findPathToUse($path));
  303. if ($result) {
  304. unset($this->namesCache[$path]);
  305. }
  306. return $result;
  307. }
  308. /**
  309. * see https://www.php.net/manual/en/function.rename.php
  310. *
  311. * @param string $source
  312. * @param string $target
  313. * @return bool
  314. */
  315. public function rename($source, $target) {
  316. // second name always NFC
  317. return $this->storage->rename($this->findPathToUse($source), $this->findPathToUse($target));
  318. }
  319. /**
  320. * see https://www.php.net/manual/en/function.copy.php
  321. *
  322. * @param string $source
  323. * @param string $target
  324. * @return bool
  325. */
  326. public function copy($source, $target) {
  327. return $this->storage->copy($this->findPathToUse($source), $this->findPathToUse($target));
  328. }
  329. /**
  330. * see https://www.php.net/manual/en/function.fopen.php
  331. *
  332. * @param string $path
  333. * @param string $mode
  334. * @return resource|bool
  335. */
  336. public function fopen($path, $mode) {
  337. $result = $this->storage->fopen($this->findPathToUse($path), $mode);
  338. if ($result && $mode !== 'r' && $mode !== 'rb') {
  339. unset($this->namesCache[$path]);
  340. }
  341. return $result;
  342. }
  343. /**
  344. * get the mimetype for a file or folder
  345. * The mimetype for a folder is required to be "httpd/unix-directory"
  346. *
  347. * @param string $path
  348. * @return string|bool
  349. */
  350. public function getMimeType($path) {
  351. return $this->storage->getMimeType($this->findPathToUse($path));
  352. }
  353. /**
  354. * see https://www.php.net/manual/en/function.hash.php
  355. *
  356. * @param string $type
  357. * @param string $path
  358. * @param bool $raw
  359. * @return string|bool
  360. */
  361. public function hash($type, $path, $raw = false) {
  362. return $this->storage->hash($type, $this->findPathToUse($path), $raw);
  363. }
  364. /**
  365. * see https://www.php.net/manual/en/function.free_space.php
  366. *
  367. * @param string $path
  368. * @return int|bool
  369. */
  370. public function free_space($path) {
  371. return $this->storage->free_space($this->findPathToUse($path));
  372. }
  373. /**
  374. * search for occurrences of $query in file names
  375. *
  376. * @param string $query
  377. * @return array|bool
  378. */
  379. public function search($query) {
  380. return $this->storage->search($query);
  381. }
  382. /**
  383. * see https://www.php.net/manual/en/function.touch.php
  384. * If the backend does not support the operation, false should be returned
  385. *
  386. * @param string $path
  387. * @param int $mtime
  388. * @return bool
  389. */
  390. public function touch($path, $mtime = null) {
  391. return $this->storage->touch($this->findPathToUse($path), $mtime);
  392. }
  393. /**
  394. * get the path to a local version of the file.
  395. * The local version of the file can be temporary and doesn't have to be persistent across requests
  396. *
  397. * @param string $path
  398. * @return string|bool
  399. */
  400. public function getLocalFile($path) {
  401. return $this->storage->getLocalFile($this->findPathToUse($path));
  402. }
  403. /**
  404. * check if a file or folder has been updated since $time
  405. *
  406. * @param string $path
  407. * @param int $time
  408. * @return bool
  409. *
  410. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  411. * returning true for other changes in the folder is optional
  412. */
  413. public function hasUpdated($path, $time) {
  414. return $this->storage->hasUpdated($this->findPathToUse($path), $time);
  415. }
  416. /**
  417. * get a cache instance for the storage
  418. *
  419. * @param string $path
  420. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
  421. * @return \OC\Files\Cache\Cache
  422. */
  423. public function getCache($path = '', $storage = null) {
  424. if (!$storage) {
  425. $storage = $this;
  426. }
  427. return $this->storage->getCache($this->findPathToUse($path), $storage);
  428. }
  429. /**
  430. * get a scanner instance for the storage
  431. *
  432. * @param string $path
  433. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
  434. * @return \OC\Files\Cache\Scanner
  435. */
  436. public function getScanner($path = '', $storage = null) {
  437. if (!$storage) {
  438. $storage = $this;
  439. }
  440. return $this->storage->getScanner($this->findPathToUse($path), $storage);
  441. }
  442. /**
  443. * get the ETag for a file or folder
  444. *
  445. * @param string $path
  446. * @return string|bool
  447. */
  448. public function getETag($path) {
  449. return $this->storage->getETag($this->findPathToUse($path));
  450. }
  451. /**
  452. * @param IStorage $sourceStorage
  453. * @param string $sourceInternalPath
  454. * @param string $targetInternalPath
  455. * @return bool
  456. */
  457. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  458. if ($sourceStorage === $this) {
  459. return $this->copy($sourceInternalPath, $this->findPathToUse($targetInternalPath));
  460. }
  461. $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
  462. if ($result) {
  463. unset($this->namesCache[$targetInternalPath]);
  464. }
  465. return $result;
  466. }
  467. /**
  468. * @param IStorage $sourceStorage
  469. * @param string $sourceInternalPath
  470. * @param string $targetInternalPath
  471. * @return bool
  472. */
  473. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  474. if ($sourceStorage === $this) {
  475. $result = $this->rename($sourceInternalPath, $this->findPathToUse($targetInternalPath));
  476. if ($result) {
  477. unset($this->namesCache[$sourceInternalPath]);
  478. unset($this->namesCache[$targetInternalPath]);
  479. }
  480. return $result;
  481. }
  482. $result = $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
  483. if ($result) {
  484. unset($this->namesCache[$sourceInternalPath]);
  485. unset($this->namesCache[$targetInternalPath]);
  486. }
  487. return $result;
  488. }
  489. public function getMetaData($path) {
  490. $entry = $this->storage->getMetaData($this->findPathToUse($path));
  491. $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
  492. return $entry;
  493. }
  494. public function getDirectoryContent($directory): \Traversable {
  495. $entries = $this->storage->getDirectoryContent($this->findPathToUse($directory));
  496. foreach ($entries as $entry) {
  497. $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
  498. yield $entry;
  499. }
  500. }
  501. }