This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
/
LanguageManager.class.php
375 lines (308 loc) · 11.4 KB
/
LanguageManager.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/************************************************************************/
/* AChecker */
/************************************************************************/
/* Copyright (c) 2008 - 2011 */
/* Inclusive Design Institute */
/* */
/* This program is free software. You can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation. */
/************************************************************************/
// $Id$
require_once(dirname(__FILE__) . '/Language.class.php');
//define('AC_LANG_STATUS_EMPTY', 0);
//define('AC_LANG_STATUS_INCOMPLETE', 1);
//define('AC_LANG_STATUS_COMPLETE', 2);
//define('AC_LANG_STATUS_PUBLISHED', 3);
/**
* LanguageManager
* Class for managing available languages as Language Objects.
* @access public
* @author Joel Kronenberg
* @see Language.class.php
* @package Language
*/
class LanguageManager {
/**
* This array stores references to all the Language Objects
* that are available in this installation.
* @access private
* @var array
*/
var $allLanguages;
/**
* This array stores references to the Language Objects
* that are available in this installation.
* @access private
* @var array
*/
var $availableLanguages;
/**
* The number of languages that are available. Does not include
* character set variations.
* @access private
* @var integer
*/
var $numEnabledLanguages;
/**
* Constructor.
*
* Initializes availableLanguages and numLanguages.
*/
function LanguageManager() {
require_once(AC_INCLUDE_PATH. 'classes/DAO/LanguagesDAO.class.php');
$languagesDAO = new LanguagesDAO();
// initialize available lanuguages. Available languages are the ones with status "enabled"
$rows = $languagesDAO->getAllEnabled();
// if there's no enabled language, set to default language and default charset
if (!is_array($rows))
{
$rows = array($languagesDAO->getByLangCodeAndCharset(DEFAULT_LANGUAGE_CODE, DEFAULT_CHARSET));
}
foreach ($rows as $i => $row) {
$this->availableLanguages[$row['language_code']][$row['charset']] = new Language($row);
}
$this->numEnabledLanguages = count($this->availableLanguages);
// initialize available lanuguages. Available languages are the ones with status "enabled"
$rows = $languagesDAO->getAll();
foreach ($rows as $i => $row) {
$this->allLanguages[$row['language_code']][$row['charset']] = new Language($row);
}
}
/**
* Returns a valid Language Object based on the given language $code and optional
* $charset, FALSE if it can't be found.
* @access public
* @param string $code The language code of the language to return.
* @param string $charset Optionally, the character set of the language to find.
* @return boolean|Language Returns FALSE if the requested language code and
* character set cannot be found. Returns a Language Object for the
* specified language code and character set.
* @see getMyLanguage()
*/
function getLanguage($code, $charset = '') {
if (!$charset) {
if (isset($this->allLanguages[$code])) {
return current($this->allLanguages[$code]);
} else {
return FALSE;
}
}
foreach ($this->allLanguages[$code] as $language) {
if ($language->getCharacterSet() == $charset) {
return $language;
}
}
return FALSE;
}
/**
* Tries to detect the user's current language preference/setting from (in order):
* _GET, _POST, _SESSION, HTTP_ACCEPT_LANGUAGE, HTTP_USER_AGENT. If no match can be made
* then it tries to detect a default setting (defined in config.inc.php) or a fallback
* setting, false if all else fails.
* @access public
* @return boolean|Language Returns a Language Object matching the user's current session.
* Returns FALSE if a valid Language Object cannot be found
* to match the request
* @see getLanguage()
*/
function getMyLanguage() {
global $addslashes, $db;
if (isset($_GET) && !empty($_GET['lang']) && isset($this->availableLanguages[$_GET['lang']])) {
$language = $this->getLanguage($_GET['lang']);
if ($language) {
return $language;
}
}
if (isset($_POST) && !empty($_POST['lang']) && isset($this->availableLanguages[$_POST['lang']])) {
$language = $this->getLanguage($_POST['lang']);
if ($language) {
return $language;
}
}
if (isset($_SESSION) && isset($_SESSION['lang']) && !empty($_SESSION['lang']) && isset($this->availableLanguages[$_SESSION['lang']])) {
$language = $this->getLanguage($_SESSION['lang']);
if ($language) {
return $language;
}
}
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// Language is not defined yet :
// try to find out user's language by checking its HTTP_ACCEPT_LANGUAGE
$accepted = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$acceptedCnt = count($accepted);
reset($accepted);
for ($i = 0; $i < $acceptedCnt; $i++) {
foreach ($this->availableLanguages as $codes) {
foreach ($codes as $language) {
if ($language->isMatchHttpAcceptLanguage($accepted[$i])) {
return $language;
}
}
}
}
}
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
// Language is not defined yet :
// try to find out user's language by checking its HTTP_USER_AGENT
foreach ($this->availableLanguages as $codes) {
foreach ($codes as $language) {
if ($language->isMatchHttpUserAgent($_SERVER['HTTP_USER_AGENT'])) {
return $language;
}
}
}
}
// Didn't catch any valid lang : we use the default settings
if (isset($this->availableLanguages[DEFAULT_LANGUAGE_CODE])) {
$language = $this->getLanguage(DEFAULT_LANGUAGE_CODE, DEFAULT_CHARSET);
if ($language) {
return $language;
}
}
// else pick one at random:
reset($this->availableLanguages);
$uknown_language = current($this->availableLanguages);
if ($unknown_language) {
return FALSE;
}
return current($uknown_language);
}
function getAvailableLanguages() {
return $this->availableLanguages;
}
// public
function printDropdown($current_language, $name, $id) {
echo '<select name="'.$name.'" id="'.$id.'">';
foreach ($this->availableLanguages as $codes) {
$language = current($codes);
if ($language->getStatus() == AC_STATUS_ENABLED) {
echo '<option value="'.$language->getCode().'"';
if ($language->getCode() == $current_language) {
echo ' selected="selected"';
}
echo '>'.htmlspecialchars($language->getNativeName()).'</option>';
}
}
echo '</select>';
}
// public
function printList($current_language, $name, $id, $url) {
$delim = false;
foreach ($this->availableLanguages as $codes) {
$language = current($codes);
if ($language->getStatus() == AC_STATUS_ENABLED) {
if ($delim){
echo ' | ';
}
if ($language->getCode() == $current_language) {
echo '<strong>'.$language->getNativeName().'</strong>';
} else {
echo '<a href="'.$url.'lang='.$language->getCode().'">'.$language->getNativeName().'</a> ';
}
$delim = true;
}
}
}
// public
function getNumEnabledLanguages() {
return $this->numEnabledLanguages;
}
// public
// checks whether or not the language exists
function exists($code) {
return isset($this->allLanguages[$code]);
}
// public
// import language pack from specified file
// return imported AChecker version if it does not match with the current version
function import($filename, $ignore_version = false) {
global $msg;
$import_path = AC_TEMP_DIR . 'import/';
$zip = new ZipArchive();
if ($zip->open($filename) === TRUE) {
$zip->extractTo($import_path);
$zip->close();
} else {
$msg->addError('CANNOT_UNZIP');
return false;
}
// import
$rtn = $this->import_from_path($import_path, $ignore_version);
// the achecker version from the imported language pack does not match with the current version
// the array of ("imported version", "import path") is returned
if (is_array($rtn)) return $rtn;
// remove uploaded zip file
@unlink($filename);
return true;
}
// public
// import the languages from the files that are in the given folder
public function import_from_path($import_path, $ignore_version=false) {
require_once(AC_INCLUDE_PATH . 'classes/Language/LanguageParser.class.php');
global $languageManager, $msg;
$language_xml = @file_get_contents($import_path.'language.xml');
$languageParser = new LanguageParser();
$languageParser->parse($language_xml);
$languageEditor = $languageParser->getLanguageEditor(0);
$import_version = $languageEditor->getACheckerVersion();
if ($import_version != VERSION && !$ignore_version) {
return array('version'=>$import_version, "import_path"=>$import_path);
}
if ($languageManager->exists($languageEditor->getCode())) {
$msg->addError('LANG_EXISTS');
}
if (!$msg->containsErrors()) {
$languageEditor->import($import_path . 'language_text.sql');
$msg->addFeedback('IMPORT_LANG_SUCCESS');
}
$this->cleanup_language_files($import_path);
return true;
}
// public
// remove language files in the given folder
public function cleanup_language_files($import_path) {
// remove the files:
@unlink($import_path . 'language.xml');
@unlink($import_path . 'language_text.sql');
@unlink($import_path . 'readme.txt');
}
// public
// imports LIVE language from the achecker language database
function liveImport($language_code) {
global $db;
$tmp_lang_db = mysql_connect(AC_LANG_DB_HOST, AC_LANG_DB_USER, AC_LANG_DB_PASS);
// set database connection using utf8
mysql_query("SET NAMES 'utf8'", $tmp_lang_db);
if (!$tmp_lang_db) {
/* AC_ERROR_NO_DB_CONNECT */
echo 'Unable to connect to db.';
exit;
}
if (!mysql_select_db('dev_achecker_langs', $tmp_lang_db)) {
echo 'DB connection established, but database "dev_achecker_langs" cannot be selected.';
exit;
}
$sql = "SELECT * FROM languages_SVN WHERE language_code='$language_code'";
$result = mysql_query($sql, $tmp_lang_db);
if ($row = mysql_fetch_assoc($result)) {
$row['reg_exp'] = addslashes($row['reg_exp']);
$row['native_name'] = addslashes($row['native_name']);
$row['english_name'] = addslashes($row['english_name']);
$sql = "REPLACE INTO ".TABLE_PREFIX."languages VALUES ('{$row['language_code']}', '{$row['charset']}', '{$row['reg_exp']}', '{$row['native_name']}', '{$row['english_name']}', 3)";
$result = mysql_query($sql, $db);
$sql = "SELECT * FROM language_text_SVN WHERE language_code='$language_code'";
$result = mysql_query($sql, $tmp_lang_db);
$sql = "REPLACE INTO ".TABLE_PREFIX."language_text VALUES ";
while ($row = mysql_fetch_assoc($result)) {
$row['text'] = addslashes($row['text']);
$row['context'] = addslashes($row['context']);
$sql .= "('{$row['language_code']}', '{$row['variable']}', '{$row['term']}', '{$row['text']}', '{$row['revised_date']}', '{$row['context']}'),";
}
$sql = substr($sql, 0, -1);
mysql_query($sql, $db);
}
}
}
?>