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