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