]> 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 afed52db34fe7b0d169cc70be00ff33f03a5f30a..177da67eb57a0f78f35cc46720a4d8c639d89caf 100644 (file)
@@ -1024,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!
@@ -1038,7 +1045,7 @@ 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!');
+       /* 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;
 
@@ -1046,24 +1053,13 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
        $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);
@@ -1076,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()) {
@@ -1112,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(
@@ -1139,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)) {
@@ -1173,42 +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: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',addMode=' . $addMode . ' - EXIT!');
+       /* 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),
-                       bigintval($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
@@ -1871,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