helper.php 19 KB

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