isAdmin() uses now for 'caching' its result
[mailer.git] / inc / mysql-manager.php
index 311fdb0a19483dedccb8b8e199d737f1974f5084..9c5f649dd89bfdec34f27bdb74296cc5fc6bbed9 100644 (file)
@@ -641,7 +641,7 @@ function isMember () {
 function isAdmin ($admin = '') {
        // Init variables
        $ret = false; $passCookie = ''; $valPass = '';
-       //* DEBUG: */ print(__LINE__."ADMIN:".$admin."<br />");
+       //* DEBUG: */ print(__FUNCTION__.':'.$admin.'<br />');
 
        // If admin login is not given take current from cookies...
        if ((empty($admin)) && (isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5'))) {
@@ -649,51 +649,54 @@ function isAdmin ($admin = '') {
                $admin = getSession('admin_login');
                $passCookie = getSession('admin_md5');
        } // END - if
-       //* DEBUG: */ print(__LINE__."ADMIN:".$admin.'/'.$passCookie."<br />");
+       //* DEBUG: */ print(__FUNCTION__.':'.$admin.'/'.$passCookie.'<br />');
 
-       // Search in array for entry
-       if (isset($GLOBALS['admin_hash'])) {
-               // Use cached string
-               $valPass = $GLOBALS['admin_hash'];
-       } elseif ((!empty($passCookie)) && (isAdminHashSet($admin) === true) && (!empty($admin))) {
-               // Login data is valid or not?
-               $valPass = generatePassString(getAdminHash($admin));
+       // Do we have cache?
+       if (!isset($GLOBALS['is_admin'][$admin]
+               // Search in array for entry
+               if (isset($GLOBALS['admin_hash'])) {
+                       // Use cached string
+                       $valPass = $GLOBALS['admin_hash'];
+               } elseif ((!empty($passCookie)) && (isAdminHashSet($admin) === true) && (!empty($admin))) {
+                       // Login data is valid or not?
+                       $valPass = generatePassString(getAdminHash($admin));
+
+                       // Cache it away
+                       $GLOBALS['admin_hash'] = $valPass;
+
+                       // Count cache hits
+                       incrementStatsEntry('cache_hits');
+               } elseif ((!empty($admin)) && ((!isExtensionActive('cache'))) || (isAdminHashSet($admin) === false)) {
+                       // Search for admin
+                       $result = SQL_QUERY_ESC("SELECT HIGH_PRIORITY `password` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `login`='%s' LIMIT 1",
+                               array($admin), __FUNCTION__, __LINE__);
 
-               // Cache it away
-               $GLOBALS['admin_hash'] = $valPass;
+                       // Is he admin?
+                       $passDB = '';
+                       if (SQL_NUMROWS($result) == 1) {
+                               // Admin login was found so let's load password from DB
+                               list($passDB) = SQL_FETCHROW($result);
 
-               // Count cache hits
-               incrementStatsEntry('cache_hits');
-       } elseif ((!empty($admin)) && ((!isExtensionActive('cache'))) || (isAdminHashSet($admin) === false)) {
-               // Search for admin
-               $result = SQL_QUERY_ESC("SELECT HIGH_PRIORITY `password` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `login`='%s' LIMIT 1",
-                       array($admin), __FUNCTION__, __LINE__);
+                               // Temporary cache it
+                               setAdminHash($admin, $passDB);
 
-               // Is he admin?
-               $passDB = '';
-               if (SQL_NUMROWS($result) == 1) {
-                       // Admin login was found so let's load password from DB
-                       list($passDB) = SQL_FETCHROW($result);
+                               // Generate password hash
+                               $valPass = generatePassString($passDB);
+                       } // END - if
 
-                       // Temporary cache it
-                       setAdminHash($admin, $passDB);
+                       // Free memory
+                       SQL_FREERESULT($result);
+               }
 
-                       // Generate password hash
-                       $valPass = generatePassString($passDB);
+               if (!empty($valPass)) {
+                       // Check if password is valid
+                       //* DEBUG: */ print(__FUNCTION__ . ':(' . $valPass . '==' . $passCookie . ')='.intval($valPass == $passCookie).'<br />');
+                       $GLOBALS['is_admin'][$admin] = (($valPass == $passCookie) || ((strlen($valPass) == 32) && ($valPass == md5($passCookie))) || (($valPass == '*FAILED*') && (!isExtensionActive('cache'))));
                } // END - if
-
-               // Free memory
-               SQL_FREERESULT($result);
-       }
-
-       if (!empty($valPass)) {
-               // Check if password is valid
-               //* DEBUG: */ print(__FUNCTION__ . ':' . $valPass . '/' . $passCookie . '<br />');
-               $ret = (($valPass == $passCookie) || ((strlen($valPass) == 32) && ($valPass == md5($passCookie))) || (($valPass == '*FAILED*') && (!isExtensionActive('cache'))));
        } // END - if
 
        // Return result of comparision
-       return $ret;
+       return $GLOBALS['is_admin'][$admin];
 }
 
 // Generates a list of "max receiveable emails per day"
@@ -965,7 +968,7 @@ function getModeAction ($mode, $what) {
                // Get it directly from URL
                return getAction();
        }
-       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ret=".$ret."<br />");
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ret=".$ret.'<br />');
 
        // Does the module have a menu?
        if (ifModuleHasMenu($mode)) {
@@ -1343,7 +1346,7 @@ function updateReferalCounter ($userid) {
                array(bigintval($userid), $GLOBALS['cache_array']['ref_level'][$userid]), __FUNCTION__, __LINE__);
 
        // When no entry was updated then we have to create it here
-       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):updated=".SQL_AFFECTEDROWS()."<br />");
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):updated=".SQL_AFFECTEDROWS().'<br />');
        if (SQL_AFFECTEDROWS() < 1) {
                // First count!
                SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
@@ -1641,11 +1644,17 @@ function FILTER_ACTIVATE_EXCHANGE () {
 // Deletes a user account with given reason
 function deleteUserAccount ($userid, $reason) {
        $points = 0;
-       $result = SQL_QUERY_ESC("SELECT (SUM(p.points) - d.used_points) AS points
-FROM `{?_MYSQL_PREFIX?}_user_points` AS p
-LEFT JOIN `{?_MYSQL_PREFIX?}_user_data` AS d
-ON p.userid=d.userid
-WHERE p.userid=%s", array(bigintval($userid)), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT
+       (SUM(p.points) - d.used_points) AS points
+FROM
+       `{?_MYSQL_PREFIX?}_user_points` AS p
+LEFT JOIN
+       `{?_MYSQL_PREFIX?}_user_data` AS d
+ON
+       p.userid=d.userid
+WHERE
+       p.userid=%s",
+               array(bigintval($userid)), __FUNCTION__, __LINE__);
        if (SQL_NUMROWS($result) == 1) {
                // Save his points to add them to the jackpot
                list($points) = SQL_FETCHROW($result);
@@ -1718,7 +1727,14 @@ function getWhatFromModule ($modCheck) {
 
                case 'login':
                case 'index':
-                       $what = getConfig('index_home');
+                       // Is ext-sql_patches installed and newer than 0.0.5?
+                       if (isExtensionInstalledAndNewer('sql_patches', '0.0.5')) {
+                               // Use it from config
+                               $what = getConfig('index_home');
+                       } else {
+                               // Use default 'welcome'
+                               $what = 'welcome';
+                       }
                        break;
 
                default:
@@ -1906,7 +1922,7 @@ VALUES ('%s','%s','%s','%s','%s','NEW', UNIX_TIMESTAMP(),'%s','%s','%s','%s')",
 // Generate a receiver list for given category and maximum receivers
 function generateReceiverList ($cat, $receiver, $mode = '') {
        // Init variables
-       $CAT_TABS     = "%s";
+       $CAT_TABS     = '';
        $CAT_WHERE    = '';
        $receiverList = '';
        $result       = false;
@@ -1925,7 +1941,7 @@ function generateReceiverList ($cat, $receiver, $mode = '') {
        if ($cat > 0) {
                // Select category
                $CAT_TABS  = "LEFT JOIN `{?_MYSQL_PREFIX?}_user_cats` AS c ON d.userid=c.userid";
-               $CAT_WHERE = " AND c.cat_id=%s";
+               $CAT_WHERE = sprintf(" AND c.cat_id=%s", $cat);
        } // END - if
 
        // Exclude users in holiday?
@@ -1938,14 +1954,12 @@ function generateReceiverList ($cat, $receiver, $mode = '') {
                // Only include HTML receivers
                $result = SQL_QUERY_ESC("SELECT d.userid FROM `{?_MYSQL_PREFIX?}_user_data` AS d ".$CAT_TABS." WHERE d.`status`='CONFIRMED' AND d.`html`='Y'".$CAT_WHERE." ORDER BY d.{?order_select?} {?order_mode?} LIMIT %s",
                        array(
-                               $cat,
                                $receiver
                        ), __FUNCTION__, __LINE__);
        } else {
                // Include all
                $result = SQL_QUERY_ESC("SELECT d.userid FROM `{?_MYSQL_PREFIX?}_user_data` AS d ".$CAT_TABS." WHERE d.`status`='CONFIRMED'".$CAT_WHERE." ORDER BY d.{?order_select?} {?order_mode?} LIMIT %s",
                        array(
-                               $cat,
                                $receiver
                        ), __FUNCTION__, __LINE__);
        }