1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* xLanguage module (eXtensible Language Management For XOOPS) |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
13
|
|
|
* @license {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License} |
14
|
|
|
* @package xlanguage |
15
|
|
|
* @since 2.0 |
16
|
|
|
* @author D.J.(phppp) [email protected] |
17
|
|
|
* @param $value |
18
|
|
|
* @param $out_charset |
19
|
|
|
* @param $in_charset |
20
|
|
|
* @return array|string |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
function xlanguage_convert_encoding($value, $out_charset, $in_charset) |
24
|
|
|
{ |
25
|
|
|
if (is_array($value)) { |
26
|
|
|
foreach ($value as $key => $val) { |
27
|
|
|
$value[$key] = xlanguage_convert_encoding($val, $out_charset, $in_charset); |
28
|
|
|
} |
29
|
|
|
} else { |
30
|
|
|
$value = xlanguage_convert_item($value, $out_charset, $in_charset); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $value; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $value |
38
|
|
|
* @param $out_charset |
39
|
|
|
* @param $in_charset |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
function xlanguage_convert_item($value, $out_charset, $in_charset) |
43
|
|
|
{ |
44
|
|
|
if (strtolower($in_charset) == strtolower($out_charset)) { |
45
|
|
|
return $value; |
46
|
|
|
} |
47
|
|
|
$xconvHandler = @xoops_getModuleHandler('xconv', 'xconv', true); |
48
|
|
|
if (is_object($xconvHandler) && $converted_value = @$xconvHandler->convert_encoding($value, $out_charset, $in_charset)) { |
49
|
|
|
return $converted_value; |
50
|
|
|
} |
51
|
|
|
if (XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) { |
52
|
|
|
$converted_value = @mb_convert_encoding($value, $out_charset, $in_charset); |
53
|
|
|
} elseif (function_exists('iconv')) { |
54
|
|
|
$converted_value = @iconv($in_charset, $out_charset, $value); |
55
|
|
|
} |
56
|
|
|
$value = empty($converted_value) ? $value : $converted_value; |
57
|
|
|
|
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
function xlanguage_createConfig() |
65
|
|
|
{ |
66
|
|
|
/** @var \XlanguageLanguageHandler $xlanguageHandler */ |
67
|
|
|
$xlanguageHandler = xoops_getModuleHandler('language', 'xlanguage'); |
68
|
|
|
|
69
|
|
|
return $xlanguageHandler->createConfig(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
function xlanguage_loadConfig() |
76
|
|
|
{ |
77
|
|
|
/** @var \XlanguageLanguageHandler $xlanguageHandler */ |
78
|
|
|
$xlanguageHandler = xoops_getModuleHandler('language', 'xlanguage'); |
79
|
|
|
$config = $xlanguageHandler->loadFileConfig(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return $config; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Analyzes some PHP environment variables to find the most probable language |
86
|
|
|
* that should be used |
87
|
|
|
* |
88
|
|
|
* @param string $str |
89
|
|
|
* @param string $envType |
90
|
|
|
* @return int|string |
91
|
|
|
* @internal param $string $ string to analyze |
92
|
|
|
* @internal param $integer $ type of the PHP environment variable which value is $str |
93
|
|
|
* |
94
|
|
|
* @global array the list of available translations |
95
|
|
|
* @global string the retained translation keyword |
96
|
|
|
* @access private |
97
|
|
|
*/ |
98
|
|
|
function xlanguage_lang_detect($str = '', $envType = '') |
99
|
|
|
{ |
100
|
|
|
global $available_languages; |
|
|
|
|
101
|
|
|
$lang = ''; |
102
|
|
|
|
103
|
|
|
if (!empty($available_languages)) { |
104
|
|
|
foreach ($available_languages as $key => $value) { |
105
|
|
|
// $envType = 1 for the 'HTTP_ACCEPT_LANGUAGE' environment variable, |
106
|
|
|
// 2 for the 'HTTP_USER_AGENT' one |
107
|
|
|
$expr = $value[0]; |
108
|
|
|
if (strpos($expr, '[-_]') === false) { |
109
|
|
|
$expr = str_replace('|', '([-_][[:alpha:]]{2,3})?|', $expr); |
110
|
|
|
} |
111
|
|
|
// if (($envType == 1 && eregi('^(' . $expr . ')(;q=[0-9]\\.[0-9])?$', $str)) |
|
|
|
|
112
|
|
|
// || ($envType == 2 && eregi('(\(|\[|;[[:space:]])(' . $expr . ')(;|\]|\))', $str))) { |
|
|
|
|
113
|
|
|
if (($envType == 1 && preg_match('#^(' . $expr . ')(;q=[0-9]\\.[0-9])?$#i', $str)) || ($envType == 2 && preg_match('#(\(|\[|;[[:space:]])(' . $expr . ')(;|\]|\))#i', $str))) { |
114
|
|
|
$lang = $key; |
115
|
|
|
//if($lang != 'en') |
|
|
|
|
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $lang; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
function xlanguage_detectLang() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
global $available_languages, $_SERVER; |
|
|
|
|
130
|
|
|
|
131
|
|
|
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
132
|
|
|
$HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
136
|
|
|
$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT']; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$lang = ''; |
|
|
|
|
140
|
|
|
$xoops_lang = ''; |
141
|
|
|
// 1. try to findout user's language by checking its HTTP_ACCEPT_LANGUAGE variable |
142
|
|
|
|
143
|
|
|
// if (empty($lang) && !empty($HTTP_ACCEPT_LANGUAGE)) { |
|
|
|
|
144
|
|
|
// $accepted = explode(',', $HTTP_ACCEPT_LANGUAGE); |
|
|
|
|
145
|
|
|
// $acceptedCnt = count($accepted); |
|
|
|
|
146
|
|
|
// reset($accepted); |
|
|
|
|
147
|
|
|
// for ($i = 0; $i < $acceptedCnt; ++$i) { |
|
|
|
|
148
|
|
|
// $lang = xlanguage_lang_detect($accepted[$i], 1); |
|
|
|
|
149
|
|
|
// if (strncasecmp($lang, 'en', 2)) { |
|
|
|
|
150
|
|
|
// break; |
151
|
|
|
// } |
152
|
|
|
// } |
153
|
|
|
// } |
154
|
|
|
|
155
|
|
|
//This returns the most preferred langauage "q=1" |
156
|
|
|
$lang = getPreferredLanguage(); |
157
|
|
|
|
158
|
|
|
// 2. if not found in HTTP_ACCEPT_LANGUAGE, try to find user's language by checking its HTTP_USER_AGENT variable |
159
|
|
|
if (empty($lang) && !empty($HTTP_USER_AGENT)) { |
160
|
|
|
$lang = xlanguage_lang_detect($HTTP_USER_AGENT, 2); |
161
|
|
|
} |
162
|
|
|
// 3. If we catch a valid language, configure it |
163
|
|
|
if (!empty($lang)) { |
164
|
|
|
$xoops_lang = $available_languages[$lang][1]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $xoops_lang; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param $output |
172
|
|
|
* @return array|mixed|string |
173
|
|
|
*/ |
174
|
|
|
function xlanguage_encoding($output) |
175
|
|
|
{ |
176
|
|
|
global $xlanguage; |
|
|
|
|
177
|
|
|
$output = xlanguage_ml($output); |
178
|
|
|
// escape XML doc |
179
|
|
|
if (preg_match("/^\<\?[\s]?xml[\s]+version=([\"'])[^\>]+\\1[\s]+encoding=([\"'])[^\>]+\\2[\s]?\?\>/i", $output)) { |
180
|
|
|
return $output; |
181
|
|
|
} |
182
|
|
|
$in_charset = $xlanguage['charset_base']; |
183
|
|
|
$out_charset = $xlanguage['charset']; |
184
|
|
|
|
185
|
|
|
$output = xlanguage_convert_encoding($output, $out_charset, $in_charset); |
186
|
|
|
return $output; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param $s |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
|
|
function xlanguage_ml($s) |
194
|
|
|
{ |
195
|
|
|
global $xoopsConfig; |
|
|
|
|
196
|
|
|
global $xlanguage_langs; |
|
|
|
|
197
|
|
|
if (!isset($xlanguage_langs)) { |
198
|
|
|
|
199
|
|
|
/** @var \XlanguageLanguageHandler $xlanguageHandler */ |
200
|
|
|
$xlanguageHandler = xoops_getModuleHandler('language', 'xlanguage'); |
201
|
|
|
$langs = $xlanguageHandler->getAll(true); |
202
|
|
|
// $langs = $GLOBALS['xlanguageHandler']->getAll(true); //mb |
|
|
|
|
203
|
|
|
foreach (array_keys($langs) as $_lang) { |
204
|
|
|
$xlanguage_langs[$_lang] = $langs[$_lang]->getVar('lang_code'); |
205
|
|
|
} |
206
|
|
|
unset($langs); |
207
|
|
|
} |
208
|
|
|
if (empty($xlanguage_langs) || count($xlanguage_langs) == 0) { |
209
|
|
|
return $s; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// escape brackets inside of <code>...</code> |
213
|
|
|
$patterns[] = '/(\<code>.*\<\/code>)/isU'; |
|
|
|
|
214
|
|
|
|
215
|
|
|
// escape brackets inside of <input type="..." value="..."> |
216
|
|
|
$patterns[] = '/(\<input\b(?![^\>]*\btype=([\'"]?)(submit|image|reset|button))[^\>]*\>)/isU'; |
217
|
|
|
|
218
|
|
|
// escape brackets inside of <textarea></textarea> |
219
|
|
|
$patterns[] = '/(\<textarea\b[^>]*>[^\<]*\<\/textarea>)/isU'; |
220
|
|
|
|
221
|
|
|
$s = preg_replace_callback($patterns, 'xlanguage_ml_escape_bracket', $s); |
222
|
|
|
|
223
|
|
|
// create the pattern between language tags |
224
|
|
|
$pqhtmltags = explode(',', preg_quote(XLANGUAGE_TAGS_RESERVED, '/')); |
225
|
|
|
$mid_pattern = '(?:(?!(' . implode('|', $pqhtmltags) . ')).)*'; |
226
|
|
|
|
227
|
|
|
$patterns = array(); |
228
|
|
|
$replaces = array(); |
229
|
|
|
/* */ |
230
|
|
|
if (isset($xlanguage_langs[$xoopsConfig['language']])) { |
231
|
|
|
$lang = $xlanguage_langs[$xoopsConfig['language']]; |
232
|
|
|
$patterns[] = '/(\[([^\]]*\|)?' . preg_quote($lang) . '(\|[^\]]*)?\])(' . $mid_pattern . ')(\[\/([^\]]*\|)?' . preg_quote($lang) . '(\|[^\]]*)?\])/isU'; |
233
|
|
|
$replaces[] = '$4'; |
234
|
|
|
} |
235
|
|
|
/* */ |
236
|
|
|
foreach (array_keys($xlanguage_langs) as $_lang) { |
237
|
|
|
if ($_lang == @$xoopsConfig['language']) { |
238
|
|
|
continue; |
239
|
|
|
} |
240
|
|
|
$name = $xlanguage_langs[$_lang]; |
241
|
|
|
$patterns[] = '/(\[([^\]]*\|)?' . preg_quote($name) . '(\|[^\]]*)?\])(' . $mid_pattern . ')(\[\/([^\]]*\|)?' . preg_quote($name) . '(\|[^\]]*)?(\]\<br[\s]?[\/]?\>|\]))/isU'; |
242
|
|
|
$replaces[] = ''; |
243
|
|
|
} |
244
|
|
|
if (!empty($xoopsConfig['language'])) { |
245
|
|
|
$s = preg_replace('/\[[\/]?[\|]?' . preg_quote($xoopsConfig['language']) . '[\|]?\](\<br \/\>)?/i', '', $s); |
246
|
|
|
} |
247
|
|
|
if (count($replaces) > 0) { |
248
|
|
|
$s = preg_replace($patterns, $replaces, $s); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $s; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param $matches |
256
|
|
|
* @return mixed |
257
|
|
|
*/ |
258
|
|
|
function xlanguage_ml_escape_bracket($matches) |
259
|
|
|
{ |
260
|
|
|
global $xlanguage_langs; |
|
|
|
|
261
|
|
|
$ret = $matches[1]; |
262
|
|
|
if (!empty($xlanguage_langs)) { |
263
|
|
|
$pattern = '/(\[([\/])?(' . implode('|', array_map('preg_quote', array_values($xlanguage_langs))) . ')([\|\]]))/isU'; |
264
|
|
|
$ret = preg_replace($pattern, "[\\2\\3\\4", $ret); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $ret; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param null $options |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
|
|
function xlanguage_select_show($options = null) |
275
|
|
|
{ |
276
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/xlanguage/blocks/xlanguage_blocks.php'; |
277
|
|
|
if (empty($options)) { |
278
|
|
|
$options[0] = 'images'; // display style: image, text, select |
279
|
|
|
$options[1] = ' '; // delimitor |
280
|
|
|
$options[2] = 5; // items per line |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$block = b_xlanguage_select_show($options); |
284
|
|
|
$block['tag'] = 'xlanguage'; |
285
|
|
|
|
286
|
|
|
$content = ''; |
287
|
|
|
$i = 1; |
288
|
|
|
if (!empty($block['display'])) { //mb |
289
|
|
|
if (in_array($block['display'], array('images', 'text'))) { |
290
|
|
|
foreach ($block['languages'] as $name => $lang) { |
291
|
|
|
$content .= '<a href="' . $block['url'] . $lang['name'] . '" title="' . $lang['desc'] . '">'; |
292
|
|
|
if ($block['display'] === 'images') { |
293
|
|
|
$content .= '<img src="' . $lang['image'] . '" alt="' . $lang['desc'] . '"'; |
294
|
|
|
if ($block['selected'] != $lang['name']) { |
295
|
|
|
$content .= ' style="MozOpacity: .8; opacity: .8; filter:Alpha(opacity=80);"'; |
296
|
|
|
} |
297
|
|
|
$content .= '>'; |
298
|
|
|
} else { |
299
|
|
|
$content .= $lang['desc']; |
300
|
|
|
} |
301
|
|
|
$content .= '</a>'; |
302
|
|
|
if ((++$i % $block['number']) == 0) { |
303
|
|
|
$content .= '<br>'; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} else { |
307
|
|
|
$content .= '<select name="' . $block['tag'] . '" |
308
|
|
|
onChange="if (this.options[this.selectedIndex].value.length >0) { window.document.location=this.options[this.selectedIndex].value;}" |
309
|
|
|
>'; |
310
|
|
|
if (!empty($block['languages'])) { //mb |
311
|
|
|
foreach ($block['languages'] as $name => $lang) { |
|
|
|
|
312
|
|
|
$content .= '<option value="' . $block['url'] . $lang['name'] . '"'; |
313
|
|
|
if ($block['selected'] == $lang['name']) { |
314
|
|
|
$content .= ' selected '; |
315
|
|
|
} |
316
|
|
|
$content .= '>' . $lang['desc'] . '</option>'; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
$content .= '</select>'; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
define('XLANGUAGE_SWITCH_CODE', $content); |
324
|
|
|
|
325
|
|
|
return true; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return int|string |
330
|
|
|
*/ |
331
|
|
|
function getPreferredLanguage() |
|
|
|
|
332
|
|
|
{ |
333
|
|
|
$langs = array(); |
334
|
|
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
335
|
|
|
// break up string into pieces (languages and q factors) |
336
|
|
|
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.\d+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); |
337
|
|
|
if (count($lang_parse[1])) { |
338
|
|
|
// create a list like "en" => 0.8 |
339
|
|
|
$langs = array_combine($lang_parse[1], $lang_parse[4]); |
340
|
|
|
// set default to 1 for any without q factor |
341
|
|
|
foreach ($langs as $lang => $val) { |
342
|
|
|
if ($val === '') { |
343
|
|
|
$langs[$lang] = 1; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
// sort list based on value |
347
|
|
|
arsort($langs, SORT_NUMERIC); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
//extract most important (first) |
351
|
|
|
foreach ($langs as $lang => $val) { |
352
|
|
|
break; |
353
|
|
|
} |
354
|
|
|
//if complex language simplify it |
355
|
|
|
if (strstr($lang, '-')) { |
356
|
|
|
$tmp = explode('-', $lang); |
|
|
|
|
357
|
|
|
$lang = $tmp[0]; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $lang; |
361
|
|
|
} |
362
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.