1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xlanguage; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* xLanguage module (eXtensible Language Management For XOOPS) |
7
|
|
|
* |
8
|
|
|
* You may not change or alter any portion of this comment or credits |
9
|
|
|
* of supporting developers from this source code or any supporting source code |
10
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14
|
|
|
* |
15
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
16
|
|
|
* @license {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License} |
17
|
|
|
* @package xlanguage |
18
|
|
|
* @since 2.0 |
19
|
|
|
* @author D.J.(phppp) [email protected] |
20
|
|
|
**/ |
21
|
|
|
|
22
|
|
|
use XoopsModules\Xlanguage; |
23
|
|
|
|
24
|
|
|
//require(XOOPS_ROOT_PATH."/class/xoopslists.php"); |
25
|
|
|
//require(XOOPS_ROOT_PATH.'/modules/xlanguage/include/vars.php'); |
26
|
|
|
//require(XOOPS_ROOT_PATH.'/modules/xlanguage/class/Utility.php'); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class LanguageHandler |
30
|
|
|
*/ |
31
|
|
|
class LanguageHandler extends \XoopsObjectHandler |
32
|
|
|
{ |
33
|
|
|
public $cachedConfig; |
34
|
|
|
|
35
|
|
|
public function loadConfig() |
36
|
|
|
{ |
37
|
|
|
$this->cachedConfig = $this->loadFileConfig(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int $id |
42
|
|
|
* @param bool $isBase |
43
|
|
|
* |
44
|
|
|
* @return Blanguage|Language|null |
45
|
|
|
*/ |
46
|
|
|
public function get($id, $isBase = true) |
47
|
|
|
{ |
48
|
|
|
$array = []; |
49
|
|
|
$lang = null; |
50
|
|
|
$id = (int)$id; |
51
|
|
|
if (!$id) { |
52
|
|
|
return $lang; |
53
|
|
|
} |
54
|
|
|
$prefix = $isBase ? 'xlanguage_base' : 'xlanguage_ext'; |
55
|
|
|
if (isset($this->cachedConfig[$prefix][$id])) { |
56
|
|
|
$array = $this->cachedConfig[$prefix][$id]; |
57
|
|
|
} else { |
58
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix($prefix) . ' WHERE lang_id=' . $id; |
59
|
|
|
/** @var \mysqli_result|false $result */ |
60
|
|
|
$result = $this->db->query($sql); |
61
|
|
|
if ($result) { |
62
|
|
|
$array = $this->db->fetchArray($result); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if (!is_array($array) || 0 == count($array)) { |
66
|
|
|
return $lang; |
67
|
|
|
} |
68
|
|
|
$lang = $this->create(false, $isBase); |
69
|
|
|
$lang->assignVars($array); |
70
|
|
|
if ($isBase) { |
71
|
|
|
$lang->isBase = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $lang; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param bool $isBase |
80
|
|
|
* |
81
|
|
|
* @return Xlanguage\Blanguage|Xlanguage\Language|null |
82
|
|
|
*/ |
83
|
|
|
public function getByName($name, $isBase=false) |
84
|
|
|
{ |
85
|
|
|
$lang = null; |
86
|
|
|
if (empty($name) || preg_match("/[^a-zA-Z0-9\_\-]/", $name)) { |
87
|
|
|
return $lang; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (isset($this->cachedConfig['xlanguage_base'][$name])) { |
91
|
|
|
$array = $this->cachedConfig['xlanguage_base'][$name]; |
92
|
|
|
$isBase = true; |
93
|
|
|
} elseif (isset($this->cachedConfig['xlanguage_ext'][$name])) { |
94
|
|
|
$array = $this->cachedConfig['xlanguage_ext'][$name]; |
95
|
|
|
} elseif (!isset($this->cachedConfig)) { |
96
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('xlanguage_base') . ' WHERE lang_name=\'' . $name . '\''; |
97
|
|
|
$result = $this->db->query($sql); |
98
|
|
|
$array = $this->db->fetchArray($result); |
99
|
|
|
if (!is_array($array) || 0 == count($array)) { |
100
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('xlanguage_ext') . ' WHERE lang_name=\'' . $name . '\''; |
101
|
|
|
$result = $this->db->query($sql); |
102
|
|
|
$array = $this->db->fetchArray($result); |
103
|
|
|
if (!is_array($array) || 0 == count($array)) { |
104
|
|
|
return $lang; |
105
|
|
|
} |
106
|
|
|
} else { |
107
|
|
|
$isBase = true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
if (empty($array)) { |
111
|
|
|
return $lang; |
112
|
|
|
} |
113
|
|
|
$lang = $this->create(false, $isBase); |
114
|
|
|
$lang->assignVars($array); |
|
|
|
|
115
|
|
|
if (!isset($array['lang_base'])) { |
116
|
|
|
$lang->isBase = true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $lang; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param bool $isBase |
124
|
|
|
* |
125
|
|
|
* @return array|false |
126
|
|
|
*/ |
127
|
|
|
public function getAll($isBase = true) |
128
|
|
|
{ |
129
|
|
|
$prefix = $isBase ? 'xlanguage_base' : 'xlanguage_ext'; |
130
|
|
|
$ret = []; |
131
|
|
|
if (isset($this->cachedConfig[$prefix])) { |
132
|
|
|
$array = $this->cachedConfig[$prefix]; |
133
|
|
|
foreach ($array as $lang_name => $myrow) { |
134
|
|
|
$lang = $this->create(false, $isBase); |
135
|
|
|
$lang->assignVars($myrow); |
136
|
|
|
$ret[$myrow['lang_name']] = &$lang; |
137
|
|
|
unset($lang); |
138
|
|
|
} |
139
|
|
|
} elseif (!isset($this->cachedConfig)) { |
140
|
|
|
// } elseif (false === $this->cachedConfig) { |
141
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix($prefix); |
142
|
|
|
/** @var \mysqli_result|false $result */ |
143
|
|
|
$result = $this->db->query($sql); |
144
|
|
|
if (!$result ) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
148
|
|
|
$lang = $this->create(false, $isBase); |
149
|
|
|
$lang->assignVars($myrow); |
150
|
|
|
$ret[$myrow['lang_name']] = &$lang; |
151
|
|
|
unset($lang); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $ret; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
public function getAllList() |
162
|
|
|
{ |
163
|
|
|
$baseArray = $this->getAll(); |
164
|
|
|
|
165
|
|
|
$extArray = $this->getAll(false); |
166
|
|
|
$ret = []; |
167
|
|
|
if ($baseArray && is_array($baseArray)) { |
168
|
|
|
foreach ($baseArray as $base) { |
169
|
|
|
$ret[$base->getVar('lang_name')]['base'] = $base; |
170
|
|
|
unset($base); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
if ($extArray && is_array($extArray)) { |
174
|
|
|
foreach ($extArray as $ext) { |
175
|
|
|
$ret[$ext->getVar('lang_base')]['ext'][] = $ext; |
176
|
|
|
unset($ext); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $ret; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param bool $isNew |
185
|
|
|
* @param bool $isBase |
186
|
|
|
* |
187
|
|
|
* @return Blanguage|Language |
188
|
|
|
*/ |
189
|
|
|
public function create($isNew = true, $isBase = true) |
190
|
|
|
{ |
191
|
|
|
if ($isBase) { |
192
|
|
|
$lang = new Xlanguage\Blanguage($isBase); |
193
|
|
|
} else { |
194
|
|
|
$lang = new Xlanguage\Language(); |
195
|
|
|
} |
196
|
|
|
if ($isNew) { |
197
|
|
|
$lang->setNew(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $lang; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param Blanguage|Language $lang |
205
|
|
|
* @return bool|string|array |
206
|
|
|
* @internal param object $lang |
207
|
|
|
*/ |
208
|
|
|
public function insert(Blanguage $lang) |
209
|
|
|
{ |
210
|
|
|
$val_array = []; |
211
|
|
|
if (!$lang->isDirty()) { |
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
if (!$lang->cleanVars()) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
$lang->prepareVars(); |
218
|
|
|
foreach ($lang->cleanVars as $k => $v) { |
219
|
|
|
${$k} = $v; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($lang->isNew()) { |
223
|
|
|
$var_array = [ |
224
|
|
|
'lang_id', |
225
|
|
|
'weight', |
226
|
|
|
'lang_name', |
227
|
|
|
'lang_desc', |
228
|
|
|
'lang_code', |
229
|
|
|
'lang_charset', |
230
|
|
|
'lang_image', |
231
|
|
|
'lang_base', |
232
|
|
|
]; |
233
|
|
|
if ($lang->isBase) { |
234
|
|
|
$var_array = [ |
235
|
|
|
'lang_id', |
236
|
|
|
'weight', |
237
|
|
|
'lang_name', |
238
|
|
|
'lang_desc', |
239
|
|
|
'lang_code', |
240
|
|
|
'lang_charset', |
241
|
|
|
'lang_image', |
242
|
|
|
]; |
243
|
|
|
} |
244
|
|
|
$lang_id = $this->db->genId($lang->table . '_lang_id_seq'); |
245
|
|
|
foreach ($var_array as $var) { |
246
|
|
|
$val_array[] = ${$var}; |
247
|
|
|
} |
248
|
|
|
$sql = 'INSERT INTO ' . $lang->table . ' (' . implode(',', $var_array) . ') VALUES (' . implode(',', $val_array) . ')'; |
249
|
|
|
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
250
|
|
|
xoops_error('Insert language error:' . $sql); |
251
|
|
|
|
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
if (0 == $lang_id) { |
255
|
|
|
$lang_id = $this->db->getInsertId(); |
256
|
|
|
} |
257
|
|
|
$lang->setVar('lang_id', $lang_id); |
258
|
|
|
} else { |
259
|
|
|
$var_array = [ |
260
|
|
|
'weight', |
261
|
|
|
'lang_name', |
262
|
|
|
'lang_desc', |
263
|
|
|
'lang_code', |
264
|
|
|
'lang_charset', |
265
|
|
|
'lang_image', |
266
|
|
|
'lang_base', |
267
|
|
|
]; |
268
|
|
|
if ($lang->isBase) { |
269
|
|
|
$var_array = ['weight', 'lang_name', 'lang_desc', 'lang_code', 'lang_charset', 'lang_image']; |
270
|
|
|
} |
271
|
|
|
$set_array = []; |
272
|
|
|
foreach ($var_array as $var) { |
273
|
|
|
$set_array[] = "$var = " . ${$var}; |
274
|
|
|
} |
275
|
|
|
$set_string = implode(',', $set_array); |
276
|
|
|
$sql = 'UPDATE ' . $lang->table . ' SET ' . $set_string . ' WHERE lang_id = ' . $lang->getVar('lang_id'); |
277
|
|
|
if (!$result = $this->db->queryF($sql)) { |
278
|
|
|
xoops_error('update language error:' . $sql); |
279
|
|
|
|
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
$this->createConfig(); |
284
|
|
|
|
285
|
|
|
return $lang->getVar('lang_id'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param \XoopsObject $lang |
290
|
|
|
* @return bool |
291
|
|
|
* @internal param object $lang |
292
|
|
|
*/ |
293
|
|
|
public function delete(\XoopsObject $lang)//delete(&$lang) |
294
|
|
|
{ |
295
|
|
|
if (!is_object($lang) || !$lang->getVar('lang_id')) { |
296
|
|
|
return true; |
297
|
|
|
} |
298
|
|
|
$sql = 'DELETE FROM ' . $lang->table . ' WHERE lang_id= ' . $lang->getVar('lang_id'); |
299
|
|
|
if (!$result = $this->db->query($sql)) { |
|
|
|
|
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
$this->createConfig(); |
303
|
|
|
|
304
|
|
|
return true; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
|
|
public function getXoopsLangList() |
311
|
|
|
{ |
312
|
|
|
return \XoopsLists::getLangList(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
public function createConfig() |
319
|
|
|
{ |
320
|
|
|
$file_config = XLANGUAGE_CONFIG_FILE; |
321
|
|
|
@unlink($file_config); |
|
|
|
|
322
|
|
|
if (!$fp = fopen($file_config, 'wb')) { |
323
|
|
|
echo '<br> the config file can not be created: ' . $file_config; |
324
|
|
|
|
325
|
|
|
return false; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$file_content = '<?php'; |
329
|
|
|
unset($this->cachedConfig); |
330
|
|
|
$baseArray = $this->getAll(); |
331
|
|
|
if ($baseArray && is_array($baseArray)) { |
332
|
|
|
$file_content .= "\n \$" . XLANGUAGE_CONFIG_VAR . "['xlanguage_base'] = array("; |
333
|
|
|
foreach ($baseArray as $lang) { |
334
|
|
|
$file_content .= "\n \"" . $lang->getVar('lang_name') . '"=>array('; |
335
|
|
|
$file_content .= "\n \"lang_id\"=>" . $lang->getVar('lang_id') . ','; |
336
|
|
|
$file_content .= "\n \"weight\"=>" . $lang->getVar('weight') . ','; |
337
|
|
|
$file_content .= "\n \"lang_name\"=>\"" . $lang->getVar('lang_name') . '",'; |
338
|
|
|
$file_content .= "\n \"lang_desc\"=>\"" . $lang->getVar('lang_desc') . '",'; |
339
|
|
|
$file_content .= "\n \"lang_code\"=>\"" . $lang->getVar('lang_code') . '",'; |
340
|
|
|
$file_content .= "\n \"lang_charset\"=>\"" . $lang->getVar('lang_charset') . '",'; |
341
|
|
|
$file_content .= "\n \"lang_image\"=>\"" . $lang->getVar('lang_image') . '"'; |
342
|
|
|
$file_content .= "\n ),"; |
343
|
|
|
} |
344
|
|
|
$file_content .= "\n );"; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$extArray = $this->getAll(false); |
348
|
|
|
if ($extArray && is_array($extArray)) { |
349
|
|
|
$file_content .= "\n \$" . XLANGUAGE_CONFIG_VAR . "['xlanguage_ext'] = array("; |
350
|
|
|
foreach ($extArray as $lang) { |
351
|
|
|
$file_content .= "\n \"" . $lang->getVar('lang_name') . '"=>array('; |
352
|
|
|
$file_content .= "\n \"lang_id\"=>" . $lang->getVar('lang_id') . ','; |
353
|
|
|
$file_content .= "\n \"weight\"=>" . $lang->getVar('weight') . ','; |
354
|
|
|
$file_content .= "\n \"lang_name\"=>\"" . $lang->getVar('lang_name') . '",'; |
355
|
|
|
$file_content .= "\n \"lang_desc\"=>\"" . $lang->getVar('lang_desc') . '",'; |
356
|
|
|
$file_content .= "\n \"lang_code\"=>\"" . $lang->getVar('lang_code') . '",'; |
357
|
|
|
$file_content .= "\n \"lang_charset\"=>\"" . $lang->getVar('lang_charset') . '",'; |
358
|
|
|
$file_content .= "\n \"lang_image\"=>\"" . $lang->getVar('lang_image') . '",'; |
359
|
|
|
$file_content .= "\n \"lang_base\"=>\"" . $lang->getVar('lang_base') . '"'; |
360
|
|
|
$file_content .= "\n ),"; |
361
|
|
|
} |
362
|
|
|
$file_content .= "\n );"; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
$file_content .= "\n?>"; |
366
|
|
|
fwrite($fp, $file_content); |
367
|
|
|
fclose($fp); |
368
|
|
|
|
369
|
|
|
return true; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return bool|null |
374
|
|
|
*/ |
375
|
|
|
public function loadFileConfig() |
376
|
|
|
{ |
377
|
|
|
$file_config = XLANGUAGE_CONFIG_FILE; |
378
|
|
|
if (!file_exists($file_config)) { |
379
|
|
|
$this->createConfig(); |
380
|
|
|
} |
381
|
|
|
if (!is_readable($file_config)) { |
382
|
|
|
$config = null; |
383
|
|
|
|
384
|
|
|
return $config; |
385
|
|
|
} |
386
|
|
|
require $file_config; |
387
|
|
|
if (isset(${XLANGUAGE_CONFIG_VAR})) { |
388
|
|
|
return ${XLANGUAGE_CONFIG_VAR}; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return false; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|