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