|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Core; |
|
26
|
|
|
|
|
27
|
|
|
use SP\Config\Config; |
|
28
|
|
|
use SP\Config\ConfigData; |
|
29
|
|
|
use SP\Core\Context\ContextInterface; |
|
30
|
|
|
use SP\Core\Context\SessionContext; |
|
31
|
|
|
use SP\Http\Request; |
|
32
|
|
|
|
|
33
|
|
|
defined('APP_ROOT') || die(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class Language para el manejo del lenguaje utilizado por la aplicación |
|
37
|
|
|
* |
|
38
|
|
|
* @package SP |
|
39
|
|
|
*/ |
|
40
|
|
|
final class Language |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* Lenguaje del usuario |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $userLang = ''; |
|
48
|
|
|
/** |
|
49
|
|
|
* Lenguaje global de la Aplicación |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static $globalLang = ''; |
|
54
|
|
|
/** |
|
55
|
|
|
* Estado de la localización. false si no existe |
|
56
|
|
|
* |
|
57
|
|
|
* @var string|false |
|
58
|
|
|
*/ |
|
59
|
|
|
public static $localeStatus; |
|
60
|
|
|
/** |
|
61
|
|
|
* Si se ha establecido a las de la App |
|
62
|
|
|
* |
|
63
|
|
|
* @var bool |
|
64
|
|
|
*/ |
|
65
|
|
|
protected static $appSet = false; |
|
66
|
|
|
/** |
|
67
|
|
|
* @var array Available languages |
|
68
|
|
|
*/ |
|
69
|
|
|
private static $langs = [ |
|
70
|
|
|
'es_ES' => 'Español', |
|
71
|
|
|
'ca_ES' => 'Catalá', |
|
72
|
|
|
'en_US' => 'English', |
|
73
|
|
|
'de_DE' => 'Deutsch', |
|
74
|
|
|
'hu_HU' => 'Magyar', |
|
75
|
|
|
'fr_FR' => 'Français', |
|
76
|
|
|
'pl_PL' => 'Polski', |
|
77
|
|
|
'ru_RU' => 'русский', |
|
78
|
|
|
'nl_NL' => 'Nederlands', |
|
79
|
|
|
'pt_BR' => 'Português', |
|
80
|
|
|
'it_IT' => 'Italiano', |
|
81
|
|
|
'da' => 'Dansk', |
|
82
|
|
|
'fo' => 'Føroyskt mál', |
|
83
|
|
|
'ja_JP' => '日本語', |
|
84
|
|
|
]; |
|
85
|
|
|
/** |
|
86
|
|
|
* @var ConfigData |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $configData; |
|
89
|
|
|
/** |
|
90
|
|
|
* @var SessionContext |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $context; |
|
93
|
|
|
/** |
|
94
|
|
|
* @var Request |
|
95
|
|
|
*/ |
|
96
|
|
|
private $request; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Language constructor. |
|
100
|
|
|
* |
|
101
|
|
|
* @param ContextInterface $session |
|
102
|
|
|
* @param Config $config |
|
103
|
|
|
* @param Request $request |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct(ContextInterface $session, Config $config, Request $request) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->context = $session; |
|
|
|
|
|
|
108
|
|
|
$this->configData = $config->getConfigData(); |
|
109
|
|
|
$this->request = $request; |
|
110
|
|
|
|
|
111
|
|
|
ksort(self::$langs); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Devolver los lenguajes disponibles |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function getAvailableLanguages() |
|
120
|
|
|
{ |
|
121
|
|
|
return self::$langs; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Establecer el lenguaje a utilizar |
|
126
|
|
|
* |
|
127
|
|
|
* @param bool $force Forzar la detección del lenguaje para los inicios de sesión |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setLanguage($force = false) |
|
130
|
|
|
{ |
|
131
|
|
|
$lang = $this->context->getLocale(); |
|
132
|
|
|
|
|
133
|
|
|
if (empty($lang) || $force === true) { |
|
134
|
|
|
self::$userLang = $this->getUserLang(); |
|
135
|
|
|
self::$globalLang = $this->getGlobalLang(); |
|
136
|
|
|
|
|
137
|
|
|
$lang = self::$userLang ?: self::$globalLang; |
|
138
|
|
|
|
|
139
|
|
|
$this->context->setLocale($lang); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
self::setLocales($lang); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Devuelve el lenguaje del usuario |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
private function getUserLang() |
|
151
|
|
|
{ |
|
152
|
|
|
$userData = $this->context->getUserData(); |
|
153
|
|
|
|
|
154
|
|
|
return ($userData->getId() > 0) ? $userData->getPreferences()->getLang() : ''; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Establece el lenguaje de la aplicación. |
|
159
|
|
|
* Esta función establece el lenguaje según esté definido en la configuración o en el navegador. |
|
160
|
|
|
*/ |
|
161
|
|
|
private function getGlobalLang() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->configData->getSiteLang() ?: $this->getBrowserLang(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Devolver el lenguaje que acepta el navegador |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
private function getBrowserLang() |
|
172
|
|
|
{ |
|
173
|
|
|
$lang = $this->request->getHeader('Accept-Language'); |
|
174
|
|
|
|
|
175
|
|
|
return $lang !== '' ? str_replace('-', '_', substr($lang, 0, 5)) : 'en_US'; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Establecer las locales de gettext |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $lang El lenguaje a utilizar |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function setLocales($lang) |
|
184
|
|
|
{ |
|
185
|
|
|
$lang .= '.utf8'; |
|
186
|
|
|
|
|
187
|
|
|
self::$localeStatus = setlocale(LC_MESSAGES, $lang); |
|
188
|
|
|
|
|
189
|
|
|
putenv('LANG=' . $lang); |
|
190
|
|
|
putenv('LANGUAGE=' . $lang); |
|
191
|
|
|
|
|
192
|
|
|
$locale = setlocale(LC_ALL, $lang); |
|
193
|
|
|
|
|
194
|
|
|
if ($locale === false) { |
|
195
|
|
|
logger('Could not set locale', 'ERROR'); |
|
196
|
|
|
logger('Domain path: ' . LOCALES_PATH); |
|
197
|
|
|
} else { |
|
198
|
|
|
logger('Locale set to: ' . $locale); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
bindtextdomain('messages', LOCALES_PATH); |
|
202
|
|
|
textdomain('messages'); |
|
203
|
|
|
bind_textdomain_codeset('messages', 'UTF-8'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Establecer el lenguaje global para las traducciones |
|
208
|
|
|
*/ |
|
209
|
|
|
public function setAppLocales() |
|
210
|
|
|
{ |
|
211
|
|
|
if ($this->configData->getSiteLang() !== $this->context->getLocale()) { |
|
212
|
|
|
self::setLocales($this->configData->getSiteLang()); |
|
213
|
|
|
|
|
214
|
|
|
self::$appSet = true; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Restablecer el lenguaje global para las traducciones |
|
220
|
|
|
*/ |
|
221
|
|
|
public function unsetAppLocales() |
|
222
|
|
|
{ |
|
223
|
|
|
if (self::$appSet === true) { |
|
224
|
|
|
self::setLocales($this->context->getLocale()); |
|
225
|
|
|
|
|
226
|
|
|
self::$appSet = false; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Comprobar si el archivo de lenguaje existe |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $lang El lenguaje a comprobar |
|
234
|
|
|
* |
|
235
|
|
|
* @return bool |
|
236
|
|
|
*/ |
|
237
|
|
|
private function checkLangFile($lang) |
|
|
|
|
|
|
238
|
|
|
{ |
|
239
|
|
|
return file_exists(LOCALES_PATH . DIRECTORY_SEPARATOR . $lang); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.