2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 08/25/2003 *
4 * =================== Last change: 11/29/2005 *
6 * -------------------------------------------------------------------- *
7 * File : language-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Language functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Sprachfunktionen *
12 * -------------------------------------------------------------------- *
13 * Copyright (c) 2003 - 2009 by Roland Haeder *
14 * Copyright (c) 2009 - 2016 by Mailer Developer Team *
15 * For more information visit: http://mxchange.org *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
38 // "Getter" for language strings
39 function getMessage ($messageId) {
40 // Default is not found
41 $return = '!' . $messageId . '!';
43 // Is the language string found?
44 if (isMessageIdValid($messageId)) {
45 // Language array element found in small_letters
46 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
48 // Missing language constant
49 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Missing message string %s detected.', $messageId));
56 // Getter for message string as a mask
57 function getMaskedMessage ($messageId, $data) {
59 $message = sprintf(getMessage($messageId), $data);
66 function initMessages () {
67 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getLanguage()=' . getLanguage());
68 $GLOBALS['messages'][getLanguage()] = array();
72 function addMessages ($messages) {
73 // Cache current language
74 $currentLanguage = getCurrentLanguage();
75 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentLanguage=' . $currentLanguage);
78 $GLOBALS['messages'][$currentLanguage] = merge_array($GLOBALS['messages'][$currentLanguage], $messages);
80 // Don't count them if we don't want it
81 if (isset($GLOBALS['count'])) {
86 if (isset($GLOBALS['msg_count'][$currentLanguage])) {
87 $GLOBALS['msg_count'][$currentLanguage] += count($messages);
89 $GLOBALS['msg_count'][$currentLanguage] = count($messages);
93 // Checks whether given message id is valid
94 function isMessageIdValid ($messageId) {
95 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
98 // Getter for current language
99 function getCurrentLanguage () {
100 return $GLOBALS['language'];
103 // Setter for current language
104 function setCurrentLanguage ($language) {
105 $GLOBALS['language'] = (string) $language;
108 // Checks whether current language is set
109 function isCurrentLanguageSet () {
110 return (isset($GLOBALS['language']));
113 // "Getter" for language
114 function getLanguage () {
116 if (!isCurrentLanguageSet()) {
117 // Default is 'de'. DO NOT CHANGE THIS!!!
120 // Set default return value to default language from config
121 if (isConfigEntrySet('DEFAULT_LANG')) {
122 $ret = getDefaultLanguage();
125 // Is the variable set
126 if (isGetRequestElementSet('mailer_lang')) {
127 // Accept only first 2 chars
128 $ret = substr(getRequestElement('mailer_lang'), 0, 2);
129 } elseif (isCurrentLanguageSet()) {
131 $ret = getCurrentLanguage();
132 } elseif (isSessionVariableSet('mailer_lang')) {
133 // Return stored value from cookie
134 $ret = getSession('mailer_lang');
136 // Fixes a warning before the session has the mailer_lang constant
138 $ret = getDefaultLanguage();
143 setCurrentLanguage($ret);
146 // Return cached value
147 return getCurrentLanguage();
150 // "Setter" for language
151 function setLanguage ($lang) {
152 // Accept only first 2 chars and still secure them
153 $lang = substr(secureString($lang), 0, 2);
156 setSession('mailer_lang', $lang);
159 // Checks whether a language file is there for optional extension
160 function isLanguageIncludeReadable ($ext_name = 'none') {
161 // Is there array element?
162 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
164 if ($ext_name == 'none') {
166 $languageInclude = sprintf('inc/language/%s.php', getLanguage());
168 // Extension's language file
169 $languageInclude = sprintf('inc/language/%s_%s.php', $ext_name, getLanguage());
172 // Look for file if no extension name is provided
173 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
174 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $ext_name . '=' . $languageInclude . '=' . intval(isIncludeReadable($languageInclude)));
178 return $GLOBALS['lang_inc'][$ext_name];
181 // Load the current language file or fixes it to 'de'
182 // If ext_name is 'none', load general language support, else load extension's
183 // language file. In installation phase load the install language file.
184 function loadLanguageFile ($ext_name = 'none') {
185 // Try to get language from session
186 $currLanguage = getLanguage();
188 // Set default language if it is not (yet) set
189 if (is_null($currLanguage)) {
190 // Get it from config
191 $currLanguage = getDefaultLanguage();
193 // And save it in session
194 setLanguage($currLanguage);
197 // Is there the language file NOT?
198 if (!isLanguageIncludeReadable($ext_name)) {
199 // Switch to default (DO NOT CHANGE!!!)
202 // And set it temporarily
203 setConfigEntry('DEFAULT_LANG', 'de');
206 // Is the file there?
207 if (isLanguageIncludeReadable($ext_name)) {
208 // Load language file
209 loadLanguageInclude($ext_name);
210 } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
211 // No language file is not so good...
212 logDebugMessage(__FUNCTION__, __LINE__, sprintf('NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s',
219 // Check for installation mode
220 if ((isInstaller()) || (!isAdminRegistered())) {
221 // Load language file
222 loadLanguageInclude('install');
226 // Loads the language file
227 function loadLanguageInclude ($ext_name = 'none') {
229 if ($ext_name == 'none') {
231 $languageInclude = sprintf('inc/language/%s.php', getLanguage());
233 // Extension's language file
234 $languageInclude = sprintf('inc/language/%s_%s.php', $ext_name, getLanguage());
237 // Check it before loading
238 if (isLanguageIncludeReadable($ext_name)) {
240 loadIncludeOnce($languageInclude);
243 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Language file %s not found or readable.', $languageInclude));
247 // Getter for an array of valid languages with no except by default
248 function getValidLanguages ($except = '') {
249 // @TODO These are all valid languages, again hard-coded
250 $langs = array('de' => 'de', 'en' => 'en');
252 // Should we keep one out?
253 if (!empty($except)) {
255 unset($langs[$except]);
262 // Compares two language files
263 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
265 $GLOBALS['lang_diff'][$target] = array();
266 $GLOBALS['lang_diff_count'][$target] = 0;
267 if (!isset($GLOBALS['lang_diff_count']['total'])) {
268 $GLOBALS['lang_diff_count']['total'] = 0;
271 // *Does* match by default
274 // Is one not readable?
275 if (!isIncludeReadable($source)) {
276 // Please report this bug!
277 reportBug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
278 } elseif (!isIncludeReadable($target)) {
279 // Please report this bug!
280 reportBug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
281 } elseif ($targetLanguage == getCurrentLanguage()) {
283 reportBug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
286 // Backup current messages/language
287 $backupLang = getCurrentLanguage();
288 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
289 $GLOBALS['messages'][$backupLang] = array();
291 // Both are readable so include current language file
292 $GLOBALS['count'] = FALSE;
293 loadInclude($source);
294 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
295 unset($GLOBALS['count']);
297 // Set target language
298 setCurrentLanguage($targetLanguage);
300 // Is there an array?
301 if (!isset($GLOBALS['messages'][$targetLanguage])) {
302 // Then create it to avoid notice
303 $GLOBALS['messages'][$targetLanguage] = array();
304 $GLOBALS['msg_count'][$targetLanguage] = 0;
307 // Load target language file
308 loadInclude($target);
309 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
312 setCurrentLanguage($backupLang);
313 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
314 unset($messages[$backupLang]);
317 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
321 // Check all differences
322 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
324 if (!isset($GLOBALS['msgs'][$target][$key])) {
325 // Then add is as difference
326 $GLOBALS['lang_diff'][$target][$key] = $value;
329 $GLOBALS['lang_diff_count'][$target]++;
330 $GLOBALS['lang_diff_count']['total']++;
339 // Getter for getting difference of target file
340 function getLanguageComparisonDifference ($target) {
341 return $GLOBALS['lang_diff_count'][$target];
344 // Checks whether the given message is masked
345 function isMessageMasked ($messageId, $strict = TRUE) {
346 // Is the message id valid?
347 if (($strict === TRUE) && (!isMessageIdValid($messageId))) {
348 // No, then abort here
349 reportBug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
352 // Now simply check it
353 $isMasked = isInString('%', getMessage($messageId));
354 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'messageId=' . $messageId . ',isMasked=' . intval($isMasked));