Util.php 22 KB

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