filesystem.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Florin Peter <github@florin-peter.de>
  9. * @author Georg Ehrke <georg@owncloud.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Robin McCorkell <robin@mccorkell.me.uk>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Sam Tuke <mail@samtuke.com>
  19. * @author Stephan Peijnik <speijnik@anexia-it.com>
  20. * @author Vincent Petry <pvince81@owncloud.com>
  21. *
  22. * @license AGPL-3.0
  23. *
  24. * This code is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License, version 3,
  26. * as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Affero General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Affero General Public License, version 3,
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>
  35. *
  36. */
  37. /**
  38. * Class for abstraction of filesystem functions
  39. * This class won't call any filesystem functions for itself but will pass them to the correct OC_Filestorage object
  40. * this class should also handle all the file permission related stuff
  41. *
  42. * Hooks provided:
  43. * read(path)
  44. * write(path, &run)
  45. * post_write(path)
  46. * create(path, &run) (when a file is created, both create and write will be emitted in that order)
  47. * post_create(path)
  48. * delete(path, &run)
  49. * post_delete(path)
  50. * rename(oldpath,newpath, &run)
  51. * post_rename(oldpath,newpath)
  52. * copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emitted in that order)
  53. * post_rename(oldpath,newpath)
  54. * post_initMountPoints(user, user_dir)
  55. *
  56. * the &run parameter can be set to false to prevent the operation from occurring
  57. */
  58. namespace OC\Files;
  59. use OC\Cache\CappedMemoryCache;
  60. use OC\Files\Config\MountProviderCollection;
  61. use OC\Files\Mount\MountPoint;
  62. use OC\Files\Storage\StorageFactory;
  63. use OCP\Files\Config\IMountProvider;
  64. use OCP\Files\Mount\IMountPoint;
  65. use OCP\Files\NotFoundException;
  66. use OCP\IUserManager;
  67. class Filesystem {
  68. /**
  69. * @var Mount\Manager $mounts
  70. */
  71. private static $mounts;
  72. public static $loaded = false;
  73. /**
  74. * @var \OC\Files\View $defaultInstance
  75. */
  76. static private $defaultInstance;
  77. static private $usersSetup = array();
  78. static private $normalizedPathCache = null;
  79. static private $listeningForProviders = false;
  80. /**
  81. * classname which used for hooks handling
  82. * used as signalclass in OC_Hooks::emit()
  83. */
  84. const CLASSNAME = 'OC_Filesystem';
  85. /**
  86. * signalname emitted before file renaming
  87. *
  88. * @param string $oldpath
  89. * @param string $newpath
  90. */
  91. const signal_rename = 'rename';
  92. /**
  93. * signal emitted after file renaming
  94. *
  95. * @param string $oldpath
  96. * @param string $newpath
  97. */
  98. const signal_post_rename = 'post_rename';
  99. /**
  100. * signal emitted before file/dir creation
  101. *
  102. * @param string $path
  103. * @param bool $run changing this flag to false in hook handler will cancel event
  104. */
  105. const signal_create = 'create';
  106. /**
  107. * signal emitted after file/dir creation
  108. *
  109. * @param string $path
  110. * @param bool $run changing this flag to false in hook handler will cancel event
  111. */
  112. const signal_post_create = 'post_create';
  113. /**
  114. * signal emits before file/dir copy
  115. *
  116. * @param string $oldpath
  117. * @param string $newpath
  118. * @param bool $run changing this flag to false in hook handler will cancel event
  119. */
  120. const signal_copy = 'copy';
  121. /**
  122. * signal emits after file/dir copy
  123. *
  124. * @param string $oldpath
  125. * @param string $newpath
  126. */
  127. const signal_post_copy = 'post_copy';
  128. /**
  129. * signal emits before file/dir save
  130. *
  131. * @param string $path
  132. * @param bool $run changing this flag to false in hook handler will cancel event
  133. */
  134. const signal_write = 'write';
  135. /**
  136. * signal emits after file/dir save
  137. *
  138. * @param string $path
  139. */
  140. const signal_post_write = 'post_write';
  141. /**
  142. * signal emitted before file/dir update
  143. *
  144. * @param string $path
  145. * @param bool $run changing this flag to false in hook handler will cancel event
  146. */
  147. const signal_update = 'update';
  148. /**
  149. * signal emitted after file/dir update
  150. *
  151. * @param string $path
  152. * @param bool $run changing this flag to false in hook handler will cancel event
  153. */
  154. const signal_post_update = 'post_update';
  155. /**
  156. * signal emits when reading file/dir
  157. *
  158. * @param string $path
  159. */
  160. const signal_read = 'read';
  161. /**
  162. * signal emits when removing file/dir
  163. *
  164. * @param string $path
  165. */
  166. const signal_delete = 'delete';
  167. /**
  168. * parameters definitions for signals
  169. */
  170. const signal_param_path = 'path';
  171. const signal_param_oldpath = 'oldpath';
  172. const signal_param_newpath = 'newpath';
  173. /**
  174. * run - changing this flag to false in hook handler will cancel event
  175. */
  176. const signal_param_run = 'run';
  177. const signal_create_mount = 'create_mount';
  178. const signal_delete_mount = 'delete_mount';
  179. const signal_param_mount_type = 'mounttype';
  180. const signal_param_users = 'users';
  181. /**
  182. * @var \OC\Files\Storage\StorageFactory $loader
  183. */
  184. private static $loader;
  185. /**
  186. * @param string $wrapperName
  187. * @param callable $wrapper
  188. * @param int $priority
  189. */
  190. public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) {
  191. $mounts = self::getMountManager()->getAll();
  192. if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) {
  193. // do not re-wrap if storage with this name already existed
  194. return;
  195. }
  196. }
  197. /**
  198. * Returns the storage factory
  199. *
  200. * @return \OCP\Files\Storage\IStorageFactory
  201. */
  202. public static function getLoader() {
  203. if (!self::$loader) {
  204. self::$loader = new StorageFactory();
  205. }
  206. return self::$loader;
  207. }
  208. /**
  209. * Returns the mount manager
  210. *
  211. * @return \OC\Files\Mount\Manager
  212. */
  213. public static function getMountManager($user = '') {
  214. if (!self::$mounts) {
  215. \OC_Util::setupFS($user);
  216. }
  217. return self::$mounts;
  218. }
  219. /**
  220. * get the mountpoint of the storage object for a path
  221. * ( note: because a storage is not always mounted inside the fakeroot, the
  222. * returned mountpoint is relative to the absolute root of the filesystem
  223. * and doesn't take the chroot into account )
  224. *
  225. * @param string $path
  226. * @return string
  227. */
  228. static public function getMountPoint($path) {
  229. if (!self::$mounts) {
  230. \OC_Util::setupFS();
  231. }
  232. $mount = self::$mounts->find($path);
  233. if ($mount) {
  234. return $mount->getMountPoint();
  235. } else {
  236. return '';
  237. }
  238. }
  239. /**
  240. * get a list of all mount points in a directory
  241. *
  242. * @param string $path
  243. * @return string[]
  244. */
  245. static public function getMountPoints($path) {
  246. if (!self::$mounts) {
  247. \OC_Util::setupFS();
  248. }
  249. $result = array();
  250. $mounts = self::$mounts->findIn($path);
  251. foreach ($mounts as $mount) {
  252. $result[] = $mount->getMountPoint();
  253. }
  254. return $result;
  255. }
  256. /**
  257. * get the storage mounted at $mountPoint
  258. *
  259. * @param string $mountPoint
  260. * @return \OC\Files\Storage\Storage
  261. */
  262. public static function getStorage($mountPoint) {
  263. if (!self::$mounts) {
  264. \OC_Util::setupFS();
  265. }
  266. $mount = self::$mounts->find($mountPoint);
  267. return $mount->getStorage();
  268. }
  269. /**
  270. * @param string $id
  271. * @return Mount\MountPoint[]
  272. */
  273. public static function getMountByStorageId($id) {
  274. if (!self::$mounts) {
  275. \OC_Util::setupFS();
  276. }
  277. return self::$mounts->findByStorageId($id);
  278. }
  279. /**
  280. * @param int $id
  281. * @return Mount\MountPoint[]
  282. */
  283. public static function getMountByNumericId($id) {
  284. if (!self::$mounts) {
  285. \OC_Util::setupFS();
  286. }
  287. return self::$mounts->findByNumericId($id);
  288. }
  289. /**
  290. * resolve a path to a storage and internal path
  291. *
  292. * @param string $path
  293. * @return array an array consisting of the storage and the internal path
  294. */
  295. static public function resolvePath($path) {
  296. if (!self::$mounts) {
  297. \OC_Util::setupFS();
  298. }
  299. $mount = self::$mounts->find($path);
  300. if ($mount) {
  301. return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/'));
  302. } else {
  303. return array(null, null);
  304. }
  305. }
  306. static public function init($user, $root) {
  307. if (self::$defaultInstance) {
  308. return false;
  309. }
  310. self::getLoader();
  311. self::$defaultInstance = new View($root);
  312. if (!self::$mounts) {
  313. self::$mounts = \OC::$server->getMountManager();
  314. }
  315. //load custom mount config
  316. self::initMountPoints($user);
  317. self::$loaded = true;
  318. return true;
  319. }
  320. static public function initMountManager() {
  321. if (!self::$mounts) {
  322. self::$mounts = \OC::$server->getMountManager();
  323. }
  324. }
  325. /**
  326. * Initialize system and personal mount points for a user
  327. *
  328. * @param string $user
  329. * @throws \OC\User\NoUserException if the user is not available
  330. */
  331. public static function initMountPoints($user = '') {
  332. if ($user == '') {
  333. $user = \OC_User::getUser();
  334. }
  335. if ($user === null || $user === false || $user === '') {
  336. throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session');
  337. }
  338. if (isset(self::$usersSetup[$user])) {
  339. return;
  340. }
  341. self::$usersSetup[$user] = true;
  342. $root = \OC_User::getHome($user);
  343. $userManager = \OC::$server->getUserManager();
  344. $userObject = $userManager->get($user);
  345. if (is_null($userObject)) {
  346. \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR);
  347. throw new \OC\User\NoUserException('Backends provided no user object for ' . $user);
  348. }
  349. $homeStorage = \OC::$server->getConfig()->getSystemValue('objectstore');
  350. if (!empty($homeStorage)) {
  351. // sanity checks
  352. if (empty($homeStorage['class'])) {
  353. \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
  354. }
  355. if (!isset($homeStorage['arguments'])) {
  356. $homeStorage['arguments'] = array();
  357. }
  358. // instantiate object store implementation
  359. $homeStorage['arguments']['objectstore'] = new $homeStorage['class']($homeStorage['arguments']);
  360. // mount with home object store implementation
  361. $homeStorage['class'] = '\OC\Files\ObjectStore\HomeObjectStoreStorage';
  362. } else {
  363. $homeStorage = array(
  364. //default home storage configuration:
  365. 'class' => '\OC\Files\Storage\Home',
  366. 'arguments' => array()
  367. );
  368. }
  369. $homeStorage['arguments']['user'] = $userObject;
  370. // check for legacy home id (<= 5.0.12)
  371. if (\OC\Files\Cache\Storage::exists('local::' . $root . '/')) {
  372. $homeStorage['arguments']['legacy'] = true;
  373. }
  374. $mount = new MountPoint($homeStorage['class'], '/' . $user, $homeStorage['arguments'], self::getLoader());
  375. self::getMountManager()->addMount($mount);
  376. $home = \OC\Files\Filesystem::getStorage($user);
  377. self::mountCacheDir($user);
  378. // Chance to mount for other storages
  379. /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */
  380. $mountConfigManager = \OC::$server->getMountProviderCollection();
  381. if ($userObject) {
  382. $mounts = $mountConfigManager->getMountsForUser($userObject);
  383. array_walk($mounts, array(self::$mounts, 'addMount'));
  384. $mounts[] = $mount;
  385. $mountConfigManager->registerMounts($userObject, $mounts);
  386. }
  387. self::listenForNewMountProviders($mountConfigManager, $userManager);
  388. \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user, 'user_dir' => $root));
  389. }
  390. /**
  391. * Get mounts from mount providers that are registered after setup
  392. *
  393. * @param MountProviderCollection $mountConfigManager
  394. * @param IUserManager $userManager
  395. */
  396. private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) {
  397. if (!self::$listeningForProviders) {
  398. self::$listeningForProviders = true;
  399. $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) {
  400. foreach (Filesystem::$usersSetup as $user => $setup) {
  401. $userObject = $userManager->get($user);
  402. if ($userObject) {
  403. $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader());
  404. array_walk($mounts, array(self::$mounts, 'addMount'));
  405. }
  406. }
  407. });
  408. }
  409. }
  410. /**
  411. * Mounts the cache directory
  412. *
  413. * @param string $user user name
  414. */
  415. private static function mountCacheDir($user) {
  416. $cacheBaseDir = \OC::$server->getConfig()->getSystemValue('cache_path', '');
  417. if ($cacheBaseDir !== '') {
  418. $cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user;
  419. if (!file_exists($cacheDir)) {
  420. mkdir($cacheDir, 0770, true);
  421. }
  422. // mount external cache dir to "/$user/cache" mount point
  423. self::mount('\OC\Files\Storage\Local', array('datadir' => $cacheDir), '/' . $user . '/cache');
  424. }
  425. }
  426. /**
  427. * get the default filesystem view
  428. *
  429. * @return View
  430. */
  431. static public function getView() {
  432. return self::$defaultInstance;
  433. }
  434. /**
  435. * tear down the filesystem, removing all storage providers
  436. */
  437. static public function tearDown() {
  438. self::clearMounts();
  439. self::$defaultInstance = null;
  440. }
  441. /**
  442. * get the relative path of the root data directory for the current user
  443. *
  444. * @return string
  445. *
  446. * Returns path like /admin/files
  447. */
  448. static public function getRoot() {
  449. if (!self::$defaultInstance) {
  450. return null;
  451. }
  452. return self::$defaultInstance->getRoot();
  453. }
  454. /**
  455. * clear all mounts and storage backends
  456. */
  457. public static function clearMounts() {
  458. if (self::$mounts) {
  459. self::$usersSetup = array();
  460. self::$mounts->clear();
  461. }
  462. }
  463. /**
  464. * mount an \OC\Files\Storage\Storage in our virtual filesystem
  465. *
  466. * @param \OC\Files\Storage\Storage|string $class
  467. * @param array $arguments
  468. * @param string $mountpoint
  469. */
  470. static public function mount($class, $arguments, $mountpoint) {
  471. if (!self::$mounts) {
  472. \OC_Util::setupFS();
  473. }
  474. $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader());
  475. self::$mounts->addMount($mount);
  476. }
  477. /**
  478. * return the path to a local version of the file
  479. * we need this because we can't know if a file is stored local or not from
  480. * outside the filestorage and for some purposes a local file is needed
  481. *
  482. * @param string $path
  483. * @return string
  484. */
  485. static public function getLocalFile($path) {
  486. return self::$defaultInstance->getLocalFile($path);
  487. }
  488. /**
  489. * @param string $path
  490. * @return string
  491. */
  492. static public function getLocalFolder($path) {
  493. return self::$defaultInstance->getLocalFolder($path);
  494. }
  495. /**
  496. * return path to file which reflects one visible in browser
  497. *
  498. * @param string $path
  499. * @return string
  500. */
  501. static public function getLocalPath($path) {
  502. $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files';
  503. $newpath = $path;
  504. if (strncmp($newpath, $datadir, strlen($datadir)) == 0) {
  505. $newpath = substr($path, strlen($datadir));
  506. }
  507. return $newpath;
  508. }
  509. /**
  510. * check if the requested path is valid
  511. *
  512. * @param string $path
  513. * @return bool
  514. */
  515. static public function isValidPath($path) {
  516. $path = self::normalizePath($path);
  517. if (!$path || $path[0] !== '/') {
  518. $path = '/' . $path;
  519. }
  520. if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') {
  521. return false;
  522. }
  523. return true;
  524. }
  525. /**
  526. * checks if a file is blacklisted for storage in the filesystem
  527. * Listens to write and rename hooks
  528. *
  529. * @param array $data from hook
  530. */
  531. static public function isBlacklisted($data) {
  532. if (isset($data['path'])) {
  533. $path = $data['path'];
  534. } else if (isset($data['newpath'])) {
  535. $path = $data['newpath'];
  536. }
  537. if (isset($path)) {
  538. if (self::isFileBlacklisted($path)) {
  539. $data['run'] = false;
  540. }
  541. }
  542. }
  543. /**
  544. * @param string $filename
  545. * @return bool
  546. */
  547. static public function isFileBlacklisted($filename) {
  548. $filename = self::normalizePath($filename);
  549. $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess'));
  550. $filename = strtolower(basename($filename));
  551. return in_array($filename, $blacklist);
  552. }
  553. /**
  554. * check if the directory should be ignored when scanning
  555. * NOTE: the special directories . and .. would cause never ending recursion
  556. *
  557. * @param String $dir
  558. * @return boolean
  559. */
  560. static public function isIgnoredDir($dir) {
  561. if ($dir === '.' || $dir === '..') {
  562. return true;
  563. }
  564. return false;
  565. }
  566. /**
  567. * following functions are equivalent to their php builtin equivalents for arguments/return values.
  568. */
  569. static public function mkdir($path) {
  570. return self::$defaultInstance->mkdir($path);
  571. }
  572. static public function rmdir($path) {
  573. return self::$defaultInstance->rmdir($path);
  574. }
  575. static public function opendir($path) {
  576. return self::$defaultInstance->opendir($path);
  577. }
  578. static public function readdir($path) {
  579. return self::$defaultInstance->readdir($path);
  580. }
  581. static public function is_dir($path) {
  582. return self::$defaultInstance->is_dir($path);
  583. }
  584. static public function is_file($path) {
  585. return self::$defaultInstance->is_file($path);
  586. }
  587. static public function stat($path) {
  588. return self::$defaultInstance->stat($path);
  589. }
  590. static public function filetype($path) {
  591. return self::$defaultInstance->filetype($path);
  592. }
  593. static public function filesize($path) {
  594. return self::$defaultInstance->filesize($path);
  595. }
  596. static public function readfile($path) {
  597. return self::$defaultInstance->readfile($path);
  598. }
  599. static public function isCreatable($path) {
  600. return self::$defaultInstance->isCreatable($path);
  601. }
  602. static public function isReadable($path) {
  603. return self::$defaultInstance->isReadable($path);
  604. }
  605. static public function isUpdatable($path) {
  606. return self::$defaultInstance->isUpdatable($path);
  607. }
  608. static public function isDeletable($path) {
  609. return self::$defaultInstance->isDeletable($path);
  610. }
  611. static public function isSharable($path) {
  612. return self::$defaultInstance->isSharable($path);
  613. }
  614. static public function file_exists($path) {
  615. return self::$defaultInstance->file_exists($path);
  616. }
  617. static public function filemtime($path) {
  618. return self::$defaultInstance->filemtime($path);
  619. }
  620. static public function touch($path, $mtime = null) {
  621. return self::$defaultInstance->touch($path, $mtime);
  622. }
  623. /**
  624. * @return string
  625. */
  626. static public function file_get_contents($path) {
  627. return self::$defaultInstance->file_get_contents($path);
  628. }
  629. static public function file_put_contents($path, $data) {
  630. return self::$defaultInstance->file_put_contents($path, $data);
  631. }
  632. static public function unlink($path) {
  633. return self::$defaultInstance->unlink($path);
  634. }
  635. static public function rename($path1, $path2) {
  636. return self::$defaultInstance->rename($path1, $path2);
  637. }
  638. static public function copy($path1, $path2) {
  639. return self::$defaultInstance->copy($path1, $path2);
  640. }
  641. static public function fopen($path, $mode) {
  642. return self::$defaultInstance->fopen($path, $mode);
  643. }
  644. /**
  645. * @return string
  646. */
  647. static public function toTmpFile($path) {
  648. return self::$defaultInstance->toTmpFile($path);
  649. }
  650. static public function fromTmpFile($tmpFile, $path) {
  651. return self::$defaultInstance->fromTmpFile($tmpFile, $path);
  652. }
  653. static public function getMimeType($path) {
  654. return self::$defaultInstance->getMimeType($path);
  655. }
  656. static public function hash($type, $path, $raw = false) {
  657. return self::$defaultInstance->hash($type, $path, $raw);
  658. }
  659. static public function free_space($path = '/') {
  660. return self::$defaultInstance->free_space($path);
  661. }
  662. static public function search($query) {
  663. return self::$defaultInstance->search($query);
  664. }
  665. /**
  666. * @param string $query
  667. */
  668. static public function searchByMime($query) {
  669. return self::$defaultInstance->searchByMime($query);
  670. }
  671. /**
  672. * @param string|int $tag name or tag id
  673. * @param string $userId owner of the tags
  674. * @return FileInfo[] array or file info
  675. */
  676. static public function searchByTag($tag, $userId) {
  677. return self::$defaultInstance->searchByTag($tag, $userId);
  678. }
  679. /**
  680. * check if a file or folder has been updated since $time
  681. *
  682. * @param string $path
  683. * @param int $time
  684. * @return bool
  685. */
  686. static public function hasUpdated($path, $time) {
  687. return self::$defaultInstance->hasUpdated($path, $time);
  688. }
  689. /**
  690. * Fix common problems with a file path
  691. *
  692. * @param string $path
  693. * @param bool $stripTrailingSlash
  694. * @param bool $isAbsolutePath
  695. * @return string
  696. */
  697. public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false) {
  698. if (is_null(self::$normalizedPathCache)) {
  699. self::$normalizedPathCache = new CappedMemoryCache();
  700. }
  701. /**
  702. * FIXME: This is a workaround for existing classes and files which call
  703. * this function with another type than a valid string. This
  704. * conversion should get removed as soon as all existing
  705. * function calls have been fixed.
  706. */
  707. $path = (string)$path;
  708. $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath]);
  709. if (isset(self::$normalizedPathCache[$cacheKey])) {
  710. return self::$normalizedPathCache[$cacheKey];
  711. }
  712. if ($path == '') {
  713. return '/';
  714. }
  715. //normalize unicode if possible
  716. $path = \OC_Util::normalizeUnicode($path);
  717. //no windows style slashes
  718. $path = str_replace('\\', '/', $path);
  719. // When normalizing an absolute path, we need to ensure that the drive-letter
  720. // is still at the beginning on windows
  721. $windows_drive_letter = '';
  722. if ($isAbsolutePath && \OC_Util::runningOnWindows() && preg_match('#^([a-zA-Z])$#', $path[0]) && $path[1] == ':' && $path[2] == '/') {
  723. $windows_drive_letter = substr($path, 0, 2);
  724. $path = substr($path, 2);
  725. }
  726. //add leading slash
  727. if ($path[0] !== '/') {
  728. $path = '/' . $path;
  729. }
  730. // remove '/./'
  731. // ugly, but str_replace() can't replace them all in one go
  732. // as the replacement itself is part of the search string
  733. // which will only be found during the next iteration
  734. while (strpos($path, '/./') !== false) {
  735. $path = str_replace('/./', '/', $path);
  736. }
  737. // remove sequences of slashes
  738. $path = preg_replace('#/{2,}#', '/', $path);
  739. //remove trailing slash
  740. if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') {
  741. $path = substr($path, 0, -1);
  742. }
  743. // remove trailing '/.'
  744. if (substr($path, -2) == '/.') {
  745. $path = substr($path, 0, -2);
  746. }
  747. $normalizedPath = $windows_drive_letter . $path;
  748. self::$normalizedPathCache[$cacheKey] = $normalizedPath;
  749. return $normalizedPath;
  750. }
  751. /**
  752. * get the filesystem info
  753. *
  754. * @param string $path
  755. * @param boolean $includeMountPoints whether to add mountpoint sizes,
  756. * defaults to true
  757. * @return \OC\Files\FileInfo|bool False if file does not exist
  758. */
  759. public static function getFileInfo($path, $includeMountPoints = true) {
  760. return self::$defaultInstance->getFileInfo($path, $includeMountPoints);
  761. }
  762. /**
  763. * change file metadata
  764. *
  765. * @param string $path
  766. * @param array $data
  767. * @return int
  768. *
  769. * returns the fileid of the updated file
  770. */
  771. public static function putFileInfo($path, $data) {
  772. return self::$defaultInstance->putFileInfo($path, $data);
  773. }
  774. /**
  775. * get the content of a directory
  776. *
  777. * @param string $directory path under datadirectory
  778. * @param string $mimetype_filter limit returned content to this mimetype or mimepart
  779. * @return \OC\Files\FileInfo[]
  780. */
  781. public static function getDirectoryContent($directory, $mimetype_filter = '') {
  782. return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter);
  783. }
  784. /**
  785. * Get the path of a file by id
  786. *
  787. * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file
  788. *
  789. * @param int $id
  790. * @throws NotFoundException
  791. * @return string
  792. */
  793. public static function getPath($id) {
  794. return self::$defaultInstance->getPath($id);
  795. }
  796. /**
  797. * Get the owner for a file or folder
  798. *
  799. * @param string $path
  800. * @return string
  801. */
  802. public static function getOwner($path) {
  803. return self::$defaultInstance->getOwner($path);
  804. }
  805. /**
  806. * get the ETag for a file or folder
  807. *
  808. * @param string $path
  809. * @return string
  810. */
  811. static public function getETag($path) {
  812. return self::$defaultInstance->getETag($path);
  813. }
  814. }