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