Completed
Push — master ( 12977a...b026b5 )
by Joas
24:55 queued 10:34
created

Factory::get()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 64
nop 3
dl 0
loc 33
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright 2016 Roeland Jago Douma <[email protected]>
5
 * @copyright 2016 Lukas Reschke <[email protected]>
6
 *
7
 * @author Bart Visscher <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Robin McCorkell <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\L10N;
32
33
use OCP\IConfig;
34
use OCP\IRequest;
35
use OCP\IUser;
36
use OCP\IUserSession;
37
use OCP\L10N\IFactory;
38
use OCP\L10N\ILanguageIterator;
39
40
/**
41
 * A factory that generates language instances
42
 */
43
class Factory implements IFactory {
44
45
	/** @var string */
46
	protected $requestLanguage = '';
47
48
	/**
49
	 * cached instances
50
	 * @var array Structure: Lang => App => \OCP\IL10N
51
	 */
52
	protected $instances = [];
53
54
	/**
55
	 * @var array Structure: App => string[]
56
	 */
57
	protected $availableLanguages = [];
58
59
	/**
60
	 * @var array
61
	 */
62
	protected $availableLocales = [];
63
64
	/**
65
	 * @var array Structure: string => callable
66
	 */
67
	protected $pluralFunctions = [];
68
69
	const COMMON_LANGUAGE_CODES = [
70
		'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it',
71
		'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
72
	];
73
74
	/** @var IConfig */
75
	protected $config;
76
77
	/** @var IRequest */
78
	protected $request;
79
80
	/** @var IUserSession */
81
	protected $userSession;
82
83
	/** @var string */
84
	protected $serverRoot;
85
86
	/**
87
	 * @param IConfig $config
88
	 * @param IRequest $request
89
	 * @param IUserSession $userSession
90
	 * @param string $serverRoot
91
	 */
92
	public function __construct(IConfig $config,
93
								IRequest $request,
94
								IUserSession $userSession,
95
								$serverRoot) {
96
		$this->config = $config;
97
		$this->request = $request;
98
		$this->userSession = $userSession;
99
		$this->serverRoot = $serverRoot;
100
	}
101
102
	/**
103
	 * Get a language instance
104
	 *
105
	 * @param string $app
106
	 * @param string|null $lang
107
	 * @param string|null $locale
108
	 * @return \OCP\IL10N
109
	 */
110
	public function get($app, $lang = null, $locale = null) {
111
		$app = \OC_App::cleanAppId($app);
112
		if ($lang !== null) {
113
			$lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang);
114
		}
115
116
		$forceLang = $this->config->getSystemValue('force_language', false);
117
		if (is_string($forceLang)) {
118
			$lang = $forceLang;
119
		}
120
121
		$forceLocale = $this->config->getSystemValue('force_locale', false);
122
		if (is_string($forceLocale)) {
123
			$locale = $forceLocale;
124
		}
125
126
		if ($lang === null || !$this->languageExists($app, $lang)) {
127
			$lang = $this->findLanguage($app);
128
		}
129
130
		if ($locale === null || !$this->localeExists($locale)) {
131
			$locale = $this->findLocale($lang);
132
		}
133
134
		if (!isset($this->instances[$lang][$app])) {
135
			$this->instances[$lang][$app] = new L10N(
136
				$this, $app, $lang, $locale,
137
				$this->getL10nFilesForApp($app, $lang)
138
			);
139
		}
140
141
		return $this->instances[$lang][$app];
142
	}
143
144
	/**
145
	 * Find the best language
146
	 *
147
	 * @param string|null $app App id or null for core
148
	 * @return string language If nothing works it returns 'en'
149
	 */
150
	public function findLanguage($app = null) {
151
		$forceLang = $this->config->getSystemValue('force_language', false);
152
		if (is_string($forceLang)) {
153
			$this->requestLanguage = $forceLang;
154
		}
155
156
		if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) {
157
			return $this->requestLanguage;
158
		}
159
160
		/**
161
		 * At this point Nextcloud might not yet be installed and thus the lookup
162
		 * in the preferences table might fail. For this reason we need to check
163
		 * whether the instance has already been installed
164
		 *
165
		 * @link https://github.com/owncloud/core/issues/21955
166
		 */
167
		if ($this->config->getSystemValue('installed', false)) {
168
			$userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() :  null;
169
			if (!is_null($userId)) {
170
				$userLang = $this->config->getUserValue($userId, 'core', 'lang', null);
171
			} else {
172
				$userLang = null;
173
			}
174
		} else {
175
			$userId = null;
176
			$userLang = null;
177
		}
178
179
		if ($userLang) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userLang of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
180
			$this->requestLanguage = $userLang;
181
			if ($this->languageExists($app, $userLang)) {
182
				return $userLang;
183
			}
184
		}
185
186
		try {
187
			// Try to get the language from the Request
188
			$lang = $this->getLanguageFromRequest($app);
189
			if ($userId !== null && $app === null && !$userLang) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userLang of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
190
				$this->config->setUserValue($userId, 'core', 'lang', $lang);
191
			}
192
			return $lang;
193
		} catch (LanguageNotFoundException $e) {
194
			// Finding language from request failed fall back to default language
195
			$defaultLanguage = $this->config->getSystemValue('default_language', false);
196
			if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) {
197
				return $defaultLanguage;
198
			}
199
		}
200
201
		// We could not find any language so fall back to english
202
		return 'en';
203
	}
204
205
	/**
206
	 * find the best locale
207
	 *
208
	 * @param string $lang
209
	 * @return null|string
210
	 */
211
	public function findLocale($lang = null) {
212
		$forceLocale = $this->config->getSystemValue('force_locale', false);
213
		if (is_string($forceLocale) && $this->localeExists($forceLocale)) {
214
			return $forceLocale;
215
		}
216
217
		if ($this->config->getSystemValue('installed', false)) {
218
			$userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() :  null;
219
			$userLocale = null;
220
			if (null !== $userId) {
221
				$userLocale = $this->config->getUserValue($userId, 'core', 'locale', null);
222
			}
223
		} else {
224
			$userId = null;
0 ignored issues
show
Unused Code introduced by
$userId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
			$userLocale = null;
226
		}
227
228
		if ($userLocale && $this->localeExists($userLocale)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userLocale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
229
			return $userLocale;
230
		}
231
232
		// Default : use system default locale
233
		$defaultLocale = $this->config->getSystemValue('default_locale', false);
234
		if ($defaultLocale !== false && $this->localeExists($defaultLocale)) {
235
			return $defaultLocale;
236
		}
237
238
		// If no user locale set, use lang as locale
239
		if (null !== $lang && $this->localeExists($lang)) {
240
			return $lang;
241
		}
242
243
		// At last, return USA
244
		return 'en_US';
245
	}
246
247
	/**
248
	 * find the matching lang from the locale
249
	 *
250
	 * @param string $app
251
	 * @param string $locale
252
	 * @return null|string
253
	 */
254
	public function findLanguageFromLocale(string $app = 'core', string $locale = null) {
255
		if ($this->languageExists($app, $locale)) {
256
			return $locale;
257
		}
258
		
259
		// Try to split e.g: fr_FR => fr
260
		$locale = explode('_', $locale)[0];
261
		if ($this->languageExists($app, $locale)) {
262
			return $locale;
263
		}
264
	}
265
266
	/**
267
	 * Find all available languages for an app
268
	 *
269
	 * @param string|null $app App id or null for core
270
	 * @return array an array of available languages
271
	 */
272
	public function findAvailableLanguages($app = null) {
273
		$key = $app;
274
		if ($key === null) {
275
			$key = 'null';
276
		}
277
278
		// also works with null as key
279
		if (!empty($this->availableLanguages[$key])) {
280
			return $this->availableLanguages[$key];
281
		}
282
283
		$available = ['en']; //english is always available
284
		$dir = $this->findL10nDir($app);
285 View Code Duplication
		if (is_dir($dir)) {
286
			$files = scandir($dir);
287
			if ($files !== false) {
288
				foreach ($files as $file) {
289
					if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
290
						$available[] = substr($file, 0, -5);
291
					}
292
				}
293
			}
294
		}
295
296
		// merge with translations from theme
297
		$theme = $this->config->getSystemValue('theme');
298
		if (!empty($theme)) {
299
			$themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot));
300
301 View Code Duplication
			if (is_dir($themeDir)) {
302
				$files = scandir($themeDir);
303
				if ($files !== false) {
304
					foreach ($files as $file) {
305
						if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
306
							$available[] = substr($file, 0, -5);
307
						}
308
					}
309
				}
310
			}
311
		}
312
313
		$this->availableLanguages[$key] = $available;
314
		return $available;
315
	}
316
317
	/**
318
	 * @return array|mixed
319
	 */
320
	public function findAvailableLocales() {
321
		if (!empty($this->availableLocales)) {
322
			return $this->availableLocales;
323
		}
324
325
		$localeData = file_get_contents(\OC::$SERVERROOT . '/resources/locales.json');
326
		$this->availableLocales = \json_decode($localeData, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like \json_decode($localeData, true) of type * is incompatible with the declared type array of property $availableLocales.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
327
328
		return $this->availableLocales;
329
	}
330
331
	/**
332
	 * @param string|null $app App id or null for core
333
	 * @param string $lang
334
	 * @return bool
335
	 */
336
	public function languageExists($app, $lang) {
337
		if ($lang === 'en') {//english is always available
338
			return true;
339
		}
340
341
		$languages = $this->findAvailableLanguages($app);
342
		return array_search($lang, $languages) !== false;
343
	}
344
345
	public function getLanguageIterator(IUser $user = null): ILanguageIterator {
346
		$user = $user ?? $this->userSession->getUser();
347
		if($user === null) {
348
			throw new \RuntimeException('Failed to get an IUser instance');
349
		}
350
		return new LanguageIterator($user, $this->config);
351
	}
352
353
	/**
354
	 * @param string $locale
355
	 * @return bool
356
	 */
357
	public function localeExists($locale) {
358
		if ($locale === 'en') { //english is always available
359
			return true;
360
		}
361
362
		$locales = $this->findAvailableLocales();
363
		$userLocale = array_filter($locales, function($value) use ($locale) {
364
			return $locale === $value['code'];
365
		});
366
367
		return !empty($userLocale);
368
	}
369
370
	/**
371
	 * @param string|null $app
372
	 * @return string
373
	 * @throws LanguageNotFoundException
374
	 */
375
	private function getLanguageFromRequest($app) {
376
		$header = $this->request->getHeader('ACCEPT_LANGUAGE');
377
		if ($header !== '') {
378
			$available = $this->findAvailableLanguages($app);
379
380
			// E.g. make sure that 'de' is before 'de_DE'.
381
			sort($available);
382
383
			$preferences = preg_split('/,\s*/', strtolower($header));
384
			foreach ($preferences as $preference) {
385
				list($preferred_language) = explode(';', $preference);
386
				$preferred_language = str_replace('-', '_', $preferred_language);
387
388
				foreach ($available as $available_language) {
389
					if ($preferred_language === strtolower($available_language)) {
390
						return $this->respectDefaultLanguage($app, $available_language);
391
					}
392
				}
393
394
				// Fallback from de_De to de
395
				foreach ($available as $available_language) {
396
					if (substr($preferred_language, 0, 2) === $available_language) {
397
						return $available_language;
398
					}
399
				}
400
			}
401
		}
402
403
		throw new LanguageNotFoundException();
404
	}
405
406
	/**
407
	 * if default language is set to de_DE (formal German) this should be
408
	 * preferred to 'de' (non-formal German) if possible
409
	 *
410
	 * @param string|null $app
411
	 * @param string $lang
412
	 * @return string
413
	 */
414
	protected function respectDefaultLanguage($app, $lang) {
415
		$result = $lang;
416
		$defaultLanguage = $this->config->getSystemValue('default_language', false);
417
418
		// use formal version of german ("Sie" instead of "Du") if the default
419
		// language is set to 'de_DE' if possible
420
		if (is_string($defaultLanguage) &&
421
			strtolower($lang) === 'de' &&
422
			strtolower($defaultLanguage) === 'de_de' &&
423
			$this->languageExists($app, 'de_DE')
424
		) {
425
			$result = 'de_DE';
426
		}
427
428
		return $result;
429
	}
430
431
	/**
432
	 * Checks if $sub is a subdirectory of $parent
433
	 *
434
	 * @param string $sub
435
	 * @param string $parent
436
	 * @return bool
437
	 */
438
	private function isSubDirectory($sub, $parent) {
439
		// Check whether $sub contains no ".."
440
		if (strpos($sub, '..') !== false) {
441
			return false;
442
		}
443
444
		// Check whether $sub is a subdirectory of $parent
445
		if (strpos($sub, $parent) === 0) {
446
			return true;
447
		}
448
449
		return false;
450
	}
451
452
	/**
453
	 * Get a list of language files that should be loaded
454
	 *
455
	 * @param string $app
456
	 * @param string $lang
457
	 * @return string[]
458
	 */
459
	// FIXME This method is only public, until OC_L10N does not need it anymore,
460
	// FIXME This is also the reason, why it is not in the public interface
461
	public function getL10nFilesForApp($app, $lang) {
462
		$languageFiles = [];
463
464
		$i18nDir = $this->findL10nDir($app);
465
		$transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json';
466
467
		if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/')
468
				|| $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/')
469
				|| $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/')
470
				|| $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/')
471
			)
472
			&& file_exists($transFile)) {
473
			// load the translations file
474
			$languageFiles[] = $transFile;
475
		}
476
477
		// merge with translations from theme
478
		$theme = $this->config->getSystemValue('theme');
479
		if (!empty($theme)) {
480
			$transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot));
481
			if (file_exists($transFile)) {
482
				$languageFiles[] = $transFile;
483
			}
484
		}
485
486
		return $languageFiles;
487
	}
488
489
	/**
490
	 * find the l10n directory
491
	 *
492
	 * @param string $app App id or empty string for core
493
	 * @return string directory
494
	 */
495
	protected function findL10nDir($app = null) {
496
		if (in_array($app, ['core', 'lib', 'settings'])) {
497
			if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) {
498
				return $this->serverRoot . '/' . $app . '/l10n/';
499
			}
500
		} else if ($app && \OC_App::getAppPath($app) !== false) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $app of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
501
			// Check if the app is in the app folder
502
			return \OC_App::getAppPath($app) . '/l10n/';
503
		}
504
		return $this->serverRoot . '/core/l10n/';
505
	}
506
507
508
	/**
509
	 * Creates a function from the plural string
510
	 *
511
	 * Parts of the code is copied from Habari:
512
	 * https://github.com/habari/system/blob/master/classes/locale.php
513
	 * @param string $string
514
	 * @return string
515
	 */
516
	public function createPluralFunction($string) {
517
		if (isset($this->pluralFunctions[$string])) {
518
			return $this->pluralFunctions[$string];
519
		}
520
521
		if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) {
522
			// sanitize
523
			$nplurals = preg_replace( '/[^0-9]/', '', $matches[1] );
524
			$plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] );
525
526
			$body = str_replace(
527
				array( 'plural', 'n', '$n$plurals', ),
528
				array( '$plural', '$n', '$nplurals', ),
529
				'nplurals='. $nplurals . '; plural=' . $plural
530
			);
531
532
			// add parents
533
			// important since PHP's ternary evaluates from left to right
534
			$body .= ';';
535
			$res = '';
536
			$p = 0;
537
			$length = strlen($body);
538
			for($i = 0; $i < $length; $i++) {
539
				$ch = $body[$i];
540
				switch ( $ch ) {
541
					case '?':
542
						$res .= ' ? (';
543
						$p++;
544
						break;
545
					case ':':
546
						$res .= ') : (';
547
						break;
548
					case ';':
549
						$res .= str_repeat( ')', $p ) . ';';
550
						$p = 0;
551
						break;
552
					default:
553
						$res .= $ch;
554
				}
555
			}
556
557
			$body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);';
558
			$function = create_function('$n', $body);
559
			$this->pluralFunctions[$string] = $function;
560
			return $function;
561
		} else {
562
			// default: one plural form for all cases but n==1 (english)
563
			$function = create_function(
564
				'$n',
565
				'$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);'
566
			);
567
			$this->pluralFunctions[$string] = $function;
568
			return $function;
569
		}
570
	}
571
572
	/**
573
	 * returns the common language and other languages in an
574
	 * associative array
575
	 *
576
	 * @return array
577
	 */
578
	public function getLanguages() {
579
		$forceLanguage = $this->config->getSystemValue('force_language', false);
580
		if ($forceLanguage !== false) {
581
			return [];
582
		}
583
584
		$languageCodes = $this->findAvailableLanguages();
585
586
		$commonLanguages = [];
587
		$languages = [];
588
589
		foreach($languageCodes as $lang) {
590
			$l = $this->get('lib', $lang);
591
			// TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
592
			$potentialName = (string) $l->t('__language_name__');
593
			if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file
594
				$ln = array(
595
					'code' => $lang,
596
					'name' => $potentialName
597
				);
598
			} else if ($lang === 'en') {
599
				$ln = array(
600
					'code' => $lang,
601
					'name' => 'English (US)'
602
				);
603
			} else {//fallback to language code
604
				$ln = array(
605
					'code' => $lang,
606
					'name' => $lang
607
				);
608
			}
609
610
			// put appropriate languages into appropriate arrays, to print them sorted
611
			// common languages -> divider -> other languages
612
			if (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
613
				$commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln;
614
			} else {
615
				$languages[] = $ln;
616
			}
617
		}
618
619
		ksort($commonLanguages);
620
621
		// sort now by displayed language not the iso-code
622
		usort( $languages, function ($a, $b) {
623 View Code Duplication
			if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
624
				// If a doesn't have a name, but b does, list b before a
625
				return 1;
626
			}
627 View Code Duplication
			if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
628
				// If a does have a name, but b doesn't, list a before b
629
				return -1;
630
			}
631
			// Otherwise compare the names
632
			return strcmp($a['name'], $b['name']);
633
		});
634
635
		return [
636
			// reset indexes
637
			'commonlanguages' => array_values($commonLanguages),
638
			'languages' => $languages
639
		];
640
	}
641
}
642