1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Manages all language stuff |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
8
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
9
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
10
|
|
|
* |
11
|
|
|
* @category phpMyFAQ |
12
|
|
|
* @package Language |
13
|
|
|
* @author Thorsten Rinne <[email protected]> |
14
|
|
|
* @author Matteo scaramuccia <[email protected]> |
15
|
|
|
* @author Aurimas Fišeras <[email protected]> |
16
|
|
|
* @copyright 2009-2016 phpMyFAQ Team |
17
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
18
|
|
|
* @link http://www.phpmyfaq.de |
19
|
|
|
* @since 2009-05-14 |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
23
|
|
|
exit(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Language |
28
|
|
|
* |
29
|
|
|
* @category phpMyFAQ |
30
|
|
|
* @package Language |
31
|
|
|
* @author Thorsten Rinne <[email protected]> |
32
|
|
|
* @author Matteo scaramuccia <[email protected]> |
33
|
|
|
* @author Aurimas Fišeras <[email protected]> |
34
|
|
|
* @copyright 2009-2016 phpMyFAQ Team |
35
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
36
|
|
|
* @link http://www.phpmyfaq.de |
37
|
|
|
* @since 2009-05-14 |
38
|
|
|
*/ |
39
|
|
|
class PMF_Language |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* The accepted language of the user agend |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $acceptedLanguage = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The current language |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public static $language = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var PMF_Configuration |
57
|
|
|
*/ |
58
|
|
|
private $config; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructor |
62
|
|
|
* |
63
|
|
|
* @param PMF_Configuration $config |
64
|
|
|
* |
65
|
|
|
* @return PMF_Language |
66
|
|
|
*/ |
67
|
|
|
public function __construct(PMF_Configuration $config) |
68
|
|
|
{ |
69
|
|
|
$this->config = $config; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns an array of country codes for a specific FAQ record ID, |
74
|
|
|
* specific category ID or all languages used by FAQ records , categories |
75
|
|
|
* |
76
|
|
|
* @param integer $id ID |
77
|
|
|
* @param string $table Specifies table |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function languageAvailable($id, $table = 'faqdata') |
82
|
|
|
{ |
83
|
|
|
$output = array(); |
84
|
|
|
|
85
|
|
|
if (isset($id)) { |
86
|
|
|
if ($id == 0) { |
87
|
|
|
// get languages for all ids |
88
|
|
|
$distinct = ' DISTINCT '; |
89
|
|
|
$where = ''; |
90
|
|
|
} else { |
91
|
|
|
// get languages for specified id |
92
|
|
|
$distinct = ''; |
93
|
|
|
$where = " WHERE id = ".$id; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$query = sprintf(" |
97
|
|
|
SELECT %s |
98
|
|
|
lang |
99
|
|
|
FROM |
100
|
|
|
%s%s |
101
|
|
|
%s", |
102
|
|
|
$distinct, |
103
|
|
|
PMF_Db::getTablePrefix(), |
104
|
|
|
$table, |
105
|
|
|
$where |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$result = $this->config->getDb()->query($query); |
109
|
|
|
|
110
|
|
|
if ($this->config->getDb()->numRows($result) > 0) { |
111
|
|
|
while ($row = $this->config->getDb()->fetchObject($result)) { |
112
|
|
|
$output[] = $row->lang; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $output; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets the current language for phpMyFAQ user session |
122
|
|
|
* |
123
|
|
|
* @param bool $configDetection Configuration detection |
124
|
|
|
* @param string $configLanguage Language from configuration |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function setLanguage($configDetection, $configLanguage) |
129
|
|
|
{ |
130
|
|
|
$_lang = array(); |
131
|
|
|
self::_getUserAgentLanguage(); |
132
|
|
|
|
133
|
|
|
// Get language from: _POST, _GET, _COOKIE, phpMyFAQ configuration and the automatic language detection |
134
|
|
|
$_lang['post'] = PMF_Filter::filterInput(INPUT_POST, 'language', FILTER_SANITIZE_STRING); |
135
|
|
View Code Duplication |
if (!is_null($_lang['post']) && !self::isASupportedLanguage($_lang['post']) ) { |
136
|
|
|
$_lang['post'] = null; |
137
|
|
|
} |
138
|
|
|
// Get the user language |
139
|
|
|
$_lang['get'] = PMF_Filter::filterInput(INPUT_GET, 'lang', FILTER_SANITIZE_STRING); |
140
|
|
View Code Duplication |
if (!is_null($_lang['get']) && !self::isASupportedLanguage($_lang['get']) ) { |
141
|
|
|
$_lang['get'] = null; |
142
|
|
|
} |
143
|
|
|
// Get the faq record language |
144
|
|
|
$_lang['artget'] = PMF_Filter::filterInput(INPUT_GET, 'artlang', FILTER_SANITIZE_STRING); |
145
|
|
View Code Duplication |
if (!is_null($_lang['artget']) && !self::isASupportedLanguage($_lang['artget']) ) { |
146
|
|
|
$_lang['artget'] = null; |
147
|
|
|
} |
148
|
|
|
// Get the language from the session |
149
|
|
|
if (isset($_SESSION['pmf_lang']) && self::isASupportedLanguage($_SESSION['pmf_lang']) ) { |
150
|
|
|
$_lang['session'] = trim($_SESSION['pmf_lang']); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Get the language from the config |
154
|
|
|
if (isset($configLanguage)) { |
155
|
|
|
$confLangCode = str_replace(array("language_", ".php"), "", $configLanguage); |
156
|
|
|
if (self::isASupportedLanguage($confLangCode) ) { |
157
|
|
|
$_lang['config'] = $confLangCode; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
// Detect the browser's language |
161
|
|
|
if ((true === $configDetection) && self::isASupportedLanguage($this->acceptedLanguage) ) { |
162
|
|
|
$_lang['detection'] = strtolower($this->acceptedLanguage); |
163
|
|
|
} |
164
|
|
|
// Select the language |
165
|
|
|
if (isset($_lang['post'])) { |
166
|
|
|
self::$language = $_lang['post']; |
167
|
|
|
$_lang = null; |
168
|
|
|
unset($_lang); |
169
|
|
|
} elseif (isset($_lang['get'])) { |
170
|
|
|
self::$language = $_lang['get']; |
171
|
|
|
} elseif (isset($_lang['artget'])) { |
172
|
|
|
self::$language = $_lang['artget']; |
173
|
|
View Code Duplication |
} elseif (isset($_lang['session'])) { |
174
|
|
|
self::$language = $_lang['session']; |
175
|
|
|
$_lang = null; |
176
|
|
|
unset($_lang); |
177
|
|
|
} elseif (isset($_lang['detection'])) { |
178
|
|
|
self::$language = $_lang['detection']; |
179
|
|
|
$_lang = null; |
180
|
|
|
unset($_lang); |
181
|
|
View Code Duplication |
} elseif (isset($_lang['config'])) { |
182
|
|
|
self::$language = $_lang['config']; |
183
|
|
|
$_lang = null; |
184
|
|
|
unset($_lang); |
185
|
|
|
} else { |
186
|
|
|
self::$language = 'en'; // just a fallback |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $_SESSION['pmf_lang'] = self::$language; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns the current language |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getLanguage() |
198
|
|
|
{ |
199
|
|
|
return self::$language; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* This function returns the available languages |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public static function getAvailableLanguages() |
208
|
|
|
{ |
209
|
|
|
global $languageCodes; |
210
|
|
|
|
211
|
|
|
$search = array("language_" , ".php"); |
212
|
|
|
$languages = $languageFiles = array(); |
213
|
|
|
|
214
|
|
|
$dir = new DirectoryIterator(PMF_LANGUAGE_DIR); |
215
|
|
|
foreach ($dir as $fileinfo) { |
216
|
|
|
if (! $fileinfo->isDot()) { |
217
|
|
|
$languageFiles[] = strtoupper( |
218
|
|
|
str_replace($search, '', trim($fileinfo->getFilename())) |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
foreach ($languageFiles as $lang) { |
224
|
|
|
// Check if the file is related to a (real) language before using it |
225
|
|
|
if (array_key_exists($lang, $languageCodes)) { |
226
|
|
|
$languages[strtolower($lang)] = $languageCodes[$lang]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// Sort the languages list |
231
|
|
|
asort($languages); |
232
|
|
|
reset($languages); |
233
|
|
|
return $languages; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* This function displays the <select> box for the available languages |
238
|
|
|
* optionally filtered by excluding some provided languages |
239
|
|
|
* |
240
|
|
|
* @param string $default |
241
|
|
|
* @param boolean $submitOnChange |
242
|
|
|
* @param array $excludedLanguages |
243
|
|
|
* @param string $id |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public static function selectLanguages($default, $submitOnChange = false, Array $excludedLanguages = array(), $id = 'language') |
247
|
|
|
{ |
248
|
|
|
global $languageCodes; |
249
|
|
|
|
250
|
|
|
$onChange = ($submitOnChange ? ' onchange="this.form.submit();"' : ''); |
251
|
|
|
$output = '<select class="language" name="' . $id . '" id="' . $id . '" size="1"' . $onChange . ">\n"; |
252
|
|
|
$languages = self::getAvailableLanguages(); |
253
|
|
|
|
254
|
|
|
if (count($languages) > 0) { |
255
|
|
|
foreach ($languages as $lang => $value) { |
256
|
|
|
if (! in_array($lang, $excludedLanguages)) { |
257
|
|
|
$output .= "\t" . '<option value="' . $lang . '"'; |
258
|
|
|
if ($lang == $default) { |
259
|
|
|
$output .= ' selected="selected"'; |
260
|
|
|
} |
261
|
|
|
$output .= '>' . $value . "</option>\n"; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} else { |
265
|
|
|
$output .= "\t<option value=\"en\">" . $languageCodes["EN"] . "</option>"; |
266
|
|
|
} |
267
|
|
|
$output .= "</select>\n"; |
268
|
|
|
return $output; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Function for displaying all languages in <option> |
273
|
|
|
* |
274
|
|
|
* @param string $lang the languange to be selected |
275
|
|
|
* @param bool $onlyThisLang print only the passed language? |
276
|
|
|
* @param bool $fileLanguageValue print the <language file> instead of the <language code> as value? |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public static function languageOptions($lang = '', $onlyThisLang = false, $fileLanguageValue = false) |
280
|
|
|
{ |
281
|
|
|
$output = ""; |
282
|
|
|
foreach (self::getAvailableLanguages() as $key => $value) { |
283
|
|
|
if ($onlyThisLang) { |
284
|
|
View Code Duplication |
if (strtolower($key) == $lang) { |
285
|
|
|
if ($fileLanguageValue) { |
286
|
|
|
$output .= "\t<option value=\"language_" . strtolower($lang) . ".php\""; |
287
|
|
|
} else { |
288
|
|
|
$output .= "\t<option value=\"" . strtolower($lang) . "\""; |
289
|
|
|
} |
290
|
|
|
$output .= " selected=\"selected\""; |
291
|
|
|
$output .= ">" . $value . "</option>\n"; |
292
|
|
|
break; |
293
|
|
|
} |
294
|
|
View Code Duplication |
} else { |
295
|
|
|
if ($fileLanguageValue) { |
296
|
|
|
$output .= "\t<option value=\"language_" . strtolower($key) . ".php\""; |
297
|
|
|
} else { |
298
|
|
|
$output .= "\t<option value=\"" . strtolower($key) . "\""; |
299
|
|
|
} |
300
|
|
|
if (strtolower($key) == $lang) { |
301
|
|
|
$output .= " selected=\"selected\""; |
302
|
|
|
} |
303
|
|
|
$output .= ">" . $value . "</option>\n"; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
return $output; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* True if the language is supported by the current phpMyFAQ installation |
311
|
|
|
* |
312
|
|
|
* @param string $langcode Language code |
313
|
|
|
* @return boolean |
314
|
|
|
*/ |
315
|
|
|
public static function isASupportedLanguage($langcode) |
316
|
|
|
{ |
317
|
|
|
global $languageCodes; |
318
|
|
|
return isset($languageCodes[strtoupper($langcode)]); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* True if the language is supported by the bundled TinyMCE editor |
323
|
|
|
* |
324
|
|
|
* TinyMCE Language is supported if there is a language file present in |
325
|
|
|
* PMF_ROOT/admin/editor/langs/$langcode.js |
326
|
|
|
* |
327
|
|
|
* TinyMCE language packs can be downloaded from |
328
|
|
|
* http://tinymce.moxiecode.com/download_i18n.php |
329
|
|
|
* and extracted to PMF_ROOT/admin/editor |
330
|
|
|
* |
331
|
|
|
* @param string $langcode Language code |
332
|
|
|
* |
333
|
|
|
* @return boolean |
334
|
|
|
*/ |
335
|
|
|
public static function isASupportedTinyMCELanguage($langcode) |
336
|
|
|
{ |
337
|
|
|
return file_exists( |
338
|
|
|
PMF_ROOT_DIR . '/admin/editor/langs/' . $langcode . '.js' |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Gets the accepted language from the user agent |
345
|
|
|
* |
346
|
|
|
* $_SERVER['HTTP_ACCEPT_LANGUAGE'] could be like the text below: |
347
|
|
|
* it,pt-br;q=0.8,en-us;q=0.5,en;q=0.3 |
348
|
|
|
* |
349
|
|
|
* @return void |
350
|
|
|
*/ |
351
|
|
|
private function _getUserAgentLanguage() |
352
|
|
|
{ |
353
|
|
|
$matches = $languages = array(); |
354
|
|
|
|
355
|
|
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
356
|
|
|
// ISO Language Codes, 2-letters: ISO 639-1, <Country tag>[-<Country subtag>] |
357
|
|
|
// Simplified language syntax detection: xx[-yy] |
358
|
|
|
preg_match_all( |
359
|
|
|
'/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', |
360
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'], |
361
|
|
|
$matches |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
if (count($matches[1])) { |
365
|
|
|
$languages = array_combine($matches[1], $matches[4]); |
366
|
|
|
foreach ($languages as $lang => $val) { |
367
|
|
|
if ($val === '') $languages[$lang] = 1; |
368
|
|
|
} |
369
|
|
|
arsort($languages, SORT_NUMERIC); |
370
|
|
|
} |
371
|
|
|
foreach ($languages as $lang => $val) { |
372
|
|
|
if (self::isASupportedLanguage(strtoupper($lang))) { |
373
|
|
|
$this->acceptedLanguage = $lang; |
374
|
|
|
break; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
} |