OC_Helper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Ardinis <Ardinis@users.noreply.github.com>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  11. * @author Felix Moeller <mail@felixmoeller.de>
  12. * @author J0WI <J0WI@users.noreply.github.com>
  13. * @author Jakob Sack <mail@jakobsack.de>
  14. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  15. * @author Joas Schilling <coding@schilljs.com>
  16. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  17. * @author Julius Härtl <jus@bitgrid.net>
  18. * @author Lukas Reschke <lukas@statuscode.ch>
  19. * @author Morris Jobke <hey@morrisjobke.de>
  20. * @author Olivier Paroz <github@oparoz.com>
  21. * @author Pellaeon Lin <nfsmwlin@gmail.com>
  22. * @author RealRancor <fisch.666@gmx.de>
  23. * @author Robin Appelman <robin@icewind.nl>
  24. * @author Robin McCorkell <robin@mccorkell.me.uk>
  25. * @author Roeland Jago Douma <roeland@famdouma.nl>
  26. * @author Simon Könnecke <simonkoennecke@gmail.com>
  27. * @author Thomas Müller <thomas.mueller@tmit.eu>
  28. * @author Thomas Tanghus <thomas@tanghus.net>
  29. * @author Vincent Petry <vincent@nextcloud.com>
  30. *
  31. * @license AGPL-3.0
  32. *
  33. * This code is free software: you can redistribute it and/or modify
  34. * it under the terms of the GNU Affero General Public License, version 3,
  35. * as published by the Free Software Foundation.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU Affero General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU Affero General Public License, version 3,
  43. * along with this program. If not, see <http://www.gnu.org/licenses/>
  44. *
  45. */
  46. use bantu\IniGetWrapper\IniGetWrapper;
  47. use OC\Files\Filesystem;
  48. use OCP\Files\Mount\IMountPoint;
  49. use OCP\ICacheFactory;
  50. use OCP\IBinaryFinder;
  51. use OCP\IUser;
  52. use OCP\Util;
  53. use Psr\Log\LoggerInterface;
  54. /**
  55. * Collection of useful functions
  56. *
  57. * @psalm-type StorageInfo = array{
  58. * free: float|int,
  59. * mountPoint: string,
  60. * mountType: string,
  61. * owner: string,
  62. * ownerDisplayName: string,
  63. * quota: float|int,
  64. * relative: float|int,
  65. * total: float|int,
  66. * used: float|int,
  67. * }
  68. */
  69. class OC_Helper {
  70. private static $templateManager;
  71. /**
  72. * Make a human file size
  73. * @param int|float $bytes file size in bytes
  74. * @return string a human readable file size
  75. *
  76. * Makes 2048 to 2 kB.
  77. */
  78. public static function humanFileSize(int|float $bytes): string {
  79. if ($bytes < 0) {
  80. return "?";
  81. }
  82. if ($bytes < 1024) {
  83. return "$bytes B";
  84. }
  85. $bytes = round($bytes / 1024, 0);
  86. if ($bytes < 1024) {
  87. return "$bytes KB";
  88. }
  89. $bytes = round($bytes / 1024, 1);
  90. if ($bytes < 1024) {
  91. return "$bytes MB";
  92. }
  93. $bytes = round($bytes / 1024, 1);
  94. if ($bytes < 1024) {
  95. return "$bytes GB";
  96. }
  97. $bytes = round($bytes / 1024, 1);
  98. if ($bytes < 1024) {
  99. return "$bytes TB";
  100. }
  101. $bytes = round($bytes / 1024, 1);
  102. return "$bytes PB";
  103. }
  104. /**
  105. * Make a computer file size
  106. * @param string $str file size in human readable format
  107. * @return false|int|float a file size in bytes
  108. *
  109. * Makes 2kB to 2048.
  110. *
  111. * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
  112. */
  113. public static function computerFileSize(string $str): false|int|float {
  114. $str = strtolower($str);
  115. if (is_numeric($str)) {
  116. return Util::numericToNumber($str);
  117. }
  118. $bytes_array = [
  119. 'b' => 1,
  120. 'k' => 1024,
  121. 'kb' => 1024,
  122. 'mb' => 1024 * 1024,
  123. 'm' => 1024 * 1024,
  124. 'gb' => 1024 * 1024 * 1024,
  125. 'g' => 1024 * 1024 * 1024,
  126. 'tb' => 1024 * 1024 * 1024 * 1024,
  127. 't' => 1024 * 1024 * 1024 * 1024,
  128. 'pb' => 1024 * 1024 * 1024 * 1024 * 1024,
  129. 'p' => 1024 * 1024 * 1024 * 1024 * 1024,
  130. ];
  131. $bytes = (float)$str;
  132. if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) {
  133. $bytes *= $bytes_array[$matches[1]];
  134. } else {
  135. return false;
  136. }
  137. return Util::numericToNumber(round($bytes));
  138. }
  139. /**
  140. * Recursive copying of folders
  141. * @param string $src source folder
  142. * @param string $dest target folder
  143. * @return void
  144. */
  145. public static function copyr($src, $dest) {
  146. if (is_dir($src)) {
  147. if (!is_dir($dest)) {
  148. mkdir($dest);
  149. }
  150. $files = scandir($src);
  151. foreach ($files as $file) {
  152. if ($file != "." && $file != "..") {
  153. self::copyr("$src/$file", "$dest/$file");
  154. }
  155. }
  156. } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) {
  157. copy($src, $dest);
  158. }
  159. }
  160. /**
  161. * Recursive deletion of folders
  162. * @param string $dir path to the folder
  163. * @param bool $deleteSelf if set to false only the content of the folder will be deleted
  164. * @return bool
  165. */
  166. public static function rmdirr($dir, $deleteSelf = true) {
  167. if (is_dir($dir)) {
  168. $files = new RecursiveIteratorIterator(
  169. new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
  170. RecursiveIteratorIterator::CHILD_FIRST
  171. );
  172. foreach ($files as $fileInfo) {
  173. /** @var SplFileInfo $fileInfo */
  174. if ($fileInfo->isLink()) {
  175. unlink($fileInfo->getPathname());
  176. } elseif ($fileInfo->isDir()) {
  177. rmdir($fileInfo->getRealPath());
  178. } else {
  179. unlink($fileInfo->getRealPath());
  180. }
  181. }
  182. if ($deleteSelf) {
  183. rmdir($dir);
  184. }
  185. } elseif (file_exists($dir)) {
  186. if ($deleteSelf) {
  187. unlink($dir);
  188. }
  189. }
  190. if (!$deleteSelf) {
  191. return true;
  192. }
  193. return !file_exists($dir);
  194. }
  195. /**
  196. * @deprecated 18.0.0
  197. * @return \OC\Files\Type\TemplateManager
  198. */
  199. public static function getFileTemplateManager() {
  200. if (!self::$templateManager) {
  201. self::$templateManager = new \OC\Files\Type\TemplateManager();
  202. }
  203. return self::$templateManager;
  204. }
  205. /**
  206. * detect if a given program is found in the search PATH
  207. *
  208. * @param string $name
  209. * @param bool $path
  210. * @internal param string $program name
  211. * @internal param string $optional search path, defaults to $PATH
  212. * @return bool true if executable program found in path
  213. */
  214. public static function canExecute($name, $path = false) {
  215. // path defaults to PATH from environment if not set
  216. if ($path === false) {
  217. $path = getenv("PATH");
  218. }
  219. // we look for an executable file of that name
  220. $exts = [""];
  221. $check_fn = "is_executable";
  222. // Default check will be done with $path directories :
  223. $dirs = explode(PATH_SEPARATOR, $path);
  224. // WARNING : We have to check if open_basedir is enabled :
  225. $obd = OC::$server->get(IniGetWrapper::class)->getString('open_basedir');
  226. if ($obd != "none") {
  227. $obd_values = explode(PATH_SEPARATOR, $obd);
  228. if (count($obd_values) > 0 and $obd_values[0]) {
  229. // open_basedir is in effect !
  230. // We need to check if the program is in one of these dirs :
  231. $dirs = $obd_values;
  232. }
  233. }
  234. foreach ($dirs as $dir) {
  235. foreach ($exts as $ext) {
  236. if ($check_fn("$dir/$name" . $ext)) {
  237. return true;
  238. }
  239. }
  240. }
  241. return false;
  242. }
  243. /**
  244. * copy the contents of one stream to another
  245. *
  246. * @param resource $source
  247. * @param resource $target
  248. * @return array the number of bytes copied and result
  249. */
  250. public static function streamCopy($source, $target) {
  251. if (!$source or !$target) {
  252. return [0, false];
  253. }
  254. $bufSize = 8192;
  255. $result = true;
  256. $count = 0;
  257. while (!feof($source)) {
  258. $buf = fread($source, $bufSize);
  259. $bytesWritten = fwrite($target, $buf);
  260. if ($bytesWritten !== false) {
  261. $count += $bytesWritten;
  262. }
  263. // note: strlen is expensive so only use it when necessary,
  264. // on the last block
  265. if ($bytesWritten === false
  266. || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf))
  267. ) {
  268. // write error, could be disk full ?
  269. $result = false;
  270. break;
  271. }
  272. }
  273. return [$count, $result];
  274. }
  275. /**
  276. * Adds a suffix to the name in case the file exists
  277. *
  278. * @param string $path
  279. * @param string $filename
  280. * @return string
  281. */
  282. public static function buildNotExistingFileName($path, $filename) {
  283. $view = \OC\Files\Filesystem::getView();
  284. return self::buildNotExistingFileNameForView($path, $filename, $view);
  285. }
  286. /**
  287. * Adds a suffix to the name in case the file exists
  288. *
  289. * @param string $path
  290. * @param string $filename
  291. * @return string
  292. */
  293. public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) {
  294. if ($path === '/') {
  295. $path = '';
  296. }
  297. if ($pos = strrpos($filename, '.')) {
  298. $name = substr($filename, 0, $pos);
  299. $ext = substr($filename, $pos);
  300. } else {
  301. $name = $filename;
  302. $ext = '';
  303. }
  304. $newpath = $path . '/' . $filename;
  305. if ($view->file_exists($newpath)) {
  306. if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) {
  307. //Replace the last "(number)" with "(number+1)"
  308. $last_match = count($matches[0]) - 1;
  309. $counter = $matches[1][$last_match][0] + 1;
  310. $offset = $matches[0][$last_match][1];
  311. $match_length = strlen($matches[0][$last_match][0]);
  312. } else {
  313. $counter = 2;
  314. $match_length = 0;
  315. $offset = false;
  316. }
  317. do {
  318. if ($offset) {
  319. //Replace the last "(number)" with "(number+1)"
  320. $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length);
  321. } else {
  322. $newname = $name . ' (' . $counter . ')';
  323. }
  324. $newpath = $path . '/' . $newname . $ext;
  325. $counter++;
  326. } while ($view->file_exists($newpath));
  327. }
  328. return $newpath;
  329. }
  330. /**
  331. * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
  332. *
  333. * @param array $input The array to work on
  334. * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
  335. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  336. * @return array
  337. *
  338. * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
  339. * based on https://www.php.net/manual/en/function.array-change-key-case.php#107715
  340. *
  341. */
  342. public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
  343. $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER;
  344. $ret = [];
  345. foreach ($input as $k => $v) {
  346. $ret[mb_convert_case($k, $case, $encoding)] = $v;
  347. }
  348. return $ret;
  349. }
  350. /**
  351. * performs a search in a nested array
  352. * @param array $haystack the array to be searched
  353. * @param string $needle the search string
  354. * @param mixed $index optional, only search this key name
  355. * @return mixed the key of the matching field, otherwise false
  356. *
  357. * performs a search in a nested array
  358. *
  359. * taken from https://www.php.net/manual/en/function.array-search.php#97645
  360. */
  361. public static function recursiveArraySearch($haystack, $needle, $index = null) {
  362. $aIt = new RecursiveArrayIterator($haystack);
  363. $it = new RecursiveIteratorIterator($aIt);
  364. while ($it->valid()) {
  365. if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) {
  366. return $aIt->key();
  367. }
  368. $it->next();
  369. }
  370. return false;
  371. }
  372. /**
  373. * calculates the maximum upload size respecting system settings, free space and user quota
  374. *
  375. * @param string $dir the current folder where the user currently operates
  376. * @param int|float $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
  377. * @return int|float number of bytes representing
  378. */
  379. public static function maxUploadFilesize($dir, $freeSpace = null) {
  380. if (is_null($freeSpace) || $freeSpace < 0) {
  381. $freeSpace = self::freeSpace($dir);
  382. }
  383. return min($freeSpace, self::uploadLimit());
  384. }
  385. /**
  386. * Calculate free space left within user quota
  387. *
  388. * @param string $dir the current folder where the user currently operates
  389. * @return int|float number of bytes representing
  390. */
  391. public static function freeSpace($dir) {
  392. $freeSpace = \OC\Files\Filesystem::free_space($dir);
  393. if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) {
  394. $freeSpace = max($freeSpace, 0);
  395. return $freeSpace;
  396. } else {
  397. return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
  398. }
  399. }
  400. /**
  401. * Calculate PHP upload limit
  402. *
  403. * @return int|float PHP upload file size limit
  404. */
  405. public static function uploadLimit() {
  406. $ini = \OC::$server->get(IniGetWrapper::class);
  407. $upload_max_filesize = Util::computerFileSize($ini->get('upload_max_filesize')) ?: 0;
  408. $post_max_size = Util::computerFileSize($ini->get('post_max_size')) ?: 0;
  409. if ($upload_max_filesize === 0 && $post_max_size === 0) {
  410. return INF;
  411. } elseif ($upload_max_filesize === 0 || $post_max_size === 0) {
  412. return max($upload_max_filesize, $post_max_size); //only the non 0 value counts
  413. } else {
  414. return min($upload_max_filesize, $post_max_size);
  415. }
  416. }
  417. /**
  418. * Checks if a function is available
  419. *
  420. * @deprecated Since 25.0.0 use \OCP\Util::isFunctionEnabled instead
  421. */
  422. public static function is_function_enabled(string $function_name): bool {
  423. return \OCP\Util::isFunctionEnabled($function_name);
  424. }
  425. /**
  426. * Try to find a program
  427. * @deprecated Since 25.0.0 Use \OC\BinaryFinder directly
  428. */
  429. public static function findBinaryPath(string $program): ?string {
  430. $result = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath($program);
  431. return $result !== false ? $result : null;
  432. }
  433. /**
  434. * Calculate the disc space for the given path
  435. *
  436. * BEWARE: this requires that Util::setupFS() was called
  437. * already !
  438. *
  439. * @param string $path
  440. * @param \OCP\Files\FileInfo $rootInfo (optional)
  441. * @param bool $includeMountPoints whether to include mount points in the size calculation
  442. * @param bool $useCache whether to use the cached quota values
  443. * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct
  444. * @return StorageInfo
  445. * @throws \OCP\Files\NotFoundException
  446. */
  447. public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) {
  448. /** @var ICacheFactory $cacheFactory */
  449. $cacheFactory = \OC::$server->get(ICacheFactory::class);
  450. $memcache = $cacheFactory->createLocal('storage_info');
  451. // return storage info without adding mount points
  452. $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
  453. $view = Filesystem::getView();
  454. if (!$view) {
  455. throw new \OCP\Files\NotFoundException();
  456. }
  457. $fullPath = Filesystem::normalizePath($view->getAbsolutePath($path));
  458. $cacheKey = $fullPath. '::' . ($includeMountPoints ? 'include' : 'exclude');
  459. if ($useCache) {
  460. $cached = $memcache->get($cacheKey);
  461. if ($cached) {
  462. return $cached;
  463. }
  464. }
  465. if (!$rootInfo) {
  466. $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false);
  467. }
  468. if (!$rootInfo instanceof \OCP\Files\FileInfo) {
  469. throw new \OCP\Files\NotFoundException('The root directory of the user\'s files is missing');
  470. }
  471. $used = $rootInfo->getSize($includeMountPoints);
  472. if ($used < 0) {
  473. $used = 0.0;
  474. }
  475. /** @var int|float $quota */
  476. $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED;
  477. $mount = $rootInfo->getMountPoint();
  478. $storage = $mount->getStorage();
  479. $sourceStorage = $storage;
  480. if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
  481. $includeExtStorage = false;
  482. }
  483. if ($includeExtStorage) {
  484. if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
  485. || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
  486. ) {
  487. /** @var \OC\Files\Storage\Home $storage */
  488. $user = $storage->getUser();
  489. } else {
  490. $user = \OC::$server->getUserSession()->getUser();
  491. }
  492. $quota = OC_Util::getUserQuota($user);
  493. if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
  494. // always get free space / total space from root + mount points
  495. return self::getGlobalStorageInfo($quota, $user, $mount);
  496. }
  497. }
  498. // TODO: need a better way to get total space from storage
  499. if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) {
  500. /** @var \OC\Files\Storage\Wrapper\Quota $storage */
  501. $quota = $sourceStorage->getQuota();
  502. }
  503. try {
  504. $free = $sourceStorage->free_space($rootInfo->getInternalPath());
  505. if (is_bool($free)) {
  506. $free = 0.0;
  507. }
  508. } catch (\Exception $e) {
  509. if ($path === "") {
  510. throw $e;
  511. }
  512. /** @var LoggerInterface $logger */
  513. $logger = \OC::$server->get(LoggerInterface::class);
  514. $logger->warning("Error while getting quota info, using root quota", ['exception' => $e]);
  515. $rootInfo = self::getStorageInfo("");
  516. $memcache->set($cacheKey, $rootInfo, 5 * 60);
  517. return $rootInfo;
  518. }
  519. if ($free >= 0) {
  520. $total = $free + $used;
  521. } else {
  522. $total = $free; //either unknown or unlimited
  523. }
  524. if ($total > 0) {
  525. if ($quota > 0 && $total > $quota) {
  526. $total = $quota;
  527. }
  528. // prevent division by zero or error codes (negative values)
  529. $relative = round(($used / $total) * 10000) / 100;
  530. } else {
  531. $relative = 0;
  532. }
  533. /** @var string $ownerId */
  534. $ownerId = $storage->getOwner($path);
  535. $ownerDisplayName = '';
  536. if ($ownerId) {
  537. $ownerDisplayName = \OC::$server->getUserManager()->getDisplayName($ownerId) ?? '';
  538. }
  539. if (substr_count($mount->getMountPoint(), '/') < 3) {
  540. $mountPoint = '';
  541. } else {
  542. [,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4);
  543. }
  544. $info = [
  545. 'free' => $free,
  546. 'used' => $used,
  547. 'quota' => $quota,
  548. 'total' => $total,
  549. 'relative' => $relative,
  550. 'owner' => $ownerId,
  551. 'ownerDisplayName' => $ownerDisplayName,
  552. 'mountType' => $mount->getMountType(),
  553. 'mountPoint' => trim($mountPoint, '/'),
  554. ];
  555. $memcache->set($cacheKey, $info, 5 * 60);
  556. return $info;
  557. }
  558. /**
  559. * Get storage info including all mount points and quota
  560. *
  561. * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct
  562. * @return StorageInfo
  563. */
  564. private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array {
  565. $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext');
  566. /** @var int|float $used */
  567. $used = $rootInfo['size'];
  568. if ($used < 0) {
  569. $used = 0.0;
  570. }
  571. $total = $quota;
  572. /** @var int|float $free */
  573. $free = $quota - $used;
  574. if ($total > 0) {
  575. if ($quota > 0 && $total > $quota) {
  576. $total = $quota;
  577. }
  578. // prevent division by zero or error codes (negative values)
  579. $relative = round(($used / $total) * 10000) / 100;
  580. } else {
  581. $relative = 0.0;
  582. }
  583. if (substr_count($mount->getMountPoint(), '/') < 3) {
  584. $mountPoint = '';
  585. } else {
  586. [,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4);
  587. }
  588. return [
  589. 'free' => $free,
  590. 'used' => $used,
  591. 'total' => $total,
  592. 'relative' => $relative,
  593. 'quota' => $quota,
  594. 'owner' => $user->getUID(),
  595. 'ownerDisplayName' => $user->getDisplayName(),
  596. 'mountType' => $mount->getMountType(),
  597. 'mountPoint' => trim($mountPoint, '/'),
  598. ];
  599. }
  600. public static function clearStorageInfo(string $absolutePath): void {
  601. /** @var ICacheFactory $cacheFactory */
  602. $cacheFactory = \OC::$server->get(ICacheFactory::class);
  603. $memcache = $cacheFactory->createLocal('storage_info');
  604. $cacheKeyPrefix = Filesystem::normalizePath($absolutePath) . '::';
  605. $memcache->remove($cacheKeyPrefix . 'include');
  606. $memcache->remove($cacheKeyPrefix . 'exclude');
  607. }
  608. /**
  609. * Returns whether the config file is set manually to read-only
  610. * @return bool
  611. */
  612. public static function isReadOnlyConfigEnabled() {
  613. return \OC::$server->getConfig()->getSystemValueBool('config_is_read_only', false);
  614. }
  615. }