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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2013 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // "Getter" for language strings
44 function getMessage ($messageId) {
45 // Default is not found
46 $return = '!' . $messageId . '!';
48 // Is the language string found?
49 if (isMessageIdValid($messageId)) {
50 // Language array element found in small_letters
51 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
53 // Missing language constant
54 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
61 // Getter for message string as a mask
62 function getMaskedMessage ($messageId, $data) {
64 $message = sprintf(getMessage($messageId), $data);
71 function initMessages () {
72 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getLanguage()=' . getLanguage());
73 $GLOBALS['messages'][getLanguage()] = array();
77 function addMessages ($messages) {
78 // Cache current language
79 $currentLanguage = getCurrentLanguage();
80 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentLanguage=' . $currentLanguage);
83 $GLOBALS['messages'][$currentLanguage] = merge_array($GLOBALS['messages'][$currentLanguage], $messages);
85 // Don't count them if we don't want it
86 if (isset($GLOBALS['count'])) {
91 if (isset($GLOBALS['msg_count'][$currentLanguage])) {
92 $GLOBALS['msg_count'][$currentLanguage] += count($messages);
94 $GLOBALS['msg_count'][$currentLanguage] = count($messages);
98 // Checks whether given message id is valid
99 function isMessageIdValid ($messageId) {
100 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
103 // Getter for current language
104 function getCurrentLanguage () {
105 return $GLOBALS['language'];
108 // Setter for current language
109 function setCurrentLanguage ($language) {
110 $GLOBALS['language'] = (string) $language;
113 // Checks whether current language is set
114 function isCurrentLanguageSet () {
115 return (isset($GLOBALS['language']));
118 // "Getter" for language
119 function getLanguage () {
121 if (!isCurrentLanguageSet()) {
122 // Default is 'de'. DO NOT CHANGE THIS!!!
125 // Set default return value to default language from config
126 if (isConfigEntrySet('DEFAULT_LANG')) {
127 $ret = getDefaultLanguage();
130 // Is the variable set
131 if (isGetRequestElementSet('mailer_lang')) {
132 // Accept only first 2 chars
133 $ret = substr(getRequestElement('mailer_lang'), 0, 2);
134 } elseif (isCurrentLanguageSet()) {
136 $ret = getCurrentLanguage();
137 } elseif (isSessionVariableSet('mailer_lang')) {
138 // Return stored value from cookie
139 $ret = getSession('mailer_lang');
141 // Fixes a warning before the session has the mailer_lang constant
143 $ret = getDefaultLanguage();
148 setCurrentLanguage($ret);
151 // Return cached value
152 return getCurrentLanguage();
155 // "Setter" for language
156 function setLanguage ($lang) {
157 // Accept only first 2 chars and still secure them
158 $lang = substr(secureString($lang), 0, 2);
161 setSession('mailer_lang', $lang);
164 // Checks whether a language file is there for optional extension
165 function isLanguageIncludeReadable ($ext_name = 'none') {
166 // Is there array element?
167 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
169 if ($ext_name == 'none') {
171 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
173 // Extension's language file
174 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
177 // Look for file if no extension name is provided
178 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
179 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $ext_name . '=' . $languageInclude . '=' . intval(isIncludeReadable($languageInclude)));
183 return $GLOBALS['lang_inc'][$ext_name];
186 // Load the current language file or fixes it to 'de'
187 // If ext_name is 'none', load general language support, else load extension's
188 // language file. In installation phase load the install language file.
189 function loadLanguageFile ($ext_name = 'none') {
190 // Try to get language from session
191 $currLanguage = getLanguage();
193 // Set default language if it is not (yet) set
194 if (is_null($currLanguage)) {
195 // Get it from config
196 $currLanguage = getDefaultLanguage();
198 // And save it in session
199 setLanguage($currLanguage);
202 // Is there the language file NOT?
203 if (!isLanguageIncludeReadable($ext_name)) {
204 // Switch to default (DO NOT CHANGE!!!)
207 // And set it temporarily
208 setConfigEntry('DEFAULT_LANG', 'de');
211 // Is the file there?
212 if (isLanguageIncludeReadable($ext_name)) {
213 // Load language file
214 loadLanguageInclude($ext_name);
215 } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
216 // No language file is not so good...
217 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
224 // Check for installation mode
225 if ((isInstallationPhase()) || (!isAdminRegistered())) {
226 // Load language file
227 loadLanguageInclude('install');
231 // Loads the language file
232 function loadLanguageInclude ($ext_name = 'none') {
234 if ($ext_name == 'none') {
236 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
238 // Extension's language file
239 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
242 // Check it before loading
243 if (isLanguageIncludeReadable($ext_name)) {
245 loadIncludeOnce($languageInclude);
248 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
252 // Getter for an array of valid languages with no except by default
253 function getValidLanguages ($except = '') {
254 // @TODO These are all valid languages, again hard-coded
255 $langs = array('de' => 'de', 'en' => 'en');
257 // Should we keep one out?
258 if (!empty($except)) {
260 unset($langs[$except]);
267 // Compares two language files
268 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
270 $GLOBALS['lang_diff'][$target] = array();
271 $GLOBALS['lang_diff_count'][$target] = 0;
272 if (!isset($GLOBALS['lang_diff_count']['total'])) {
273 $GLOBALS['lang_diff_count']['total'] = 0;
276 // *Does* match by default
279 // Is one not readable?
280 if (!isIncludeReadable($source)) {
281 // Please report this bug!
282 reportBug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
283 } elseif (!isIncludeReadable($target)) {
284 // Please report this bug!
285 reportBug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
286 } elseif ($targetLanguage == getCurrentLanguage()) {
288 reportBug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
291 // Backup current messages/language
292 $backupLang = getCurrentLanguage();
293 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
294 $GLOBALS['messages'][$backupLang] = array();
296 // Both are readable so include current language file
297 $GLOBALS['count'] = FALSE;
298 loadInclude($source);
299 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
300 unset($GLOBALS['count']);
302 // Set target language
303 setCurrentLanguage($targetLanguage);
305 // Is there an array?
306 if (!isset($GLOBALS['messages'][$targetLanguage])) {
307 // Then create it to avoid notice
308 $GLOBALS['messages'][$targetLanguage] = array();
309 $GLOBALS['msg_count'][$targetLanguage] = 0;
312 // Load target language file
313 loadInclude($target);
314 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
317 setCurrentLanguage($backupLang);
318 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
319 unset($messages[$backupLang]);
322 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
326 // Check all differences
327 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
329 if (!isset($GLOBALS['msgs'][$target][$key])) {
330 // Then add is as difference
331 $GLOBALS['lang_diff'][$target][$key] = $value;
334 $GLOBALS['lang_diff_count'][$target]++;
335 $GLOBALS['lang_diff_count']['total']++;
344 // Getter for getting difference of target file
345 function getLanguageComparisonDifference ($target) {
346 return $GLOBALS['lang_diff_count'][$target];
349 // Checks whether the given message is masked
350 function isMessageMasked ($messageId, $strict = TRUE) {
351 // Is the message id valid?
352 if (($strict === TRUE) && (!isMessageIdValid($messageId))) {
353 // No, then abort here
354 reportBug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
357 // Now simply check it
358 $isMasked = isInString('%', getMessage($messageId));
359 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'messageId=' . $messageId . ',isMasked=' . intval($isMasked));