forked from NorHei/multilingual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.functions.php
313 lines (275 loc) · 11.5 KB
/
lang.functions.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
<?php
//no direct file access
if (count(get_included_files()) ==1) {
$z="HTTP/1.0 404 Not Found";
header($z);
die($z);
}
include_once(WB_PATH.'/framework/functions.php');
include_once(WB_PATH.'/framework/module.functions.php');
require_once(WB_PATH.'/framework/class.frontend.php');
require_once(WB_PATH.'/framework/class.admin.php');
$default_language = DEFAULT_LANGUAGE;
// later get from addons or directory
$allowed_langs = array('bg','ca','cs','da','de','en','es','et','fi','fr','hr','hu','it','lv','nl','no','pl','pt','ru','sv','sk','tr');
// Browsersprache ermitteln
function lang_getfrombrowser($allowed_langs, $default_language, $lang_variable = null, $strict_mode = true)
{
// $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde
if ($lang_variable === null) {
$lang_variable = isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
? $_SERVER['HTTP_ACCEPT_LANGUAGE']
: 'en-GB';
}
// wurde irgendwelche Information mitgeschickt?
if (empty($lang_variable)) {
// Nein? => Standardsprache zurueckgeben
return strtoupper($default_language);
}
// Den Header auftrennen
$accepted_languages = preg_split('/,\s*/', $lang_variable);
// Die Standardwerte einstellen
$current_lang = $default_language;
$current_q = 0;
// Nun alle mitgegebenen Sprachen abarbeiten
foreach ($accepted_languages as $accepted_language) {
// Alle Infos ueber diese Sprache rausholen
$res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)'.
'(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches);
// war die Syntax gueltig?
if (!$res) {
// Nein? Dann ignorieren
continue;
}
// Sprachcode holen und dann sofort in die Einzelteile trennen
$lang_code = explode('-', $matches[1]);
// Wurde eine Qualitaet mitgegeben?
if (isset($matches[2])) {
// die Qualitaet benutzen
$lang_quality = (float)$matches[2];
} else {
// Kompabilitaetsmodus: Qualitaet 1 annehmen
$lang_quality = 1.0;
}
// Bis der Sprachcode leer ist...
while (count($lang_code)) {
// mal sehen, ob der Sprachcode angeboten wird
if (in_array(strtolower(join('-', $lang_code)), $allowed_langs)) {
// Qualitaet anschauen
if ($lang_quality > $current_q) {
// diese Sprache verwenden
$current_lang = strtolower(join('-', $lang_code));
$current_q = $lang_quality;
// Hier die innere while-Schleife verlassen
break;
}
}
// Wenn wir im strengen Modus sind, die Sprache nicht versuchen zu minimalisieren
if ($strict_mode) {
// innere While-Schleife aufbrechen
break;
}
// den rechtesten Teil des Sprachcodes abschneiden
array_pop($lang_code);
}
}
// die gefundene Sprache zurueckgeben
return strtoupper($current_lang);
}
function get_languages($langKey)
{
global $database, $admin;
$return_value = 'English';
$sql = 'SELECT `name`,`type`,`directory` FROM `'.TABLE_PREFIX.'addons` ';
$sql .= 'WHERE `type` = \'language\' AND `directory` = \''.$langKey.'\' ';
$query_num = $database->query($sql);
if (!$database->is_error() && ($query_num->numRows() == 1)) {
$result = $query_num->fetchRow(MYSQLI_ASSOC);
$return_value = $result['name'];
} else {
$return_value = $admin->print_error($database->get_error(), '');
}
return $return_value;
}
function set_language_icon($pageId = 0, $ext='txt')
{
$return_value = array();
$mod_path = dirname(__FILE__);
$mod_rel = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', $mod_path));
$mod_name = basename($mod_path);
$array = get_page_languages();
$array2= get_pageCode_values($pageId);
$langPageArray = array_merge($array, $array2);
foreach ($langPageArray as $key=>$value) {
$langKey = $key;
if ($array[$langKey]['visibility'] == 'hidden') {
continue;
}
$page_title = get_languages($langKey);
$langUrl = get_page_url($value);
$class = strtoupper($langKey) == LANGUAGE ? 'class="current"' : ' class="default"';
$return_value [ $langKey ] = "\t\t".'<a '.$class.' href ="'. $langUrl .'" title="'.$page_title.'" >'.PHP_EOL;
$return_value [ $langKey ] .= "\t\t\t".'<span>';
if ($ext=='TXT') {
$return_value [ $langKey ] .= " ".$array[$key]['page_title']. " " ;
} elseif ($ext=='txt') {
$return_value [ $langKey ] .=" $langKey " ;
} else {
$return_value [ $langKey ] .= PHP_EOL."\t\t\t\t".'<img src="'.WB_URL.'/modules/'.$mod_name.'/flags/'.strtolower($langKey).'.'.$ext.'" alt="'.$page_title.'" title="'.$page_title.'" />'.PHP_EOL."\t\t\t";
}
$return_value [ $langKey ] .= '</span>'.PHP_EOL."\t\t".'</a>'.PHP_EOL;
}
return $return_value;
}
function set_language_array($pageId = 0)
{
$return_value = array();
$mod_path = dirname(__FILE__);
$mod_rel = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', $mod_path));
$mod_name = basename($mod_path);
$array = get_page_languages();
$array2= get_pageCode_values($pageId);
$langPageArray = array_merge($array, $array2);
foreach ($langPageArray as $key=>$value) {
$langKey = $key;
if ($array[$langKey]['visibility'] == 'hidden') {
continue;
}
$return_value[$langKey]['title'] = get_languages($langKey);
$return_value[$langKey]['url'] = get_page_url($value);
$return_value[$langKey]['active'] = strtoupper($langKey) == LANGUAGE ? true : false;
$return_value[$langKey]['image'] = WB_URL.'/modules/'.$mod_name.'/flags/'.strtolower($langKey).'.';
}
return $return_value;
}
function get_pageCode_values($pageId = 0)
{
global $database;
$return_value = array();
$sql = 'SELECT `page_code` FROM `'.TABLE_PREFIX.'pages` ';
$sql .= 'WHERE `page_id` = '.(int)$pageId.' ';
$sql .= '';
$query_num = $database->query($sql);
if ($database->is_error()) {
return $return_value;
}
if ($query_num->numRows() == 1) {
$query_result = $query_num->fetchRow(MYSQLI_ASSOC);
$result = trim($query_result['page_code']);
if (!empty($result)) {
// search entry in pages
$sql = 'SELECT `page_id`, `page_code`,`language`,`visibility` FROM `'.TABLE_PREFIX.'pages` WHERE `page_code` = '.$result.' ORDER BY `position`';
$query_code = $database->query($sql);
if ($numrows = $query_code->numRows()) {
while ($return = $query_code->fetchRow(MYSQLI_ASSOC)) {
$return_value[$return['language']] = $return;
}
}
}
}
return $return_value;
}
// function to update a var/value-pair into table
function db_update_field_entry($page_id, $table, $entry=null)
{
global $database;
if (!isset($entry)) { // set page_code = $page_id
$sql = 'UPDATE `'.TABLE_PREFIX.$table.'` SET `page_code` = '.$page_id.', ';
} else { // if set an entry
$sql = 'UPDATE `'.TABLE_PREFIX.$table.'` SET `page_code` = '.$entry.', ';
}
$sql .= '`modified_when` = '.time().' WHERE `page_id` = '.$page_id;
$sql .= '';
return $database->query($sql);
}
// set array with languages setting in wb_pages
function get_page_languages()
{
global $database, $admin, $wb;
$result=array();
$query = 'SELECT `level`,`page_title`,`language`,`visibility`,`viewing_groups`,`viewing_users`,`page_id`,`page_code`,`link`,`parent` ';
$query .= 'FROM `'.TABLE_PREFIX.'pages` ';
$query .= 'WHERE `level` = \'0\' ';
$query .= 'AND `menu_title` LIKE \'__\' ';
// $query .= 'AND `visibility` = \'public\' ';
$query .= 'GROUP BY `language` ';
$query .= 'ORDER BY `position` ';
$get_query = $database->query($query);
if ($get_query->numRows() > 0) {
while ($value = $get_query->fetchRow(MYSQLI_ASSOC)) {
if (!$admin->page_is_visible($value)) {
continue;
}
$result[$value['language']]['page_id'] = $value['page_id'];
$result[$value['language']]['page_code'] = $value['page_code'];
$result[$value['language']]['language'] = $value['language'];
$result[$value['language']]['visibility'] = $value['visibility'];
$result[$value['language']]['page_title'] = $value['page_title'];
}
}
return $result;
}
// set the absolute url with spezified page_id
function get_page_url($value)
{
global $database;
$return_value = strtolower(WB_URL.PAGES_DIRECTORY.'/'.strtolower(LANGUAGE).PAGE_EXTENSION);
// if(!isset($langPageArray)) { return $return_value; }
$sql = 'SELECT `page_id`, `menu_title`,`link` FROM `'.TABLE_PREFIX.'pages` ';
$sql .= 'WHERE `page_code` = '.intval($value['page_code']).' ';
$sql .= 'AND `language` = \''.$value['language'].'\' ';
if ($query_menu = $database->query($sql)) {
if ($query_menu->numRows() > 0) {
$page = $query_menu->fetchRow(MYSQLI_ASSOC);
$return_value = page_link($page['link']);
}
}
return $return_value;
}
function get_page_list($parent, $this_page=0)
{
global $database, $entries;
$sql = 'SELECT `page_id`, `language`, `menu_title`, `page_code`, `parent` FROM `'.TABLE_PREFIX.'pages` WHERE `parent` = '.$parent.' ORDER BY `position`';
$get_query = $database->query($sql);
if ($get_query->numRows()) {
while ($value = $get_query->fetchRow(MYSQLI_ASSOC)) {
if (($value['page_id'] != $this_page)) {
$entries [$value['page_id']]['language'] = $value['language'];
$entries [$value['page_id']]['menu_title'] = $value['menu_title'];
get_page_list($value['page_id'], $this_page);
}
}
}
return $entries;
}
//
function show_vars($array)
{
echo '<pre>';
print_r($array);
echo '</pre>';
}
if (!function_exists('getBaseUrl')) {
function getBaseUrl()
{
global $mod_path;
// identify Server Document_Root
// on WIN/IIS create this entry
$script_name = str_replace('\\', '/', dirname(dirname(__FILE__)));
$sys_root = (!isset($_SERVER['DOCUMENT_ROOT']) && $_SERVER['DOCUMENT_ROOT'] == '') ? (str_replace('\\', '/', $script_name)) : str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
$_SERVER['DOCUMENT_ROOT'] = $sys_root;
$wb_rel = str_replace($sys_root, '', ($script_name));
$mod_path = (!empty($mod_path)) ? $mod_path : '/' ;
$regex = '#(?=\\'.$mod_path.').*#i';
$replace = '';
$wb_rel = preg_replace($regex, $replace, $wb_rel, -1);
$wb_rel = str_replace('//', '/', $wb_rel);
if (!defined('WB_REL')) {
define('WB_REL', $wb_rel);
}
if (!defined('ADMIN_REL')) {
define('ADMIN_REL', $wb_rel.'/admin');
}
}
getBaseUrl();
}