]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
Fixes for referal system, shell scripts overworked:
[mailer.git] / inc / mysql-manager.php
index 4b9ee0e0138ad015586eaf8baec4091bb696f930..177da67eb57a0f78f35cc46720a4d8c639d89caf 100644 (file)
@@ -98,13 +98,16 @@ function addYouAreHereLink ($accessLevel, $FQFN, $return = false) {
                // This is a 'what file'!
                $type = 'what';
                $search = substr($file, 5);
-               $ADD = " AND `visible`='Y' AND `locked`='N'";
 
                // Get access level from it
                $modCheck = getModuleFromFileName($file, $accessLevel);
 
                // Do we have admin? Then display all
-               if (isAdmin()) $ADD = '';
+               $ADD = " AND `visible`='Y' AND `locked`='N'";
+               if (isAdmin()) {
+                       // Display all!
+                       $ADD = '';
+               } // END - if
 
                $dummy = substr($search, 0, -4);
                $ADD .= sprintf(" AND `action`='%s'", getActionFromModuleWhat($accessLevel, $dummy));
@@ -260,7 +263,7 @@ ORDER BY
                                                $OUT .= '<a name="menu" class="menu_blur" href="{%url=modules.php?module=' . getModule() . '&amp;what=' . $content['sub_what'] . '%}" target="_self">';
                                        } else {
                                                // Not found - open
-                                               $OUT .= '<em style="cursor:help" class="notice" title="{%message,ADMIN_MENU_WHAT_404=' . $content['sub_what'] . '%}">';
+                                               $OUT .= '<em style="cursor:help" class="notice" title="{%message,ADMIN_MENU_WHAT_404_TITLE=' . $content['sub_what'] . '%}">';
                                        }
 
                                        // Menu title
@@ -921,7 +924,7 @@ function removeReceiver (&$receivers, $key, $userid, $pool_id, $stats_id = 0, $i
 }
 
 // Calculate sum (default) or count records of given criteria
-function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatement = 'userid', $countRows = false, $add = '') {
+function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatement = 'userid', $countRows = false, $add = '', $mode = '=') {
        // Init count/sum
        $data['res'] = '0';
 
@@ -931,22 +934,40 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
                if ($countRows === true) {
                        // Count whole table
                        $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`".$add,
-                               array($lookFor, $tableName), __FUNCTION__, __LINE__);
+                               array(
+                                       $lookFor,
+                                       $tableName
+                               ), __FUNCTION__, __LINE__);
                } else {
                        // Sum whole table
                        $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`".$add,
-                               array($lookFor, $tableName), __FUNCTION__, __LINE__);
+                               array(
+                                       $lookFor,
+                                       $tableName
+                               ), __FUNCTION__, __LINE__);
                }
        } elseif (($countRows === true) || ($lookFor == 'userid')) {
                // Count rows
                //* DEBUG: */ debugOutput('COUNT!');
-               $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s'".$add,
-                       array($lookFor, $tableName, $whereStatement, $search), __FUNCTION__, __LINE__);
+               $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'".$add,
+                       array(
+                               $lookFor,
+                               $tableName,
+                               $whereStatement,
+                               $mode,
+                               $search
+                       ), __FUNCTION__, __LINE__);
        } else {
                // Add all rows
                //* DEBUG: */ debugOutput('SUM!');
-               $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s'".$add,
-                       array($lookFor, $tableName, $whereStatement, $search), __FUNCTION__, __LINE__);
+               $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'".$add,
+                       array(
+                               $lookFor,
+                               $tableName,
+                               $whereStatement,
+                               $mode,
+                               $search
+                       ), __FUNCTION__, __LINE__);
        }
 
        // Load row
@@ -986,7 +1007,7 @@ function getReferalLevelPercents ($level) {
                } // END - if
        } elseif (!isExtensionActive('cache')) {
                // Get referal data
-               $result_level = SQL_QUERY_ESC("SELECT `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level`='%s' LIMIT 1",
+               $result_level = SQL_QUERY_ESC("SELECT `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level`=%s LIMIT 1",
                        array(bigintval($level)), __FUNCTION__, __LINE__);
 
                // Entry found?
@@ -1003,6 +1024,13 @@ function getReferalLevelPercents ($level) {
        return $data['percents'];
 }
 
+// Initializes the referal system
+function initReferalSystem () {
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal system initialized!');
+       $GLOBALS['ref_level']  = NULL;
+       $GLOBALS['ref_system'] = true;
+}
+
 /**
  *
  * Dynamic referal and points system, can also send mails!
@@ -1017,32 +1045,21 @@ function getReferalLevelPercents ($level) {
  *               for default value will cause no referal will get points ever!!!)
  */
 function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify = false, $refid = '0', $addMode = 'REFERAL') {
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',addMode=' . $addMode . ' - ENTERED!');
        // By default nothing has been added
        $added = false;
 
-       //* DEBUG: */ debugOutput('----------------------- <font color="#00aa00">' . __FUNCTION__ . ' - ENTRY</font> ------------------------<ul><li>');
        // Convert mode to upper-case
        $addMode = strtoupper($addMode);
 
        // When $userid = '0' add points to jackpot
-       if (($userid == '0') && (isExtensionActive('jackpot'))) {
-               // Add points to jackpot
+       if (($userid == '0') && ($addMode == 'DIRECT') && (isExtensionActive('jackpot'))) {
+               // Add points to jackpot only in DIRECT mode
                return addPointsToJackpot($points);
        } // END - if
 
-       // Count up referal depth
-       if (!isset($GLOBALS['ref_level'])) {
-               // Initialialize referal system
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal system initialized!');
-               $GLOBALS['ref_level'] = NULL;
-       } else {
-               // Increase referal level
-               $GLOBALS['ref_level']++;
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased. DEPTH='.$GLOBALS['ref_level']);
-       }
-
        // Check user account
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points);
        if (fetchUserData($userid)) {
                // Determine wether the user has some mails to click before he/she gets the points
                $locked = ifUserPointsLocked($userid);
@@ -1055,31 +1072,43 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
 
                // Get percents
                $per = getReferalLevelPercents($GLOBALS['ref_level']);
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$addMode);
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$addMode);
 
                // Some percents found?
                if ($per > 0) {
                        // Calculate new points
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',per='.$per.',depth='.$GLOBALS['ref_level']);
                        $ref_points = $points * $per / 100;
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',per='.$per.',depth='.$GLOBALS['ref_level'].',ref_points='.$ref_points);
 
                        // Pay refback here if level > 0 and in ref-mode
                        if ((isExtensionActive('refback')) && ($GLOBALS['ref_level'] > 0) && ($per < 100) && ($addMode == 'REFERAL') && (isset($GLOBALS['cache_array']['add_userid'][$userid]))) {
-                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - BEFORE!');
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - BEFORE!');
                                $ref_points = addRefbackPoints($GLOBALS['cache_array']['add_userid'][$userid], $userid, $points, $ref_points);
-                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - AFTER!');
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - AFTER!');
                        } // END - if
 
                        // Update points...
-                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth`=%s LIMIT 1",
-                               array(
-                                       $pointsColumn,
-                                       $pointsColumn,
-                                       $ref_points,
-                                       bigintval($userid),
-                                       makeZeroToNull($GLOBALS['ref_level'])
-                               ), __FUNCTION__, __LINE__);
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$addMode.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
+                       if (is_null($GLOBALS['ref_level'])) {
+                               // Level NULL (self)
+                               SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth` IS NULL LIMIT 1",
+                                       array(
+                                               $pointsColumn,
+                                               $pointsColumn,
+                                               $ref_points,
+                                               bigintval($userid)
+                                       ), __FUNCTION__, __LINE__);
+                       } else {
+                               // Level 1+
+                               SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth`=%s LIMIT 1",
+                                       array(
+                                               $pointsColumn,
+                                               $pointsColumn,
+                                               $ref_points,
+                                               bigintval($userid),
+                                               bigintval($GLOBALS['ref_level'])
+                                       ), __FUNCTION__, __LINE__);
+                       }
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$addMode.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
 
                        // No entry updated?
                        if (SQL_HASZEROAFFECTED()) {
@@ -1091,12 +1120,12 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                                                makeZeroToNull($GLOBALS['ref_level']),
                                                $ref_points
                                        ), __FUNCTION__, __LINE__);
-                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$addMode.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$addMode.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
                        } // END - if
 
                        // Check affected rows
                        $added = SQL_AFFECTEDROWS();
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
 
                        // Prepare data for the filter
                        $filterData = array(
@@ -1118,7 +1147,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
 
                        // Extract $added
                        $added = $filterData['added'];
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
 
                        // Points updated, maybe I shall send him an email?
                        if (($sendNotify === true) && (isValidUserId(getUserData('refid'))) && ($locked === false)) {
@@ -1152,39 +1181,51 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                                } // END - if
                        }
 
+                       // Increase referal level
+                       $GLOBALS['ref_level']++;
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased, ref_level=' . $GLOBALS['ref_level']);
+
                        // Maybe there's another ref?
                        if ((isValidUserId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid) && ($addMode == 'REFERAL')) {
                                // Then let's credit him here...
-                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',ref='.getUserData('refid').',points='.$points.' - ADVANCE!');
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . getUserData('refid') . ',points=' . $points . ',ref_points=' . $ref_points . ' - ADVANCE!');
                                $added = ($added && addPointsThroughReferalSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, $sendNotify, getUserData('refid')));
                        } // END - if
                } // END - if
        } // END - if
 
-       //* DEBUG: */ debugOutput('</li></ul>----------------------- <font color="#aa0000">'.__FUNCTION__.': added=' . intval($added) . ' - EXIT</font> ------------------------<br />');
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',addMode=' . $addMode . ' - EXIT!');
        return $added;
 }
 
 // Updates the referal counter
 function updateReferalCounter ($userid) {
        // Make it sure referal level zero (member him-/herself) is at least selected
-       if (empty($GLOBALS['cache_array']['ref_level'][$userid])) $GLOBALS['cache_array']['ref_level'][$userid] = 1;
+       if (empty($GLOBALS['cache_array']['ref_level'][$userid])) {
+               $GLOBALS['cache_array']['ref_level'][$userid] = NULL;
+       } // END - if
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',level='.$GLOBALS['cache_array']['ref_level'][$userid]);
 
        // Update counter
-       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET `counter`=`counter`+1 WHERE `userid`=%s AND `level`='%s' LIMIT 1",
-               array(bigintval($userid), $GLOBALS['cache_array']['ref_level'][$userid]), __FUNCTION__, __LINE__);
-
-       // When no entry was updated then we have to create it here
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updated=' . SQL_AFFECTEDROWS());
-       if (SQL_HASZEROAFFECTED()) {
-               // First count!
-               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
+       if (!is_null($GLOBALS['cache_array']['ref_level'][$userid])) {
+               // Level is > 0
+               SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET `counter`=`counter`+1 WHERE `userid`=%s AND `level`=%s LIMIT 1",
                        array(
-                               bigintval($userid),
-                               $GLOBALS['cache_array']['ref_level'][$userid]
+                                       bigintval($userid),
+                               bigintval($GLOBALS['cache_array']['ref_level'][$userid])
                        ), __FUNCTION__, __LINE__);
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid);
+
+               // When no entry was updated then we have to create it here
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updated=' . SQL_AFFECTEDROWS());
+               if (SQL_HASZEROAFFECTED()) {
+                       // First count!
+                       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
+                               array(
+                                       bigintval($userid),
+                                       makeZeroToNull($GLOBALS['cache_array']['ref_level'][$userid])
+                               ), __FUNCTION__, __LINE__);
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS());
+               } // END - if
        } // END - if
 
        // Init referal id
@@ -1847,7 +1888,9 @@ function generateReceiverList ($categoryId, $receiver, $mode = '') {
                // Load all entries
                while ($content = SQL_FETCHARRAY($result)) {
                        // Add receiver when not empty
-                       if (!empty($content['userid'])) $receiverList .= $content['userid'] . ';';
+                       if (!empty($content['userid'])) {
+                               $receiverList .= $content['userid'] . ';';
+                       } // END - if
                } // END - while
 
                // Free memory
@@ -1863,33 +1906,29 @@ function generateReceiverList ($categoryId, $receiver, $mode = '') {
 
 // "Getter" for array for user refs and points in given level
 function getUserReferalPoints ($userid, $level) {
-       //* DEBUG: */ debugOutput('----------------------- <font color="#00aa00">'.__FUNCTION__.' - ENTRY</font> ------------------------<ul><li>');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!');
        // Default is no refs and no nickname
-       $add = '';
        $refs = array();
 
-       // Do we have nickname extension installed?
-       if (isExtensionActive('nickname')) {
-               $add = ', ud.nickname';
-       } // END - if
-
        // Get refs from database
        $result = SQL_QUERY_ESC("SELECT
-       ur.id, ur.refid, ud.status, ud.last_online, ud.mails_confirmed, ud.emails_received".$add."
+       ur.`id`, ur.`refid`, ud.`status`, ud.`last_online`, ud.`mails_confirmed`, ud.`emails_received`
 FROM
        `{?_MYSQL_PREFIX?}_user_refs` AS ur
 LEFT JOIN
        `{?_MYSQL_PREFIX?}_user_points` AS up
 ON
-       ur.refid=up.userid AND ur.level=0
+       ur.refid=up.userid AND
+       (ur.level=0 OR ur.level IS NULL)
 LEFT JOIN
        `{?_MYSQL_PREFIX?}_user_data` AS ud
 ON
-       ur.refid=ud.userid
+       ur.`refid`=ud.`userid`
 WHERE
-       ur.userid=%s AND ur.level=%s
+       ur.`userid`=%s AND
+       ur.`level`=%s
 ORDER BY
-       ur.refid ASC",
+       ur.`refid` ASC",
                array(
                        bigintval($userid),
                        bigintval($level)
@@ -1937,7 +1976,7 @@ ORDER BY
        SQL_FREERESULT($result);
 
        // Return result
-       //* DEBUG: */ debugOutput('</li></ul>----------------------- <font color="#aa0000">'.__FUNCTION__.' - EXIT</font> ------------------------<br />');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - EXIT!');
        return $refs;
 }
 
@@ -1975,7 +2014,7 @@ function reduceRecipientReceivedMails ($column, $id, $count) {
                                array(implode(',', $userids), count($userids)), __FUNCTION__, __LINE__);
                } else {
                        // Nothing deleted
-                       displayMessage(getMaskedMessage('ADMIN_MAIL_NOTHING_DELETED', $id));
+                       displayMessage('{%message,ADMIN_MAIL_NOTHING_DELETED=' . $id . '%}');
                }
        } // END - if