Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / module-functions.php
index a102350bb853d169810af9b931d7a7373052746d..8f1f04968407fe6a00ad78b147c029d2c3cdfbeb 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Modulfunktionen                                  *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -46,7 +41,7 @@ function getModuleTitle ($module) {
        if (!isset($GLOBALS[__FUNCTION__][$module])) {
                // Init variables
                $data['title'] = '';
-               $result = false;
+               $result = FALSE;
 
                // Is the script installed?
                if ((isInstalled()) && ($module != 'error')) {
@@ -59,17 +54,17 @@ function getModuleTitle ($module) {
                                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",
+                               $result = sqlQueryEscaped("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) {
+                               if (sqlNumRows($result) == 1) {
                                        // Get the title from database
-                                       $data = SQL_FETCHARRAY($result);
+                                       $data = sqlFetchArray($result);
                                } // END - if
 
                                // Free the result
-                               SQL_FREERESULT($result);
+                               sqlFreeResult($result);
                        }
                } // END - if
 
@@ -81,11 +76,11 @@ function getModuleTitle ($module) {
                        // Is it 'error'?
                        if ($module == 'error') {
                                // Error (real module was not found)
-                               $data['title'] = '{--MODULE_ERROR_404--}';
+                               $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))) {
+                               if ((is_resource($result)) && (ifSqlHasZeroNumRows($result))) {
                                        // Add module to database and ignore return value
                                        checkModulePermissions($module);
                                } // END - if
@@ -116,7 +111,7 @@ function getModuleStatus ($module) {
        // Is the module_status entry there?
        if (!isModuleStatusSet($module)) {
                // Abort
-               reportBug('Module status not set. module=' . $module);
+               reportBug(__FUNCTION__, __LINE__, 'Module status not set. module=' . $module);
        } // END - if
 
        // Return it
@@ -126,7 +121,7 @@ function getModuleStatus ($module) {
 // 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')) {
@@ -137,7 +132,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');
@@ -145,11 +140,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 = sqlQueryEscaped("SELECT
+       `locked`,
+       `admin_only`,
+       `mem_only`
+FROM
+       `{?_MYSQL_PREFIX?}_mod_reg`
+WHERE
+       `module`='%s'
+LIMIT 1",
                        array($module), __FUNCTION__, __LINE__);
-               if (SQL_NUMROWS($result) == 1) {
+               if (sqlNumRows($result) == 1) {
                        // Read data
-                       $data = SQL_FETCHARRAY($result);
+                       $data = sqlFetchArray($result);
 
                        // Set all entries
                        foreach ($data as $key => $value) {
@@ -157,14 +160,14 @@ function isModuleRegistered ($module) {
                        } // END - foreach
 
                        // Mark as found
-                       $found = true;
+                       $found = TRUE;
                } elseif (isDebugModeEnabled()) {
                        // Debug message only in debug-mode...
                        logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found.');
                }
 
                // Free result
-               SQL_FREERESULT($result);
+               sqlFreeResult($result);
        }
 
        // Return status
@@ -180,15 +183,6 @@ function isModuleLocked ($module) {
        return $return;
 }
 
-// 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'));
-
-       // Return determined value
-       return $return;
-}
-
 // 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
@@ -241,7 +235,7 @@ function checkModulePermissions ($module = '') {
        setModuleStatus($module_chk, 'major');
 
        // Check if script is installed if not return a 'done' to prevent some errors
-       if ((isInstallationPhase()) || (!isAdminRegistered())) {
+       if ((isInstaller()) || (!isAdminRegistered())) {
                // Not installed or no admin registered or in installation phase
                setModuleStatus($module_chk, 'done');
 
@@ -253,7 +247,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? ---
@@ -271,10 +265,9 @@ function checkModulePermissions ($module = '') {
                        setModuleStatus($module_chk, 'admin_only');
                } else {
                        // @TODO Nothing helped???
-                       logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%d,hidden=%d,mem=%d,admin=%d",
+                       logDebugMessage(__FUNCTION__, __LINE__, sprintf('ret=%s,locked=%d,mem=%d,admin=%d',
                                getModuleStatus($module_chk),
                                intval(isModuleLocked($module_chk)),
-                               intval(isModuleHidden($module_chk)),
                                intval(isModuleMemberOnly($module_chk)),
                                intval(isModuleAdminOnly($module_chk))
                        ));
@@ -282,29 +275,29 @@ 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)))) {
+               if ((isIncludeReadable(sprintf('inc/modules/%s.php', $module))) || (isIncludeReadable($module . '.php')) || (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`)
+                               sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
+(`module`, `locked`, `mem_only`, `admin_only`, `has_menu`)
 VALUES
-('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
+('%s','Y'','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`)
+                               sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
+(`module`, `locked`, `mem_only`, `admin_only`)
 VALUES
-('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
+('%s','Y','N','N')", array($module_chk), __FUNCTION__, __LINE__);
                        }
 
                        // Everthing is fine?
-                       if (SQL_HASZEROAFFECTED()) {
+                       if (ifSqlHasZeroAffectedRows()) {
                                // Something bad happend!
                                setModuleStatus($module_chk, 'major');
                                return 'major';
@@ -326,13 +319,12 @@ 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",
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf('Problem in module %s detected. getModuleStatus()=%s,isModuleLocked()=%d,mem=%d,admin=%d,output_mode=%s',
                        $module_chk,
                        getModuleStatus($module_chk),
                        intval(isModuleLocked($module_chk)),
-                       intval(isModuleHidden($module_chk)),
                        intval(isModuleMemberOnly($module_chk)),
                        intval(isModuleAdminOnly($module_chk)),
                        getScriptOutputMode()
@@ -340,16 +332,16 @@ VALUES
        }
 
        // Debug log
-       //* DEBUG: */ 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')) {
@@ -366,15 +358,15 @@ 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",
+               $result = sqlQueryEscaped("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
                        array($module), __FUNCTION__, __LINE__);
 
                // Entry found?
-               if (SQL_NUMROWS($result) == 1) {
+               if (sqlNumRows($result) == 1) {
                        // Load "has_menu" column
-                       $data = SQL_FETCHARRAY($result);
+                       $data = sqlFetchArray($result);
 
                        // Fake cache... ;-)
                        $GLOBALS['cache_array']['extension']['ext_menu'][$module] = $data['has_menu'];
@@ -384,13 +376,13 @@ function ifModuleHasMenu ($module, $forceDb = false) {
                } // END  - if
 
                // Free memory
-               SQL_FREERESULT($result);
+               sqlFreeResult($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 (!isInstaller()) {
+               // Unsupported state, but ignored in installation phase
+               logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached, module[' . gettype($module) . ']=' . $module . ',forceDb=' . intval($forceDb));
        }
 
        // Return status
@@ -402,13 +394,12 @@ 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`,`title`,`locked`,`hidden`,`admin_only`,`mem_only`) VALUES ('" . $module . "','" . $title . "','" . $locked . "','" . $hidden . "','" . $adminOnly . "','" . $memOnly . "')");
+               addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `title`, `locked`, `admin_only`, `mem_only`) VALUES ('" . $module . "', '" . $title . "', '" . $locked . "', '" . $adminOnly . "', '" . $memOnly . "')");
        } else {
                // Already registered
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Already registered: module=%s,locked=%s,hidden=%s,admin=%s,mem=%s",
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf('Already registered: module=%s,locked=%s,admin=%s,mem=%s',
                        $module,
                        $locked,
-                       $hidden,
                        $adminOnly,
                        $memOnly
                ));
@@ -418,13 +409,10 @@ function addModuleSql ($module, $title, $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();
@@ -438,7 +426,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');
@@ -477,7 +465,7 @@ function loadModule () {
 
                default:
                        // Unknown module status
-                       logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown status %s return from module check. Module=%s", $moduleState, getModule()));
+                       logDebugMessage(__FUNCTION__, __LINE__, sprintf('Unknown status %s return from module check. Module=%s', $moduleState, getModule()));
                        addFatalMessage(__FUNCTION__, __LINE__, '{%message,UNKNOWN_MODULE_STATUS=' . $moduleState . '%}');
                        break;
        } // END - switch
@@ -491,11 +479,11 @@ function doIncludeModule () {
        // Set content type
        setContentType('text/html');
 
-       // The header file
-       loadIncludeOnce('inc/header.php');
+       // Load page header
+       loadPageHeader();
 
        // Modules are by default not valid!
-       $isModuleValid = false;
+       $isModuleValid = FALSE;
 
        // By default NULL is used
        $GLOBALS['module_inc'] = NULL;
@@ -504,7 +492,7 @@ function doIncludeModule () {
        if ((isExtensionActive('maintenance')) && (isMaintenanceEnabled()) && (!isAdmin()) && (getModule() != 'admin')) {
                // Maintain mode is active and you are no admin
                addFatalMessage(__FUNCTION__, __LINE__, '{--MAILER_DOWN_FOR_MAINTENANCE--}');
-       } elseif ((SQL_IS_LINK_UP()) && (!ifFatalErrorsDetected())) {
+       } elseif ((isSqlLinkUp()) && (!ifFatalErrorsDetected())) {
                // Do the small "load module" call
                $isModuleValid = loadModule();
        } elseif (!ifFatalErrorsDetected()) {
@@ -513,7 +501,7 @@ function doIncludeModule () {
        }
 
        // Is the module valid?
-       if (($isModuleValid === true) && (!is_null($GLOBALS['module_inc']))) {
+       if (($isModuleValid === TRUE) && (!is_null($GLOBALS['module_inc']))) {
                // Run pre-filter
                runFilterChain('pre_module_load');
 
@@ -525,7 +513,37 @@ function doIncludeModule () {
        } // END - if
 
        // Add the footer (this will call doShutdown())
-       loadIncludeOnce('inc/footer.php');
+       loadPageFooter();
+}
+
+// "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 (isInstaller()) {
+                       // 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]