1: <?php
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: function xlanguage_convert_encoding($value, $out_charset, $in_charset)
27: {
28: if (is_array($value)) {
29: foreach ($value as $key => $val) {
30: $value[$key] = xlanguage_convert_encoding($val, $out_charset, $in_charset);
31: }
32: } else {
33: $value = xlanguage_convert_item($value, $out_charset, $in_charset);
34: }
35: return $value;
36: }
37:
38: 39: 40: 41: 42: 43: 44:
45: function xlanguage_convert_item($value, $out_charset, $in_charset)
46: {
47: $xoops = Xoops::getInstance();
48: if (strtolower($in_charset) == strtolower($out_charset)) {
49: return $value;
50: }
51: $xconv_handler = $xoops->getModuleHandler('xconv', 'xconv', true);
52: if (is_object($xconv_handler) && $converted_value = @$xconv_handler->convert_encoding($value, $out_charset, $in_charset)) {
53: return $converted_value;
54: }
55: if (XoopsLocale::isMultiByte() && function_exists('mb_convert_encoding')) {
56: $converted_value = @mb_convert_encoding($value, $out_charset, $in_charset);
57: } elseif (function_exists('iconv')) {
58: $converted_value = @iconv($in_charset, $out_charset, $value);
59: }
60: $value = empty($converted_value) ? $value : $converted_value;
61:
62: return $value;
63: }
64:
65: 66: 67: 68: 69: 70: 71: 72: 73:
74: function xlanguage_lang_detect($str = '', $envType = 0)
75: {
76: $xoops = Xoops::getInstance();
77: $lang = 'en';
78: foreach ($xoops->registry()->get('XLANGUAGE_AVAILABLE_LANGUAGES') as $key => $value) {
79:
80:
81: $expr = $value[0];
82: if (strpos($expr, '[-_]') === false) {
83: $expr = str_replace('|', '([-_][[:alpha:]]{2,3})?|', $expr);
84: }
85: if (($envType == 1 && preg_match('^(' . $expr . ')(;q=[0-9]\\.[0-9])?$^', $str)) || ($envType == 2 && preg_match('(\(|\[|;[[:space:]])(' . $expr . ')(;|\]|\))', $str))) {
86: $lang = $key;
87: break;
88: }
89: }
90: return $lang;
91: }
92:
93: 94: 95:
96: function xlanguage_detectLang()
97: {
98: $xoops = Xoops::getInstance();
99:
100: if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
101: $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
102: }
103:
104: if (!empty($_SERVER['HTTP_USER_AGENT'])) {
105: $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
106: }
107:
108: $lang = '';
109: $xoops_lang = '';
110:
111:
112: if (empty($lang) && !empty($HTTP_ACCEPT_LANGUAGE)) {
113: $accepted = explode(',', $HTTP_ACCEPT_LANGUAGE);
114: $acceptedCnt = count($accepted);
115: reset($accepted);
116: for ($i = 0; $i < $acceptedCnt; ++$i) {
117: $lang = xlanguage_lang_detect($accepted[$i], 1);
118: if (strncasecmp($lang, 'en', 2)) {
119: break;
120: }
121: }
122: }
123:
124: if (empty($lang) && !empty($HTTP_USER_AGENT)) {
125: $lang = xlanguage_lang_detect($HTTP_USER_AGENT, 2);
126: }
127:
128: if (!empty($lang)) {
129: $available = $xoops->registry()->get('XLANGUAGE_AVAILABLE_LANGUAGES');
130: $xoops_lang = $available[$lang][1];
131: }
132: return $xoops_lang;
133: }
134:
135: 136: 137: 138: 139:
140: function xlanguage_encoding($output)
141: {
142: $xoops = Xoops::getInstance();
143: $xlanguage = $xoops->registry()->get('XLANGUAGE');
144: $output = xlanguage_ml($output);
145:
146: if (preg_match("/^\<\?[\s]?xml[\s]+version=([\"'])[^\>]+\\1[\s]+encoding=([\"'])[^\>]+\\2[\s]?\?\>/i", $output)) {
147: return $output;
148: }
149: $in_charset = $xlanguage['charset_base'];
150: $out_charset = $xlanguage['charset'];
151:
152: return $output = xlanguage_convert_encoding($output, $out_charset, $in_charset);
153: }
154:
155: 156: 157: 158: 159:
160: function xlanguage_ml($s)
161: {
162: static $xlanguage_langs;
163:
164: $xoops = Xoops::getInstance();
165: $xlanguage_handler = $xoops->registry()->get('XLANGUAGE_HANDLER');
166:
167: if (!is_object($xoops)) {
168: $xoops = Xoops::getInstance();
169: }
170: $xoopsConfigLanguage = $xoops->getConfig('locale');
171:
172: if (!isset($xlanguage_langs)) {
173: $langs = $xlanguage_handler->cached_config;
174: foreach (array_keys($langs) as $_lang) {
175: $xlanguage_langs[$_lang] = $langs[$_lang]['xlanguage_code'];
176: }
177: unset($langs);
178: }
179: $xoops->registry()->set('XLANGUAGE_LANGS', $xlanguage_langs);
180: if (empty($xlanguage_langs) || count($xlanguage_langs) == 0) {
181: return $s;
182: }
183:
184:
185: $patterns[] = "/(\<code>.*\<\/code>)/isU";
186:
187:
188: $patterns[] = '/(\<input\b(?![^\>]*\btype=([\'"]?)(submit|image|reset|button))[^\>]*\>)/isU';
189:
190:
191: $patterns[] = "/(\<textarea\b[^>]*>[^\<]*\<\/textarea>)/isU";
192:
193: $s = preg_replace_callback($patterns, 'xlanguage_ml_escape_bracket', $s);
194:
195:
196: $pqhtmltags = explode(',', preg_quote($xoops->registry()->get('XLANGUAGE_TAGS_RESERVED'), '/'));
197: $mid_pattern = '(?:(?!(' . implode('|', $pqhtmltags) . ')).)*';
198:
199: $patterns = array();
200: $replaces = array();
201:
202: if (isset($xlanguage_langs[$xoopsConfigLanguage])) {
203: $lang = $xlanguage_langs[$xoopsConfigLanguage];
204: $patterns[] = '/(\[([^\]]*\|)?' . preg_quote($lang) . '(\|[^\]]*)?\])(' . $mid_pattern . ')(\[\/([^\]]*\|)?' . preg_quote($lang) . '(\|[^\]]*)?\])/isU';
205: $replaces[] = '$4';
206: }
207:
208: foreach (array_keys($xlanguage_langs) as $_lang) {
209: if ($_lang == $xoopsConfigLanguage) {
210: continue;
211: }
212: $name = $xlanguage_langs[$_lang];
213: $patterns[] = '/(\[([^\]]*\|)?' . preg_quote($name) . '(\|[^\]]*)?\])(' . $mid_pattern . ')(\[\/([^\]]*\|)?' . preg_quote($name) . '(\|[^\]]*)?(\]\<br[\s]?[\/]?\>|\]))/isU';
214: $replaces[] = '';
215: }
216: if (!empty($xoopsConfigLanguage)) {
217: $s = preg_replace('/\[[\/]?[\|]?' . preg_quote($xoopsConfigLanguage) . '[\|]?\](\<br \/\>)?/i', '', $s);
218: }
219: if (count($replaces) > 0) {
220: $s = preg_replace($patterns, $replaces, $s);
221: }
222:
223: return $s;
224: }
225:
226: 227: 228: 229: 230:
231: function xlanguage_ml_escape_bracket($matches)
232: {
233: $xoops = Xoops::getInstance();
234: $xlanguage_langs = $xoops->registry()->get('XLANGUAGE_LANGS');
235:
236: $ret = $matches[1];
237: if (!empty($xlanguage_langs)) {
238: $pattern = '/(\[([\/])?(' . implode('|', array_map('preg_quote', array_values($xlanguage_langs))) . ')([\|\]]))/isU';
239: $ret = preg_replace($pattern, '[\\2\\3\\4', $ret);
240: }
241: return $ret;
242: }
243:
244: 245: 246: 247: 248:
249: function xlanguage_select_show($options = null)
250: {
251: $xoops = Xoops::getInstance();
252: if (!$xoops->registry()->get('XLANGUAGE_THEME_ENABLE')) {
253: return false;
254: }
255: include_once \XoopsBaseConfig::get('root-path') . '/modules/xlanguage/blocks/xlanguage_blocks.php';
256: if (empty($options)) {
257: $options[0] = 'images';
258: $options[1] = ' ';
259: $options[2] = 5;
260: }
261:
262: $block = b_xlanguage_select_show($options);
263: $xoops->theme()->addStylesheet('modules/xlanguage/css/block.css');
264: $xoops->tpl()->assign('block', $block);
265: $xlanguage_switch_code = "<div id='xo-language' class='" . $options[0] . "'>" . $xoops->tpl()->fetch('block:xlanguage/xlanguage_block.tpl') . "</div>";
266: $xoops->tpl()->assign('xlanguage_switch_code', $xlanguage_switch_code);
267: return true;
268: }
269: