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