X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Freferal-functions.php;h=001b7724cdb59807f5aa87029fcf86fb31429605;hb=0e104a9282a1cdc7d769bbccb03bd3462aa8f4e0;hp=57b00298b196776d9576e397c32268c8e856bd33;hpb=1972cc4b6538e319712a70421f1d311decfc8bf9;p=mailer.git diff --git a/inc/referal-functions.php b/inc/referal-functions.php index 57b00298b1..001b7724cd 100644 --- a/inc/referal-functions.php +++ b/inc/referal-functions.php @@ -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 = '0') { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . $refid . ' - ENTERED!'); +function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NULL) { + //* 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 = '0' $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 = '0' // 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 = '0' 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 = '0' // 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 = '0' } // 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]) @@ -368,12 +373,6 @@ ORDER BY if (!SQL_HASZERONUMS($result)) { // Fetch all entries while ($row = SQL_FETCHARRAY($result)) { - // Get total points of this user - $row['points'] = getTotalPoints($row['refid']); - - // Get unconfirmed mails - $row['unconfirmed'] = countSumTotalData($row['refid'], 'user_links', 'id', 'userid', true); - // Init click rate with zero $row['click_rate'] = '0'; @@ -412,7 +411,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 +429,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 +443,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 @@ -512,6 +511,9 @@ function updateReferalTable ($userid) { // The last step is to flush all userid's entries to the database flushReferalTableToDatabase($userid); + // Rebuild cache + rebuildCache('refsystem', 'refsystem'); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); } @@ -582,15 +584,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 @@ -624,6 +626,7 @@ function flushReferalTableToDatabase ($userid) { // Prepare SQL $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES '; + $executeSql = false; // Now walk through the array, first levels foreach ($GLOBALS['referal_refid'][$userid] as $level=>$levelArray) { @@ -643,6 +646,9 @@ function flushReferalTableToDatabase ($userid) { if ($count == 0) { // Then add it to the SQL $SQL .= '(' . $userid . ',' . $level . ',' . $refid . '),'; + + // Some has been added, so execute the query + $executeSql = true; } // END - if } // END - foreach } // END - foreach @@ -652,7 +658,9 @@ function flushReferalTableToDatabase ($userid) { // And run it //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL); - SQL_QUERY($SQL, __FUNCTION__, __LINE__); + if ($executeSql === true) { + SQL_QUERY($SQL, __FUNCTION__, __LINE__); + } // END - if //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); }