Completed
Push — master ( 2c3fbd...0d4753 )
by Thomas
12:58
created

Util::getStatusInfo()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 1
b 0
f 0
nc 24
nop 2
dl 0
loc 32
rs 6.7272
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Björn Schießle <[email protected]>
6
 * @author Frank Karlitschek <[email protected]>
7
 * @author Georg Ehrke <[email protected]>
8
 * @author Individual IT Services <[email protected]>
9
 * @author Jens-Christian Fischer <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Lukas Reschke <[email protected]>
12
 * @author Michael Gapczynski <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Nicolas Grekas <[email protected]>
15
 * @author Pellaeon Lin <[email protected]>
16
 * @author Randolph Carter <[email protected]>
17
 * @author Robin Appelman <[email protected]>
18
 * @author Robin McCorkell <[email protected]>
19
 * @author Roeland Jago Douma <[email protected]>
20
 * @author Stefan Herbrechtsmeier <[email protected]>
21
 * @author Thomas Müller <[email protected]>
22
 * @author Thomas Tanghus <[email protected]>
23
 * @author Victor Dubiniuk <[email protected]>
24
 * @author Vincent Petry <[email protected]>
25
 *
26
 * @copyright Copyright (c) 2017, ownCloud GmbH
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
/**
44
 * Public interface of ownCloud for apps to use.
45
 * Utility Class.
46
 *
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
/**
55
 * This class provides different helper functions to make the life of a developer easier
56
 * @since 4.0.0
57
 */
58
class Util {
59
	// consts for Logging
60
	const DEBUG=0;
61
	const INFO=1;
62
	const WARN=2;
63
	const ERROR=3;
64
	const FATAL=4;
65
66
	/** \OCP\Share\IManager */
67
	private static $shareManager;
68
69
	/**
70
	 * get the current installed version of ownCloud
71
	 * @return array
72
	 * @since 4.0.0
73
	 */
74
	public static function getVersion() {
75
		return(\OC_Util::getVersion());
76
	}
77
	
78
	/**
79
	 * Set current update channel
80
	 * @param string $channel
81
	 * @since 8.1.0
82
	 */
83
	public static function setChannel($channel) {
84
		//Flush timestamp to reload version.php
85
		\OC::$server->getSession()->set('OC_Version_Timestamp', 0);
86
		\OC::$server->getAppConfig()->setValue('core', 'OC_Channel', $channel);
87
	}
88
	
89
	/**
90
	 * Get current update channel
91
	 * @return string
92
	 * @since 8.1.0
93
	 */
94
	public static function getChannel() {
95
		return \OC_Util::getChannel();
96
	}
97
98
	/**
99
	 * send an email
100
	 * @param string $toaddress
101
	 * @param string $toname
102
	 * @param string $subject
103
	 * @param string $mailtext
104
	 * @param string $fromaddress
105
	 * @param string $fromname
106
	 * @param int $html
107
	 * @param string $altbody
108
	 * @param string $ccaddress
109
	 * @param string $ccname
110
	 * @param string $bcc
111
	 * @deprecated 8.1.0 Use \OCP\Mail\IMailer instead
112
	 * @since 4.0.0
113
	 */
114
	public static function sendMail($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
115
		$html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') {
116
		$mailer = \OC::$server->getMailer();
117
		$message = $mailer->createMessage();
118
		$message->setTo([$toaddress => $toname]);
119
		$message->setSubject($subject);
120
		$message->setPlainBody($mailtext);
121
		$message->setFrom([$fromaddress => $fromname]);
122
		if($html === 1) {
123
			$message->setHtmlBody($altbody);
124
		}
125
126
		if($altbody === '') {
127
			$message->setHtmlBody($mailtext);
128
			$message->setPlainBody('');
129
		} else {
130
			$message->setHtmlBody($mailtext);
131
			$message->setPlainBody($altbody);
132
		}
133
134
		if(!empty($ccaddress)) {
135
			if(!empty($ccname)) {
136
				$message->setCc([$ccaddress => $ccname]);
137
			} else {
138
				$message->setCc([$ccaddress]);
139
			}
140
		}
141
		if(!empty($bcc)) {
142
			$message->setBcc([$bcc]);
143
		}
144
145
		$mailer->send($message);
146
	}
147
148
	/**
149
	 * write a message in the log
150
	 * @param string $app
151
	 * @param string $message
152
	 * @param int $level
153
	 * @since 4.0.0
154
	 */
155
	public static function writeLog( $app, $message, $level ) {
156
		$context = ['app' => $app];
157
		\OC::$server->getLogger()->log($level, $message, $context);
158
	}
159
160
	/**
161
	 * write exception into the log
162
	 * @param string $app app name
163
	 * @param \Exception $ex exception to log
164
	 * @param int $level log level, defaults to \OCP\Util::FATAL
165
	 * @since ....0.0 - parameter $level was added in 7.0.0
166
	 * @deprecated 8.2.0 use logException of \OCP\ILogger
167
	 */
168
	public static function logException( $app, \Exception $ex, $level = \OCP\Util::FATAL ) {
0 ignored issues
show
Unused Code introduced by
The parameter $level is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
		\OC::$server->getLogger()->logException($ex, ['app' => $app]);
170
	}
171
172
	/**
173
	 * check if sharing is disabled for the current user
174
	 *
175
	 * @return boolean
176
	 * @since 7.0.0
177
	 * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser
178
	 */
179
	public static function isSharingDisabledForUser() {
180
		if (self::$shareManager === null) {
181
			self::$shareManager = \OC::$server->getShareManager();
182
		}
183
184
		$user = \OC::$server->getUserSession()->getUser();
185
		if ($user !== null) {
186
			$user = $user->getUID();
187
		}
188
189
		return self::$shareManager->sharingDisabledForUser($user);
190
	}
191
192
	/**
193
	 * get l10n object
194
	 * @param string $application
195
	 * @param string|null $language
196
	 * @return \OCP\IL10N
197
	 * @since 6.0.0 - parameter $language was added in 8.0.0
198
	 */
199
	public static function getL10N($application, $language = null) {
200
		return \OC::$server->getL10N($application, $language);
201
	}
202
203
	/**
204
	 * add a css file
205
	 * @param string $application
206
	 * @param string $file
207
	 * @since 4.0.0
208
	 */
209
	public static function addStyle( $application, $file = null ) {
210
		\OC_Util::addStyle( $application, $file );
211
	}
212
213
	/**
214
	 * add a javascript file
215
	 * @param string $application
216
	 * @param string $file
217
	 * @since 4.0.0
218
	 */
219
	public static function addScript( $application, $file = null ) {
220
		\OC_Util::addScript( $application, $file );
221
	}
222
223
	/**
224
	 * Add a translation JS file
225
	 * @param string $application application id
226
	 * @param string $languageCode language code, defaults to the current locale
227
	 * @since 8.0.0
228
	 */
229
	public static function addTranslations($application, $languageCode = null) {
230
		\OC_Util::addTranslations($application, $languageCode);
231
	}
232
233
	/**
234
	 * Add a custom element to the header
235
	 * If $text is null then the element will be written as empty element.
236
	 * So use "" to get a closing tag.
237
	 * @param string $tag tag name of the element
238
	 * @param array $attributes array of attributes for the element
239
	 * @param string $text the text content for the element
240
	 * @since 4.0.0
241
	 */
242
	public static function addHeader($tag, $attributes, $text=null) {
243
		\OC_Util::addHeader($tag, $attributes, $text);
244
	}
245
246
	/**
247
	 * formats a timestamp in the "right" way
248
	 * @param int $timestamp $timestamp
249
	 * @param bool $dateOnly option to omit time from the result
250
	 * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
251
	 * @return string timestamp
252
	 *
253
	 * @deprecated 8.0.0 Use \OC::$server->query('DateTimeFormatter') instead
254
	 * @since 4.0.0
255
	 */
256
	public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) {
257
		return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone));
258
	}
259
260
	/**
261
	 * check if some encrypted files are stored
262
	 * @return bool
263
	 *
264
	 * @deprecated 8.1.0 No longer required
265
	 * @since 6.0.0
266
	 */
267
	public static function encryptedFiles() {
268
		return false;
269
	}
270
271
	/**
272
	 * Creates an absolute url to the given app and file.
273
	 * @param string $app app
274
	 * @param string $file file
275
	 * @param array $args array with param=>value, will be appended to the returned url
276
	 * 	The value of $args will be urlencoded
277
	 * @return string the url
278
	 * @since 4.0.0 - parameter $args was added in 4.5.0
279
	 */
280
	public static function linkToAbsolute( $app, $file, $args = []) {
281
		$urlGenerator = \OC::$server->getURLGenerator();
282
		return $urlGenerator->getAbsoluteURL(
283
			$urlGenerator->linkTo($app, $file, $args)
284
		);
285
	}
286
287
	/**
288
	 * Creates an absolute url for remote use.
289
	 * @param string $service id
290
	 * @return string the url
291
	 * @since 4.0.0
292
	 */
293
	public static function linkToRemote( $service ) {
294
		$urlGenerator = \OC::$server->getURLGenerator();
295
		$remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service;
296
		return $urlGenerator->getAbsoluteURL(
297
			$remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '')
298
		);
299
	}
300
301
	/**
302
	 * Creates an absolute url for public use
303
	 * @param string $service id
304
	 * @return string the url
305
	 * @since 4.5.0
306
	 */
307
	public static function linkToPublic($service) {
308
		return \OC_Helper::linkToPublic($service);
309
	}
310
311
	/**
312
	 * Creates an url using a defined route
313
	 * @param string $route
314
	 * @param array $parameters
315
	 * @internal param array $args with param=>value, will be appended to the returned url
316
	 * @return string the url
317
	 * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkToRoute($route, $parameters)
318
	 * @since 5.0.0
319
	 */
320
	public static function linkToRoute( $route, $parameters = []) {
321
		return \OC::$server->getURLGenerator()->linkToRoute($route, $parameters);
322
	}
323
324
	/**
325
	 * Creates an url to the given app and file
326
	 * @param string $app app
327
	 * @param string $file file
328
	 * @param array $args array with param=>value, will be appended to the returned url
329
	 * 	The value of $args will be urlencoded
330
	 * @return string the url
331
	 * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkTo($app, $file, $args)
332
	 * @since 4.0.0 - parameter $args was added in 4.5.0
333
	 */
334
	public static function linkTo( $app, $file, $args = []) {
335
		return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
336
	}
337
338
	/**
339
	 * Returns the server host, even if the website uses one or more reverse proxy
340
	 * @return string the server host
341
	 * @deprecated 8.1.0 Use \OCP\IRequest::getServerHost
342
	 * @since 4.0.0
343
	 */
344
	public static function getServerHost() {
345
		return \OC::$server->getRequest()->getServerHost();
346
	}
347
348
	/**
349
	 * Returns the server host name without an eventual port number
350
	 * @return string the server hostname
351
	 * @since 5.0.0
352
	 */
353
	public static function getServerHostName() {
354
		$host_name = self::getServerHost();
355
		// strip away port number (if existing)
356
		$colon_pos = strpos($host_name, ':');
357
		if ($colon_pos != FALSE) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $colon_pos of type integer to the boolean FALSE. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
358
			$host_name = substr($host_name, 0, $colon_pos);
359
		}
360
		return $host_name;
361
	}
362
363
	/**
364
	 * Returns the default email address
365
	 * @param string $user_part the user part of the address
366
	 * @return string the default email address
367
	 *
368
	 * Assembles a default email address (using the server hostname
369
	 * and the given user part, and returns it
370
	 * Example: when given lostpassword-noreply as $user_part param,
371
	 *     and is currently accessed via http(s)://example.com/,
372
	 *     it would return '[email protected]'
373
	 *
374
	 * If the configuration value 'mail_from_address' is set in
375
	 * config.php, this value will override the $user_part that
376
	 * is passed to this function
377
	 * @since 5.0.0
378
	 */
379
	public static function getDefaultEmailAddress($user_part) {
380
		$config = \OC::$server->getConfig();
381
		$user_part = $config->getSystemValue('mail_from_address', $user_part);
382
		$host_name = self::getServerHostName();
383
		$host_name = $config->getSystemValue('mail_domain', $host_name);
384
		$defaultEmailAddress = $user_part.'@'.$host_name;
385
386
		$mailer = \OC::$server->getMailer();
387
		if ($mailer->validateMailAddress($defaultEmailAddress)) {
388
			return $defaultEmailAddress;
389
		}
390
391
		// in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
392
		return $user_part.'@localhost.localdomain';
393
	}
394
395
	/**
396
	 * Returns the server protocol. It respects reverse proxy servers and load balancers
397
	 * @return string the server protocol
398
	 * @deprecated 8.1.0 Use \OCP\IRequest::getServerProtocol
399
	 * @since 4.5.0
400
	 */
401
	public static function getServerProtocol() {
402
		return \OC::$server->getRequest()->getServerProtocol();
403
	}
404
405
	/**
406
	 * Returns the request uri, even if the website uses one or more reverse proxies
407
	 * @return string the request uri
408
	 * @deprecated 8.1.0 Use \OCP\IRequest::getRequestUri
409
	 * @since 5.0.0
410
	 */
411
	public static function getRequestUri() {
412
		return \OC::$server->getRequest()->getRequestUri();
413
	}
414
415
	/**
416
	 * Returns the script name, even if the website uses one or more reverse proxies
417
	 * @return string the script name
418
	 * @deprecated 8.1.0 Use \OCP\IRequest::getScriptName
419
	 * @since 5.0.0
420
	 */
421
	public static function getScriptName() {
422
		return \OC::$server->getRequest()->getScriptName();
423
	}
424
425
	/**
426
	 * Creates path to an image
427
	 * @param string $app app
428
	 * @param string $image image name
429
	 * @return string the url
430
	 * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->imagePath($app, $image)
431
	 * @since 4.0.0
432
	 */
433
	public static function imagePath( $app, $image ) {
434
		return \OC::$server->getURLGenerator()->imagePath($app, $image);
435
	}
436
437
	/**
438
	 * Make a human file size (2048 to 2 kB)
439
	 * @param int $bytes file size in bytes
440
	 * @return string a human readable file size
441
	 * @since 4.0.0
442
	 */
443
	public static function humanFileSize( $bytes ) {
444
		return(\OC_Helper::humanFileSize( $bytes ));
445
	}
446
447
	/**
448
	 * Make a computer file size (2 kB to 2048)
449
	 * @param string $str file size in a fancy format
450
	 * @return int a file size in bytes
451
	 *
452
	 * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418
453
	 * @since 4.0.0
454
	 */
455
	public static function computerFileSize( $str ) {
456
		return(\OC_Helper::computerFileSize( $str ));
457
	}
458
459
	/**
460
	 * connects a function to a hook
461
	 *
462
	 * @param string $signalClass class name of emitter
463
	 * @param string $signalName name of signal
464
	 * @param string|object $slotClass class name of slot
465
	 * @param string $slotName name of slot
466
	 * @return bool
467
	 *
468
	 * This function makes it very easy to connect to use hooks.
469
	 *
470
	 * TODO: write example
471
	 * @since 4.0.0
472
	 */
473
	static public function connectHook($signalClass, $signalName, $slotClass, $slotName ) {
474
		return(\OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName ));
475
	}
476
477
	/**
478
	 * Emits a signal. To get data from the slot use references!
479
	 * @param string $signalclass class name of emitter
480
	 * @param string $signalname name of signal
481
	 * @param array $params default: array() array with additional data
482
	 * @return bool true if slots exists or false if not
483
	 *
484
	 * TODO: write example
485
	 * @since 4.0.0
486
	 */
487
	static public function emitHook( $signalclass, $signalname, $params = []) {
488
		return(\OC_Hook::emit( $signalclass, $signalname, $params ));
489
	}
490
491
	/**
492
	 * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
493
	 * multiple OC_Template elements which invoke `callRegister`. If the value
494
	 * would not be cached these unit-tests would fail.
495
	 * @var string
496
	 */
497
	private static $token = '';
498
499
	/**
500
	 * Register an get/post call. This is important to prevent CSRF attacks
501
	 * @since 4.5.0
502
	 */
503
	public static function callRegister() {
504
		if(self::$token === '') {
505
			self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue();
506
		}
507
		return self::$token;
508
	}
509
510
	/**
511
	 * Check an ajax get/post call if the request token is valid. exit if not.
512
	 * @since 4.5.0
513
	 * @deprecated 9.0.0 Use annotations based on the app framework.
514
	 */
515
	public static function callCheck() {
516
		if (!(\OC::$server->getRequest()->passesCSRFCheck())) {
517
			exit();
518
		}
519
	}
520
521
	/**
522
	 * Used to sanitize HTML
523
	 *
524
	 * This function is used to sanitize HTML and should be applied on any
525
	 * string or array of strings before displaying it on a web page.
526
	 *
527
	 * @param string|array $value
528
	 * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter.
529
	 * @since 4.5.0
530
	 */
531
	public static function sanitizeHTML($value) {
532
		return \OC_Util::sanitizeHTML($value);
533
	}
534
535
	/**
536
	 * Public function to encode url parameters
537
	 *
538
	 * This function is used to encode path to file before output.
539
	 * Encoding is done according to RFC 3986 with one exception:
540
	 * Character '/' is preserved as is.
541
	 *
542
	 * @param string $component part of URI to encode
543
	 * @return string
544
	 * @since 6.0.0
545
	 */
546
	public static function encodePath($component) {
547
		return(\OC_Util::encodePath($component));
548
	}
549
550
	/**
551
	 * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
552
	 *
553
	 * @param array $input The array to work on
554
	 * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
555
	 * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
556
	 * @return array
557
	 * @since 4.5.0
558
	 */
559
	public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
560
		return(\OC_Helper::mb_array_change_key_case($input, $case, $encoding));
561
	}
562
563
	/**
564
	 * replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.
565
	 *
566
	 * @param string $string The input string. Opposite to the PHP build-in function does not accept an array.
567
	 * @param string $replacement The replacement string.
568
	 * @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.
569
	 * @param int $length Length of the part to be replaced
570
	 * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
571
	 * @return string
572
	 * @since 4.5.0
573
	 * @deprecated 8.2.0 Use substr_replace() instead.
574
	 */
575
	public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') {
0 ignored issues
show
Unused Code introduced by
The parameter $encoding is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
576
		return substr_replace($string, $replacement, $start, $length);
577
	}
578
579
	/**
580
	 * Replace all occurrences of the search string with the replacement string
581
	 *
582
	 * @param string $search The value being searched for, otherwise known as the needle. String.
583
	 * @param string $replace The replacement string.
584
	 * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack.
585
	 * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
586
	 * @param int $count If passed, this will be set to the number of replacements performed.
587
	 * @return string
588
	 * @since 4.5.0
589
	 * @deprecated 8.2.0 Use str_replace() instead.
590
	 */
591
	public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $encoding is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
592
		return str_replace($search, $replace, $subject, $count);
593
	}
594
595
	/**
596
	 * performs a search in a nested array
597
	 *
598
	 * @param array $haystack the array to be searched
599
	 * @param string $needle the search string
600
	 * @param int $index optional, only search this key name
601
	 * @return mixed the key of the matching field, otherwise false
602
	 * @since 4.5.0
603
	 */
604
	public static function recursiveArraySearch($haystack, $needle, $index = null) {
605
		return(\OC_Helper::recursiveArraySearch($haystack, $needle, $index));
606
	}
607
608
	/**
609
	 * calculates the maximum upload size respecting system settings, free space and user quota
610
	 *
611
	 * @param string $dir the current folder where the user currently operates
612
	 * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
613
	 * @return int number of bytes representing
614
	 * @since 5.0.0
615
	 */
616
	public static function maxUploadFilesize($dir, $free = null) {
617
		return \OC_Helper::maxUploadFilesize($dir, $free);
618
	}
619
620
	/**
621
	 * Calculate free space left within user quota
622
	 * @param string $dir the current folder where the user currently operates
623
	 * @return int number of bytes representing
624
	 * @since 7.0.0
625
	 */
626
	public static function freeSpace($dir) {
627
		return \OC_Helper::freeSpace($dir);
628
	}
629
630
	/**
631
	 * Calculate PHP upload limit
632
	 *
633
	 * @return int number of bytes representing
634
	 * @since 7.0.0
635
	 */
636
	public static function uploadLimit() {
637
		return \OC_Helper::uploadLimit();
638
	}
639
640
	/**
641
	 * Returns whether the given file name is valid
642
	 * @param string $file file name to check
643
	 * @return bool true if the file name is valid, false otherwise
644
	 * @deprecated 8.1.0 use \OC\Files\View::verifyPath()
645
	 * @since 7.0.0
646
	 */
647
	public static function isValidFileName($file) {
648
		return \OC_Util::isValidFileName($file);
649
	}
650
651
	/**
652
	 * Generates a cryptographic secure pseudo-random string
653
	 * @param int $length of the random string
654
	 * @return string
655
	 * @deprecated 8.0.0 Use \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length); instead
656
	 * @since 7.0.0
657
	 */
658
	public static function generateRandomBytes($length = 30) {
659
		return \OC::$server->getSecureRandom()->generate($length, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
660
	}
661
662
	/**
663
	 * Compare two strings to provide a natural sort
664
	 * @param string $a first string to compare
665
	 * @param string $b second string to compare
666
	 * @return -1 if $b comes before $a, 1 if $a comes before $b
0 ignored issues
show
Documentation introduced by
The doc-type -1 could not be parsed: Unknown type name "-1" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
667
	 * or 0 if the strings are identical
668
	 * @since 7.0.0
669
	 */
670
	public static function naturalSortCompare($a, $b) {
671
		return \OC\NaturalSort::getInstance()->compare($a, $b);
672
	}
673
674
	/**
675
	 * check if a password is required for each public link
676
	 * @return boolean
677
	 * @since 7.0.0
678
	 */
679
	public static function isPublicLinkPasswordRequired() {
680
		return \OC_Util::isPublicLinkPasswordRequired();
681
	}
682
683
	/**
684
	 * check if share API enforces a default expire date
685
	 * @return boolean
686
	 * @since 8.0.0
687
	 */
688
	public static function isDefaultExpireDateEnforced() {
689
		return \OC_Util::isDefaultExpireDateEnforced();
690
	}
691
692
	protected static $needUpgradeCache = null;
693
694
	/**
695
	 * Checks whether the current version needs upgrade.
696
	 *
697
	 * @return bool true if upgrade is needed, false otherwise
698
	 * @since 7.0.0
699
	 */
700
	public static function needUpgrade() {
701
		if (!isset(self::$needUpgradeCache)) {
702
			self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getConfig());
703
		}		
704
		return self::$needUpgradeCache;
705
	}
706
707
	/**
708
	 * Collects all status infos.
709
	 *
710
	 * @param bool $includeVersion
711
	 * @return array
712
	 * @since 10.0
713
	 */
714
	public static function getStatusInfo($includeVersion = false, $serverHide = false) {
715
		$systemConfig = \OC::$server->getSystemConfig();
716
717
		$installed = (bool) $systemConfig->getValue('installed', false);
718
		$maintenance = (bool) $systemConfig->getValue('maintenance', false);
719
		# see core/lib/private/legacy/defaults.php and core/themes/example/defaults.php
720
		# for description and defaults
721
		$defaults = new \OCP\Defaults();
722
		$values = [
723
			'installed'=> $installed ? 'true' : 'false',
724
			'maintenance' => $maintenance ? 'true' : 'false',
725
			'needsDbUpgrade' => self::needUpgrade() ? 'true' : 'false',
726
			'version' => '',
727
			'versionstring' => '',
728
			'edition' => '',
729
			'productname' => ''];
730
731
		# expose version and servername details 
732
		if ($includeVersion || (bool) $systemConfig->getValue('version.hide', false) === false) {
733
			$values['version'] = implode('.', self::getVersion());
734
			$values['versionstring'] = \OC_Util::getVersionString();
735
			$values['edition'] = \OC_Util::getEditionString();
736
			$values['productname'] = $defaults->getName();
737
			# expose the servername only if allowed via version, but never when called via status.php
738
			if ($serverHide === false) {
739
				$values['hostname'] = gethostname();
740
			}
741
		}
742
743
744
		return $values;
745
	}
746
}
747