Passed
Push — master ( d1af72...ccd8da )
by Roeland
33:49 queued 22:20
created

Util::isPublicLinkPasswordRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
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
	 * @deprecated 15.0.0 - use OCP\IURLGenerator
234
	 */
235
	public static function linkToPublic($service) {
236
		$urlGenerator = \OC::$server->getURLGenerator();
237
		if ($service === 'files') {
238
			return $urlGenerator->getAbsoluteURL('/s');
239
		}
240
		return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service);
241
	}
242
243
	/**
244
	 * Returns the server host name without an eventual port number
245
	 * @return string the server hostname
246
	 * @since 5.0.0
247
	 */
248
	public static function getServerHostName() {
249
		$host_name = \OC::$server->getRequest()->getServerHost();
250
		// strip away port number (if existing)
251
		$colon_pos = strpos($host_name, ':');
252
		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...
253
			$host_name = substr($host_name, 0, $colon_pos);
254
		}
255
		return $host_name;
256
	}
257
258
	/**
259
	 * Returns the default email address
260
	 * @param string $user_part the user part of the address
261
	 * @return string the default email address
262
	 *
263
	 * Assembles a default email address (using the server hostname
264
	 * and the given user part, and returns it
265
	 * Example: when given lostpassword-noreply as $user_part param,
266
	 *     and is currently accessed via http(s)://example.com/,
267
	 *     it would return '[email protected]'
268
	 *
269
	 * If the configuration value 'mail_from_address' is set in
270
	 * config.php, this value will override the $user_part that
271
	 * is passed to this function
272
	 * @since 5.0.0
273
	 */
274
	public static function getDefaultEmailAddress($user_part) {
275
		$config = \OC::$server->getConfig();
276
		$user_part = $config->getSystemValue('mail_from_address', $user_part);
277
		$host_name = self::getServerHostName();
278
		$host_name = $config->getSystemValue('mail_domain', $host_name);
279
		$defaultEmailAddress = $user_part.'@'.$host_name;
280
281
		$mailer = \OC::$server->getMailer();
282
		if ($mailer->validateMailAddress($defaultEmailAddress)) {
283
			return $defaultEmailAddress;
284
		}
285
286
		// in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
287
		return $user_part.'@localhost.localdomain';
288
	}
289
290
	/**
291
	 * Make a human file size (2048 to 2 kB)
292
	 * @param int $bytes file size in bytes
293
	 * @return string a human readable file size
294
	 * @since 4.0.0
295
	 */
296
	public static function humanFileSize($bytes) {
297
		return \OC_Helper::humanFileSize($bytes);
298
	}
299
300
	/**
301
	 * Make a computer file size (2 kB to 2048)
302
	 * @param string $str file size in a fancy format
303
	 * @return float a file size in bytes
304
	 *
305
	 * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418
306
	 * @since 4.0.0
307
	 */
308
	public static function computerFileSize($str) {
309
		return \OC_Helper::computerFileSize($str);
0 ignored issues
show
Bug Best Practice introduced by
The expression return OC_Helper::computerFileSize($str) could also return false which is incompatible with the documented return type double. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
310
	}
311
312
	/**
313
	 * connects a function to a hook
314
	 *
315
	 * @param string $signalClass class name of emitter
316
	 * @param string $signalName name of signal
317
	 * @param string|object $slotClass class name of slot
318
	 * @param string $slotName name of slot
319
	 * @return bool
320
	 *
321
	 * This function makes it very easy to connect to use hooks.
322
	 *
323
	 * TODO: write example
324
	 * @since 4.0.0
325
	 */
326
	static public function connectHook($signalClass, $signalName, $slotClass, $slotName) {
327
		return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName);
328
	}
329
330
	/**
331
	 * Emits a signal. To get data from the slot use references!
332
	 * @param string $signalclass class name of emitter
333
	 * @param string $signalname name of signal
334
	 * @param array $params default: array() array with additional data
335
	 * @return bool true if slots exists or false if not
336
	 *
337
	 * TODO: write example
338
	 * @since 4.0.0
339
	 */
340
	static public function emitHook($signalclass, $signalname, $params = array()) {
341
		return \OC_Hook::emit($signalclass, $signalname, $params);
342
	}
343
344
	/**
345
	 * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
346
	 * multiple OC_Template elements which invoke `callRegister`. If the value
347
	 * would not be cached these unit-tests would fail.
348
	 * @var string
349
	 */
350
	private static $token = '';
351
352
	/**
353
	 * Register an get/post call. This is important to prevent CSRF attacks
354
	 * @since 4.5.0
355
	 */
356
	public static function callRegister() {
357
		if(self::$token === '') {
358
			self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue();
359
		}
360
		return self::$token;
361
	}
362
363
	/**
364
	 * Used to sanitize HTML
365
	 *
366
	 * This function is used to sanitize HTML and should be applied on any
367
	 * string or array of strings before displaying it on a web page.
368
	 *
369
	 * @param string|array $value
370
	 * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter.
371
	 * @since 4.5.0
372
	 */
373
	public static function sanitizeHTML($value) {
374
		return \OC_Util::sanitizeHTML($value);
375
	}
376
377
	/**
378
	 * Public function to encode url parameters
379
	 *
380
	 * This function is used to encode path to file before output.
381
	 * Encoding is done according to RFC 3986 with one exception:
382
	 * Character '/' is preserved as is.
383
	 *
384
	 * @param string $component part of URI to encode
385
	 * @return string
386
	 * @since 6.0.0
387
	 */
388
	public static function encodePath($component) {
389
		return \OC_Util::encodePath($component);
390
	}
391
392
	/**
393
	 * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
394
	 *
395
	 * @param array $input The array to work on
396
	 * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
397
	 * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
398
	 * @return array
399
	 * @since 4.5.0
400
	 */
401
	public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
402
		return \OC_Helper::mb_array_change_key_case($input, $case, $encoding);
403
	}
404
405
	/**
406
	 * performs a search in a nested array
407
	 *
408
	 * @param array $haystack the array to be searched
409
	 * @param string $needle the search string
410
	 * @param mixed $index optional, only search this key name
411
	 * @return mixed the key of the matching field, otherwise false
412
	 * @since 4.5.0
413
	 * @deprecated 15.0.0
414
	 */
415
	public static function recursiveArraySearch($haystack, $needle, $index = null) {
416
		return \OC_Helper::recursiveArraySearch($haystack, $needle, $index);
417
	}
418
419
	/**
420
	 * calculates the maximum upload size respecting system settings, free space and user quota
421
	 *
422
	 * @param string $dir the current folder where the user currently operates
423
	 * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
424
	 * @return int number of bytes representing
425
	 * @since 5.0.0
426
	 */
427
	public static function maxUploadFilesize($dir, $free = null) {
428
		return \OC_Helper::maxUploadFilesize($dir, $free);
429
	}
430
431
	/**
432
	 * Calculate free space left within user quota
433
	 * @param string $dir the current folder where the user currently operates
434
	 * @return int number of bytes representing
435
	 * @since 7.0.0
436
	 */
437
	public static function freeSpace($dir) {
438
		return \OC_Helper::freeSpace($dir);
439
	}
440
441
	/**
442
	 * Calculate PHP upload limit
443
	 *
444
	 * @return int number of bytes representing
445
	 * @since 7.0.0
446
	 */
447
	public static function uploadLimit() {
448
		return \OC_Helper::uploadLimit();
449
	}
450
451
	/**
452
	 * Returns whether the given file name is valid
453
	 * @param string $file file name to check
454
	 * @return bool true if the file name is valid, false otherwise
455
	 * @deprecated 8.1.0 use \OC\Files\View::verifyPath()
456
	 * @since 7.0.0
457
	 * @suppress PhanDeprecatedFunction
458
	 */
459
	public static function isValidFileName($file) {
460
		return \OC_Util::isValidFileName($file);
0 ignored issues
show
Deprecated Code introduced by
The function OC_Util::isValidFileName() has been deprecated: use \OC\Files\View::verifyPath() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

460
		return /** @scrutinizer ignore-deprecated */ \OC_Util::isValidFileName($file);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
461
	}
462
463
	/**
464
	 * Compare two strings to provide a natural sort
465
	 * @param string $a first string to compare
466
	 * @param string $b second string to compare
467
	 * @return int -1 if $b comes before $a, 1 if $a comes before $b
468
	 * or 0 if the strings are identical
469
	 * @since 7.0.0
470
	 */
471
	public static function naturalSortCompare($a, $b) {
472
		return \OC\NaturalSort::getInstance()->compare($a, $b);
473
	}
474
475
	/**
476
	 * check if a password is required for each public link
477
	 * @return boolean
478
	 * @since 7.0.0
479
	 */
480
	public static function isPublicLinkPasswordRequired() {
481
		return \OC_Util::isPublicLinkPasswordRequired();
482
	}
483
484
	/**
485
	 * check if share API enforces a default expire date
486
	 * @return boolean
487
	 * @since 8.0.0
488
	 */
489
	public static function isDefaultExpireDateEnforced() {
490
		return \OC_Util::isDefaultExpireDateEnforced();
491
	}
492
493
	protected static $needUpgradeCache = null;
494
495
	/**
496
	 * Checks whether the current version needs upgrade.
497
	 *
498
	 * @return bool true if upgrade is needed, false otherwise
499
	 * @since 7.0.0
500
	 */
501
	public static function needUpgrade() {
502
		if (!isset(self::$needUpgradeCache)) {
503
			self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getSystemConfig());
504
		}		
505
		return self::$needUpgradeCache;
506
	}
507
508
	/**
509
	 * is this Internet explorer ?
510
	 *
511
	 * @return boolean
512
	 * @since 14.0.0
513
	 */
514
	public static function isIe() {
515
		return \OC_Util::isIe();
516
	}
517
}
518