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