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