But only when it is not empty
[mailer.git] / inc / module-functions.php
index cc75df10fbe2d8f8bedab8e985a530fd63aef3cf..39fab38c7ae6e8e2885eb1a9ed7623f745028c4b 100644 (file)
@@ -16,8 +16,8 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
@@ -42,56 +42,62 @@ if (!defined('__SECURITY')) {
 
 // "Getter" for module title
 function getModuleTitle ($module) {
-       // Init variables
-       $data['title'] = '';
-       $result = false;
-
-       // Is the script installed?
-       if ((isInstalled()) && ($module != 'error')) {
-               // Check if cache is valid
-               if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($module, $GLOBALS['cache_array']['modules']['module']))) {
-                       // Load from cache
-                       $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
-
-                       // Update cache hits
-                       incrementStatsEntry('cache_hits');
-               } elseif (!isExtensionActive('cache')) {
-                       // Load from database
-                       $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
-                               array($module), __FUNCTION__, __LINE__);
-
-                       // Is the entry there?
-                       if (SQL_NUMROWS($result) == 1) {
-                               // Get the title from database
-                               $data = SQL_FETCHARRAY($result);
-                       } // END - if
+       // Is there cache?
+       if (!isset($GLOBALS[__FUNCTION__][$module])) {
+               // Init variables
+               $data['title'] = '';
+               $result = FALSE;
+
+               // Is the script installed?
+               if ((isInstalled()) && ($module != 'error')) {
+                       // Check if cache is valid
+                       if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($module, $GLOBALS['cache_array']['modules']['module']))) {
+                               // Load from cache
+                               $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
+
+                               // Update cache hits
+                               incrementStatsEntry('cache_hits');
+                       } elseif (!isExtensionActive('cache')) {
+                               // Load from database
+                               $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
+                                       array($module), __FUNCTION__, __LINE__);
+
+                               // Is the entry there?
+                               if (SQL_NUMROWS($result) == 1) {
+                                       // Get the title from database
+                                       $data = SQL_FETCHARRAY($result);
+                               } // END - if
+
+                               // Free the result
+                               SQL_FREERESULT($result);
+                       }
+               } // END - if
 
-                       // Free the result
-                       SQL_FREERESULT($result);
-               }
-       } // END - if
+               // Trim name
+               $data['title'] = trim($data['title']);
+
+               // Still no luck or empty title?
+               if (empty($data['title'])) {
+                       // Is it 'error'?
+                       if ($module == 'error') {
+                               // Error (real module was not found)
+                               $data['title'] = '{--MODULE_ERROR_404_TITLE--}';
+                       }  else {
+                               // No name found
+                               $data['title'] = '{%message,UNKNOWN_MODULE_DETECTED_TITLE=' . $module . '%}';
+                               if ((is_resource($result)) && (SQL_HASZERONUMS($result))) {
+                                       // Add module to database and ignore return value
+                                       checkModulePermissions($module);
+                               } // END - if
+                       }
+               } // END - if
 
-       // Trim name
-       $data['title'] = trim($data['title']);
-
-       // Still no luck or empty title?
-       if (empty($data['title'])) {
-               // Is it 'error'?
-               if ($module == 'error') {
-                       // Error (real module was not found)
-                       $data['title'] = '{--MODULE_ERROR_404--}';
-               }  else {
-                       // No name found
-                       $data['title'] = '{%message,UNKNOWN_MODULE_DETECTED_TITLE=' . $module . '%}';
-                       if ((is_resource($result)) && (SQL_HASZERONUMS($result))) {
-                               // Add module to database and ignore return value
-                               checkModulePermissions($module);
-                       } // END - if
-               }
+               // Store in cache
+               $GLOBALS[__FUNCTION__][$module] = $data['title'];
        } // END - if
 
-       // Return name
-       return $data['title'];
+       // Return it
+       return $GLOBALS[__FUNCTION__][$module];
 }
 
 // Checks if module_status entry is there
@@ -110,17 +116,17 @@ function getModuleStatus ($module) {
        // Is the module_status entry there?
        if (!isModuleStatusSet($module)) {
                // Abort
-               debug_report_bug('Module status not set. module=' . $module);
+               reportBug(__FUNCTION__, __LINE__, 'Module status not set. module=' . $module);
        } // END - if
 
        // Return it
        return $GLOBALS['module_status'][$module];
 }
 
-// Checks wether the given module is registered
+// Checks whether the given module is registered
 function isModuleRegistered ($module) {
        // By default nothing is found
-       $found  = false;
+       $found  = FALSE;
 
        // Check if cache is latest version
        if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
@@ -131,7 +137,7 @@ function isModuleRegistered ($module) {
                        incrementStatsEntry('cache_hits');
 
                        // Is found
-                       $found = true;
+                       $found = TRUE;
                } else {
                        // No, then we have to update it!
                        setModuleStatus($module, 'cache_miss');
@@ -139,19 +145,19 @@ function isModuleRegistered ($module) {
        } elseif (!isExtensionActive('cache')) {
                // Check for module in database
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
-               $result = SQL_QUERY_ESC("SELECT `locked`,`hidden`,`admin_only`,`mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
+               $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
                        array($module), __FUNCTION__, __LINE__);
                if (SQL_NUMROWS($result) == 1) {
                        // Read data
                        $data = SQL_FETCHARRAY($result);
 
                        // Set all entries
-                       foreach ($data as $key=>$value) {
+                       foreach ($data as $key => $value) {
                                $GLOBALS['cache_array']['modules'][$key][$module] = $value;
                        } // END - foreach
 
                        // Mark as found
-                       $found = true;
+                       $found = TRUE;
                } elseif (isDebugModeEnabled()) {
                        // Debug message only in debug-mode...
                        logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found.');
@@ -165,7 +171,7 @@ function isModuleRegistered ($module) {
        return $found;
 }
 
-// Checks wether the given module is locked by just checking the cache
+// Checks whether the given module is locked by just checking the cache
 function isModuleLocked ($module) {
        // Determine if there a cache entry and is it set
        $return = ((isset($GLOBALS['cache_array']['modules']['locked'][$module])) && ($GLOBALS['cache_array']['modules']['locked'][$module] == 'Y'));
@@ -174,7 +180,7 @@ function isModuleLocked ($module) {
        return $return;
 }
 
-// Checks wether the given module is hidden by just checking the cache
+// Checks whether the given module is hidden by just checking the cache
 function isModuleHidden ($module) {
        // Determine if there a cache entry and is it set
        $return = ((isset($GLOBALS['cache_array']['modules']['hidden'][$module])) && ($GLOBALS['cache_array']['modules']['hidden'][$module] == 'Y'));
@@ -183,7 +189,7 @@ function isModuleHidden ($module) {
        return $return;
 }
 
-// Checks wether the given module is mem_only by just checking the cache
+// Checks whether the given module is mem_only by just checking the cache
 function isModuleMemberOnly ($module) {
        // Determine if there a cache entry and is it set
        $return = ((isset($GLOBALS['cache_array']['modules']['mem_only'][$module])) && ($GLOBALS['cache_array']['modules']['mem_only'][$module] == 'Y'));
@@ -192,7 +198,7 @@ function isModuleMemberOnly ($module) {
        return $return;
 }
 
-// Checks wether the given module is admin_only by just checking the cache
+// Checks whether the given module is admin_only by just checking the cache
 function isModuleAdminOnly ($module) {
        // Determine if there a cache entry and is it set
        $return = ((isset($GLOBALS['cache_array']['modules']['admin_only'][$module])) && ($GLOBALS['cache_array']['modules']['admin_only'][$module] == 'Y'));
@@ -209,7 +215,7 @@ function checkModulePermissions ($module = '') {
                $module = getModule();
        } // END - if
 
-       // Do we have cache?
+       // Is there cache?
        if (isModuleStatusSet($module)) {
                // Then use it
                return getModuleStatus($module);
@@ -223,7 +229,7 @@ function checkModulePermissions ($module = '') {
        $extension = ''; $module_chk = $module;
        //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
        if (count($modSplit) == 2) {
-               // Okay, there is a seperator (_) in the name so is the first part a module?
+               // Okay, there is a separator (_) in the name so is the first part a module?
                //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
                if (isExtensionActive($modSplit[0])) {
                        // The prefix is an extension's name, so let's set it
@@ -247,7 +253,7 @@ function checkModulePermissions ($module = '') {
        $found = isModuleRegistered($module_chk);
 
        // Is the module found?
-       if ($found === true) {
+       if ($found === TRUE) {
                // Check returned values against current access permissions
                //
                // Admin access                                                              ----- Guest access -----                                                                                                                         --- Guest   or   member? ---
@@ -276,23 +282,23 @@ function checkModulePermissions ($module = '') {
        } // END - if
 
        // Still no luck or not found?
-       if (($found === false) && (!isExtensionActive('cache')) && (getModuleStatus($module_chk) != 'done'))  {
+       if (($found === FALSE) && (!isExtensionActive('cache')) && (getModuleStatus($module_chk) != 'done'))  {
                //              ----- Default module -----                                  ---- Module in base folder  ----                       --- Module with extension's name ---
                if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
                        // Data is missing so we add it
                        if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
                                /*
-                                * Since 0.3.6 we have a has_menu column, this took me a half
+                                * Since 0.3.6 there is a has_menu column, this took me a half
                                 * hour to find a loop here... *sigh*
                                 */
                                SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
-(`module`,`locked`,`hidden`,`mem_only`,`admin_only`,`has_menu`)
+(`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`)
 VALUES
 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
                        } else {
                                // Wrong/missing sql_patches!
                                SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
-(`module`,`locked`,`hidden`,`mem_only`,`admin_only`)
+(`module`, `locked`, `hidden`, `mem_only`, `admin_only`)
 VALUES
 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
                        }
@@ -320,7 +326,7 @@ VALUES
        } elseif ((getModuleStatus($module_chk) == 'cache_miss') && (isHtmlOutputMode())) {
                // Rebuild the cache files
                rebuildCache('modules', 'modules');
-       } elseif ($found === false) {
+       } elseif ($found === FALSE) {
                // Problem with module detected
                logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. getModuleStatus()=%s,locked=%d,hidden=%d,mem=%d,admin=%d,output_mode=%s",
                        $module_chk,
@@ -334,16 +340,16 @@ VALUES
        }
 
        // Debug log
-       logDebugMessage(__FUNCTION__, __LINE__, sprintf("module=%s, status=%s", $module_chk, getModuleStatus($module_chk)));
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("module=%s, status=%s", $module_chk, getModuleStatus($module_chk)));
 
        // Return the value
        return getModuleStatus($module_chk);
 }
 
 // Checks if the module has a menu
-function ifModuleHasMenu ($module, $forceDb = false) {
+function ifModuleHasMenu ($module, $forceDb = FALSE) {
        // All is false by default
-       $ret = false;
+       $ret = FALSE;
 
        // Extension installed and newer than or has version 0.1.2?
        if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
@@ -360,7 +366,7 @@ function ifModuleHasMenu ($module, $forceDb = false) {
                        // Admin/guest/member/sponsor modules have always a menu!
                        $ret = in_array($module, array('admin', 'index', 'login', 'sponsor'));
                }
-       } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
+       } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === TRUE))) {
                // Check database for entry
                $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
                        array($module), __FUNCTION__, __LINE__);
@@ -380,11 +386,11 @@ function ifModuleHasMenu ($module, $forceDb = false) {
                // Free memory
                SQL_FREERESULT($result);
        } elseif (!isExtensionInstalled('sql_patches')) {
-               // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
+               // No ext-sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
                $ret = in_array($module, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
-       } else {
-               // Unsupported state!
-               logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
+       } elseif (!isInstallationPhase()) {
+               // Unsupported state, but ignored in installation phase
+               logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached, module[' . gettype($module) . ']=' . $module . ',forceDb=' . intval($forceDb));
        }
 
        // Return status
@@ -392,11 +398,11 @@ function ifModuleHasMenu ($module, $forceDb = false) {
 }
 
 // Adds a SQL for given module
-function addModuleSql ($module, $locked, $hidden, $adminOnly, $memOnly) {
+function addModuleSql ($module, $title, $locked, $hidden, $adminOnly, $memOnly) {
        // Is the module already registered?
        if (!isModuleRegistered($module)) {
                // Add it
-               addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`,`locked`,`hidden`,`admin_only`,`mem_only`) VALUES ('" . $module . "','" . $locked . "','" . $hidden . "','" . $adminOnly . "','" . $memOnly . "')");
+               addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `title`, `locked`, `hidden`, `admin_only`, `mem_only`) VALUES ('" . $module . "', '" . $title . "', '" . $locked . "', '" . $hidden . "', '" . $adminOnly . "', '" . $memOnly . "')");
        } else {
                // Already registered
                logDebugMessage(__FUNCTION__, __LINE__, sprintf("Already registered: module=%s,locked=%s,hidden=%s,admin=%s,mem=%s",
@@ -412,13 +418,10 @@ function addModuleSql ($module, $locked, $hidden, $adminOnly, $memOnly) {
 // Load the currently set module
 function loadModule () {
        // By default all modules are invalid
-       $isModuleValid = false;
-
-       // Init module state as 'failed' (always failed first)
-       $moduleState = 'failed';
+       $isModuleValid = FALSE;
 
        // Construct module name
-       $GLOBALS['module_inc'] =  sprintf("inc/modules/%s.php", getModule());
+       $GLOBALS['module_inc'] = sprintf("inc/modules/%s.php", getModule());
 
        // Check module permission (again)
        $moduleState = checkModulePermissions();
@@ -432,7 +435,7 @@ function loadModule () {
                        // Does the module exists on local file system?
                        if ((isIncludeReadable($GLOBALS['module_inc'])) && (!ifFatalErrorsDetected())) {
                                // Module is valid, active and located on the local disk...
-                               $isModuleValid = true;
+                               $isModuleValid = TRUE;
                        } elseif (!ifFatalErrorsDetected()) {
                                // Set HTTP status
                                setHttpStatus('404');
@@ -455,14 +458,14 @@ function loadModule () {
 
                case 'locked':
                        // Set HTTP status
-                       setHttpStatus('403 FORBIDDEN');
+                       setHttpStatus('403 Forbidden');
 
                        if (!isIncludeReadable($GLOBALS['module_inc'])) {
                                // Set HTTP status again
-                               setHttpStatus('404 NOT FOUND');
+                               setHttpStatus('404 Not Found');
 
                                // Module does addionally not exists
-                               addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
+                               addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_LOCKED_404--}');
                        } // END - if
 
                        // Add fatal message
@@ -489,7 +492,7 @@ function doIncludeModule () {
        loadIncludeOnce('inc/header.php');
 
        // Modules are by default not valid!
-       $isModuleValid = false;
+       $isModuleValid = FALSE;
 
        // By default NULL is used
        $GLOBALS['module_inc'] = NULL;
@@ -506,14 +509,51 @@ function doIncludeModule () {
                addFatalMessage(__FUNCTION__, __LINE__, '{--MYSQL_ERRORS--}');
        }
 
-       if (($isModuleValid === true) && (!is_null($GLOBALS['module_inc']))) {
+       // Is the module valid?
+       if (($isModuleValid === TRUE) && (!is_null($GLOBALS['module_inc']))) {
+               // Run pre-filter
+               runFilterChain('pre_module_load');
+
                // Everything is okay so we can load the module
                loadIncludeOnce($GLOBALS['module_inc']);
+
+               // Run post-filter
+               runFilterChain('post_module_load');
        } // END - if
 
-       // Add the footer (this will call shutdown())
+       // Add the footer (this will call doShutdown())
        loadIncludeOnce('inc/footer.php');
 }
 
+// "Getter" for menu mode from given module
+function getMenuModeFromModule () {
+       // Is cache set?
+       if (!isset($GLOBALS[__FUNCTION__])) {
+               // Default is 'noindex' which is invalid for SQL tables but okay for meta data template
+               $GLOBALS[__FUNCTION__] = 'noindex';
+
+               // Determine it hard-coded
+               if (getModule() == 'login') {
+                       // Is member area
+                       $GLOBALS[__FUNCTION__] = 'member';
+               } elseif (getModule() == 'index') {
+                       // Is guest area
+                       $GLOBALS[__FUNCTION__] = 'guest';
+               } elseif (getModule() == 'admin') {
+                       // Is admin area
+                       $GLOBALS[__FUNCTION__] = 'admin';
+               } elseif (isInstallationPhase()) {
+                       // Is installation phase
+                       $GLOBALS[__FUNCTION__] = 'install';
+               } else {
+                       // Get it from filter
+                       $GLOBALS[__FUNCTION__] = runFilterChain('determine_menu_mode');
+               }
+       } // END - if
+
+       // Return it
+       return $GLOBALS[__FUNCTION__];
+}
+
 // [EOF]
 ?>