3d716d966fad60481d27654dbcf188a3c7f5eda8
[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 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://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 whether 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 whether 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 (isGetRequestElementSet('mailer_lang')) {
133                         // Accept only first 2 chars
134                         $ret = substr(getRequestElement('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 whether 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'])) {
274                 $GLOBALS['lang_diff_count']['total'] = 0;
275         } // END - if
276
277         // *Does* match by default
278         $matches = true;
279
280         // Is one not readable?
281         if (!isIncludeReadable($source)) {
282                 // Please report this bug!
283                 reportBug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
284         } elseif (!isIncludeReadable($target)) {
285                 // Please report this bug!
286                 reportBug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
287         } elseif ($targetLanguage == getCurrentLanguage()) {
288                 // Must be different
289                 reportBug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
290         }
291
292         // Backup current messages/language
293         $backupLang = getCurrentLanguage();
294         $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
295         $GLOBALS['messages'][$backupLang] = array();
296
297         // Both are readable so include current language file
298         $GLOBALS['count'] = false;
299         loadInclude($source);
300         $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
301         unset($GLOBALS['count']);
302
303         // Set target language
304         setCurrentLanguage($targetLanguage);
305
306         // Do we have an array?
307         if (!isset($GLOBALS['messages'][$targetLanguage])) {
308                 // Then create it to avoid notice
309                 $GLOBALS['messages'][$targetLanguage] = array();
310                 $GLOBALS['msg_count'][$targetLanguage] = 0;
311         } // END - if
312
313         // Load target language file
314         loadInclude($target);
315         $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
316
317         // Set backup back
318         setCurrentLanguage($backupLang);
319         $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
320         unset($messages[$backupLang]);
321
322         // Do they mismatch?
323         if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
324                 // Does not match
325                 $matches = false;
326
327                 // Check all differences
328                 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
329                         // Don't we have it?
330                         if (!isset($GLOBALS['msgs'][$target][$key])) {
331                                 // Then add is as difference
332                                 $GLOBALS['lang_diff'][$target][$key] = $value;
333
334                                 // ... and count it
335                                 $GLOBALS['lang_diff_count'][$target]++;
336                                 $GLOBALS['lang_diff_count']['total']++;
337                         } // END - if
338                 } // END - foreach
339         } // END - if
340
341         // Return result
342         return $matches;
343 }
344
345 // Getter for getting difference of target file
346 function getLanguageComparisonDifference ($target) {
347         return $GLOBALS['lang_diff_count'][$target];
348 }
349
350 // Checks whether the given message is masked
351 function isMessageMasked ($messageId, $strict = true) {
352         // Is the message id valid?
353         if (($strict === true) && (!isMessageIdValid($messageId))) {
354                 // No, then abort here
355                 reportBug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
356         } // END - if
357
358         // Now simply check it
359         $masked = isInString('%', getMessage($messageId));
360
361         // Return result
362         return $masked;
363 }
364
365 // [EOF]
366 ?>