Completed
Push — master ( 52012b...54b1f4 )
by Christoph
23:28 queued 11:46
created

Util::isPublicLinkPasswordRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Björn Schießle <[email protected]>
8
 * @author Frank Karlitschek <[email protected]>
9
 * @author Georg Ehrke <[email protected]>
10
 * @author Individual IT Services <[email protected]>
11
 * @author Jens-Christian Fischer <[email protected]>
12
 * @author Joas Schilling <[email protected]>
13
 * @author Julius Härtl <[email protected]>
14
 * @author Lukas Reschke <[email protected]>
15
 * @author Michael Gapczynski <[email protected]>
16
 * @author Morris Jobke <[email protected]>
17
 * @author Nicolas Grekas <[email protected]>
18
 * @author Pellaeon Lin <[email protected]>
19
 * @author Randolph Carter <[email protected]>
20
 * @author Robin Appelman <[email protected]>
21
 * @author Robin McCorkell <[email protected]>
22
 * @author Roeland Jago Douma <[email protected]>
23
 * @author Stefan Herbrechtsmeier <[email protected]>
24
 * @author Thomas Müller <[email protected]>
25
 * @author Thomas Tanghus <[email protected]>
26
 * @author Victor Dubiniuk <[email protected]>
27
 * @author Vincent Petry <[email protected]>
28
 *
29
 * @license AGPL-3.0
30
 *
31
 * This code is free software: you can redistribute it and/or modify
32
 * it under the terms of the GNU Affero General Public License, version 3,
33
 * as published by the Free Software Foundation.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38
 * GNU Affero General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU Affero General Public License, version 3,
41
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
42
 *
43
 */
44
45
/**
46
 * Public interface of ownCloud for apps to use.
47
 * Utility Class.
48
 *
49
 */
50
51
// use OCP namespace for all classes that are considered public.
52
// This means that they should be used by apps instead of the internal ownCloud classes
53
namespace OCP;
54
55
/**
56
 * This class provides different helper functions to make the life of a developer easier
57
 * @since 4.0.0
58
 */
59
class Util {
60
	/**
61
	 * @deprecated 14.0.0 use \OCP\ILogger::DEBUG
62
	 */
63
	const DEBUG=0;
64
	/**
65
	 * @deprecated 14.0.0 use \OCP\ILogger::INFO
66
	 */
67
	const INFO=1;
68
	/**
69
	 * @deprecated 14.0.0 use \OCP\ILogger::WARN
70
	 */
71
	const WARN=2;
72
	/**
73
	 * @deprecated 14.0.0 use \OCP\ILogger::ERROR
74
	 */
75
	const ERROR=3;
76
	/**
77
	 * @deprecated 14.0.0 use \OCP\ILogger::FATAL
78
	 */
79
	const FATAL=4;
80
81
	/** \OCP\Share\IManager */
82
	private static $shareManager;
83
84
	/**
85
	 * get the current installed version of Nextcloud
86
	 * @return array
87
	 * @since 4.0.0
88
	 */
89
	public static function getVersion() {
90
		return \OC_Util::getVersion();
91
	}
92
	
93
	/**
94
	 * Set current update channel
95
	 * @param string $channel
96
	 * @since 8.1.0
97
	 */
98
	public static function setChannel($channel) {
99
		\OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel);
100
	}
101
	
102
	/**
103
	 * Get current update channel
104
	 * @return string
105
	 * @since 8.1.0
106
	 */
107
	public static function getChannel() {
108
		return \OC_Util::getChannel();
109
	}
110
111
	/**
112
	 * write a message in the log
113
	 * @param string $app
114
	 * @param string $message
115
	 * @param int $level
116
	 * @since 4.0.0
117
	 * @deprecated 13.0.0 use log of \OCP\ILogger
118
	 */
119
	public static function writeLog( $app, $message, $level ) {
120
		$context = ['app' => $app];
121
		\OC::$server->getLogger()->log($level, $message, $context);
122
	}
123
124
	/**
125
	 * check if sharing is disabled for the current user
126
	 *
127
	 * @return boolean
128
	 * @since 7.0.0
129
	 * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser
130
	 */
131
	public static function isSharingDisabledForUser() {
132
		if (self::$shareManager === null) {
133
			self::$shareManager = \OC::$server->getShareManager();
134
		}
135
136
		$user = \OC::$server->getUserSession()->getUser();
137
		if ($user !== null) {
138
			$user = $user->getUID();
139
		}
140
141
		return self::$shareManager->sharingDisabledForUser($user);
142
	}
143
144
	/**
145
	 * get l10n object
146
	 * @param string $application
147
	 * @param string|null $language
148
	 * @return \OCP\IL10N
149
	 * @since 6.0.0 - parameter $language was added in 8.0.0
150
	 */
151
	public static function getL10N($application, $language = null) {
152
		return \OC::$server->getL10N($application, $language);
153
	}
154
155
	/**
156
	 * add a css file
157
	 * @param string $application
158
	 * @param string $file
159
	 * @since 4.0.0
160
	 */
161
	public static function addStyle( $application, $file = null ) {
162
		\OC_Util::addStyle( $application, $file );
163
	}
164
165
	/**
166
	 * add a javascript file
167
	 * @param string $application
168
	 * @param string $file
169
	 * @since 4.0.0
170
	 */
171
	public static function addScript( $application, $file = null ) {
172
		\OC_Util::addScript( $application, $file );
173
	}
174
175
	/**
176
	 * Add a translation JS file
177
	 * @param string $application application id
178
	 * @param string $languageCode language code, defaults to the current locale
179
	 * @since 8.0.0
180
	 */
181
	public static function addTranslations($application, $languageCode = null) {
182
		\OC_Util::addTranslations($application, $languageCode);
183
	}
184
185
	/**
186
	 * Add a custom element to the header
187
	 * If $text is null then the element will be written as empty element.
188
	 * So use "" to get a closing tag.
189
	 * @param string $tag tag name of the element
190
	 * @param array $attributes array of attributes for the element
191
	 * @param string $text the text content for the element
192
	 * @since 4.0.0
193
	 */
194
	public static function addHeader($tag, $attributes, $text=null) {
195
		\OC_Util::addHeader($tag, $attributes, $text);
196
	}
197
198
	/**
199
	 * Creates an absolute url to the given app and file.
200
	 * @param string $app app
201
	 * @param string $file file
202
	 * @param array $args array with param=>value, will be appended to the returned url
203
	 * 	The value of $args will be urlencoded
204
	 * @return string the url
205
	 * @since 4.0.0 - parameter $args was added in 4.5.0
206
	 */
207
	public static function linkToAbsolute( $app, $file, $args = array() ) {
208
		$urlGenerator = \OC::$server->getURLGenerator();
209
		return $urlGenerator->getAbsoluteURL(
210
			$urlGenerator->linkTo($app, $file, $args)
211
		);
212
	}
213
214
	/**
215
	 * Creates an absolute url for remote use.
216
	 * @param string $service id
217
	 * @return string the url
218
	 * @since 4.0.0
219
	 */
220
	public static function linkToRemote( $service ) {
221
		$urlGenerator = \OC::$server->getURLGenerator();
222
		$remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service;
223
		return $urlGenerator->getAbsoluteURL(
224
			$remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '')
225
		);
226
	}
227
228
	/**
229
	 * Creates an absolute url for public use
230
	 * @param string $service id
231
	 * @return string the url
232
	 * @since 4.5.0
233
	 */
234
	public static function linkToPublic($service) {
235
		return \OC_Helper::linkToPublic($service);
236
	}
237
238
	/**
239
	 * Returns the server host name without an eventual port number
240
	 * @return string the server hostname
241
	 * @since 5.0.0
242
	 */
243
	public static function getServerHostName() {
244
		$host_name = \OC::$server->getRequest()->getServerHost();
245
		// strip away port number (if existing)
246
		$colon_pos = strpos($host_name, ':');
247
		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...
248
			$host_name = substr($host_name, 0, $colon_pos);
249
		}
250
		return $host_name;
251
	}
252
253
	/**
254
	 * Returns the default email address
255
	 * @param string $user_part the user part of the address
256
	 * @return string the default email address
257
	 *
258
	 * Assembles a default email address (using the server hostname
259
	 * and the given user part, and returns it
260
	 * Example: when given lostpassword-noreply as $user_part param,
261
	 *     and is currently accessed via http(s)://example.com/,
262
	 *     it would return '[email protected]'
263
	 *
264
	 * If the configuration value 'mail_from_address' is set in
265
	 * config.php, this value will override the $user_part that
266
	 * is passed to this function
267
	 * @since 5.0.0
268
	 */
269
	public static function getDefaultEmailAddress($user_part) {
270
		$config = \OC::$server->getConfig();
271
		$user_part = $config->getSystemValue('mail_from_address', $user_part);
272
		$host_name = self::getServerHostName();
273
		$host_name = $config->getSystemValue('mail_domain', $host_name);
274
		$defaultEmailAddress = $user_part.'@'.$host_name;
275
276
		$mailer = \OC::$server->getMailer();
277
		if ($mailer->validateMailAddress($defaultEmailAddress)) {
278
			return $defaultEmailAddress;
279
		}
280
281
		// in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
282
		return $user_part.'@localhost.localdomain';
283
	}
284
285
	/**
286
	 * Make a human file size (2048 to 2 kB)
287
	 * @param int $bytes file size in bytes
288
	 * @return string a human readable file size
289
	 * @since 4.0.0
290
	 */
291
	public static function humanFileSize($bytes) {
292
		return \OC_Helper::humanFileSize($bytes);
293
	}
294
295
	/**
296
	 * Make a computer file size (2 kB to 2048)
297
	 * @param string $str file size in a fancy format
298
	 * @return float a file size in bytes
299
	 *
300
	 * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418
301
	 * @since 4.0.0
302
	 */
303
	public static function computerFileSize($str) {
304
		return \OC_Helper::computerFileSize($str);
305
	}
306
307
	/**
308
	 * connects a function to a hook
309
	 *
310
	 * @param string $signalClass class name of emitter
311
	 * @param string $signalName name of signal
312
	 * @param string|object $slotClass class name of slot
313
	 * @param string $slotName name of slot
314
	 * @return bool
315
	 *
316
	 * This function makes it very easy to connect to use hooks.
317
	 *
318
	 * TODO: write example
319
	 * @since 4.0.0
320
	 */
321
	static public function connectHook($signalClass, $signalName, $slotClass, $slotName) {
322
		return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName);
323
	}
324
325
	/**
326
	 * Emits a signal. To get data from the slot use references!
327
	 * @param string $signalclass class name of emitter
328
	 * @param string $signalname name of signal
329
	 * @param array $params default: array() array with additional data
330
	 * @return bool true if slots exists or false if not
331
	 *
332
	 * TODO: write example
333
	 * @since 4.0.0
334
	 */
335
	static public function emitHook($signalclass, $signalname, $params = array()) {
336
		return \OC_Hook::emit($signalclass, $signalname, $params);
337
	}
338
339
	/**
340
	 * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
341
	 * multiple OC_Template elements which invoke `callRegister`. If the value
342
	 * would not be cached these unit-tests would fail.
343
	 * @var string
344
	 */
345
	private static $token = '';
346
347
	/**
348
	 * Register an get/post call. This is important to prevent CSRF attacks
349
	 * @since 4.5.0
350
	 */
351
	public static function callRegister() {
352
		if(self::$token === '') {
353
			self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue();
354
		}
355
		return self::$token;
356
	}
357
358
	/**
359
	 * Check an ajax get/post call if the request token is valid. exit if not.
360
	 * @since 4.5.0
361
	 * @deprecated 9.0.0 Use annotations based on the app framework.
362
	 */
363
	public static function callCheck() {
364
		if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
365
			header('Location: '.\OC::$WEBROOT);
366
			exit();
367
		}
368
369
		if (!\OC::$server->getRequest()->passesCSRFCheck()) {
370
			exit();
371
		}
372
	}
373
374
	/**
375
	 * Used to sanitize HTML
376
	 *
377
	 * This function is used to sanitize HTML and should be applied on any
378
	 * string or array of strings before displaying it on a web page.
379
	 *
380
	 * @param string|array $value
381
	 * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter.
382
	 * @since 4.5.0
383
	 */
384
	public static function sanitizeHTML($value) {
385
		return \OC_Util::sanitizeHTML($value);
386
	}
387
388
	/**
389
	 * Public function to encode url parameters
390
	 *
391
	 * This function is used to encode path to file before output.
392
	 * Encoding is done according to RFC 3986 with one exception:
393
	 * Character '/' is preserved as is.
394
	 *
395
	 * @param string $component part of URI to encode
396
	 * @return string
397
	 * @since 6.0.0
398
	 */
399
	public static function encodePath($component) {
400
		return \OC_Util::encodePath($component);
401
	}
402
403
	/**
404
	 * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
405
	 *
406
	 * @param array $input The array to work on
407
	 * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
408
	 * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
409
	 * @return array
410
	 * @since 4.5.0
411
	 */
412
	public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
413
		return \OC_Helper::mb_array_change_key_case($input, $case, $encoding);
414
	}
415
416
	/**
417
	 * performs a search in a nested array
418
	 *
419
	 * @param array $haystack the array to be searched
420
	 * @param string $needle the search string
421
	 * @param mixed $index optional, only search this key name
422
	 * @return mixed the key of the matching field, otherwise false
423
	 * @since 4.5.0
424
	 */
425
	public static function recursiveArraySearch($haystack, $needle, $index = null) {
426
		return \OC_Helper::recursiveArraySearch($haystack, $needle, $index);
427
	}
428
429
	/**
430
	 * calculates the maximum upload size respecting system settings, free space and user quota
431
	 *
432
	 * @param string $dir the current folder where the user currently operates
433
	 * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
434
	 * @return int number of bytes representing
435
	 * @since 5.0.0
436
	 */
437
	public static function maxUploadFilesize($dir, $free = null) {
438
		return \OC_Helper::maxUploadFilesize($dir, $free);
439
	}
440
441
	/**
442
	 * Calculate free space left within user quota
443
	 * @param string $dir the current folder where the user currently operates
444
	 * @return int number of bytes representing
445
	 * @since 7.0.0
446
	 */
447
	public static function freeSpace($dir) {
448
		return \OC_Helper::freeSpace($dir);
449
	}
450
451
	/**
452
	 * Calculate PHP upload limit
453
	 *
454
	 * @return int number of bytes representing
455
	 * @since 7.0.0
456
	 */
457
	public static function uploadLimit() {
458
		return \OC_Helper::uploadLimit();
459
	}
460
461
	/**
462
	 * Returns whether the given file name is valid
463
	 * @param string $file file name to check
464
	 * @return bool true if the file name is valid, false otherwise
465
	 * @deprecated 8.1.0 use \OC\Files\View::verifyPath()
466
	 * @since 7.0.0
467
	 * @suppress PhanDeprecatedFunction
468
	 */
469
	public static function isValidFileName($file) {
470
		return \OC_Util::isValidFileName($file);
471
	}
472
473
	/**
474
	 * Compare two strings to provide a natural sort
475
	 * @param string $a first string to compare
476
	 * @param string $b second string to compare
477
	 * @return int -1 if $b comes before $a, 1 if $a comes before $b
478
	 * or 0 if the strings are identical
479
	 * @since 7.0.0
480
	 */
481
	public static function naturalSortCompare($a, $b) {
482
		return \OC\NaturalSort::getInstance()->compare($a, $b);
483
	}
484
485
	/**
486
	 * check if a password is required for each public link
487
	 * @return boolean
488
	 * @since 7.0.0
489
	 */
490
	public static function isPublicLinkPasswordRequired() {
491
		return \OC_Util::isPublicLinkPasswordRequired();
492
	}
493
494
	/**
495
	 * check if share API enforces a default expire date
496
	 * @return boolean
497
	 * @since 8.0.0
498
	 */
499
	public static function isDefaultExpireDateEnforced() {
500
		return \OC_Util::isDefaultExpireDateEnforced();
501
	}
502
503
	protected static $needUpgradeCache = null;
504
505
	/**
506
	 * Checks whether the current version needs upgrade.
507
	 *
508
	 * @return bool true if upgrade is needed, false otherwise
509
	 * @since 7.0.0
510
	 */
511
	public static function needUpgrade() {
512
		if (!isset(self::$needUpgradeCache)) {
513
			self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getSystemConfig());
514
		}		
515
		return self::$needUpgradeCache;
516
	}
517
518
	/**
519
	 * is this Internet explorer ?
520
	 *
521
	 * @return boolean
522
	 * @since 14.0.0
523
	 */
524
	public static function isIe() {
525
		return \OC_Util::isIe();
526
	}
527
}
528