You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.php 22KB

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