[2] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare API - Translation class for SQL * |
---|
| 4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
| 5 | * and Dan Kuykendall <seek3r@phpgroupware.org> * |
---|
| 6 | * Handles multi-language support use SQL tables * |
---|
| 7 | * Copyright (C) 2000, 2001 Joseph Engo * |
---|
| 8 | * ------------------------------------------------------------------------ * |
---|
| 9 | * This library is part of the eGroupWare API * |
---|
| 10 | * http://www.egroupware.org/api * |
---|
| 11 | * ------------------------------------------------------------------------ * |
---|
| 12 | * This library is free software; you can redistribute it and/or modify it * |
---|
| 13 | * under the terms of the GNU Lesser General Public License as published by * |
---|
| 14 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
| 15 | * or any later version. * |
---|
| 16 | * This library is distributed in the hope that it will be useful, but * |
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
| 19 | * See the GNU Lesser General Public License for more details. * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 21 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 22 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
| 23 | \**************************************************************************/ |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | // define the maximal length of a message_id, all message_ids have to be unique |
---|
| 27 | // in this length, our column is varchar 255, but addslashes might add some length |
---|
| 28 | if (!defined('MAX_MESSAGE_ID_LENGTH')) |
---|
| 29 | { |
---|
| 30 | define('MAX_MESSAGE_ID_LENGTH',230); |
---|
| 31 | } |
---|
| 32 | // some constanst for pre php4.3 |
---|
| 33 | if (!defined('PHP_SHLIB_SUFFIX')) |
---|
| 34 | { |
---|
| 35 | define('PHP_SHLIB_SUFFIX',strtoupper(substr(PHP_OS, 0,3)) == 'WIN' ? 'dll' : 'so'); |
---|
| 36 | } |
---|
| 37 | if (!defined('PHP_SHLIB_PREFIX')) |
---|
| 38 | { |
---|
| 39 | define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : ''); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | class translation |
---|
| 43 | { |
---|
| 44 | var $userlang = 'en'; |
---|
| 45 | var $loaded_apps = array(); |
---|
| 46 | var $line_rejected = array(); |
---|
| 47 | |
---|
| 48 | function translation($warnings = False) |
---|
| 49 | { |
---|
| 50 | for ($i = 1; $i <= 9; $i++) |
---|
| 51 | { |
---|
| 52 | $this->placeholders[] = '%'.$i; |
---|
| 53 | } |
---|
| 54 | $this->db = is_object($GLOBALS['phpgw']->db) ? $GLOBALS['phpgw']->db : $GLOBALS['phpgw_setup']->db; |
---|
| 55 | if (!isset($GLOBALS['phpgw_setup'])) |
---|
| 56 | { |
---|
| 57 | $this->system_charset = @$GLOBALS['phpgw_info']['server']['system_charset']; |
---|
| 58 | } |
---|
| 59 | else |
---|
| 60 | { |
---|
| 61 | $this->db->query("SELECT config_value FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='system_charset'",__LINE__,__FILE__); |
---|
| 62 | if ($this->db->next_record()) |
---|
| 63 | { |
---|
| 64 | $this->system_charset = $this->db->f(0); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | // load multi-byte-string-extension if needed, and set its internal encodeing to your system_charset |
---|
| 68 | if ($this->system_charset && substr($this->system_charset,0,9) != 'iso-8859-1') |
---|
| 69 | { |
---|
| 70 | if ($this->mbstring = extension_loaded('mbstring') || @dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX)) |
---|
| 71 | { |
---|
| 72 | ini_set('mbstring.internal_encoding',$this->system_charset); |
---|
| 73 | if (ini_get('mbstring.func_overload') < 7) |
---|
| 74 | { |
---|
| 75 | if ($warnings) |
---|
| 76 | { |
---|
| 77 | echo "<p>Warning: Please set <b>mbstring.func_overload = 7</b> in your php.ini for useing <b>$this->system_charset</b> as your charset !!!</p>\n"; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | else |
---|
| 82 | { |
---|
| 83 | if ($warnings) |
---|
| 84 | { |
---|
| 85 | echo "<p>Warning: Please get and/or enable the <b>mbstring extension</b> in your php.ini for useing <b>$this->system_charset</b> as your charset, we are defaulting to <b>iconv</b> for now !!!</p>\n"; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /* |
---|
| 92 | @function charset |
---|
| 93 | @abstract returns the charset to use (!$lang) or the charset of the lang-files or $lang |
---|
| 94 | */ |
---|
| 95 | function charset($lang=False) |
---|
| 96 | { |
---|
| 97 | if ($lang) |
---|
| 98 | { |
---|
| 99 | if (!isset($this->charsets[$lang])) |
---|
| 100 | { |
---|
| 101 | $this->db->query("SELECT content FROM phpgw_lang WHERE lang='$lang' AND message_id='charset' AND app_name='common'",__LINE__,__FILE__); |
---|
| 102 | $this->charsets[$lang] = $this->db->next_record() ? strtolower($this->db->f(0)) : 'iso-8859-1'; |
---|
| 103 | } |
---|
| 104 | return $this->charsets[$lang]; |
---|
| 105 | } |
---|
| 106 | if ($this->system_charset) // do we have a system-charset ==> return it |
---|
| 107 | { |
---|
| 108 | return $this->system_charset; |
---|
| 109 | } |
---|
| 110 | // if no translations are loaded (system-startup) use a default, else lang('charset') |
---|
| 111 | return !is_array(@$GLOBALS['lang']) ? 'iso-8859-1' : strtolower($this->translate('charset')); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | function init() |
---|
| 115 | { |
---|
| 116 | // post-nuke and php-nuke are using $GLOBALS['lang'] too |
---|
| 117 | // but not as array! |
---|
| 118 | // this produces very strange results |
---|
| 119 | if (!is_array(@$GLOBALS['lang'])) |
---|
| 120 | { |
---|
| 121 | $GLOBALS['lang'] = array(); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if ($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) |
---|
| 125 | { |
---|
| 126 | $this->userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
| 127 | } |
---|
[266] | 128 | elseif($GLOBALS['_SERVER']['HTTP_ACCEPT_LANGUAGE']) |
---|
| 129 | $this->userlang = $GLOBALS['_SERVER']['HTTP_ACCEPT_LANGUAGE']; |
---|
[2] | 130 | $this->add_app('common'); |
---|
| 131 | if (!count($GLOBALS['lang'])) |
---|
| 132 | { |
---|
| 133 | $this->userlang = 'en'; |
---|
| 134 | $this->add_app('common'); |
---|
| 135 | } |
---|
| 136 | $this->add_app($GLOBALS['phpgw_info']['flags']['currentapp']); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /*! |
---|
| 140 | @function translate |
---|
| 141 | @abstract translates a phrase and evtl. substitute some variables |
---|
| 142 | @returns the translation |
---|
| 143 | */ |
---|
| 144 | function translate($key, $vars=false, $not_found='*' ) |
---|
| 145 | { |
---|
| 146 | if (!is_array(@$GLOBALS['lang']) || !count($GLOBALS['lang'])) |
---|
| 147 | { |
---|
| 148 | $this->init(); |
---|
| 149 | } |
---|
| 150 | $ret = $key.$not_found; // save key if we dont find a translation |
---|
| 151 | |
---|
| 152 | if (isset($GLOBALS['lang'][$key])) |
---|
| 153 | { |
---|
| 154 | $ret = $GLOBALS['lang'][$key]; |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | $new_key = strtolower(trim(substr($key,0,MAX_MESSAGE_ID_LENGTH))); |
---|
| 159 | |
---|
| 160 | if (isset($GLOBALS['lang'][$new_key])) |
---|
| 161 | { |
---|
| 162 | // we save the original key for performance |
---|
| 163 | $ret = $GLOBALS['lang'][$key] = $GLOBALS['lang'][$new_key]; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | if (is_array($vars) && count($vars)) |
---|
| 167 | { |
---|
| 168 | if (count($vars) > 1) |
---|
| 169 | { |
---|
| 170 | $ret = str_replace($this->placeholders,$vars,$ret); |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | $ret = str_replace('%1',$vars[0],$ret); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | return $ret; |
---|
| 178 | } |
---|
| 179 | |
---|
[199] | 180 | |
---|
| 181 | function translate_async($key,$vars=false, $not_found='*' ) |
---|
| 182 | { |
---|
| 183 | $lindex = $_SESSION['phpgw_info']['calendar']['langAlarm']; |
---|
| 184 | if (!is_array(@$lindex) || !count($lindex)) |
---|
| 185 | { |
---|
| 186 | $this->init(); |
---|
| 187 | } |
---|
| 188 | $ret = $key.$not_found; // save key if we dont find a translation |
---|
| 189 | |
---|
| 190 | if (isset($lindex[$key])) |
---|
| 191 | { |
---|
| 192 | $ret = $lindex[$key]; |
---|
| 193 | } |
---|
| 194 | else |
---|
| 195 | { |
---|
| 196 | $new_key = strtolower(trim(substr($key,0,MAX_MESSAGE_ID_LENGTH))); |
---|
| 197 | |
---|
| 198 | if (isset($lindex[$new_key])) |
---|
| 199 | { |
---|
| 200 | // we save the original key for performance |
---|
| 201 | $ret = $lindex[$key] = $lindex[$new_key]; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | if (is_array($vars) && count($vars)) |
---|
| 205 | { |
---|
| 206 | if (count($vars) > 1) |
---|
| 207 | { |
---|
| 208 | $ret = str_replace($this->placeholders,$vars,$ret); |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | $ret = str_replace('%1',$vars[0],$ret); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | return $ret; |
---|
| 216 | } |
---|
| 217 | |
---|
[2] | 218 | /*! |
---|
| 219 | @function add_app |
---|
| 220 | @abstract adds translations for an application from the database to the lang-array |
---|
| 221 | @syntax add_app($app,$lang=False) |
---|
| 222 | @param $app name of the application to add (or 'common' for the general translations) |
---|
| 223 | @param $lang 2-char code of the language to use or False if the users language should be used |
---|
| 224 | */ |
---|
| 225 | function add_app($app,$lang=False) |
---|
| 226 | { |
---|
| 227 | $lang = $lang ? $lang : $this->userlang; |
---|
| 228 | |
---|
| 229 | if (!isset($this->loaded_apps[$app]) || $this->loaded_apps[$app] != $lang) |
---|
| 230 | { |
---|
| 231 | $sql = "select message_id,content from phpgw_lang where lang='".$lang."' and app_name='".$app."'"; |
---|
| 232 | $this->db->query($sql,__LINE__,__FILE__); |
---|
| 233 | while ($this->db->next_record()) |
---|
| 234 | { |
---|
| 235 | $GLOBALS['lang'][strtolower ($this->db->f('message_id'))] = $this->db->f('content'); |
---|
| 236 | } |
---|
| 237 | $this->loaded_apps[$app] = $lang; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /*! |
---|
| 242 | @function get_installed_langs |
---|
| 243 | @abstract returns a list of installed langs |
---|
| 244 | @returns array with 2-character lang-code as key and descriptiv lang-name as data |
---|
| 245 | */ |
---|
| 246 | function get_installed_langs() |
---|
| 247 | { |
---|
| 248 | if (!is_array($this->langs)) |
---|
| 249 | { |
---|
| 250 | $this->db->query("SELECT DISTINCT l.lang,ln.lang_name FROM phpgw_lang l,phpgw_languages ln WHERE l.lang = ln.lang_id",__LINE__,__FILE__); |
---|
| 251 | if (!$this->db->num_rows()) |
---|
| 252 | { |
---|
| 253 | return False; |
---|
| 254 | } |
---|
| 255 | while ($this->db->next_record()) |
---|
| 256 | { |
---|
| 257 | $this->langs[$this->db->f('lang')] = $this->db->f('lang_name'); |
---|
| 258 | } |
---|
| 259 | foreach($this->langs as $lang => $name) |
---|
| 260 | { |
---|
| 261 | $this->langs[$lang] = $this->translate($name,False,''); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | return $this->langs; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /*! |
---|
| 268 | @function get_installed_charsets |
---|
| 269 | @abstract returns a list of installed charsets |
---|
| 270 | @returns array with charset as key and comma-separated list of langs useing the charset as data |
---|
| 271 | */ |
---|
| 272 | function get_installed_charsets() |
---|
| 273 | { |
---|
| 274 | if (!is_array($this->charsets)) |
---|
| 275 | { |
---|
| 276 | $distinct = 'DISTINCT'; |
---|
| 277 | switch($this->db->Type) |
---|
| 278 | { |
---|
| 279 | case 'mssql': |
---|
| 280 | $distinct = ''; // cant use distinct on text columns (l.content is text) |
---|
| 281 | } |
---|
| 282 | $this->db->query("SELECT $distinct l.lang,lx.lang_name,l.content AS charset FROM phpgw_lang l,phpgw_languages lx WHERE l.lang = lx.lang_id AND l.message_id='charset'",__LINE__,__FILE__); |
---|
| 283 | if (!$this->db->num_rows()) |
---|
| 284 | { |
---|
| 285 | return False; |
---|
| 286 | } |
---|
| 287 | while ($this->db->next_record()) |
---|
| 288 | { |
---|
| 289 | $data = &$this->charsets[$charset = strtolower($this->db->f('charset'))]; |
---|
| 290 | $lang = $this->db->f('lang_name').' ('.$this->db->f('lang').')'; |
---|
| 291 | if ($distinct || strstr($data,$lang) === false) |
---|
| 292 | { |
---|
| 293 | $data .= ($data ? ', ' : $charset.': ').$lang; |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | return $this->charsets; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | /*! |
---|
| 301 | @function convert |
---|
| 302 | @abstract converts a string $data from charset $from to charset $to |
---|
| 303 | @syntax convert($data,$from=False,$to=False) |
---|
| 304 | @param $data string or array of strings to convert |
---|
| 305 | @param $from charset $data is in or False if it should be detected |
---|
| 306 | @param $to charset to convert to or False for the system-charset |
---|
| 307 | @returns the converted string |
---|
| 308 | */ |
---|
| 309 | function convert($data,$from=False,$to=False) |
---|
| 310 | { |
---|
| 311 | if (is_array($data)) |
---|
| 312 | { |
---|
| 313 | foreach($data as $key => $str) |
---|
| 314 | { |
---|
| 315 | $ret[$key] = $this->convert($str,$from,$to); |
---|
| 316 | } |
---|
| 317 | return $ret; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | if ($from) |
---|
| 321 | { |
---|
| 322 | $from = strtolower($from); |
---|
| 323 | } |
---|
| 324 | if ($to) |
---|
| 325 | { |
---|
| 326 | $to = strtolower($to); |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | if (!$from) |
---|
| 330 | { |
---|
| 331 | $from = $this->mbstring ? strtolower(mb_detect_encoding($data)) : 'iso-8859-1'; |
---|
| 332 | if($from == 'ascii') |
---|
| 333 | { |
---|
| 334 | $from = 'iso-8859-1'; |
---|
| 335 | } |
---|
| 336 | //echo "<p>autodetected charset of '$data' = '$from'</p>\n"; |
---|
| 337 | } |
---|
| 338 | /* |
---|
| 339 | php does not seem to support gb2312 |
---|
| 340 | but seems to be able to decode it as EUC-CN |
---|
| 341 | */ |
---|
| 342 | switch($from) |
---|
| 343 | { |
---|
| 344 | case 'gb2312': |
---|
| 345 | case 'gb18030': |
---|
| 346 | $from = 'EUC-CN'; |
---|
| 347 | break; |
---|
| 348 | case 'us-ascii': |
---|
| 349 | case 'macroman': |
---|
| 350 | $from = 'iso-8859-1'; |
---|
| 351 | break; |
---|
| 352 | } |
---|
| 353 | if (!$to) |
---|
| 354 | { |
---|
| 355 | $to = $this->charset(); |
---|
| 356 | } |
---|
| 357 | if ($from == $to || !$from || !$to || !$data) |
---|
| 358 | { |
---|
| 359 | return $data; |
---|
| 360 | } |
---|
| 361 | if ($from == 'iso-8859-1' && $to == 'utf-8') |
---|
| 362 | { |
---|
| 363 | return utf8_encode($data); |
---|
| 364 | } |
---|
| 365 | if ($to == 'iso-8859-1' && $from == 'utf-8') |
---|
| 366 | { |
---|
| 367 | return utf8_decode($data); |
---|
| 368 | } |
---|
| 369 | if ($this->mbstring) |
---|
| 370 | { |
---|
| 371 | return @mb_convert_encoding($data,$to,$from); |
---|
| 372 | } |
---|
| 373 | if(function_exists('iconv')) |
---|
| 374 | { |
---|
| 375 | if($from=='EUC-CN') $from='gb18030'; |
---|
| 376 | // the above is to workaround patch #962307 |
---|
| 377 | // if using EUC-CN, for iconv it strickly follow GB2312 and fail |
---|
| 378 | // in an email on the first Traditional/Japanese/Korean character, |
---|
| 379 | // but in reality when people send mails in GB2312, UMA mostly use |
---|
| 380 | // extended GB13000/GB18030 which allow T/Jap/Korean characters. |
---|
| 381 | if (($data = iconv($from,$to,$data))) |
---|
| 382 | { |
---|
| 383 | return $data; |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | #die("<p>Can't convert from charset '$from' to '$to' without the <b>mbstring extension</b> !!!</p>"); |
---|
| 387 | |
---|
| 388 | // this is not good, not convert did succed |
---|
| 389 | return $data; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | /*! |
---|
| 393 | @function install_langs |
---|
| 394 | @abstract installs translations for the selected langs into the database |
---|
| 395 | @syntax install_langs($langs,$upgrademethod='dumpold') |
---|
| 396 | @param $langs array of langs to install (as data NOT keys (!)) |
---|
| 397 | @param $upgrademethod 'dumpold' (recommended & fastest), 'addonlynew' languages, 'addmissing' phrases |
---|
| 398 | */ |
---|
| 399 | function install_langs($langs,$upgrademethod='dumpold',$only_app=False) |
---|
| 400 | { |
---|
| 401 | @set_time_limit(0); // we might need some time |
---|
| 402 | |
---|
| 403 | if (!isset($GLOBALS['phpgw_info']['server']) && $upgrademethod != 'dumpold') |
---|
| 404 | { |
---|
| 405 | $this->db->query("SELECT * FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='lang_ctimes'",__LINE__,__FILE__); |
---|
| 406 | if ($this->db->next_record()) |
---|
| 407 | { |
---|
| 408 | $GLOBALS['phpgw_info']['server']['lang_ctimes'] = unserialize(stripslashes($this->db->f('config_value'))); |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | if (!is_array($langs) || !count($langs)) |
---|
| 413 | { |
---|
| 414 | return; |
---|
| 415 | } |
---|
| 416 | $this->db->transaction_begin(); |
---|
| 417 | |
---|
| 418 | if ($upgrademethod == 'dumpold') |
---|
| 419 | { |
---|
| 420 | // dont delete the custom main- & loginscreen messages every time |
---|
| 421 | $this->db->query("DELETE FROM phpgw_lang WHERE app_name != 'mainscreen' AND app_name != 'loginscreen'",__LINE__,__FILE__); |
---|
| 422 | //echo '<br>Test: dumpold'; |
---|
| 423 | $GLOBALS['phpgw_info']['server']['lang_ctimes'] = array(); |
---|
| 424 | } |
---|
| 425 | foreach($langs as $lang) |
---|
| 426 | { |
---|
| 427 | //echo '<br>Working on: ' . $lang; |
---|
| 428 | $addlang = False; |
---|
| 429 | if ($upgrademethod == 'addonlynew') |
---|
| 430 | { |
---|
| 431 | //echo "<br>Test: addonlynew - select count(*) from phpgw_lang where lang='".$lang."'"; |
---|
| 432 | $this->db->query("SELECT COUNT(*) FROM phpgw_lang WHERE lang='".$lang."'",__LINE__,__FILE__); |
---|
| 433 | $this->db->next_record(); |
---|
| 434 | |
---|
| 435 | if ($this->db->f(0) == 0) |
---|
| 436 | { |
---|
| 437 | //echo '<br>Test: addonlynew - True'; |
---|
| 438 | $addlang = True; |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | if ($addlang && $upgrademethod == 'addonlynew' || $upgrademethod != 'addonlynew') |
---|
| 442 | { |
---|
| 443 | //echo '<br>Test: loop above file()'; |
---|
| 444 | if (!is_object($GLOBALS['phpgw_setup'])) |
---|
| 445 | { |
---|
| 446 | $GLOBALS['phpgw_setup'] = CreateObject('phpgwapi.setup'); |
---|
| 447 | $GLOBALS['phpgw_setup']->db = $this->db; |
---|
| 448 | } |
---|
| 449 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_versions(); |
---|
| 450 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_db_versions($setup_info); |
---|
| 451 | $raw = array(); |
---|
| 452 | $apps = $only_app ? array($only_app) : array_keys($setup_info); |
---|
| 453 | // Visit each app/setup dir, look for a phpgw_lang file |
---|
| 454 | foreach($apps as $app) |
---|
| 455 | { |
---|
| 456 | $appfile = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_' . strtolower($lang) . '.lang'; |
---|
| 457 | //echo '<br>Checking in: ' . $app['name']; |
---|
| 458 | if($GLOBALS['phpgw_setup']->app_registered($app) && file_exists($appfile)) |
---|
| 459 | { |
---|
| 460 | //echo '<br>Including: ' . $appfile; |
---|
| 461 | $lines = file($appfile); |
---|
| 462 | foreach($lines as $line) |
---|
| 463 | { |
---|
| 464 | // explode with "\t" and removing "\n" with str_replace, needed to work with mbstring.overload=7 |
---|
| 465 | list($message_id,$app_name,,$content) = $_f_buffer = explode("\t",$line); |
---|
| 466 | $content=str_replace(array("\n","\r"),'',$content); |
---|
| 467 | |
---|
| 468 | if( count($_f_buffer) != 4 ) |
---|
| 469 | { |
---|
| 470 | $line_display = str_replace(array("\t","\n"), |
---|
| 471 | array("<font color='red'><b>\\t</b></font>","<font color='red'><b>\\n</b></font>"), $line); |
---|
| 472 | $this->line_rejected[] = array( |
---|
| 473 | 'appfile' => $appfile, |
---|
| 474 | 'line' => $line_display, |
---|
| 475 | ); |
---|
| 476 | } |
---|
| 477 | $message_id = substr(strtolower(chop($message_id)),0,MAX_MESSAGE_ID_LENGTH); |
---|
| 478 | $app_name = chop($app_name); |
---|
| 479 | $raw[$app_name][$message_id] = $content; |
---|
| 480 | } |
---|
| 481 | $GLOBALS['phpgw_info']['server']['lang_ctimes'][$lang][$app] = filectime($appfile); |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | $charset = strtolower(@$raw['common']['charset'] ? $raw['common']['charset'] : $this->charset($lang)); |
---|
| 485 | //echo "<p>lang='$lang', charset='$charset', system_charset='$this->system_charset')</p>\n"; |
---|
| 486 | //echo "<p>raw($lang)=<pre>".print_r($raw,True)."</pre>\n"; |
---|
| 487 | foreach($raw as $app_name => $ids) |
---|
| 488 | { |
---|
| 489 | $app_name = $this->db->db_addslashes($app_name); |
---|
| 490 | |
---|
| 491 | foreach($ids as $message_id => $content) |
---|
| 492 | { |
---|
| 493 | if ($this->system_charset) |
---|
| 494 | { |
---|
| 495 | $content = $this->convert($content,$charset,$this->system_charset); |
---|
| 496 | } |
---|
| 497 | $message_id = $this->db->db_addslashes($message_id); |
---|
| 498 | $content = $this->db->db_addslashes($content); |
---|
| 499 | |
---|
| 500 | $addit = False; |
---|
| 501 | //echo '<br>APPNAME:' . $app_name . ' PHRASE:' . $message_id; |
---|
| 502 | if ($upgrademethod == 'addmissing') |
---|
| 503 | { |
---|
| 504 | //echo '<br>Test: addmissing'; |
---|
| 505 | $this->db->query("SELECT content,CASE WHEN app_name IN ('common') then 1 else 0 END AS in_api FROM phpgw_lang WHERE message_id='$message_id' AND lang='$lang' AND (app_name='$app_name' OR app_name='common') ORDER BY in_api DESC",__LINE__,__FILE__); |
---|
| 506 | |
---|
| 507 | if (!($row = $this->db->row(True))) |
---|
| 508 | { |
---|
| 509 | $addit = True; |
---|
| 510 | } |
---|
| 511 | else |
---|
| 512 | { |
---|
| 513 | if ($row['in_api']) // same phrase is in the api |
---|
| 514 | { |
---|
| 515 | $addit = $row['content'] != $content; // only add if not identical |
---|
| 516 | } |
---|
| 517 | $row2 = $this->db->row(True); |
---|
| 518 | if (!$row['in_api'] || $app_name=='common' || $row2) // phrase is alread in the db |
---|
| 519 | { |
---|
| 520 | $addit = $content != ($row2 ? $row2['content'] : $row['content']); |
---|
| 521 | if ($addit) // if we want to add/update it ==> delete it |
---|
| 522 | { |
---|
| 523 | $this->db->query($q="DELETE FROM phpgw_lang WHERE message_id='$message_id' AND lang='$lang' AND app_name='$app_name'",__LINE__,__FILE__); |
---|
| 524 | } |
---|
| 525 | } |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | if ($addit || $upgrademethod == 'addonlynew' || $upgrademethod == 'dumpold') |
---|
| 530 | { |
---|
| 531 | if($message_id && $content) |
---|
| 532 | { |
---|
| 533 | //echo "<br>adding - insert into phpgw_lang values ('$message_id','$app_name','$lang','$content')"; |
---|
| 534 | $result = $this->db->query("INSERT INTO phpgw_lang (message_id,app_name,lang,content) VALUES('$message_id','$app_name','$lang','$content')",__LINE__,__FILE__); |
---|
| 535 | if ((int)$result <= 0) |
---|
| 536 | { |
---|
| 537 | echo "<br>Error inserting record: phpgw_lang values ('$message_id','$app_name','$lang','$content')"; |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | } |
---|
| 544 | } |
---|
| 545 | $this->db->transaction_commit(); |
---|
| 546 | |
---|
| 547 | // update the ctimes of the installed langsfiles for the autoloading of the lang-files |
---|
| 548 | // |
---|
| 549 | $config = CreateObject('phpgwapi.config.save_value'); |
---|
| 550 | $config->save_value('lang_ctimes',$GLOBALS['phpgw_info']['server']['lang_ctimes'],'phpgwapi'); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | /*! |
---|
| 554 | @function autolaod_changed_langfiles |
---|
| 555 | @abstract re-loads all (!) langfiles if one langfile for the an app and the language of the user has changed |
---|
| 556 | */ |
---|
| 557 | function autoload_changed_langfiles() |
---|
| 558 | { |
---|
| 559 | //echo "<h1>check_langs()</h1>\n"; |
---|
| 560 | if ($GLOBALS['phpgw_info']['server']['lang_ctimes'] && !is_array($GLOBALS['phpgw_info']['server']['lang_ctimes'])) |
---|
| 561 | { |
---|
| 562 | $GLOBALS['phpgw_info']['server']['lang_ctimes'] = unserialize($GLOBALS['phpgw_info']['server']['lang_ctimes']); |
---|
| 563 | } |
---|
| 564 | //_debug_array($GLOBALS['phpgw_info']['server']['lang_ctimes']); |
---|
| 565 | |
---|
| 566 | $lang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
| 567 | $apps = $GLOBALS['phpgw_info']['user']['apps']; |
---|
| 568 | $apps['phpgwapi'] = True; // check the api too |
---|
| 569 | foreach($apps as $app => $data) |
---|
| 570 | { |
---|
| 571 | $fname = PHPGW_SERVER_ROOT . "/$app/setup/phpgw_$lang.lang"; |
---|
| 572 | |
---|
| 573 | if (file_exists($fname)) |
---|
| 574 | { |
---|
| 575 | $ctime = filectime($fname); |
---|
| 576 | /* This is done to avoid string offset error at least in php5 */ |
---|
| 577 | $tmp = $GLOBALS['phpgw_info']['server']['lang_ctimes'][$lang]; |
---|
| 578 | $ltime = (int)$tmp[$app]; |
---|
| 579 | unset($tmp); |
---|
| 580 | //echo "checking lang='$lang', app='$app', ctime='$ctime', ltime='$ltime'<br>\n"; |
---|
| 581 | |
---|
| 582 | if ($ctime != $ltime) |
---|
| 583 | { |
---|
| 584 | // update all langs |
---|
| 585 | $installed = $this->get_installed_langs(); |
---|
| 586 | //echo "<p>install_langs(".print_r($installed,True).")</p>\n"; |
---|
| 587 | $this->install_langs($installed ? array_keys($installed) : array()); |
---|
| 588 | break; |
---|
| 589 | } |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | /* Following functions are called for app (un)install */ |
---|
| 595 | |
---|
| 596 | /*! |
---|
| 597 | @function get_langs |
---|
| 598 | @abstract return array of installed languages, e.g. array('de','en') |
---|
| 599 | */ |
---|
| 600 | function get_langs($DEBUG=False) |
---|
| 601 | { |
---|
| 602 | if($DEBUG) |
---|
| 603 | { |
---|
| 604 | echo '<br>get_langs(): checking db...' . "\n"; |
---|
| 605 | } |
---|
| 606 | if (!$this->langs) |
---|
| 607 | { |
---|
| 608 | $this->get_installed_langs(); |
---|
| 609 | } |
---|
| 610 | return $this->langs ? array_keys($this->langs) : array(); |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | /*! |
---|
| 614 | @function drop_langs |
---|
| 615 | @abstract delete all lang entries for an application, return True if langs were found |
---|
| 616 | @param $appname app_name whose translations you want to delete |
---|
| 617 | */ |
---|
| 618 | function drop_langs($appname,$DEBUG=False) |
---|
| 619 | { |
---|
| 620 | if($DEBUG) |
---|
| 621 | { |
---|
| 622 | echo '<br>drop_langs(): Working on: ' . $appname; |
---|
| 623 | } |
---|
| 624 | $this->db->query("SELECT COUNT(message_id) FROM phpgw_lang WHERE app_name='$appname'",__LINE__,__FILE__); |
---|
| 625 | $this->db->next_record(); |
---|
| 626 | if($this->db->f(0)) |
---|
| 627 | { |
---|
| 628 | $this->db->query("DELETE FROM phpgw_lang WHERE app_name='$appname'",__LINE__,__FILE__); |
---|
| 629 | return True; |
---|
| 630 | } |
---|
| 631 | return False; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | /*! |
---|
| 635 | @function add_langs |
---|
| 636 | @abstract process an application's lang files, calling get_langs() to see what langs the admin installed already |
---|
| 637 | @param $appname app_name of application to process |
---|
| 638 | */ |
---|
| 639 | function add_langs($appname,$DEBUG=False,$force_langs=False) |
---|
| 640 | { |
---|
| 641 | $langs = $this->get_langs($DEBUG); |
---|
| 642 | if(is_array($force_langs)) |
---|
| 643 | { |
---|
| 644 | foreach($force_langs as $lang) |
---|
| 645 | { |
---|
| 646 | if (!in_array($lang,$langs)) |
---|
| 647 | { |
---|
| 648 | $langs[] = $lang; |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | if($DEBUG) |
---|
| 654 | { |
---|
| 655 | echo '<br>add_langs(): chose these langs: '; |
---|
| 656 | _debug_array($langs); |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | $this->install_langs($langs,'addmissing',$appname); |
---|
| 660 | } |
---|
| 661 | } |
---|