Double question mark fixed
[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         // Do we have cache?
117         if (!isset($GLOBALS['language'])) {
118                 // Default is 'de'. DO NOT CHANGE THIS!!!
119                 $ret = 'de';
120
121                 // Set default return value to default language from config
122                 if (isConfigEntrySet('DEFAULT_LANG')) {
123                         $ret = getDefaultLanguage();
124                 } // END - if
125
126                 // Is the variable set
127                 if (isGetRequestParameterSet('mx_lang')) {
128                         // Accept only first 2 chars
129                         $ret = substr(getRequestParameter('mx_lang'), 0, 2);
130                 } elseif (isset($GLOBALS['language'])) {
131                         // Use cached
132                         $ret = getCurrentLanguage();
133                 } elseif (isSessionVariableSet('mx_lang')) {
134                         // Return stored value from cookie
135                         $ret = getSession('mx_lang');
136
137                         // Fixes a warning before the session has the mx_lang constant
138                         if (empty($ret)) {
139                                 $ret = getDefaultLanguage();
140                         } // END - if
141                 }
142
143                 // Cache entry
144                 setCurrentLanguage($ret);
145         } // END - if
146
147         // Return cached value
148         return getCurrentLanguage();
149 }
150
151 // "Setter" for language
152 function setLanguage ($lang) {
153         // Accept only first 2 chars and still secure them
154         $lang = substr(secureString($lang), 0, 2);
155
156         // Set cookie
157         setSession('mx_lang', $lang);
158 }
159
160 // Checks wether a language file is there for optional extension
161 function isLanguageIncludeReadable ($ext_name = 'none') {
162         // Do we have array element?
163         if (!isset($GLOBALS['lang_inc'][$ext_name])) {
164                 // Generate filename
165                 if ($ext_name == 'none') {
166                         // Generic
167                         $languageInclude = sprintf("inc/language/%s.php", getLanguage());
168                 } else {
169                         // Extension's language file
170                         $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
171                 }
172
173                 // Look for file if no extension name is provided
174                 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
175                 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $ext_name . '=' . $languageInclude . '=' . intval(isIncludeReadable($languageInclude)));
176         } // END - if
177
178         // Return it
179         return $GLOBALS['lang_inc'][$ext_name];
180 }
181
182 // Load the current language file or fixes it to 'de'
183 // If ext_name is 'none', load general language support, else load extension's
184 // language file. In installation phase load the install language file.
185 function loadLanguageFile ($ext_name = 'none') {
186         // Try to get language from session
187         $currLanguage = getLanguage();
188
189         // Set default language if it is not (yet) set
190         if (is_null($currLanguage)) {
191                 // Get it from config
192                 $currLanguage = getDefaultLanguage();
193
194                 // And save it in session
195                 setLanguage($currLanguage);
196         } // END - if
197
198         // Do we have the language file NOT?
199         if (!isLanguageIncludeReadable($ext_name)) {
200                 // Switch to default (DO NOT CHANGE!!!)
201                 setLanguage('de');
202
203                 // And set it temporarily
204                 setConfigEntry('DEFAULT_LANG', 'de');
205         } // END - if
206
207         // Is the file there?
208         if (isLanguageIncludeReadable($ext_name)) {
209                 // Load language file
210                 loadLanguageInclude($ext_name);
211         } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
212                 // No language file is not so good...
213                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
214                         $ext_name,
215                         getLanguage(),
216                         getExtensionMode()
217                 ));
218         }
219
220         // Check for installation mode
221         if ((isInstallationPhase()) || (!isAdminRegistered())) {
222                 // Load language file
223                 loadLanguageInclude('install');
224         } // END - if
225 }
226
227 // Loads the language file
228 function loadLanguageInclude ($ext_name = 'none') {
229         // Generate filename
230         if ($ext_name == 'none') {
231                 // Generic
232                 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
233         } else {
234                 // Extension's language file
235                 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
236         }
237
238         // Check it before loading
239         if (isLanguageIncludeReadable($ext_name)) {
240                 // Load it
241                 loadIncludeOnce($languageInclude);
242         } else {
243                 // Not readable!
244                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
245         }
246 }
247
248 // Getter for an array of valid languages with no except by default
249 function getValidLanguages ($except = '') {
250         // @TODO These are all valid languages, again hard-coded
251         $langs = array('de' => 'de', 'en' => 'en');
252
253         // Should we keep one out?
254         if (!empty($except)) {
255                 // Remove this
256                 unset($langs[$except]);
257         } // END - if
258
259         // Return the array
260         return $langs;
261 }
262
263 // Compares two language files
264 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
265         // Init differences
266         $GLOBALS['lang_diff'][$target] = array();
267         $GLOBALS['lang_diff_count'][$target] = 0;
268         if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
269
270         // *Does* match by default
271         $matches = true;
272
273         // Is one not readable?
274         if (!isIncludeReadable($source)) {
275                 // Please report this bug!
276                 debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
277         } elseif (!isIncludeReadable($target)) {
278                 // Please report this bug!
279                 debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
280         } elseif ($targetLanguage == getCurrentLanguage()) {
281                 // Must be different
282                 debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
283         }
284
285         // Backup current messages/language
286         $backupLang = getCurrentLanguage();
287         $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
288         $GLOBALS['messages'][$backupLang] = array();
289
290         // Both are readable so include current language file
291         $GLOBALS['count'] = false;
292         loadInclude($source);
293         $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
294         unset($GLOBALS['count']);
295
296         // Set target language
297         setCurrentLanguage($targetLanguage);
298
299         // Do we have an array?
300         if (!isset($GLOBALS['messages'][$targetLanguage])) {
301                 // Then create it to avoid notice
302                 $GLOBALS['messages'][$targetLanguage] = array();
303                 $GLOBALS['msg_count'][$targetLanguage] = 0;
304         } // END - if
305
306         // Load target language file
307         loadInclude($target);
308         $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
309
310         // Set backup back
311         setCurrentLanguage($backupLang);
312         $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
313         unset($messages[$backupLang]);
314
315         // Do they mismatch?
316         if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
317                 // Does not match
318                 $matches = false;
319
320                 // Check all differences
321                 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
322                         // Don't we have it?
323                         if (!isset($GLOBALS['msgs'][$target][$key])) {
324                                 // Then add is as difference
325                                 $GLOBALS['lang_diff'][$target][$key] = $value;
326
327                                 // ... and count it
328                                 $GLOBALS['lang_diff_count'][$target]++;
329                                 $GLOBALS['lang_diff_count']['total']++;
330                         } // END - if
331                 } // END - foreach
332         } // END - if
333
334         // Return result
335         return $matches;
336 }
337
338 // Getter for getting difference of target file
339 function getLanguageComparisonDifference ($target) {
340         return $GLOBALS['lang_diff_count'][$target];
341 }
342
343 // Checks wether the given message is masked
344 function isMessageMasked ($messageId) {
345         // Is the message id valid?
346         if (!isMessageIdValid($messageId)) {
347                 // No, then abort here
348                 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
349         } // END - if
350
351         // Now simply check it
352         $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);
353
354         // Return result
355         return $masked;
356 }
357
358 // [EOF]
359 ?>