OC_Helper.php 20 KB

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