Passed
Push — master ( 8a5259...821a0d )
by John
13:48 queued 13s
created
lib/public/Util.php 1 patch
Indentation   +547 added lines, -547 removed lines patch added patch discarded remove patch
@@ -55,551 +55,551 @@
 block discarded – undo
55 55
  * @since 4.0.0
56 56
  */
57 57
 class Util {
58
-	/**
59
-	 * @deprecated 14.0.0 use \OCP\ILogger::DEBUG
60
-	 */
61
-	public const DEBUG = 0;
62
-	/**
63
-	 * @deprecated 14.0.0 use \OCP\ILogger::INFO
64
-	 */
65
-	public const INFO = 1;
66
-	/**
67
-	 * @deprecated 14.0.0 use \OCP\ILogger::WARN
68
-	 */
69
-	public const WARN = 2;
70
-	/**
71
-	 * @deprecated 14.0.0 use \OCP\ILogger::ERROR
72
-	 */
73
-	public const ERROR = 3;
74
-	/**
75
-	 * @deprecated 14.0.0 use \OCP\ILogger::FATAL
76
-	 */
77
-	public const FATAL = 4;
78
-
79
-	/** @var \OCP\Share\IManager */
80
-	private static $shareManager;
81
-
82
-	/** @var array */
83
-	private static $scripts = [];
84
-
85
-	/** @var array */
86
-	private static $scriptDeps = [];
87
-
88
-	/** @var array */
89
-	private static $sortedScriptDeps = [];
90
-
91
-	/**
92
-	 * get the current installed version of Nextcloud
93
-	 * @return array
94
-	 * @since 4.0.0
95
-	 */
96
-	public static function getVersion() {
97
-		return \OC_Util::getVersion();
98
-	}
99
-
100
-	/**
101
-	 * @since 17.0.0
102
-	 */
103
-	public static function hasExtendedSupport(): bool {
104
-		try {
105
-			/** @var \OCP\Support\Subscription\IRegistry */
106
-			$subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class);
107
-			return $subscriptionRegistry->delegateHasExtendedSupport();
108
-		} catch (AppFramework\QueryException $e) {
109
-		}
110
-		return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false);
111
-	}
112
-
113
-	/**
114
-	 * Set current update channel
115
-	 * @param string $channel
116
-	 * @since 8.1.0
117
-	 */
118
-	public static function setChannel($channel) {
119
-		\OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel);
120
-	}
121
-
122
-	/**
123
-	 * Get current update channel
124
-	 * @return string
125
-	 * @since 8.1.0
126
-	 */
127
-	public static function getChannel() {
128
-		return \OC_Util::getChannel();
129
-	}
130
-
131
-	/**
132
-	 * write a message in the log
133
-	 * @param string $app
134
-	 * @param string $message
135
-	 * @param int $level
136
-	 * @since 4.0.0
137
-	 * @deprecated 13.0.0 use log of \OCP\ILogger
138
-	 */
139
-	public static function writeLog($app, $message, $level) {
140
-		$context = ['app' => $app];
141
-		\OC::$server->getLogger()->log($level, $message, $context);
142
-	}
143
-
144
-	/**
145
-	 * check if sharing is disabled for the current user
146
-	 *
147
-	 * @return boolean
148
-	 * @since 7.0.0
149
-	 * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser
150
-	 */
151
-	public static function isSharingDisabledForUser() {
152
-		if (self::$shareManager === null) {
153
-			self::$shareManager = \OC::$server->getShareManager();
154
-		}
155
-
156
-		$user = \OC::$server->getUserSession()->getUser();
157
-		if ($user !== null) {
158
-			$user = $user->getUID();
159
-		}
160
-
161
-		return self::$shareManager->sharingDisabledForUser($user);
162
-	}
163
-
164
-	/**
165
-	 * get l10n object
166
-	 * @param string $application
167
-	 * @param string|null $language
168
-	 * @return \OCP\IL10N
169
-	 * @since 6.0.0 - parameter $language was added in 8.0.0
170
-	 */
171
-	public static function getL10N($application, $language = null) {
172
-		return \OC::$server->getL10N($application, $language);
173
-	}
174
-
175
-	/**
176
-	 * add a css file
177
-	 * @param string $application
178
-	 * @param string $file
179
-	 * @since 4.0.0
180
-	 */
181
-	public static function addStyle($application, $file = null) {
182
-		\OC_Util::addStyle($application, $file);
183
-	}
184
-
185
-	/**
186
-	 * add a javascript file
187
-	 *
188
-	 * @param string $application
189
-	 * @param string|null $file
190
-	 * @param string $afterAppId
191
-	 * @since 4.0.0
192
-	 */
193
-	public static function addScript(string $application, string $file = null, string $afterAppId = 'core'): void {
194
-		if (!empty($application)) {
195
-			$path = "$application/js/$file";
196
-		} else {
197
-			$path = "js/$file";
198
-		}
199
-
200
-		// Inject js translations if we load a script for
201
-		// a specific app that is not core, as those js files
202
-		// need separate handling
203
-		if ($application !== 'core'
204
-			&& $file !== null
205
-			&& strpos($file, 'l10n') === false) {
206
-			self::addTranslations($application);
207
-		}
208
-
209
-		// store app in dependency list
210
-		if (!array_key_exists($application, self::$scriptDeps)) {
211
-			self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]);
212
-		} else {
213
-			self::$scriptDeps[$application]->addDep($afterAppId);
214
-		}
215
-
216
-		self::$scripts[$application][] = $path;
217
-	}
218
-
219
-	/**
220
-	 * Return the list of scripts injected to the page
221
-	 *
222
-	 * @return array
223
-	 * @since 24.0.0
224
-	 */
225
-	public static function getScripts(): array {
226
-		// Sort scriptDeps into sortedScriptDeps
227
-		$scriptSort = \OC::$server->get(AppScriptSort::class);
228
-		$sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps);
229
-
230
-		// Flatten array and remove duplicates
231
-		$sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : [];
232
-
233
-		// Override core-common and core-main order
234
-		array_unshift($sortedScripts, 'core/js/common', 'core/js/main');
235
-
236
-		return array_unique($sortedScripts);
237
-	}
238
-
239
-	/**
240
-	 * Add a translation JS file
241
-	 * @param string $application application id
242
-	 * @param string $languageCode language code, defaults to the current locale
243
-	 * @since 8.0.0
244
-	 */
245
-	public static function addTranslations($application, $languageCode = null) {
246
-		if (is_null($languageCode)) {
247
-			$languageCode = \OC::$server->getL10NFactory()->findLanguage($application);
248
-		}
249
-		if (!empty($application)) {
250
-			$path = "$application/l10n/$languageCode";
251
-		} else {
252
-			$path = "l10n/$languageCode";
253
-		}
254
-		self::$scripts[$application][] = $path;
255
-	}
256
-
257
-	/**
258
-	 * Add a custom element to the header
259
-	 * If $text is null then the element will be written as empty element.
260
-	 * So use "" to get a closing tag.
261
-	 * @param string $tag tag name of the element
262
-	 * @param array $attributes array of attributes for the element
263
-	 * @param string $text the text content for the element
264
-	 * @since 4.0.0
265
-	 */
266
-	public static function addHeader($tag, $attributes, $text = null) {
267
-		\OC_Util::addHeader($tag, $attributes, $text);
268
-	}
269
-
270
-	/**
271
-	 * Creates an absolute url to the given app and file.
272
-	 * @param string $app app
273
-	 * @param string $file file
274
-	 * @param array $args array with param=>value, will be appended to the returned url
275
-	 * 	The value of $args will be urlencoded
276
-	 * @return string the url
277
-	 * @since 4.0.0 - parameter $args was added in 4.5.0
278
-	 */
279
-	public static function linkToAbsolute($app, $file, $args = []) {
280
-		$urlGenerator = \OC::$server->getURLGenerator();
281
-		return $urlGenerator->getAbsoluteURL(
282
-			$urlGenerator->linkTo($app, $file, $args)
283
-		);
284
-	}
285
-
286
-	/**
287
-	 * Creates an absolute url for remote use.
288
-	 * @param string $service id
289
-	 * @return string the url
290
-	 * @since 4.0.0
291
-	 */
292
-	public static function linkToRemote($service) {
293
-		$urlGenerator = \OC::$server->getURLGenerator();
294
-		$remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service;
295
-		return $urlGenerator->getAbsoluteURL(
296
-			$remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '')
297
-		);
298
-	}
299
-
300
-	/**
301
-	 * Creates an absolute url for public use
302
-	 * @param string $service id
303
-	 * @return string the url
304
-	 * @since 4.5.0
305
-	 * @deprecated 15.0.0 - use OCP\IURLGenerator
306
-	 */
307
-	public static function linkToPublic($service) {
308
-		$urlGenerator = \OC::$server->getURLGenerator();
309
-		if ($service === 'files') {
310
-			return $urlGenerator->getAbsoluteURL('/s');
311
-		}
312
-		return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service);
313
-	}
314
-
315
-	/**
316
-	 * Returns the server host name without an eventual port number
317
-	 * @return string the server hostname
318
-	 * @since 5.0.0
319
-	 */
320
-	public static function getServerHostName() {
321
-		$host_name = \OC::$server->getRequest()->getServerHost();
322
-		// strip away port number (if existing)
323
-		$colon_pos = strpos($host_name, ':');
324
-		if ($colon_pos != false) {
325
-			$host_name = substr($host_name, 0, $colon_pos);
326
-		}
327
-		return $host_name;
328
-	}
329
-
330
-	/**
331
-	 * Returns the default email address
332
-	 * @param string $user_part the user part of the address
333
-	 * @return string the default email address
334
-	 *
335
-	 * Assembles a default email address (using the server hostname
336
-	 * and the given user part, and returns it
337
-	 * Example: when given lostpassword-noreply as $user_part param,
338
-	 *     and is currently accessed via http(s)://example.com/,
339
-	 *     it would return '[email protected]'
340
-	 *
341
-	 * If the configuration value 'mail_from_address' is set in
342
-	 * config.php, this value will override the $user_part that
343
-	 * is passed to this function
344
-	 * @since 5.0.0
345
-	 */
346
-	public static function getDefaultEmailAddress($user_part) {
347
-		$config = \OC::$server->getConfig();
348
-		$user_part = $config->getSystemValue('mail_from_address', $user_part);
349
-		$host_name = self::getServerHostName();
350
-		$host_name = $config->getSystemValue('mail_domain', $host_name);
351
-		$defaultEmailAddress = $user_part.'@'.$host_name;
352
-
353
-		$mailer = \OC::$server->getMailer();
354
-		if ($mailer->validateMailAddress($defaultEmailAddress)) {
355
-			return $defaultEmailAddress;
356
-		}
357
-
358
-		// in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
359
-		return $user_part.'@localhost.localdomain';
360
-	}
361
-
362
-	/**
363
-	 * Make a human file size (2048 to 2 kB)
364
-	 * @param int $bytes file size in bytes
365
-	 * @return string a human readable file size
366
-	 * @since 4.0.0
367
-	 */
368
-	public static function humanFileSize($bytes) {
369
-		return \OC_Helper::humanFileSize($bytes);
370
-	}
371
-
372
-	/**
373
-	 * Make a computer file size (2 kB to 2048)
374
-	 * @param string $str file size in a fancy format
375
-	 * @return float a file size in bytes
376
-	 *
377
-	 * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
378
-	 * @since 4.0.0
379
-	 */
380
-	public static function computerFileSize($str) {
381
-		return \OC_Helper::computerFileSize($str);
382
-	}
383
-
384
-	/**
385
-	 * connects a function to a hook
386
-	 *
387
-	 * @param string $signalClass class name of emitter
388
-	 * @param string $signalName name of signal
389
-	 * @param string|object $slotClass class name of slot
390
-	 * @param string $slotName name of slot
391
-	 * @return bool
392
-	 *
393
-	 * This function makes it very easy to connect to use hooks.
394
-	 *
395
-	 * TODO: write example
396
-	 * @since 4.0.0
397
-	 * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener
398
-	 */
399
-	public static function connectHook($signalClass, $signalName, $slotClass, $slotName) {
400
-		return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName);
401
-	}
402
-
403
-	/**
404
-	 * Emits a signal. To get data from the slot use references!
405
-	 * @param string $signalclass class name of emitter
406
-	 * @param string $signalname name of signal
407
-	 * @param array $params default: array() array with additional data
408
-	 * @return bool true if slots exists or false if not
409
-	 *
410
-	 * TODO: write example
411
-	 * @since 4.0.0
412
-	 * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent
413
-	 */
414
-	public static function emitHook($signalclass, $signalname, $params = []) {
415
-		return \OC_Hook::emit($signalclass, $signalname, $params);
416
-	}
417
-
418
-	/**
419
-	 * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
420
-	 * multiple OC_Template elements which invoke `callRegister`. If the value
421
-	 * would not be cached these unit-tests would fail.
422
-	 * @var string
423
-	 */
424
-	private static $token = '';
425
-
426
-	/**
427
-	 * Register an get/post call. This is important to prevent CSRF attacks
428
-	 * @since 4.5.0
429
-	 */
430
-	public static function callRegister() {
431
-		if (self::$token === '') {
432
-			self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue();
433
-		}
434
-		return self::$token;
435
-	}
436
-
437
-	/**
438
-	 * Used to sanitize HTML
439
-	 *
440
-	 * This function is used to sanitize HTML and should be applied on any
441
-	 * string or array of strings before displaying it on a web page.
442
-	 *
443
-	 * @param string|string[] $value
444
-	 * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter.
445
-	 * @since 4.5.0
446
-	 */
447
-	public static function sanitizeHTML($value) {
448
-		return \OC_Util::sanitizeHTML($value);
449
-	}
450
-
451
-	/**
452
-	 * Public function to encode url parameters
453
-	 *
454
-	 * This function is used to encode path to file before output.
455
-	 * Encoding is done according to RFC 3986 with one exception:
456
-	 * Character '/' is preserved as is.
457
-	 *
458
-	 * @param string $component part of URI to encode
459
-	 * @return string
460
-	 * @since 6.0.0
461
-	 */
462
-	public static function encodePath($component) {
463
-		return \OC_Util::encodePath($component);
464
-	}
465
-
466
-	/**
467
-	 * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
468
-	 *
469
-	 * @param array $input The array to work on
470
-	 * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
471
-	 * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
472
-	 * @return array
473
-	 * @since 4.5.0
474
-	 */
475
-	public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
476
-		return \OC_Helper::mb_array_change_key_case($input, $case, $encoding);
477
-	}
478
-
479
-	/**
480
-	 * performs a search in a nested array
481
-	 *
482
-	 * @param array $haystack the array to be searched
483
-	 * @param string $needle the search string
484
-	 * @param mixed $index optional, only search this key name
485
-	 * @return mixed the key of the matching field, otherwise false
486
-	 * @since 4.5.0
487
-	 * @deprecated 15.0.0
488
-	 */
489
-	public static function recursiveArraySearch($haystack, $needle, $index = null) {
490
-		return \OC_Helper::recursiveArraySearch($haystack, $needle, $index);
491
-	}
492
-
493
-	/**
494
-	 * calculates the maximum upload size respecting system settings, free space and user quota
495
-	 *
496
-	 * @param string $dir the current folder where the user currently operates
497
-	 * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
498
-	 * @return int number of bytes representing
499
-	 * @since 5.0.0
500
-	 */
501
-	public static function maxUploadFilesize($dir, $free = null) {
502
-		return \OC_Helper::maxUploadFilesize($dir, $free);
503
-	}
504
-
505
-	/**
506
-	 * Calculate free space left within user quota
507
-	 * @param string $dir the current folder where the user currently operates
508
-	 * @return int number of bytes representing
509
-	 * @since 7.0.0
510
-	 */
511
-	public static function freeSpace($dir) {
512
-		return \OC_Helper::freeSpace($dir);
513
-	}
514
-
515
-	/**
516
-	 * Calculate PHP upload limit
517
-	 *
518
-	 * @return int number of bytes representing
519
-	 * @since 7.0.0
520
-	 */
521
-	public static function uploadLimit() {
522
-		return \OC_Helper::uploadLimit();
523
-	}
524
-
525
-	/**
526
-	 * Returns whether the given file name is valid
527
-	 * @param string $file file name to check
528
-	 * @return bool true if the file name is valid, false otherwise
529
-	 * @deprecated 8.1.0 use \OC\Files\View::verifyPath()
530
-	 * @since 7.0.0
531
-	 * @suppress PhanDeprecatedFunction
532
-	 */
533
-	public static function isValidFileName($file) {
534
-		return \OC_Util::isValidFileName($file);
535
-	}
536
-
537
-	/**
538
-	 * Compare two strings to provide a natural sort
539
-	 * @param string $a first string to compare
540
-	 * @param string $b second string to compare
541
-	 * @return int -1 if $b comes before $a, 1 if $a comes before $b
542
-	 * or 0 if the strings are identical
543
-	 * @since 7.0.0
544
-	 */
545
-	public static function naturalSortCompare($a, $b) {
546
-		return \OC\NaturalSort::getInstance()->compare($a, $b);
547
-	}
548
-
549
-	/**
550
-	 * check if a password is required for each public link
551
-	 * @return boolean
552
-	 * @since 7.0.0
553
-	 */
554
-	public static function isPublicLinkPasswordRequired() {
555
-		return \OC_Util::isPublicLinkPasswordRequired();
556
-	}
557
-
558
-	/**
559
-	 * check if share API enforces a default expire date
560
-	 * @return boolean
561
-	 * @since 8.0.0
562
-	 */
563
-	public static function isDefaultExpireDateEnforced() {
564
-		return \OC_Util::isDefaultExpireDateEnforced();
565
-	}
566
-
567
-	protected static $needUpgradeCache = null;
568
-
569
-	/**
570
-	 * Checks whether the current version needs upgrade.
571
-	 *
572
-	 * @return bool true if upgrade is needed, false otherwise
573
-	 * @since 7.0.0
574
-	 */
575
-	public static function needUpgrade() {
576
-		if (!isset(self::$needUpgradeCache)) {
577
-			self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getSystemConfig());
578
-		}
579
-		return self::$needUpgradeCache;
580
-	}
581
-
582
-	/**
583
-	 * Sometimes a string has to be shortened to fit within a certain maximum
584
-	 * data length in bytes. substr() you may break multibyte characters,
585
-	 * because it operates on single byte level. mb_substr() operates on
586
-	 * characters, so does not ensure that the shortend string satisfies the
587
-	 * max length in bytes.
588
-	 *
589
-	 * For example, json_encode is messing with multibyte characters a lot,
590
-	 * replacing them with something along "\u1234".
591
-	 *
592
-	 * This function shortens the string with by $accurancy (-5) from
593
-	 * $dataLength characters, until it fits within $dataLength bytes.
594
-	 *
595
-	 * @since 23.0.0
596
-	 */
597
-	public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string {
598
-		$temp = mb_substr($subject, 0, $dataLength);
599
-		// json encodes encapsulates the string in double quotes, they need to be substracted
600
-		while ((strlen(json_encode($temp)) - 2) > $dataLength) {
601
-			$temp = mb_substr($temp, 0, -$accuracy);
602
-		}
603
-		return $temp;
604
-	}
58
+    /**
59
+     * @deprecated 14.0.0 use \OCP\ILogger::DEBUG
60
+     */
61
+    public const DEBUG = 0;
62
+    /**
63
+     * @deprecated 14.0.0 use \OCP\ILogger::INFO
64
+     */
65
+    public const INFO = 1;
66
+    /**
67
+     * @deprecated 14.0.0 use \OCP\ILogger::WARN
68
+     */
69
+    public const WARN = 2;
70
+    /**
71
+     * @deprecated 14.0.0 use \OCP\ILogger::ERROR
72
+     */
73
+    public const ERROR = 3;
74
+    /**
75
+     * @deprecated 14.0.0 use \OCP\ILogger::FATAL
76
+     */
77
+    public const FATAL = 4;
78
+
79
+    /** @var \OCP\Share\IManager */
80
+    private static $shareManager;
81
+
82
+    /** @var array */
83
+    private static $scripts = [];
84
+
85
+    /** @var array */
86
+    private static $scriptDeps = [];
87
+
88
+    /** @var array */
89
+    private static $sortedScriptDeps = [];
90
+
91
+    /**
92
+     * get the current installed version of Nextcloud
93
+     * @return array
94
+     * @since 4.0.0
95
+     */
96
+    public static function getVersion() {
97
+        return \OC_Util::getVersion();
98
+    }
99
+
100
+    /**
101
+     * @since 17.0.0
102
+     */
103
+    public static function hasExtendedSupport(): bool {
104
+        try {
105
+            /** @var \OCP\Support\Subscription\IRegistry */
106
+            $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class);
107
+            return $subscriptionRegistry->delegateHasExtendedSupport();
108
+        } catch (AppFramework\QueryException $e) {
109
+        }
110
+        return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false);
111
+    }
112
+
113
+    /**
114
+     * Set current update channel
115
+     * @param string $channel
116
+     * @since 8.1.0
117
+     */
118
+    public static function setChannel($channel) {
119
+        \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel);
120
+    }
121
+
122
+    /**
123
+     * Get current update channel
124
+     * @return string
125
+     * @since 8.1.0
126
+     */
127
+    public static function getChannel() {
128
+        return \OC_Util::getChannel();
129
+    }
130
+
131
+    /**
132
+     * write a message in the log
133
+     * @param string $app
134
+     * @param string $message
135
+     * @param int $level
136
+     * @since 4.0.0
137
+     * @deprecated 13.0.0 use log of \OCP\ILogger
138
+     */
139
+    public static function writeLog($app, $message, $level) {
140
+        $context = ['app' => $app];
141
+        \OC::$server->getLogger()->log($level, $message, $context);
142
+    }
143
+
144
+    /**
145
+     * check if sharing is disabled for the current user
146
+     *
147
+     * @return boolean
148
+     * @since 7.0.0
149
+     * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser
150
+     */
151
+    public static function isSharingDisabledForUser() {
152
+        if (self::$shareManager === null) {
153
+            self::$shareManager = \OC::$server->getShareManager();
154
+        }
155
+
156
+        $user = \OC::$server->getUserSession()->getUser();
157
+        if ($user !== null) {
158
+            $user = $user->getUID();
159
+        }
160
+
161
+        return self::$shareManager->sharingDisabledForUser($user);
162
+    }
163
+
164
+    /**
165
+     * get l10n object
166
+     * @param string $application
167
+     * @param string|null $language
168
+     * @return \OCP\IL10N
169
+     * @since 6.0.0 - parameter $language was added in 8.0.0
170
+     */
171
+    public static function getL10N($application, $language = null) {
172
+        return \OC::$server->getL10N($application, $language);
173
+    }
174
+
175
+    /**
176
+     * add a css file
177
+     * @param string $application
178
+     * @param string $file
179
+     * @since 4.0.0
180
+     */
181
+    public static function addStyle($application, $file = null) {
182
+        \OC_Util::addStyle($application, $file);
183
+    }
184
+
185
+    /**
186
+     * add a javascript file
187
+     *
188
+     * @param string $application
189
+     * @param string|null $file
190
+     * @param string $afterAppId
191
+     * @since 4.0.0
192
+     */
193
+    public static function addScript(string $application, string $file = null, string $afterAppId = 'core'): void {
194
+        if (!empty($application)) {
195
+            $path = "$application/js/$file";
196
+        } else {
197
+            $path = "js/$file";
198
+        }
199
+
200
+        // Inject js translations if we load a script for
201
+        // a specific app that is not core, as those js files
202
+        // need separate handling
203
+        if ($application !== 'core'
204
+            && $file !== null
205
+            && strpos($file, 'l10n') === false) {
206
+            self::addTranslations($application);
207
+        }
208
+
209
+        // store app in dependency list
210
+        if (!array_key_exists($application, self::$scriptDeps)) {
211
+            self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]);
212
+        } else {
213
+            self::$scriptDeps[$application]->addDep($afterAppId);
214
+        }
215
+
216
+        self::$scripts[$application][] = $path;
217
+    }
218
+
219
+    /**
220
+     * Return the list of scripts injected to the page
221
+     *
222
+     * @return array
223
+     * @since 24.0.0
224
+     */
225
+    public static function getScripts(): array {
226
+        // Sort scriptDeps into sortedScriptDeps
227
+        $scriptSort = \OC::$server->get(AppScriptSort::class);
228
+        $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps);
229
+
230
+        // Flatten array and remove duplicates
231
+        $sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : [];
232
+
233
+        // Override core-common and core-main order
234
+        array_unshift($sortedScripts, 'core/js/common', 'core/js/main');
235
+
236
+        return array_unique($sortedScripts);
237
+    }
238
+
239
+    /**
240
+     * Add a translation JS file
241
+     * @param string $application application id
242
+     * @param string $languageCode language code, defaults to the current locale
243
+     * @since 8.0.0
244
+     */
245
+    public static function addTranslations($application, $languageCode = null) {
246
+        if (is_null($languageCode)) {
247
+            $languageCode = \OC::$server->getL10NFactory()->findLanguage($application);
248
+        }
249
+        if (!empty($application)) {
250
+            $path = "$application/l10n/$languageCode";
251
+        } else {
252
+            $path = "l10n/$languageCode";
253
+        }
254
+        self::$scripts[$application][] = $path;
255
+    }
256
+
257
+    /**
258
+     * Add a custom element to the header
259
+     * If $text is null then the element will be written as empty element.
260
+     * So use "" to get a closing tag.
261
+     * @param string $tag tag name of the element
262
+     * @param array $attributes array of attributes for the element
263
+     * @param string $text the text content for the element
264
+     * @since 4.0.0
265
+     */
266
+    public static function addHeader($tag, $attributes, $text = null) {
267
+        \OC_Util::addHeader($tag, $attributes, $text);
268
+    }
269
+
270
+    /**
271
+     * Creates an absolute url to the given app and file.
272
+     * @param string $app app
273
+     * @param string $file file
274
+     * @param array $args array with param=>value, will be appended to the returned url
275
+     * 	The value of $args will be urlencoded
276
+     * @return string the url
277
+     * @since 4.0.0 - parameter $args was added in 4.5.0
278
+     */
279
+    public static function linkToAbsolute($app, $file, $args = []) {
280
+        $urlGenerator = \OC::$server->getURLGenerator();
281
+        return $urlGenerator->getAbsoluteURL(
282
+            $urlGenerator->linkTo($app, $file, $args)
283
+        );
284
+    }
285
+
286
+    /**
287
+     * Creates an absolute url for remote use.
288
+     * @param string $service id
289
+     * @return string the url
290
+     * @since 4.0.0
291
+     */
292
+    public static function linkToRemote($service) {
293
+        $urlGenerator = \OC::$server->getURLGenerator();
294
+        $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service;
295
+        return $urlGenerator->getAbsoluteURL(
296
+            $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '')
297
+        );
298
+    }
299
+
300
+    /**
301
+     * Creates an absolute url for public use
302
+     * @param string $service id
303
+     * @return string the url
304
+     * @since 4.5.0
305
+     * @deprecated 15.0.0 - use OCP\IURLGenerator
306
+     */
307
+    public static function linkToPublic($service) {
308
+        $urlGenerator = \OC::$server->getURLGenerator();
309
+        if ($service === 'files') {
310
+            return $urlGenerator->getAbsoluteURL('/s');
311
+        }
312
+        return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service);
313
+    }
314
+
315
+    /**
316
+     * Returns the server host name without an eventual port number
317
+     * @return string the server hostname
318
+     * @since 5.0.0
319
+     */
320
+    public static function getServerHostName() {
321
+        $host_name = \OC::$server->getRequest()->getServerHost();
322
+        // strip away port number (if existing)
323
+        $colon_pos = strpos($host_name, ':');
324
+        if ($colon_pos != false) {
325
+            $host_name = substr($host_name, 0, $colon_pos);
326
+        }
327
+        return $host_name;
328
+    }
329
+
330
+    /**
331
+     * Returns the default email address
332
+     * @param string $user_part the user part of the address
333
+     * @return string the default email address
334
+     *
335
+     * Assembles a default email address (using the server hostname
336
+     * and the given user part, and returns it
337
+     * Example: when given lostpassword-noreply as $user_part param,
338
+     *     and is currently accessed via http(s)://example.com/,
339
+     *     it would return '[email protected]'
340
+     *
341
+     * If the configuration value 'mail_from_address' is set in
342
+     * config.php, this value will override the $user_part that
343
+     * is passed to this function
344
+     * @since 5.0.0
345
+     */
346
+    public static function getDefaultEmailAddress($user_part) {
347
+        $config = \OC::$server->getConfig();
348
+        $user_part = $config->getSystemValue('mail_from_address', $user_part);
349
+        $host_name = self::getServerHostName();
350
+        $host_name = $config->getSystemValue('mail_domain', $host_name);
351
+        $defaultEmailAddress = $user_part.'@'.$host_name;
352
+
353
+        $mailer = \OC::$server->getMailer();
354
+        if ($mailer->validateMailAddress($defaultEmailAddress)) {
355
+            return $defaultEmailAddress;
356
+        }
357
+
358
+        // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
359
+        return $user_part.'@localhost.localdomain';
360
+    }
361
+
362
+    /**
363
+     * Make a human file size (2048 to 2 kB)
364
+     * @param int $bytes file size in bytes
365
+     * @return string a human readable file size
366
+     * @since 4.0.0
367
+     */
368
+    public static function humanFileSize($bytes) {
369
+        return \OC_Helper::humanFileSize($bytes);
370
+    }
371
+
372
+    /**
373
+     * Make a computer file size (2 kB to 2048)
374
+     * @param string $str file size in a fancy format
375
+     * @return float a file size in bytes
376
+     *
377
+     * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
378
+     * @since 4.0.0
379
+     */
380
+    public static function computerFileSize($str) {
381
+        return \OC_Helper::computerFileSize($str);
382
+    }
383
+
384
+    /**
385
+     * connects a function to a hook
386
+     *
387
+     * @param string $signalClass class name of emitter
388
+     * @param string $signalName name of signal
389
+     * @param string|object $slotClass class name of slot
390
+     * @param string $slotName name of slot
391
+     * @return bool
392
+     *
393
+     * This function makes it very easy to connect to use hooks.
394
+     *
395
+     * TODO: write example
396
+     * @since 4.0.0
397
+     * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener
398
+     */
399
+    public static function connectHook($signalClass, $signalName, $slotClass, $slotName) {
400
+        return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName);
401
+    }
402
+
403
+    /**
404
+     * Emits a signal. To get data from the slot use references!
405
+     * @param string $signalclass class name of emitter
406
+     * @param string $signalname name of signal
407
+     * @param array $params default: array() array with additional data
408
+     * @return bool true if slots exists or false if not
409
+     *
410
+     * TODO: write example
411
+     * @since 4.0.0
412
+     * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent
413
+     */
414
+    public static function emitHook($signalclass, $signalname, $params = []) {
415
+        return \OC_Hook::emit($signalclass, $signalname, $params);
416
+    }
417
+
418
+    /**
419
+     * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
420
+     * multiple OC_Template elements which invoke `callRegister`. If the value
421
+     * would not be cached these unit-tests would fail.
422
+     * @var string
423
+     */
424
+    private static $token = '';
425
+
426
+    /**
427
+     * Register an get/post call. This is important to prevent CSRF attacks
428
+     * @since 4.5.0
429
+     */
430
+    public static function callRegister() {
431
+        if (self::$token === '') {
432
+            self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue();
433
+        }
434
+        return self::$token;
435
+    }
436
+
437
+    /**
438
+     * Used to sanitize HTML
439
+     *
440
+     * This function is used to sanitize HTML and should be applied on any
441
+     * string or array of strings before displaying it on a web page.
442
+     *
443
+     * @param string|string[] $value
444
+     * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter.
445
+     * @since 4.5.0
446
+     */
447
+    public static function sanitizeHTML($value) {
448
+        return \OC_Util::sanitizeHTML($value);
449
+    }
450
+
451
+    /**
452
+     * Public function to encode url parameters
453
+     *
454
+     * This function is used to encode path to file before output.
455
+     * Encoding is done according to RFC 3986 with one exception:
456
+     * Character '/' is preserved as is.
457
+     *
458
+     * @param string $component part of URI to encode
459
+     * @return string
460
+     * @since 6.0.0
461
+     */
462
+    public static function encodePath($component) {
463
+        return \OC_Util::encodePath($component);
464
+    }
465
+
466
+    /**
467
+     * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
468
+     *
469
+     * @param array $input The array to work on
470
+     * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
471
+     * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
472
+     * @return array
473
+     * @since 4.5.0
474
+     */
475
+    public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
476
+        return \OC_Helper::mb_array_change_key_case($input, $case, $encoding);
477
+    }
478
+
479
+    /**
480
+     * performs a search in a nested array
481
+     *
482
+     * @param array $haystack the array to be searched
483
+     * @param string $needle the search string
484
+     * @param mixed $index optional, only search this key name
485
+     * @return mixed the key of the matching field, otherwise false
486
+     * @since 4.5.0
487
+     * @deprecated 15.0.0
488
+     */
489
+    public static function recursiveArraySearch($haystack, $needle, $index = null) {
490
+        return \OC_Helper::recursiveArraySearch($haystack, $needle, $index);
491
+    }
492
+
493
+    /**
494
+     * calculates the maximum upload size respecting system settings, free space and user quota
495
+     *
496
+     * @param string $dir the current folder where the user currently operates
497
+     * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
498
+     * @return int number of bytes representing
499
+     * @since 5.0.0
500
+     */
501
+    public static function maxUploadFilesize($dir, $free = null) {
502
+        return \OC_Helper::maxUploadFilesize($dir, $free);
503
+    }
504
+
505
+    /**
506
+     * Calculate free space left within user quota
507
+     * @param string $dir the current folder where the user currently operates
508
+     * @return int number of bytes representing
509
+     * @since 7.0.0
510
+     */
511
+    public static function freeSpace($dir) {
512
+        return \OC_Helper::freeSpace($dir);
513
+    }
514
+
515
+    /**
516
+     * Calculate PHP upload limit
517
+     *
518
+     * @return int number of bytes representing
519
+     * @since 7.0.0
520
+     */
521
+    public static function uploadLimit() {
522
+        return \OC_Helper::uploadLimit();
523
+    }
524
+
525
+    /**
526
+     * Returns whether the given file name is valid
527
+     * @param string $file file name to check
528
+     * @return bool true if the file name is valid, false otherwise
529
+     * @deprecated 8.1.0 use \OC\Files\View::verifyPath()
530
+     * @since 7.0.0
531
+     * @suppress PhanDeprecatedFunction
532
+     */
533
+    public static function isValidFileName($file) {
534
+        return \OC_Util::isValidFileName($file);
535
+    }
536
+
537
+    /**
538
+     * Compare two strings to provide a natural sort
539
+     * @param string $a first string to compare
540
+     * @param string $b second string to compare
541
+     * @return int -1 if $b comes before $a, 1 if $a comes before $b
542
+     * or 0 if the strings are identical
543
+     * @since 7.0.0
544
+     */
545
+    public static function naturalSortCompare($a, $b) {
546
+        return \OC\NaturalSort::getInstance()->compare($a, $b);
547
+    }
548
+
549
+    /**
550
+     * check if a password is required for each public link
551
+     * @return boolean
552
+     * @since 7.0.0
553
+     */
554
+    public static function isPublicLinkPasswordRequired() {
555
+        return \OC_Util::isPublicLinkPasswordRequired();
556
+    }
557
+
558
+    /**
559
+     * check if share API enforces a default expire date
560
+     * @return boolean
561
+     * @since 8.0.0
562
+     */
563
+    public static function isDefaultExpireDateEnforced() {
564
+        return \OC_Util::isDefaultExpireDateEnforced();
565
+    }
566
+
567
+    protected static $needUpgradeCache = null;
568
+
569
+    /**
570
+     * Checks whether the current version needs upgrade.
571
+     *
572
+     * @return bool true if upgrade is needed, false otherwise
573
+     * @since 7.0.0
574
+     */
575
+    public static function needUpgrade() {
576
+        if (!isset(self::$needUpgradeCache)) {
577
+            self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getSystemConfig());
578
+        }
579
+        return self::$needUpgradeCache;
580
+    }
581
+
582
+    /**
583
+     * Sometimes a string has to be shortened to fit within a certain maximum
584
+     * data length in bytes. substr() you may break multibyte characters,
585
+     * because it operates on single byte level. mb_substr() operates on
586
+     * characters, so does not ensure that the shortend string satisfies the
587
+     * max length in bytes.
588
+     *
589
+     * For example, json_encode is messing with multibyte characters a lot,
590
+     * replacing them with something along "\u1234".
591
+     *
592
+     * This function shortens the string with by $accurancy (-5) from
593
+     * $dataLength characters, until it fits within $dataLength bytes.
594
+     *
595
+     * @since 23.0.0
596
+     */
597
+    public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string {
598
+        $temp = mb_substr($subject, 0, $dataLength);
599
+        // json encodes encapsulates the string in double quotes, they need to be substracted
600
+        while ((strlen(json_encode($temp)) - 2) > $dataLength) {
601
+            $temp = mb_substr($temp, 0, -$accuracy);
602
+        }
603
+        return $temp;
604
+    }
605 605
 }
Please login to merge, or discard this patch.