]> git.mxchange.org Git - mailer.git/blobdiff - inc/referal-functions.php
A lot has been rewritten, ext-teams added, ext-forced continued:
[mailer.git] / inc / referal-functions.php
index 8868904eaddc30140d39e89c31f45f0dfbe77ccb..636d88aacbbedf138eac9c5a1bc118ca9777f5ef 100644 (file)
@@ -83,17 +83,15 @@ function getReferalLevelPercents ($level) {
 }
 
 /**
- *
  * Dynamic referal and points system, can also send mails!
  *
  * subject       = Subject line, write in lower-case letters and underscore is allowed
  * userid        = Referal id wich should receive...
  * points        = ... xxx points
  * refid         = inc/modules/guest/what-confirm.php need this
- * locked        = Shall I pay it to normal (false) or locked (true) points ammount?
  */
 function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NULL) {
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . $refid . ' - ENTERED!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . makeNullToZero($refid) . ' - ENTERED!');
        // By default nothing has been added
        $added = false;
 
@@ -101,14 +99,17 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NUL
        $paymentMethod = strtoupper(getPaymentMethodFromSubject($subject));
        $sendNotify    = isPaymentRecipientNotificationEnabled($subject);
 
-       // When $userid = '0' add points to jackpot
-       if (($userid == '0') && ($paymentMethod == 'DIRECT') && (isExtensionActive('jackpot'))) {
+       // When $userid is NULL add points to jackpot
+       if ((!isValidUserId($userid)) && ($paymentMethod == 'DIRECT') && (isExtensionActive('jackpot'))) {
                // Add points to jackpot only in DIRECT mode
                return addPointsToJackpot($points);
        } // END - if
 
+       // Set userid as current
+       setCurrentUserId($userid);
+
        // Check user account
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',points=' . $points);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',points=' . $points . ',paymentMethod=' . $paymentMethod . ',sendNotify=' . intval($sendNotify));
        if (fetchUserData($userid)) {
                // Determine wether the user has some mails to click before he/she gets the points
                $locked = ifUserPointsLocked($userid);
@@ -116,24 +117,21 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NUL
                // Detect database column
                $pointsColumn = determinePointsColumnFromSubjectLocked($subject, $locked);
 
-               // This is the user and his ref
-               $GLOBALS['cache_array']['add_userid'][getUserData('refid')] = $userid;
-
                // Get percents
                $per = getReferalLevelPercents($GLOBALS['ref_level']);
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$paymentMethod);
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',points=' . $points . ',depth=' . makeNullToZero($GLOBALS['ref_level']) . ',per=' . $per . ',mode=' . $paymentMethod . ',pointsColumn=' . $pointsColumn . ',locked=' . intval($locked) . ',refid=' . getUserData('refid'));
 
                // Some percents found?
                if ($per > 0) {
                        // Calculate new points
                        $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) && ($paymentMethod == '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!');
-                               $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 . ',refid=' . makeNullToZero(getUserData('refid')) . ',points=' . $points . ',paymentMethod=' . $paymentMethod);
+                       if (($userid != $refid) && ($paymentMethod == 'REFERAL') && (isValidUserId(getUserData('refid'))) && (isExtensionActive('refback'))) {
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . makeNullToZero(getUserData('refid')) . ',ref_points=' . $ref_points . ',depth=' . makeNullToZero($GLOBALS['ref_level']) . ' - BEFORE!');
+                               $ref_points = addRefbackPoints($userid, getUserData('refid'), $points, $ref_points);
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . makeNullToZero(getUserData('refid')) . ',ref_points=' . $ref_points . ',depth=' . makeNullToZero($GLOBALS['ref_level']) . ' - AFTER!');
                        } // END - if
 
                        // Update points...
@@ -157,24 +155,24 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NUL
                                                bigintval($GLOBALS['ref_level'])
                                        ), __FUNCTION__, __LINE__);
                        }
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.makeNullToZero($GLOBALS['ref_level']).',mode='.$paymentMethod.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
 
                        // No entry updated?
                        if (SQL_HASZEROAFFECTED()) {
                                // First ref in this level! :-)
-                               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `%s`) VALUES (%s, %s, %s)",
+                               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`,`ref_depth`,`%s`) VALUES (%s, %s, %s)",
                                        array(
                                                $pointsColumn,
                                                bigintval($userid),
                                                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='.$paymentMethod.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.makeNullToZero($GLOBALS['ref_level']).',mode='.$paymentMethod.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
                        } // END - if
 
                        // Check affected rows
-                       $added = SQL_AFFECTEDROWS();
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
+                       $added = (SQL_AFFECTEDROWS() == 1);
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added=' . intval($added) . ' - BEFORE FILTER');
 
                        // Prepare data for the filter
                        $filterData = array(
@@ -196,7 +194,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NUL
 
                        // Extract $added
                        $added = $filterData['added'];
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added=' . intval($added) . ' - AFTER FILTER');
 
                        // Points updated, maybe I shall send him an email?
                        if (($sendNotify === true) && (isValidUserId(getUserData('refid'))) && ($locked === false)) {
@@ -231,20 +229,27 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NUL
                                } // END - if
                        }
 
-                       // Increase referal level
-                       $GLOBALS['ref_level']++;
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased, ref_level=' . $GLOBALS['ref_level']);
+                       // Increase referal level, if payment method is REFERAL
+                       if ($paymentMethod == 'REFERAL') {
+                               // Increase it
+                               $GLOBALS['ref_level']++;
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Referal level increased, ref_level=' . makeNullToZero($GLOBALS['ref_level']) . ',points=' . $points . ',refid=' . makeNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod);
+                       } elseif (isDebugModeEnabled()) {
+                               // Not increasing referal level, DIRECT payment method
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Referal level *NOT* increased, ref_level=' . makeNullToZero($GLOBALS['ref_level']) . ',points=' . $points . ',refid=' . makeNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod);
+                       }
 
                        // Maybe there's another ref?
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'points=' . $points . ',refid(var|data)=' . makeNullToZero($refid) . '|' . makeNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod);
                        if ((isValidUserId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid) && ($paymentMethod == 'REFERAL')) {
                                // Then let's credit him here...
-                               //* 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, getUserData('refid')));
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . makeNullToZero(getUserData('refid')) . ',points=' . $points . ',ref_points=' . $ref_points . ',added[' . gettype($added) . ']=' . intval($added) . ' - ADVANCE!');
+                               $added = ($added && addPointsThroughReferalSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, getFetchedUserData('userid', getUserData('refid'), 'refid')));
                        } // END - if
                } // END - if
        } // END - if
 
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - EXIT!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . makeNullToZero($refid) . ',paymentMethod=' . $paymentMethod . ' - EXIT!');
        return $added;
 }
 
@@ -288,7 +293,7 @@ function updateReferalCounter ($userid) {
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',updated=' . SQL_AFFECTEDROWS());
                if (SQL_HASZEROAFFECTED()) {
                        // First count!
-                       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
+                       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`,`level`,`counter`) VALUES (%s,%s,1)",
                                array(
                                        bigintval($ref),
                                        makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref])
@@ -412,7 +417,7 @@ ORDER BY
 
 // Get points data for given subject
 function getPointsDataArrayFromSubject ($subject) {
-       // Extension sql_patches must be up-to-date
+       // Extension ext-sql_patches must be up-to-date
        if (isExtensionInstalledAndOlder('sql_patches', '0.8.2')) {
                // Please update ext-sql_patches
                debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.2 to continue. subject=' . $subject);
@@ -430,7 +435,7 @@ function getPointsDataArrayFromSubject ($subject) {
        } // END - if
 
        // Now checkout the entry in database table
-       $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
+       $result = SQL_QUERY_ESC("SELECT `id`,`subject`,`column_name`,`locked_mode`,`payment_method`,`notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
                array($subject), __FUNCTION__, __LINE__);
 
        // Do we have an entry?
@@ -444,7 +449,7 @@ function getPointsDataArrayFromSubject ($subject) {
                } // END - foreach
        } else {
                // Register this automatically
-               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient`) VALUES ('%s','points','LOCKED','REFERAL','N')",
+               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`,`column_name`,`locked_mode`,`payment_method`,`notify_recipient`) VALUES ('%s','points','LOCKED','REFERAL','N')",
                        array($subject), __FUNCTION__, __LINE__);
 
                // Re-request it
@@ -585,15 +590,15 @@ function addMissingReferalLevels ($userid) {
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid);
                        // Does the refid have an array?
                        if (isset($GLOBALS['referal_refid'][$refid])) {
-                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',count()=' . count($GLOBALS['referal_refid'][$refid]));
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . makeNullToZero($refid) . ',count()=' . count($GLOBALS['referal_refid'][$refid]));
                                // Okay, then walk through here, too
                                foreach ($GLOBALS['referal_refid'][$refid] as $refLevel=>$refArray) {
-                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',count()=' . count($refArray));
+                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . makeNullToZero($refid) . ',refLevel=' . $refLevel . ',count()=' . count($refArray));
                                        // Also walk through this one
                                        foreach ($refArray as $refRefid) {
                                                // Calculate new level
                                                $newLevel =  $level + $refLevel;
-                                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',refRefid=' . $refRefid . ',newLevel=' . $newLevel);
+                                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . makeNullToZero($refid) . ',refLevel=' . $refLevel . ',refRefid=' . $refRefid . ',newLevel=' . $newLevel);
                                                // Is the refRefid not in?
                                                if ((!isset($GLOBALS['referal_refid'][$userid][$newLevel])) || (!in_array($refRefid, $GLOBALS['referal_refid'][$userid][$newLevel]))) {
                                                        // Then we must add this ref's refid to the userid's next level