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