1
0

util.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Frank Karlitschek <frank@owncloud.org>
  7. * @author Georg Ehrke <georg@owncloud.com>
  8. * @author Individual IT Services <info@individual-it.net>
  9. * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch>
  10. * @author Joas Schilling <nickvergessen@owncloud.com>
  11. * @author Lukas Reschke <lukas@owncloud.com>
  12. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Nicolas Grekas <nicolas.grekas@gmail.com>
  15. * @author Pellaeon Lin <nfsmwlin@gmail.com>
  16. * @author Randolph Carter <RandolphCarter@fantasymail.de>
  17. * @author Robin Appelman <icewind@owncloud.com>
  18. * @author Robin McCorkell <robin@mccorkell.me.uk>
  19. * @author Roeland Jago Douma <rullzer@owncloud.com>
  20. * @author Stefan Herbrechtsmeier <stefan@herbrechtsmeier.net>
  21. * @author Thomas Müller <thomas.mueller@tmit.eu>
  22. * @author Thomas Tanghus <thomas@tanghus.net>
  23. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  24. * @author Vincent Petry <pvince81@owncloud.com>
  25. *
  26. * @copyright Copyright (c) 2016, ownCloud, Inc.
  27. * @license AGPL-3.0
  28. *
  29. * This code is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU Affero General Public License, version 3,
  31. * as published by the Free Software Foundation.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU Affero General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Affero General Public License, version 3,
  39. * along with this program. If not, see <http://www.gnu.org/licenses/>
  40. *
  41. */
  42. /**
  43. * Public interface of ownCloud for apps to use.
  44. * Utility Class.
  45. *
  46. */
  47. // use OCP namespace for all classes that are considered public.
  48. // This means that they should be used by apps instead of the internal ownCloud classes
  49. namespace OCP;
  50. use DateTimeZone;
  51. /**
  52. * This class provides different helper functions to make the life of a developer easier
  53. * @since 4.0.0
  54. */
  55. class Util {
  56. // consts for Logging
  57. const DEBUG=0;
  58. const INFO=1;
  59. const WARN=2;
  60. const ERROR=3;
  61. const FATAL=4;
  62. /**
  63. * get the current installed version of ownCloud
  64. * @return array
  65. * @since 4.0.0
  66. */
  67. public static function getVersion() {
  68. return(\OC_Util::getVersion());
  69. }
  70. /**
  71. * Set current update channel
  72. * @param string $channel
  73. * @since 8.1.0
  74. */
  75. public static function setChannel($channel) {
  76. //Flush timestamp to reload version.php
  77. \OC::$server->getSession()->set('OC_Version_Timestamp', 0);
  78. \OC::$server->getAppConfig()->setValue('core', 'OC_Channel', $channel);
  79. }
  80. /**
  81. * Get current update channel
  82. * @return string
  83. * @since 8.1.0
  84. */
  85. public static function getChannel() {
  86. return \OC_Util::getChannel();
  87. }
  88. /**
  89. * send an email
  90. * @param string $toaddress
  91. * @param string $toname
  92. * @param string $subject
  93. * @param string $mailtext
  94. * @param string $fromaddress
  95. * @param string $fromname
  96. * @param int $html
  97. * @param string $altbody
  98. * @param string $ccaddress
  99. * @param string $ccname
  100. * @param string $bcc
  101. * @deprecated 8.1.0 Use \OCP\Mail\IMailer instead
  102. * @since 4.0.0
  103. */
  104. public static function sendMail($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
  105. $html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') {
  106. $mailer = \OC::$server->getMailer();
  107. $message = $mailer->createMessage();
  108. $message->setTo([$toaddress => $toname]);
  109. $message->setSubject($subject);
  110. $message->setPlainBody($mailtext);
  111. $message->setFrom([$fromaddress => $fromname]);
  112. if($html === 1) {
  113. $message->setHTMLBody($altbody);
  114. }
  115. if($altbody === '') {
  116. $message->setHTMLBody($mailtext);
  117. $message->setPlainBody('');
  118. } else {
  119. $message->setHtmlBody($mailtext);
  120. $message->setPlainBody($altbody);
  121. }
  122. if(!empty($ccaddress)) {
  123. if(!empty($ccname)) {
  124. $message->setCc([$ccaddress => $ccname]);
  125. } else {
  126. $message->setCc([$ccaddress]);
  127. }
  128. }
  129. if(!empty($bcc)) {
  130. $message->setBcc([$bcc]);
  131. }
  132. $mailer->send($message);
  133. }
  134. /**
  135. * write a message in the log
  136. * @param string $app
  137. * @param string $message
  138. * @param int $level
  139. * @since 4.0.0
  140. */
  141. public static function writeLog( $app, $message, $level ) {
  142. $context = ['app' => $app];
  143. \OC::$server->getLogger()->log($level, $message, $context);
  144. }
  145. /**
  146. * write exception into the log
  147. * @param string $app app name
  148. * @param \Exception $ex exception to log
  149. * @param int $level log level, defaults to \OCP\Util::FATAL
  150. * @since ....0.0 - parameter $level was added in 7.0.0
  151. * @deprecated 8.2.0 use logException of \OCP\ILogger
  152. */
  153. public static function logException( $app, \Exception $ex, $level = \OCP\Util::FATAL ) {
  154. \OC::$server->getLogger()->logException($ex, ['app' => $app]);
  155. }
  156. /**
  157. * check if sharing is disabled for the current user
  158. *
  159. * @return boolean
  160. * @since 7.0.0
  161. */
  162. public static function isSharingDisabledForUser() {
  163. return \OC_Util::isSharingDisabledForUser(
  164. \OC::$server->getConfig(),
  165. \OC::$server->getGroupManager(),
  166. \OC::$server->getUserSession()->getUser()
  167. );
  168. }
  169. /**
  170. * get l10n object
  171. * @param string $application
  172. * @param string|null $language
  173. * @return \OC_L10N
  174. * @since 6.0.0 - parameter $language was added in 8.0.0
  175. */
  176. public static function getL10N($application, $language = null) {
  177. return \OC::$server->getL10N($application, $language);
  178. }
  179. /**
  180. * add a css file
  181. * @param string $application
  182. * @param string $file
  183. * @since 4.0.0
  184. */
  185. public static function addStyle( $application, $file = null ) {
  186. \OC_Util::addStyle( $application, $file );
  187. }
  188. /**
  189. * add a javascript file
  190. * @param string $application
  191. * @param string $file
  192. * @since 4.0.0
  193. */
  194. public static function addScript( $application, $file = null ) {
  195. \OC_Util::addScript( $application, $file );
  196. }
  197. /**
  198. * Add a translation JS file
  199. * @param string $application application id
  200. * @param string $languageCode language code, defaults to the current locale
  201. * @since 8.0.0
  202. */
  203. public static function addTranslations($application, $languageCode = null) {
  204. \OC_Util::addTranslations($application, $languageCode);
  205. }
  206. /**
  207. * Add a custom element to the header
  208. * If $text is null then the element will be written as empty element.
  209. * So use "" to get a closing tag.
  210. * @param string $tag tag name of the element
  211. * @param array $attributes array of attributes for the element
  212. * @param string $text the text content for the element
  213. * @since 4.0.0
  214. */
  215. public static function addHeader($tag, $attributes, $text=null) {
  216. \OC_Util::addHeader($tag, $attributes, $text);
  217. }
  218. /**
  219. * formats a timestamp in the "right" way
  220. * @param int $timestamp $timestamp
  221. * @param bool $dateOnly option to omit time from the result
  222. * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
  223. * @return string timestamp
  224. *
  225. * @deprecated 8.0.0 Use \OC::$server->query('DateTimeFormatter') instead
  226. * @since 4.0.0
  227. */
  228. public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) {
  229. return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone));
  230. }
  231. /**
  232. * check if some encrypted files are stored
  233. * @return bool
  234. *
  235. * @deprecated 8.1.0 No longer required
  236. * @since 6.0.0
  237. */
  238. public static function encryptedFiles() {
  239. return false;
  240. }
  241. /**
  242. * Creates an absolute url to the given app and file.
  243. * @param string $app app
  244. * @param string $file file
  245. * @param array $args array with param=>value, will be appended to the returned url
  246. * The value of $args will be urlencoded
  247. * @return string the url
  248. * @since 4.0.0 - parameter $args was added in 4.5.0
  249. */
  250. public static function linkToAbsolute( $app, $file, $args = array() ) {
  251. $urlGenerator = \OC::$server->getURLGenerator();
  252. return $urlGenerator->getAbsoluteURL(
  253. $urlGenerator->linkTo($app, $file, $args)
  254. );
  255. }
  256. /**
  257. * Creates an absolute url for remote use.
  258. * @param string $service id
  259. * @return string the url
  260. * @since 4.0.0
  261. */
  262. public static function linkToRemote( $service ) {
  263. $urlGenerator = \OC::$server->getURLGenerator();
  264. $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service;
  265. return $urlGenerator->getAbsoluteURL(
  266. $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '')
  267. );
  268. }
  269. /**
  270. * Creates an absolute url for public use
  271. * @param string $service id
  272. * @return string the url
  273. * @since 4.5.0
  274. */
  275. public static function linkToPublic($service) {
  276. return \OC_Helper::linkToPublic($service);
  277. }
  278. /**
  279. * Creates an url using a defined route
  280. * @param string $route
  281. * @param array $parameters
  282. * @internal param array $args with param=>value, will be appended to the returned url
  283. * @return string the url
  284. * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkToRoute($route, $parameters)
  285. * @since 5.0.0
  286. */
  287. public static function linkToRoute( $route, $parameters = array() ) {
  288. return \OC::$server->getURLGenerator()->linkToRoute($route, $parameters);
  289. }
  290. /**
  291. * Creates an url to the given app and file
  292. * @param string $app app
  293. * @param string $file file
  294. * @param array $args array with param=>value, will be appended to the returned url
  295. * The value of $args will be urlencoded
  296. * @return string the url
  297. * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkTo($app, $file, $args)
  298. * @since 4.0.0 - parameter $args was added in 4.5.0
  299. */
  300. public static function linkTo( $app, $file, $args = array() ) {
  301. return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
  302. }
  303. /**
  304. * Returns the server host, even if the website uses one or more reverse proxy
  305. * @return string the server host
  306. * @deprecated 8.1.0 Use \OCP\IRequest::getServerHost
  307. * @since 4.0.0
  308. */
  309. public static function getServerHost() {
  310. return \OC::$server->getRequest()->getServerHost();
  311. }
  312. /**
  313. * Returns the server host name without an eventual port number
  314. * @return string the server hostname
  315. * @since 5.0.0
  316. */
  317. public static function getServerHostName() {
  318. $host_name = self::getServerHost();
  319. // strip away port number (if existing)
  320. $colon_pos = strpos($host_name, ':');
  321. if ($colon_pos != FALSE) {
  322. $host_name = substr($host_name, 0, $colon_pos);
  323. }
  324. return $host_name;
  325. }
  326. /**
  327. * Returns the default email address
  328. * @param string $user_part the user part of the address
  329. * @return string the default email address
  330. *
  331. * Assembles a default email address (using the server hostname
  332. * and the given user part, and returns it
  333. * Example: when given lostpassword-noreply as $user_part param,
  334. * and is currently accessed via http(s)://example.com/,
  335. * it would return 'lostpassword-noreply@example.com'
  336. *
  337. * If the configuration value 'mail_from_address' is set in
  338. * config.php, this value will override the $user_part that
  339. * is passed to this function
  340. * @since 5.0.0
  341. */
  342. public static function getDefaultEmailAddress($user_part) {
  343. $config = \OC::$server->getConfig();
  344. $user_part = $config->getSystemValue('mail_from_address', $user_part);
  345. $host_name = self::getServerHostName();
  346. $host_name = $config->getSystemValue('mail_domain', $host_name);
  347. $defaultEmailAddress = $user_part.'@'.$host_name;
  348. $mailer = \OC::$server->getMailer();
  349. if ($mailer->validateMailAddress($defaultEmailAddress)) {
  350. return $defaultEmailAddress;
  351. }
  352. // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
  353. return $user_part.'@localhost.localdomain';
  354. }
  355. /**
  356. * Returns the server protocol. It respects reverse proxy servers and load balancers
  357. * @return string the server protocol
  358. * @deprecated 8.1.0 Use \OCP\IRequest::getServerProtocol
  359. * @since 4.5.0
  360. */
  361. public static function getServerProtocol() {
  362. return \OC::$server->getRequest()->getServerProtocol();
  363. }
  364. /**
  365. * Returns the request uri, even if the website uses one or more reverse proxies
  366. * @return string the request uri
  367. * @deprecated 8.1.0 Use \OCP\IRequest::getRequestUri
  368. * @since 5.0.0
  369. */
  370. public static function getRequestUri() {
  371. return \OC::$server->getRequest()->getRequestUri();
  372. }
  373. /**
  374. * Returns the script name, even if the website uses one or more reverse proxies
  375. * @return string the script name
  376. * @deprecated 8.1.0 Use \OCP\IRequest::getScriptName
  377. * @since 5.0.0
  378. */
  379. public static function getScriptName() {
  380. return \OC::$server->getRequest()->getScriptName();
  381. }
  382. /**
  383. * Creates path to an image
  384. * @param string $app app
  385. * @param string $image image name
  386. * @return string the url
  387. * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->imagePath($app, $image)
  388. * @since 4.0.0
  389. */
  390. public static function imagePath( $app, $image ) {
  391. return \OC::$server->getURLGenerator()->imagePath($app, $image);
  392. }
  393. /**
  394. * Make a human file size (2048 to 2 kB)
  395. * @param int $bytes file size in bytes
  396. * @return string a human readable file size
  397. * @since 4.0.0
  398. */
  399. public static function humanFileSize( $bytes ) {
  400. return(\OC_Helper::humanFileSize( $bytes ));
  401. }
  402. /**
  403. * Make a computer file size (2 kB to 2048)
  404. * @param string $str file size in a fancy format
  405. * @return int a file size in bytes
  406. *
  407. * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418
  408. * @since 4.0.0
  409. */
  410. public static function computerFileSize( $str ) {
  411. return(\OC_Helper::computerFileSize( $str ));
  412. }
  413. /**
  414. * connects a function to a hook
  415. *
  416. * @param string $signalClass class name of emitter
  417. * @param string $signalName name of signal
  418. * @param string|object $slotClass class name of slot
  419. * @param string $slotName name of slot
  420. * @return bool
  421. *
  422. * This function makes it very easy to connect to use hooks.
  423. *
  424. * TODO: write example
  425. * @since 4.0.0
  426. */
  427. static public function connectHook($signalClass, $signalName, $slotClass, $slotName ) {
  428. return(\OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName ));
  429. }
  430. /**
  431. * Emits a signal. To get data from the slot use references!
  432. * @param string $signalclass class name of emitter
  433. * @param string $signalname name of signal
  434. * @param array $params default: array() array with additional data
  435. * @return bool true if slots exists or false if not
  436. *
  437. * TODO: write example
  438. * @since 4.0.0
  439. */
  440. static public function emitHook( $signalclass, $signalname, $params = array()) {
  441. return(\OC_Hook::emit( $signalclass, $signalname, $params ));
  442. }
  443. /**
  444. * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
  445. * multiple OC_Template elements which invoke `callRegister`. If the value
  446. * would not be cached these unit-tests would fail.
  447. * @var string
  448. */
  449. private static $token = '';
  450. /**
  451. * Register an get/post call. This is important to prevent CSRF attacks
  452. * @since 4.5.0
  453. */
  454. public static function callRegister() {
  455. if(self::$token === '') {
  456. self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue();
  457. }
  458. return self::$token;
  459. }
  460. /**
  461. * Check an ajax get/post call if the request token is valid. exit if not.
  462. * @since 4.5.0
  463. * @deprecated 9.0.0 Use annotations based on the app framework.
  464. */
  465. public static function callCheck() {
  466. if (!(\OC::$server->getRequest()->passesCSRFCheck())) {
  467. exit();
  468. }
  469. }
  470. /**
  471. * Used to sanitize HTML
  472. *
  473. * This function is used to sanitize HTML and should be applied on any
  474. * string or array of strings before displaying it on a web page.
  475. *
  476. * @param string|array $value
  477. * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter.
  478. * @since 4.5.0
  479. */
  480. public static function sanitizeHTML($value) {
  481. return \OC_Util::sanitizeHTML($value);
  482. }
  483. /**
  484. * Public function to encode url parameters
  485. *
  486. * This function is used to encode path to file before output.
  487. * Encoding is done according to RFC 3986 with one exception:
  488. * Character '/' is preserved as is.
  489. *
  490. * @param string $component part of URI to encode
  491. * @return string
  492. * @since 6.0.0
  493. */
  494. public static function encodePath($component) {
  495. return(\OC_Util::encodePath($component));
  496. }
  497. /**
  498. * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
  499. *
  500. * @param array $input The array to work on
  501. * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
  502. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  503. * @return array
  504. * @since 4.5.0
  505. */
  506. public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
  507. return(\OC_Helper::mb_array_change_key_case($input, $case, $encoding));
  508. }
  509. /**
  510. * replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.
  511. *
  512. * @param string $string The input string. Opposite to the PHP build-in function does not accept an array.
  513. * @param string $replacement The replacement string.
  514. * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string.
  515. * @param int $length Length of the part to be replaced
  516. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  517. * @return string
  518. * @since 4.5.0
  519. * @deprecated 8.2.0 Use substr_replace() instead.
  520. */
  521. public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') {
  522. return substr_replace($string, $replacement, $start, $length);
  523. }
  524. /**
  525. * Replace all occurrences of the search string with the replacement string
  526. *
  527. * @param string $search The value being searched for, otherwise known as the needle. String.
  528. * @param string $replace The replacement string.
  529. * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack.
  530. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  531. * @param int $count If passed, this will be set to the number of replacements performed.
  532. * @return string
  533. * @since 4.5.0
  534. * @deprecated 8.2.0 Use str_replace() instead.
  535. */
  536. public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) {
  537. return str_replace($search, $replace, $subject, $count);
  538. }
  539. /**
  540. * performs a search in a nested array
  541. *
  542. * @param array $haystack the array to be searched
  543. * @param string $needle the search string
  544. * @param int $index optional, only search this key name
  545. * @return mixed the key of the matching field, otherwise false
  546. * @since 4.5.0
  547. */
  548. public static function recursiveArraySearch($haystack, $needle, $index = null) {
  549. return(\OC_Helper::recursiveArraySearch($haystack, $needle, $index));
  550. }
  551. /**
  552. * calculates the maximum upload size respecting system settings, free space and user quota
  553. *
  554. * @param string $dir the current folder where the user currently operates
  555. * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
  556. * @return int number of bytes representing
  557. * @since 5.0.0
  558. */
  559. public static function maxUploadFilesize($dir, $free = null) {
  560. return \OC_Helper::maxUploadFilesize($dir, $free);
  561. }
  562. /**
  563. * Calculate free space left within user quota
  564. * @param string $dir the current folder where the user currently operates
  565. * @return int number of bytes representing
  566. * @since 7.0.0
  567. */
  568. public static function freeSpace($dir) {
  569. return \OC_Helper::freeSpace($dir);
  570. }
  571. /**
  572. * Calculate PHP upload limit
  573. *
  574. * @return int number of bytes representing
  575. * @since 7.0.0
  576. */
  577. public static function uploadLimit() {
  578. return \OC_Helper::uploadLimit();
  579. }
  580. /**
  581. * Returns whether the given file name is valid
  582. * @param string $file file name to check
  583. * @return bool true if the file name is valid, false otherwise
  584. * @deprecated 8.1.0 use \OC\Files\View::verifyPath()
  585. * @since 7.0.0
  586. */
  587. public static function isValidFileName($file) {
  588. return \OC_Util::isValidFileName($file);
  589. }
  590. /**
  591. * Generates a cryptographic secure pseudo-random string
  592. * @param int $length of the random string
  593. * @return string
  594. * @deprecated 8.0.0 Use \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length); instead
  595. * @since 7.0.0
  596. */
  597. public static function generateRandomBytes($length = 30) {
  598. return \OC::$server->getSecureRandom()->generate($length, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
  599. }
  600. /**
  601. * Compare two strings to provide a natural sort
  602. * @param string $a first string to compare
  603. * @param string $b second string to compare
  604. * @return -1 if $b comes before $a, 1 if $a comes before $b
  605. * or 0 if the strings are identical
  606. * @since 7.0.0
  607. */
  608. public static function naturalSortCompare($a, $b) {
  609. return \OC\NaturalSort::getInstance()->compare($a, $b);
  610. }
  611. /**
  612. * check if a password is required for each public link
  613. * @return boolean
  614. * @since 7.0.0
  615. */
  616. public static function isPublicLinkPasswordRequired() {
  617. return \OC_Util::isPublicLinkPasswordRequired();
  618. }
  619. /**
  620. * check if share API enforces a default expire date
  621. * @return boolean
  622. * @since 8.0.0
  623. */
  624. public static function isDefaultExpireDateEnforced() {
  625. return \OC_Util::isDefaultExpireDateEnforced();
  626. }
  627. protected static $needUpgradeCache = null;
  628. /**
  629. * Checks whether the current version needs upgrade.
  630. *
  631. * @return bool true if upgrade is needed, false otherwise
  632. * @since 7.0.0
  633. */
  634. public static function needUpgrade() {
  635. if (!isset(self::$needUpgradeCache)) {
  636. self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getConfig());
  637. }
  638. return self::$needUpgradeCache;
  639. }
  640. }