Wrapper functions introduced for configuration, loaders refactured:
[mailer.git] / inc / language-functions.php
index 5cde4b6ebe59fee6eaded2f5f8c65681dece0570..29e07cf33a87d0b9433429757ec984ac00531e99 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 08/25/2003 *
- * ===============                              Last change: 11/29/2005 *
+ * Mailer v0.2.1-FINAL                                Start: 08/25/2003 *
+ * ===================                          Last change: 11/29/2005 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : language-functions.php                           *
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
  * MA  02110-1301  USA                                                  *
  ************************************************************************/
+
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
-       require($INC);
-}
+       die();
+} // END - if
 
 // "Getter" for language strings
 // @TODO Rewrite all language constants to this function.
 function getMessage ($messageId) {
-       // Default is not found!
-       $return = '!'.$messageId.'!';
+       // Default is not found
+       $return = '!' . $messageId . '!';
 
        // Is the language string found?
-       if (isset($GLOBALS['msg'][strtolower($messageId)])) {
+       if (isMessageIdValid($messageId)) {
                // Language array element found in small_letters
-               $return = $GLOBALS['msg'][$messageId];
-       } elseif (isset($GLOBALS['msg'][strtoupper($messageId)])) {
-               // @DEPRECATED Language array element found in BIG_LETTERS
-               $return = $GLOBALS['msg'][$messageId];
-       } elseif (defined($messageId)) {
-               // @DEPRECATED Deprecated constant found
-               $return = constant($messageId);
+               $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
        } else {
                // Missing language constant
-               DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
        }
 
        // Return the string
        return $return;
 }
 
+// Getter for message string as a mask
+function getMaskedMessage ($messageId, $data) {
+       // Construct message
+       $message = sprintf(getMessage($messageId), $data);
+
+       // Return it
+       return $message;
+}
+
+// Init messages
+function initMessages () {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getLanguage()=' . getLanguage());
+       $GLOBALS['messages'][getLanguage()] = array();
+}
+
+// Add messages
+function addMessages ($messages) {
+       // Cache current language
+       $currentLanguage = getCurrentLanguage();
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentLanguage=' . $currentLanguage);
+
+       // Merge both
+       $GLOBALS['messages'][$currentLanguage] = merge_array($GLOBALS['messages'][$currentLanguage], $messages);
+
+       // Don't count them if we don't want it
+       if (isset($GLOBALS['count'])) {
+               return;
+       } // END - if
+
+       // And count them
+       if (isset($GLOBALS['msg_count'][$currentLanguage])) {
+               $GLOBALS['msg_count'][$currentLanguage] += count($messages);
+       } else {
+               $GLOBALS['msg_count'][$currentLanguage] = count($messages);
+       }
+}
+
+// Checks wether given message id is valid
+function isMessageIdValid ($messageId) {
+       return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
+}
+
+// Getter for current language
+function getCurrentLanguage () {
+       return $GLOBALS['language'];
+}
+
+// Setter for current language
+function setCurrentLanguage ($language) {
+       $GLOBALS['language'] = (string) $language;
+}
+
+// Checks wether current language is set
+function isCurrentLanguageSet () {
+       return (isset($GLOBALS['language']));
+}
+
 // "Getter" for language
 function getLanguage () {
-       // Set default return value to default language from config
-       $ret = getConfig('DEFAULT_LANG');
-
-       // Init variable
-       $lang = '';
-
-       // Is the variable set
-       if (REQUEST_ISSET_GET('mx_lang')) {
-               // Accept only first 2 chars
-               $lang = substr(REQUEST_GET('mx_lang'), 0, 2);
-       } elseif (isset($GLOBALS['cache_array']['language'])) {
-               // Use cached
-               $ret = $GLOBALS['cache_array']['language'];
-       } elseif (!empty($lang)) {
-               // Check if main language file does exist
-               if (isFileReadable(constant('PATH') . 'inc/language/'.$lang.'.php')) {
-                       // Okay found, so let's update cookies
-                       setLanguage($lang);
+       // Do we have cache?
+       if (!isCurrentLanguageSet()) {
+               // Default is 'de'. DO NOT CHANGE THIS!!!
+               $ret = 'de';
+
+               // Set default return value to default language from config
+               if (isConfigEntrySet('DEFAULT_LANG')) {
+                       $ret = getDefaultLanguage();
                } // END - if
-       } elseif (isSessionVariableSet('mx_lang')) {
-               // Return stored value from cookie
-               $ret = getSession('mx_lang');
 
-               // Fixes a warning before the session has the mx_lang constant
-               if (empty($ret)) $ret = getConfig('DEFAULT_LANG');
-       }
+               // Is the variable set
+               if (isGetRequestParameterSet('mailer_lang')) {
+                       // Accept only first 2 chars
+                       $ret = substr(getRequestParameter('mailer_lang'), 0, 2);
+               } elseif (isCurrentLanguageSet()) {
+                       // Use cached
+                       $ret = getCurrentLanguage();
+               } elseif (isSessionVariableSet('mailer_lang')) {
+                       // Return stored value from cookie
+                       $ret = getSession('mailer_lang');
 
-       // Cache entry
-       $GLOBALS['cache_array']['language'] = $ret;
+                       // Fixes a warning before the session has the mailer_lang constant
+                       if (empty($ret)) {
+                               $ret = getDefaultLanguage();
+                       } // END - if
+               }
+
+               // Cache entry
+               setCurrentLanguage($ret);
+       } // END - if
 
-       // Return value
-       return $ret;
+       // Return cached value
+       return getCurrentLanguage();
 }
 
 // "Setter" for language
 function setLanguage ($lang) {
-       // Accept only first 2 chars!
-       $lang = substr(SQL_ESCAPE(strip_tags($lang)), 0, 2);
+       // Accept only first 2 chars and still secure them
+       $lang = substr(secureString($lang), 0, 2);
 
        // Set cookie
-       setSession('mx_lang', $lang);
+       setSession('mailer_lang', $lang);
+}
+
+// Checks wether a language file is there for optional extension
+function isLanguageIncludeReadable ($ext_name = 'none') {
+       // Do we have array element?
+       if (!isset($GLOBALS['lang_inc'][$ext_name])) {
+               // Generate filename
+               if ($ext_name == 'none') {
+                       // Generic
+                       $languageInclude = sprintf("inc/language/%s.php", getLanguage());
+               } else {
+                       // Extension's language file
+                       $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
+               }
+
+               // Look for file if no extension name is provided
+               $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
+               //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $ext_name . '=' . $languageInclude . '=' . intval(isIncludeReadable($languageInclude)));
+       } // END - if
+
+       // Return it
+       return $GLOBALS['lang_inc'][$ext_name];
 }
 
 // Load the current language file or fixes it to 'de'
-function loadLanguageFile () {
+// If ext_name is 'none', load general language support, else load extension's
+// language file. In installation phase load the install language file.
+function loadLanguageFile ($ext_name = 'none') {
        // Try to get language from session
-       $mx_lang = getSession('mx_lang');
+       $currLanguage = getLanguage();
 
        // Set default language if it is not (yet) set
-       if (is_null($mx_lang)) $mx_lang = getConfig('DEFAULT_LANG');
+       if (is_null($currLanguage)) {
+               // Get it from config
+               $currLanguage = getDefaultLanguage();
 
-       // Generate filename
-       $INC = sprintf("inc/language/%s.php", SQL_ESCAPE($mx_lang));
+               // And save it in session
+               setLanguage($currLanguage);
+       } // END - if
 
-       // Look for file
-       if (!isIncludeReadable($INC)) {
+       // Do we have the language file NOT?
+       if (!isLanguageIncludeReadable($ext_name)) {
                // Switch to default (DO NOT CHANGE!!!)
                setLanguage('de');
-               $INC = 'inc/language/de.php';
 
                // And set it temporarily
                setConfigEntry('DEFAULT_LANG', 'de');
        } // END - if
 
-       // Load language file
-       loadIncludeOnce($INC);
+       // Is the file there?
+       if (isLanguageIncludeReadable($ext_name)) {
+               // Load language file
+               loadLanguageInclude($ext_name);
+       } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
+               // No language file is not so good...
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
+                       $ext_name,
+                       getLanguage(),
+                       getExtensionMode()
+               ));
+       }
 
        // Check for installation mode
-       if ((isInstalling()) || (!isInstalled()) || (!isAdminRegistered())) {
-               // Load matching language file
-               loadInclude('inc/language/install_' . getSession('mx_lang') . '.php');
+       if ((isInstallationPhase()) || (!isAdminRegistered())) {
+               // Load language file
+               loadLanguageInclude('install');
+       } // END - if
+}
+
+// Loads the language file
+function loadLanguageInclude ($ext_name = 'none') {
+       // Generate filename
+       if ($ext_name == 'none') {
+               // Generic
+               $languageInclude = sprintf("inc/language/%s.php", getLanguage());
+       } else {
+               // Extension's language file
+               $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
+       }
+
+       // Check it before loading
+       if (isLanguageIncludeReadable($ext_name)) {
+               // Load it
+               loadIncludeOnce($languageInclude);
+       } else {
+               // Not readable!
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
+       }
+}
+
+// Getter for an array of valid languages with no except by default
+function getValidLanguages ($except = '') {
+       // @TODO These are all valid languages, again hard-coded
+       $langs = array('de' => 'de', 'en' => 'en');
+
+       // Should we keep one out?
+       if (!empty($except)) {
+               // Remove this
+               unset($langs[$except]);
+       } // END - if
+
+       // Return the array
+       return $langs;
+}
+
+// Compares two language files
+function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
+       // Init differences
+       $GLOBALS['lang_diff'][$target] = array();
+       $GLOBALS['lang_diff_count'][$target] = 0;
+       if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
+
+       // *Does* match by default
+       $matches = true;
+
+       // Is one not readable?
+       if (!isIncludeReadable($source)) {
+               // Please report this bug!
+               debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
+       } elseif (!isIncludeReadable($target)) {
+               // Please report this bug!
+               debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
+       } elseif ($targetLanguage == getCurrentLanguage()) {
+               // Must be different
+               debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
+       }
+
+       // Backup current messages/language
+       $backupLang = getCurrentLanguage();
+       $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
+       $GLOBALS['messages'][$backupLang] = array();
+
+       // Both are readable so include current language file
+       $GLOBALS['count'] = false;
+       loadInclude($source);
+       $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
+       unset($GLOBALS['count']);
+
+       // Set target language
+       setCurrentLanguage($targetLanguage);
+
+       // Do we have an array?
+       if (!isset($GLOBALS['messages'][$targetLanguage])) {
+               // Then create it to avoid notice
+               $GLOBALS['messages'][$targetLanguage] = array();
+               $GLOBALS['msg_count'][$targetLanguage] = 0;
+       } // END - if
+
+       // Load target language file
+       loadInclude($target);
+       $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
+
+       // Set backup back
+       setCurrentLanguage($backupLang);
+       $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
+       unset($messages[$backupLang]);
+
+       // Do they mismatch?
+       if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
+               // Does not match
+               $matches = false;
+
+               // Check all differences
+               foreach ($GLOBALS['msgs'][$source] as $key => $value) {
+                       // Don't we have it?
+                       if (!isset($GLOBALS['msgs'][$target][$key])) {
+                               // Then add is as difference
+                               $GLOBALS['lang_diff'][$target][$key] = $value;
+
+                               // ... and count it
+                               $GLOBALS['lang_diff_count'][$target]++;
+                               $GLOBALS['lang_diff_count']['total']++;
+                       } // END - if
+               } // END - foreach
        } // END - if
+
+       // Return result
+       return $matches;
+}
+
+// Getter for getting difference of target file
+function getLanguageComparisonDifference ($target) {
+       return $GLOBALS['lang_diff_count'][$target];
+}
+
+// Checks wether the given message is masked
+function isMessageMasked ($messageId) {
+       // Is the message id valid?
+       if (!isMessageIdValid($messageId)) {
+               // No, then abort here
+               debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
+       } // END - if
+
+       // Now simply check it
+       $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);
+
+       // Return result
+       return $masked;
 }
 
 // [EOF]