074b25e447f03c6bc9f92d7a8eff5c40cfe0611c
[mailer.git] / inc / language-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 08/25/2003 *
4  * ===================                          Last change: 11/29/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : language-functions.php                           *
8  * -------------------------------------------------------------------- *
9  * Short description : Language functions                               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sprachfunktionen                                 *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // "Getter" for language strings
44 // @TODO Rewrite all language constants to this function.
45 function getMessage ($messageId) {
46         // Default is not found!
47         $return = '!' . $messageId . '!';
48
49         // Is the language string found?
50         if (isMessageIdValid($messageId)) {
51                 // Language array element found in small_letters
52                 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
53         } else {
54                 // Missing language constant
55                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
56         }
57
58         // Return the string
59         return $return;
60 }
61
62 // Getter for message string as a mask
63 function getMaskedMessage ($messageId, $data) {
64         // Construct message
65         $message = sprintf(getMessage($messageId), $data);
66
67         // Return it
68         return $message;
69 }
70
71 // Init messages
72 function initMessages () {
73         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getLanguage()=' . getLanguage());
74         $GLOBALS['messages'][getLanguage()] = array();
75 }
76
77 // Add messages
78 function addMessages ($messages) {
79         // Cache current language
80         $currentLanguage = getCurrentLanguage();
81         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentLanguage=' . $currentLanguage);
82
83         // Merge both
84         $GLOBALS['messages'][$currentLanguage] = merge_array($GLOBALS['messages'][$currentLanguage], $messages);
85
86         // Don't count them if we don't want it
87         if (isset($GLOBALS['count'])) {
88                 return;
89         } // END - if
90
91         // And count them
92         if (isset($GLOBALS['msg_count'][$currentLanguage])) {
93                 $GLOBALS['msg_count'][$currentLanguage] += count($messages);
94         } else {
95                 $GLOBALS['msg_count'][$currentLanguage] = count($messages);
96         }
97 }
98
99 // Checks wether given message id is valid
100 function isMessageIdValid ($messageId) {
101         return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
102 }
103
104 // Getter for current language
105 function getCurrentLanguage () {
106         return $GLOBALS['language'];
107 }
108
109 // Setter for current language
110 function setCurrentLanguage ($language) {
111         $GLOBALS['language'] = (string) $language;
112 }
113
114 // "Getter" for language
115 function getLanguage () {
116         // Default is 'de'. DO NOT CHANGE THIS!!!
117         $ret = 'de';
118
119         // Set default return value to default language from config
120         if (isConfigEntrySet('DEFAULT_LANG')) $ret = getDefaultLanguage();
121
122         // Is the variable set
123         if (isGetRequestParameterSet('mx_lang')) {
124                 // Accept only first 2 chars
125                 $ret = substr(getRequestParameter('mx_lang'), 0, 2);
126         } elseif (isset($GLOBALS['language'])) {
127                 // Use cached
128                 $ret = getCurrentLanguage();
129         } elseif (isSessionVariableSet('mx_lang')) {
130                 // Return stored value from cookie
131                 $ret = getSession('mx_lang');
132
133                 // Fixes a warning before the session has the mx_lang constant
134                 if (empty($ret)) $ret = getDefaultLanguage();
135         }
136
137         // Cache entry
138         setCurrentLanguage($ret);
139
140         // Return value
141         return $ret;
142 }
143
144 // "Setter" for language
145 function setLanguage ($lang) {
146         // Accept only first 2 chars and still secure them
147         $lang = substr(secureString($lang), 0, 2);
148
149         // Set cookie
150         setSession('mx_lang', $lang);
151 }
152
153 // Checks wether a language file is there for optional extension
154 function isLanguageIncludeReadable ($ext_name = 'none') {
155         // Do we have array element?
156         if (!isset($GLOBALS['lang_inc'][$ext_name])) {
157                 // Generate filename
158                 if ($ext_name == 'none') {
159                         // Generic
160                         $languageInclude = sprintf("inc/language/%s.php", getLanguage());
161                 } else {
162                         // Extension's language file
163                         $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
164                 }
165
166                 // Look for file if no extension name is provided
167                 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
168                 //* DEBUG: */ debugOutput(__FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)));
169         } // END - if
170
171         // Return it
172         return $GLOBALS['lang_inc'][$ext_name];
173 }
174
175 // Load the current language file or fixes it to 'de'
176 // If ext_name is 'none', load general language support, else load extension's
177 // language file. In installation phase load the install language file.
178 function loadLanguageFile ($ext_name = 'none') {
179         // Try to get language from session
180         $currLanguage = getLanguage();
181
182         // Set default language if it is not (yet) set
183         if (is_null($currLanguage)) {
184                 // Get it from config
185                 $currLanguage = getDefaultLanguage();
186
187                 // And save it in session
188                 setLanguage($currLanguage);
189         } // END - if
190
191         // Do we have the language file NOT?
192         if (!isLanguageIncludeReadable($ext_name)) {
193                 // Switch to default (DO NOT CHANGE!!!)
194                 setLanguage('de');
195
196                 // And set it temporarily
197                 setConfigEntry('DEFAULT_LANG', 'de');
198         } // END - if
199
200         // Is the file there?
201         if (isLanguageIncludeReadable($ext_name)) {
202                 // Load language file
203                 loadLanguageInclude($ext_name);
204         } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
205                 // No language file is not so good...
206                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
207                         $ext_name,
208                         getLanguage(),
209                         getExtensionMode()
210                 ));
211         }
212
213         // Check for installation mode
214         if ((isInstallationPhase()) || (!isAdminRegistered())) {
215                 // Load language file
216                 loadLanguageInclude('install');
217         } // END - if
218 }
219
220 // Loads the language file
221 function loadLanguageInclude ($ext_name = 'none') {
222         // Generate filename
223         if ($ext_name == 'none') {
224                 // Generic
225                 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
226         } else {
227                 // Extension's language file
228                 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
229         }
230
231         // Check it before loading
232         if (isLanguageIncludeReadable($ext_name)) {
233                 // Load it
234                 loadIncludeOnce($languageInclude);
235         } else {
236                 // Not readable!
237                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
238         }
239 }
240
241 // Getter for an array of valid languages with no except by default
242 function getValidLanguages ($except = '') {
243         // @TODO These are all valid languages, again hard-coded
244         $langs = array('de' => 'de', 'en' => 'en');
245
246         // Should we keep one out?
247         if (!empty($except)) {
248                 // Remove this
249                 unset($langs[$except]);
250         } // END - if
251
252         // Return the array
253         return $langs;
254 }
255
256 // Compares two language files
257 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
258         // Init differences
259         $GLOBALS['lang_diff'][$target] = array();
260         $GLOBALS['lang_diff_count'][$target] = 0;
261         if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
262
263         // *Does* match by default
264         $matches = true;
265
266         // Is one not readable?
267         if (!isIncludeReadable($source)) {
268                 // Please report this bug!
269                 debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
270         } elseif (!isIncludeReadable($target)) {
271                 // Please report this bug!
272                 debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
273         } elseif ($targetLanguage == getCurrentLanguage()) {
274                 // Must be different
275                 debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
276         }
277
278         // Backup current messages/language
279         $backupLang = getCurrentLanguage();
280         $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
281         $GLOBALS['messages'][$backupLang] = array();
282
283         // Both are readable so include current language file
284         $GLOBALS['count'] = false;
285         loadInclude($source);
286         $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
287         unset($GLOBALS['count']);
288
289         // Set target language
290         setCurrentLanguage($targetLanguage);
291
292         // Do we have an array?
293         if (!isset($GLOBALS['messages'][$targetLanguage])) {
294                 // Then create it to avoid notice
295                 $GLOBALS['messages'][$targetLanguage] = array();
296                 $GLOBALS['msg_count'][$targetLanguage] = 0;
297         } // END - if
298
299         // Load target language file
300         loadInclude($target);
301         $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
302
303         // Set backup back
304         setCurrentLanguage($backupLang);
305         $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
306         unset($messages[$backupLang]);
307
308         // Do they mismatch?
309         if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
310                 // Does not match
311                 $matches = false;
312
313                 // Check all differences
314                 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
315                         // Don't we have it?
316                         if (!isset($GLOBALS['msgs'][$target][$key])) {
317                                 // Then add is as difference
318                                 $GLOBALS['lang_diff'][$target][$key] = $value;
319
320                                 // ... and count it
321                                 $GLOBALS['lang_diff_count'][$target]++;
322                                 $GLOBALS['lang_diff_count']['total']++;
323                         } // END - if
324                 } // END - foreach
325         } // END - if
326
327         // Return result
328         return $matches;
329 }
330
331 // Getter for getting difference of target file
332 function getLanguageComparisonDifference ($target) {
333         return $GLOBALS['lang_diff_count'][$target];
334 }
335
336 // Checks wether the given message is masked
337 function isMessageMasked ($messageId) {
338         // Is the message id valid?
339         if (!isMessageIdValid($messageId)) {
340                 // No, then abort here
341                 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
342         } // END - if
343
344         // Now simply check it
345         $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);
346
347         // Return result
348         return $masked;
349 }
350
351 // [EOF]
352 ?>