Ticket resolved, code rewrites and caching:
[mailer.git] / inc / mysql-manager.php
index 9484506fc9896cfc92060df76c2e21ef76a5f9a3..4e10c8874056bca75b325df90ad6ea0926799ff0 100644 (file)
@@ -37,10 +37,13 @@ if (!defined('__SECURITY')) {
        require($INC);
 }
 
-//
-function ADD_MODULE_TITLE($mod) {
+// Returns the title for given module
+function ADD_MODULE_TITLE ($mod) {
        global $cacheArray;
-       $name = ""; $result = false;
+
+       // Init variables
+       $name = "";
+       $result = false;
 
        // Is the script installed?
        if (isBooleanConstantAndTrue('mxchange_installed')) {
@@ -65,7 +68,7 @@ function ADD_MODULE_TITLE($mod) {
        // Still no luck or empty title?
        if (empty($name)) {
                // No name found
-               $name = sprintf("%s (%s)", LANG_UNKNOWN_MODULE, $mod);
+               $name = sprintf("%s (%s)", getMessage('LANG_UNKNOWN_MODULE'), $mod);
                if (SQL_NUMROWS($result) == 0) {
                        // Add module to database
                        $dummy = CHECK_MODULE($mod);
@@ -133,7 +136,7 @@ function CHECK_MODULE ($mod) {
        }
 
        // Is the module found?
-       if ($found) {
+       if ($found === true) {
                // Check returned values against current access permissions
                //
                //  Admin access            ----- Guest access -----           --- Guest   or   member? ---
@@ -204,6 +207,7 @@ function CHECK_MODULE ($mod) {
 // Add menu description pending on given file name (without path!)
 function ADD_DESCR ($ACC_LVL, $file, $return = false, $output = true) {
        global $NAV_DEPTH;
+
        // Use only filename of the file ;)
        $file = basename($file);
 
@@ -322,6 +326,7 @@ function ADD_DESCR ($ACC_LVL, $file, $return = false, $output = true) {
                return $OUT;
        }
 }
+
 //
 function ADD_MENU ($MODE, $act, $wht) {
        // Init some variables
@@ -437,6 +442,7 @@ function ADD_MENU ($MODE, $act, $wht) {
                OUTPUT_HTML("</table>");
        }
 }
+
 // This patched function will reduce many SELECT queries for the specified or current admin login
 function IS_ADMIN ($admin="") {
        global $cacheArray;
@@ -458,14 +464,14 @@ function IS_ADMIN ($admin="") {
                // Use cached string
                $valPass = $cacheArray['admin_hash'];
        } elseif ((!empty($passCookie)) && (isset($cacheArray['admins']['password'][$admin])) && (!empty($admin))) {
-               // Count cache hits
-               incrementConfigEntry('cache_hits');
-
                // Login data is valid or not?
                $valPass = generatePassString($cacheArray['admins']['password'][$admin]);
 
                // Cache it away
                $cacheArray['admin_hash'] = $valPass;
+
+               // Count cache hits
+               incrementConfigEntry('cache_hits');
        } elseif ((!empty($admin)) && ((!EXT_IS_ACTIVE("cache"))) || (!isset($cacheArray['admins']['password'][$admin]))) {
                // Search for admin
                $result = SQL_QUERY_ESC("SELECT HIGH_PRIORITY password FROM `{!_MYSQL_PREFIX!}_admins` WHERE login='%s' LIMIT 1",
@@ -498,21 +504,18 @@ function IS_ADMIN ($admin="") {
        //* DEBUG: */ if (!$ret) echo __LINE__."OK!<br />";
        return $ret;
 }
-//
-function ADD_MAX_RECEIVE_LIST($MODE, $default="", $return=false)
-{
+
+// Generates a list of "max receiveable emails per day"
+function ADD_MAX_RECEIVE_LIST ($MODE, $default = "", $return = false) {
        global $_POST;
        $OUT = "";
-       switch ($MODE)
-       {
+       switch ($MODE) {
        case "guest":
                // Guests (in the registration form) are not allowed to select 0 mails per day.
                $result = SQL_QUERY("SELECT value, comment FROM `{!_MYSQL_PREFIX!}_max_receive` WHERE value > 0 ORDER BY value", __FILE__, __LINE__);
-               if (SQL_NUMROWS($result) > 0)
-               {
+               if (SQL_NUMROWS($result) > 0) {
                        $OUT = "";
-                       while (list($value, $comment) = SQL_FETCHROW($result))
-                       {
+                       while (list($value, $comment) = SQL_FETCHROW($result)) {
                                $OUT .= "      <option value=\"".$value."\"";
                                if ($_POST['max_mails'] == $value) $OUT .= " selected=\"selected\"";
                                $OUT .= ">".$value." ".PER_DAY;
@@ -524,21 +527,18 @@ function ADD_MAX_RECEIVE_LIST($MODE, $default="", $return=false)
                        // Free memory
                        SQL_FREERESULT($result);
                        $OUT = LOAD_TEMPLATE("guest_receive_table", true);
-               }
-                else
-               {
+               } else {
                        // Maybe the admin has to setup some maximum values?
+                       debug_report_bug("Nothing is being done here?");
                }
                break;
 
        case "member":
                // Members are allowed to set to zero mails per day (we will change this soon!)
                $result = SQL_QUERY("SELECT value, comment FROM `{!_MYSQL_PREFIX!}_max_receive` ORDER BY value", __FILE__, __LINE__);
-               if (SQL_NUMROWS($result) > 0)
-               {
+               if (SQL_NUMROWS($result) > 0) {
                        $OUT = "";
-                       while (list($value, $comment) = SQL_FETCHROW($result))
-                       {
+                       while (list($value, $comment) = SQL_FETCHROW($result)) {
                                $OUT .= "      <option value=\"".$value."\"";
                                if ($default == $value) $OUT .= " selected=\"selected\"";
                                $OUT .= ">".$value." ".PER_DAY;
@@ -548,53 +548,75 @@ function ADD_MAX_RECEIVE_LIST($MODE, $default="", $return=false)
                        define('__MAX_RECEIVE_OPTIONS', $OUT);
                        SQL_FREERESULT($result);
                        $OUT = LOAD_TEMPLATE("member_receive_table", true);
-               }
-                else
-               {
+               } else {
                        // Maybe the admin has to setup some maximum values?
                        $OUT = LOAD_TEMPLATE("admin_settings_saved", true, NO_MAX_VALUES);
                }
                break;
        }
-       if ($return)
-       {
+
+       if ($return) {
                // Return generated HTML code
                return $OUT;
-       }
-        else
-       {
+       } else {
                // Output directly (default)
                OUTPUT_HTML($OUT);
        }
 }
-//
-function SEARCH_EMAIL_USERTAB($email)
-{
-       $ret = false;
-       $result = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE email LIKE '{PER}%s{PER}' LIMIT 1", array($email), __FILE__, __LINE__);
-       if (SQL_NUMROWS($result) == 1) $ret = true;
+
+// Checks wether the given email address is used.
+function SEARCH_EMAIL_USERTAB ($email) {
+       // Query the database
+       $result = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE email LIKE '{PER}%s{PER}' LIMIT 1",
+               array($email), __FILE__, __LINE__);
+
+       // Is the email there?
+       $ret = (SQL_NUMROWS($result) == 1);
+
+       // Free the result
        SQL_FREERESULT($result);
+
+       // Return result
        return $ret;
 }
-//
-function WHAT_IS_VALID($act, $wht, $type="guest")
-{
-       if (IS_ADMIN())
-       {
+
+// Checks wether 'what' is valid
+// @TODO This cache can be flushed to hard drive
+function WHAT_IS_VALID ($act, $wht, $type="guest") {
+       global $cacheArray;
+
+       // Is admin or cache entry is there?
+       if (IS_ADMIN()) {
                // Everything is valid to the admin :-)
-               return true;
-       }
-        else
-       {
+               $ret = true;
+       } elseif (isset($cacheArray['what_valid'][$type][$act][$what])) {
+               // Use the cache
+               $ret = $cacheArray['what_valid'][$type][$act][$what];
+
+               // Count the cache hit
+               incrementConfigEntry('cache_hits');
+       } else {
+               // By default is nothing valid
                $ret = false;
+
+               // Look in database
                $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_%s_menu` WHERE action='%s' AND what='%s' AND `locked`='N' LIMIT 1", array($type, $act, $wht), __FILE__, __LINE__);
+
                // Is "what" valid?
-               if (SQL_NUMROWS($result) == 1) $ret = true;
+               $ret = (SQL_NUMROWS($result) == 1);
+
+               // Free the result
                SQL_FREERESULT($result);
-               return $ret;
        }
+
+       // Cache the entry
+       $cacheArray['what_valid'][$type][$act][$what] = $ret;
+
+       // Return the result
+       return $ret;
 }
-//
+
+// Checks wether the current user is a member
 function IS_MEMBER () {
        global $status, $LAST, $cacheArray;
        if (!is_array($LAST)) $LAST = array();
@@ -657,12 +679,16 @@ function IS_MEMBER () {
        // Return status
        return $ret;
 }
-//
-function VALIDATE_MENU_ACTION ($MODE, $act, $wht, $UPDATE=false)
-{
+
+// Validate the given menu action
+function VALIDATE_MENU_ACTION ($MODE, $act, $wht, $UPDATE=false) {
+       // By default nothing is valid
        $ret = false;
+
+       // Look in all menus or only unlocked
        $ADD = "";
        if ((!IS_ADMIN()) && ($MODE != "admin")) $ADD = " AND `locked`='N'";
+
        //* DEBUG: */ echo __LINE__.":".$MODE."/".$act."/".$wht."*<br />\n";
        if (($MODE != "admin") && ($UPDATE)) {
                // Update guest or member menu