Passed
Push — master ( abb1fd...21d836 )
by John
26:07 queued 12:38
created

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

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