0) { // Calculate new points $ref_points = $points * $percents / 100; // Pay refback here if level > 0 and in ref-mode //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . convertNullToZero(getUserData('refid')) . ',points=' . $points . ',paymentMethod=' . $paymentMethod); if (($userid != $refid) && (substr($subject, 0, 8) != 'refback:') &&($paymentMethod == 'REFERRAL') && (isValidId(getUserData('refid'))) && (isExtensionActive('refback'))) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . convertNullToZero(getUserData('refid')) . ',ref_points=' . $ref_points . ',depth=' . convertNullToZero($GLOBALS['ref_level']) . ' - BEFORE!'); $ref_points = addRefbackPoints($userid, getUserData('refid'), $points, $ref_points); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . convertNullToZero(getUserData('refid')) . ',ref_points=' . $ref_points . ',depth=' . convertNullToZero($GLOBALS['ref_level']) . ' - AFTER!'); } // END - if // Update points... 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='.convertNullToZero($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)", array( $pointsColumn, bigintval($userid), convertZeroToNull($GLOBALS['ref_level']), $ref_points ), __FUNCTION__, __LINE__); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.convertNullToZero($GLOBALS['ref_level']).',mode='.$paymentMethod.' - INSERTED! ('.SQL_AFFECTEDROWS().')'); } // END - if // Check affected rows $added = (SQL_AFFECTEDROWS() == 1); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added=' . intval($added) . ' - BEFORE FILTER'); // Prepare data for the filter $filterData = array( 'subject' => $subject, 'userid' => $userid, 'points' => $points, 'ref_points' => $ref_points, 'column' => $pointsColumn, 'notify' => $sendNotify, 'refid' => $refid, 'locked' => $isLocked, 'points_mode' => 'add', 'add_mode' => $paymentMethod, 'added' => $added ); // Filter it now $filterData = runFilterChain('post_add_points', $filterData); // Extract $added $added = $filterData['added']; //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added=' . intval($added) . ' - AFTER FILTER'); // Debug message //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ',sendNotify=' . intval($sendNotify) . ',isLocked=' . intval($isLocked)); // Send "referral confirmed" mails out? if ((isValidUserid($refid)) && ($refid != $userid) && ($sendNotify === TRUE)) { // Calculate the referral's points and percents $percentsReferral = getReferralLevelPercents($GLOBALS['ref_level'] + 1); // Calculate new points $ref_points = $ref_points * $percentsReferral / 100; // Prepare content $content = array( 'userid' => bigintval($userid), 'refid' => $refid, 'level' => bigintval($GLOBALS['ref_level'] + 1), 'percents' => $percentsReferral, 'points' => ($paymentMethod == 'REFERRAL' ? $ref_points : '0'), 'payment_method' => $paymentMethod, 'subject' => $subject, ); // Load email template $message = loadEmailTemplate('guest_user_confirmed_referral', $content, bigintval($userid)); // Send email sendEmail($userid, '{--THANKS_REFERRAL_ONE_SUBJECT--}', $message); } // END - if // Points updated, maybe I shall send him an email? if (($sendNotify === TRUE) && ($isLocked === FALSE)) { // "Explode" subject $subjectArray = explode(':', $subject); $subjectUserid = (isset($subjectArray[1])) ? $subjectArray[1] : '0'; // Generic delivery of mails, so prepare data $content = array( 'userid' => $userid, 'percents' => $percents, 'level' => bigintval($GLOBALS['ref_level']), 'points' => $ref_points, 'column' => $pointsColumn, 'subject' => $subjectArray[0], 'subject_userid' => $subjectUserid, 'payment_method' => $paymentMethod, ); // Load email template $message = loadEmailTemplate('member_' . $subjectArray[0] . '_' . strtolower($paymentMethod), $content, $userid); // Send email sendEmail($userid, '{%message,MEMBER_' . $paymentMethod . '_' . strtoupper($subjectArray[0]) . '_SUBJECT=' . $subjectUserid . '%}', $message); // Also send admin notification sendAdminNotification( // Subject '{%message,ADMIN_' . $paymentMethod . '_' . strtoupper($subjectArray[0]) . '_SUBJECT=' . $subjectUserid . '%}', // Template name 'admin_' . $subjectArray[0] . '_' . strtolower($paymentMethod), // Template content (data array) $content, // User id $userid ); } // END - if // Increase referral level, if payment method is 'REFERRAL' if ($paymentMethod == 'REFERRAL') { // Increase it $GLOBALS['ref_level']++; //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Referral level increased, ref_level=' . convertNullToZero($GLOBALS['ref_level']) . ',points=' . $points . ',refid=' . convertNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod); } elseif (isDebugModeEnabled()) { // Not increasing referral level, DIRECT payment method //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Referral level *NOT* increased, ref_level=' . convertNullToZero($GLOBALS['ref_level']) . ',points=' . $points . ',refid=' . convertNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod); } // Remove any :x $subject = removeDoubleDotFromSubject($subject); // Maybe there's another ref? //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'points=' . $points . ',refid(var|data)=' . convertNullToZero($refid) . '|' . convertNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod . ',subject=' . $subject . ',ref_level=' . $GLOBALS['ref_level']); if (($paymentMethod == 'REFERRAL') && (isValidId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid)) { // Is _ref there? if (substr($subject, -4, 4) == '_ref') { // Then remove it, no double _ref suffix! $subject = substr($subject, 0, -4); } // END - if // Then let's credit him here... //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . convertNullToZero(getUserData('refid')) . ',points=' . $points . ',ref_points=' . $ref_points . ',added[' . gettype($added) . ']=' . intval($added) . ',ref_level=' . $GLOBALS['ref_level'] . ' - ADVANCE!'); $added = ($added && addPointsThroughReferralSystem(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=' . convertNullToZero($refid) . ',paymentMethod=' . $paymentMethod . ' - EXIT!'); return $added; } // Updates the referral counter function updateReferralCounter ($userid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); // Init referral id $refid = NULL; // Check for his referral if (fetchUserData($userid)) { // Get it $refid = getUserData('refid'); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . convertZeroToNull($refid) . ' - FETCHED!'); } // END - if // Init entries if (empty($GLOBALS['cache_array']['ref_level'][$userid])) { $GLOBALS['cache_array']['ref_level'][$userid] = NULL; } // END - if if (empty($GLOBALS['cache_array']['ref_level'][$refid])) { $GLOBALS['cache_array']['ref_level'][$refid] = NULL; } // END - if //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . convertZeroToNull($refid)); // When he has a referral... if ((isValidId($refid)) && ($refid != $userid)) { // Move to next referral level and count his counter one up $GLOBALS['cache_array']['ref_level'][$refid] = $GLOBALS['cache_array']['ref_level'][$userid] + 1; //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid(' . $refid . ')=' . $GLOBALS['cache_array']['ref_level'][$refid] . ' - ADVANCED!'); // Update counter SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET `counter`=`counter`+1 WHERE `userid`=%s AND `level`=%s LIMIT 1", array( bigintval($refid), bigintval($GLOBALS['cache_array']['ref_level'][$refid]) ), __FUNCTION__, __LINE__); // When no entry was updated then we have to create it here //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . $refid . ',level=' . $GLOBALS['cache_array']['ref_level'][$refid] . ',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($refid), convertZeroToNull($GLOBALS['cache_array']['ref_level'][$refid]) ), __FUNCTION__, __LINE__); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . $refid . ',level=' . $GLOBALS['cache_array']['ref_level'][$refid] . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS()); } // END - if // Advance to next level updateReferralCounter($refid); } elseif ((($refid == $userid) || (!isValidId($refid))) && (isExtensionInstalledAndNewer('cache', '0.1.2'))) { // Remove cache here //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . convertZeroToNull($refid) . ' - CACHE!'); rebuildCache('refsystem', 'refsystem'); } // Update the referral table updateReferralTable($userid); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . convertZeroToNull($refid) . ',level=' . convertZeroToNull($GLOBALS['cache_array']['ref_level'][$refid]) . ' - EXIT!'); } // Subtract points from database and mediadata cache function subtractPoints ($subject, $userid, $points) { // Add points to used points SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `used_points`=`used_points`+%s WHERE `userid`=%s LIMIT 1", array( $points, bigintval($userid) ), __FUNCTION__, __LINE__); // Prepare filter data $filterData = array( 'subject' => $subject, 'userid' => $userid, 'points' => $points, 'points_mode' => 'sub', 'column' => 'used_points', 'added' => (!SQL_HASZEROAFFECTED()) ); // Insert booking record $filterData = runFilterChain('post_sub_points', $filterData); // Return result return $filterData['added']; } // "Getter" for array for user refs and points in given level function getUserReferralPoints ($userid, $level) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!'); // Default is no refs and no nickname $refs = array(); // Get refs from database $result = SQL_QUERY_ESC('SELECT `ur`.`id`, `ur`.`refid`, `ud`.`status`, `ud`.`last_online`, `ud`.`mails_confirmed`, `ud`.`emails_received`, `ud`.`subid` FROM `{?_MYSQL_PREFIX?}_user_refs` AS `ur` LEFT JOIN `{?_MYSQL_PREFIX?}_user_points` AS `up` ON `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` WHERE `ur`.`userid`=%s AND `ur`.`level`=%s ORDER BY `ur`.`refid` ASC', array( bigintval($userid), bigintval($level) ), __FUNCTION__, __LINE__); // Are there some entries? if (!SQL_HASZERONUMS($result)) { // Fetch all entries while ($row = SQL_FETCHARRAY($result)) { // Init click rate with zero $row['click_rate'] = '0'; // Is at least one mail received? if ($row['emails_received'] > 0) { // Calculate click rate $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100); } // END - if // Activity is 'active' by default because if ext-autopurge is not installed $row['activity'] = '{--MEMBER_ACTIVITY_ACTIVE--}'; // Is autopurge installed and the user inactive? if ((isExtensionActive('autopurge')) && ((time() - getApInactiveSince()) >= $row['last_online'])) { // Inactive user! $row['activity'] = '{--MEMBER_ACTIVITY_INACTIVE--}'; } // END - if // Remove some entries unset($row['mails_confirmed']); unset($row['emails_received']); unset($row['last_online']); // Add row $refs[$row['id']] = $row; } // END - while } // END - if // Free result SQL_FREERESULT($result); // Return result //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - EXIT!'); return $refs; } // Get points data for given subject function getPointsDataArrayFromSubject ($subject) { // Extension ext-sql_patches must be up-to-date if (isExtensionInstalledAndOlder('sql_patches', '0.8.2')) { // Please update ext-sql_patches reportBug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.2 to continue. subject=' . $subject); } elseif (substr($subject, -8, 8) == '_ref_ref') { // Please report ALL finiding reportBug(__FUNCTION__, __LINE__, 'subject=' . $subject . ' contains invalid double-suffix _ref.'); } // Remove any double-dot from it $subject = removeDoubleDotFromSubject($subject); // If we have cache, shortcut it here if (isset($GLOBALS['cache_array']['points_data'][$subject])) { // Count cache hit incrementStatsEntry('cache_hits'); // Return it return $GLOBALS['cache_array']['points_data'][$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", array($subject), __FUNCTION__, __LINE__); // Is there an entry? if (SQL_NUMROWS($result) == 1) { // Then load it $pointsData = SQL_FETCHARRAY($result); // Add all entries to our cache array foreach ($pointsData as $key => $value) { $GLOBALS['cache_array']['points_data'][$subject][$key] = $value; } // 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','REFERRAL','N')", array($subject), __FUNCTION__, __LINE__); // Re-request it return getPointsDataArrayFromSubject($subject); } // Free result SQL_FREERESULT($result); // Return it return $GLOBALS['cache_array']['points_data'][$subject]; } // Determines the right points column name for given subject and 'locked' function getPointsColumnNameFromSubjectLocked ($subject, $isLocked) { // Get the points_data entry $pointsData = getPointsDataArrayFromSubject($subject); // Regular points by default $columnName = $pointsData['column_name']; // Are the points locked? if (($isLocked === TRUE) && ($pointsData['locked_mode'] == 'LOCKED')) { // Locked points, so prefix it $columnName = 'locked_' . $pointsData['column_name']; } // END - if // Return the result return $columnName; } // Determines the payment method for given extension and 'locked' function getPaymentMethodFromSubject ($subject) { // Get the points_data entry $pointsData = getPointsDataArrayFromSubject($subject); // Regular points by default $paymentMethod = $pointsData['payment_method']; // Return the result return $paymentMethod; } // Checks whether notification of points recipient is enabled function isPaymentRecipientNotificationEnabled ($subject) { // Get the points_data entry $pointsData = getPointsDataArrayFromSubject($subject); // Is it enabled? $isEnabled = ($pointsData['notify_recipient'] == 'Y'); // Return the result return $isEnabled; } // Update "referral table" function updateReferralTable ($userid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); // Load all referrals loadReferralTable($userid); // Add missing level > 1 addMissingReferralLevels($userid); // The last step is to flush all userid's entries to the database flushReferralTableToDatabase($userid); // Rebuild cache rebuildCache('refsystem', 'refsystem'); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); } // Loads all referrals for given userid function loadReferralTable ($userid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); // Init array $GLOBALS['referral_refid'][$userid] = array(); // Get all level entries from the refsystem table $GLOBALS['referral_result'][$userid] = SQL_QUERY_ESC('SELECT `level` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `userid`=%s ORDER BY `level` ASC', array($userid), __FUNCTION__, __LINE__); // Are there entries? //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_NUMROWS()=' . SQL_NUMROWS($GLOBALS['referral_result'][$userid])); if (SQL_NUMROWS($GLOBALS['referral_result'][$userid]) > 0) { // Then walk through all levels while (list($level) = SQL_FETCHROW($GLOBALS['referral_result'][$userid])) { // Init array //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level); $GLOBALS['referral_refid'][$userid][$level] = array(); // Level is = 1? if ($level == 1) { // Load all referrals of this user $GLOBALS['referral_result_refs'][$userid] = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC', array($userid), __FUNCTION__, __LINE__); // Are there entries? //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',SQL_NUMROWS()=' . SQL_NUMROWS($GLOBALS['referral_result_refs'][$userid])); if (SQL_NUMROWS($GLOBALS['referral_result_refs'][$userid]) > 0) { // Then again walk through all while (list($refid) = SQL_FETCHROW($GLOBALS['referral_result_refs'][$userid])) { // Add this refid array_push($GLOBALS['referral_refid'][$userid][$level], $refid); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ' - ADDED!'); // Load the refid's array as well loadReferralTable($refid); } // END - while } // END - if // Free result SQL_FREERESULT($GLOBALS['referral_result_refs'][$userid]); } // END - if } // END - while } // END - if // Free result SQL_FREERESULT($GLOBALS['referral_result'][$userid]); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); } // Adds missing referral levels to the array function addMissingReferralLevels ($userid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); // If the array is gone, you have called this function without calling loadReferralTable() if (!isset($GLOBALS['referral_refid'][$userid])) { // Please fix your code reportBug(__FUNCTION__, __LINE__, 'Called without calling loadReferralTable() before! userid=' . $userid); } // END - if //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referral_refid'][$userid])); // Sort the array reversed krsort($GLOBALS['referral_refid']); // Now walk through the array, first levels foreach ($GLOBALS['referral_refid'][$userid] as $level => $levelArray) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray)); // Next are the users foreach ($levelArray as $refid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); // Does the refid have an array? if (isset($GLOBALS['referral_refid'][$refid])) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . convertNullToZero($refid) . ',count()=' . count($GLOBALS['referral_refid'][$refid])); // Add also this user's (maybe) missing levels addMissingReferralLevels($refid); // Okay, then walk through here, too foreach ($GLOBALS['referral_refid'][$refid] as $refLevel => $refArray) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . convertNullToZero($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=' . convertNullToZero($refid) . ',refLevel=' . $refLevel . ',refRefid=' . $refRefid . ',newLevel=' . $newLevel); // Is the refRefid not in? if ((!isset($GLOBALS['referral_refid'][$userid][$newLevel])) || (!in_array($refRefid, $GLOBALS['referral_refid'][$userid][$newLevel]))) { // Then we must add this ref's refid to the userid's next level //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',newLevel=' . $newLevel . ',refRefid=' . $refRefid . ' - ADDED!'); array_push($GLOBALS['referral_refid'][$userid][$newLevel], $refRefid); // Add also this user's (maybe) missing levels addMissingReferralLevels($refRefid); } // END - if } // END - foreach } // END - foreach } // END - foreach } // END - foreach } // END - foreach //die('
'.print_r($GLOBALS['referral_refid'][$userid],TRUE).'
'); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); } // Flush all entries for given userid to database function flushReferralTableToDatabase ($userid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); // If the array is gone, you have called this function without calling loadReferralTable() if (!isset($GLOBALS['referral_refid'][$userid])) { // Please fix your code reportBug(__FUNCTION__, __LINE__, 'Called without calling loadReferralTable() before! userid=' . $userid); } // END - if //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referral_refid'][$userid])); // If no entries are there, skip this whole step if (count($GLOBALS['referral_refid'][$userid]) == 0) { // No entries found //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ABORTING...'); return; } // END - if // Prepare SQL $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`, `level`, `refid`) VALUES '; $executeSql = FALSE; // Now walk through the array, first levels foreach ($GLOBALS['referral_refid'][$userid] as $level => $levelArray) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray)); // Next are the users foreach ($levelArray as $refid) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); // Query the user_refs table $count = countSumTotalData(bigintval($userid), 'user_refs', 'id', 'userid', TRUE, ' AND `level`=' . bigintval($level) . ' AND `refid`=' . bigintval($refid)); // Is there no entry? 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 // Remove last comma from SQL $SQL = substr($SQL, 0, -1); // And run it //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL); if ($executeSql === TRUE) { SQL_QUERY($SQL, __FUNCTION__, __LINE__); } // END - if //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); } // Generator (somewhat getter) for points_data, locked_mode function generatePointsLockedModeOptions ($lockedMode = NULL) { // Is this cached? if (!isset($GLOBALS[__FUNCTION__][$lockedMode])) { // Generate output and cache it $GLOBALS[__FUNCTION__][$lockedMode] = generateOptions( '/ARRAY/', array( 'LOCKED', 'UNLOCKED' ), array(), $lockedMode, '', '', array(), 'translatePointsLockedMode' ); } // END - if // Return content return $GLOBALS[__FUNCTION__][$lockedMode]; } // Generator (somewhat getter) for points_data, payment_method function generatePointsPaymentMethodOptions ($paymentMethod = NULL) { // Is this cached? if (!isset($GLOBALS[__FUNCTION__][$paymentMethod])) { // Generate output and cache it $GLOBALS[__FUNCTION__][$paymentMethod] = generateOptions( '/ARRAY/', array( 'DIRECT', 'REFERRAL' ), array(), $paymentMethod, '', '', array(), 'translatePointsPaymentMethod' ); } // END - if // Return content return $GLOBALS[__FUNCTION__][$paymentMethod]; } // Generator (somewhat getter) for points_data, notify_recipient function generatePointsNotifyRecipientOptions ($notifyRecipient = NULL) { // Is this cached? if (!isset($GLOBALS[__FUNCTION__][$notifyRecipient])) { // Generate output and cache it $GLOBALS[__FUNCTION__][$notifyRecipient] = generateOptions( '/ARRAY/', array( 'Y', 'N' ), array(), $notifyRecipient, '', '', array(), 'translatePointsNotifyRecipient' ); } // END - if // Return content return $GLOBALS[__FUNCTION__][$notifyRecipient]; } // Setter for referral id (no bigintval, or nicknames will fail!) function setReferralId ($refid) { $GLOBALS['__refid'] = $refid; } // Checks if 'refid' is valid function isReferralIdValid () { return ((isset($GLOBALS['__refid'])) && (getReferralId() !== NULL) && (getReferralId() > 0)); } // Getter for referral id function getReferralId () { return $GLOBALS['__refid']; } // Determines referral id and sets it function determineReferralId () { // Is it already detected? if (isReferralIdValid()) { // Do not determine it, just return it return getReferralId(); } elseif ((!isHtmlOutputMode()) && (basename($_SERVER['PHP_SELF']) != 'ref.php')) { // Skip this in non-html-mode and outside ref.php return FALSE; } // Check if refid is set if (isReferralIdValid()) { // This is fine... //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from GLOBALS (' . getReferralId() . ')'); } elseif (isPostRequestElementSet('refid')) { // Get referral id from POST element refid //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from POST data (' . postRequestElement('refid') . ')'); setReferralId(secureString(postRequestElement('refid'))); } elseif (isGetRequestElementSet('refid')) { // Get referral id from GET parameter refid //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from GET data (' . getRequestElement('refid') . ')'); setReferralId(getRequestElement('refid')); } elseif (isGetRequestElementSet('ref')) { // Set refid=ref (the referral link uses such variable) //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using ref from GET data (' . getRequestElement('ref') . ')'); setReferralId(getRequestElement('ref')); } elseif ((isGetRequestElementSet('user')) && (basename($_SERVER['PHP_SELF']) == 'click.php')) { // The variable user comes from click.php //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using user from GET data (' . getRequestElement('user') . ')'); setReferralId(bigintval(getRequestElement('user'))); } elseif ((isSessionVariableSet('refid')) && (isValidId(getSession('refid')))) { // Set session refid as global //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from SESSION data (' . getSession('refid') . ')'); setReferralId(bigintval(getSession('refid'))); } elseif ((isExtensionInstalledAndNewer('user', '0.3.4')) && (isRandomReferralIdEnabled())) { // Select a random user which has confirmed enougth mails //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Checking random referral id'); setReferralId(determineRandomReferralId()); } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.1.2')) && (isValidId(getDefRefid()))) { // Set default refid as refid in URL //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using default refid (' . getDefRefid() . ')'); setReferralId(getDefRefid()); } else { // No default id when ext-sql_patches is not installed or none set //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using NULL as refid'); setReferralId(NULL); } // Set cookie when default refid > 0 if ((!isSessionVariableSet('refid')) || (!isValidId(getReferralId())) || ((!isValidId(getSession('refid'))) && (isExtensionInstalledAndNewer('sql_patches', '0.1.2')) && (isValidId(getDefRefid())))) { // Default is not found $found = FALSE; // Is there nickname or userid set? if ((isExtensionActive('nickname')) && (isNicknameUsed(getReferralId()))) { // Nickname in URL, so load the id $found = fetchUserData(getReferralId(), 'nickname'); // If we found it, use the userid as referral id if ($found === TRUE) { // Set the userid as 'refid' //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from user account by nickname (' . getUserData('userid') . ')'); setReferralId(getUserData('userid')); } // END - if } elseif (isValidId(getReferralId())) { // Direct userid entered //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using direct userid (' . getReferralId() . ')'); $found = fetchUserData(getReferralId()); } // Is the record valid? if ((($found === FALSE) || (!isUserDataValid())) && (isExtensionInstalledAndNewer('sql_patches', '0.1.2'))) { // No, then reset referral id //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using default refid (' . getDefRefid() . ')'); setReferralId(getDefRefid()); } // END - if // Set cookie //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Saving refid to session (' . getReferralId() . ') #1'); setSession('refid', getReferralId()); } elseif ((!isReferralIdValid()) || (!fetchUserData(getReferralId()))) { // Not valid! //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not valid referral id (' . getReferralId() . '), setting NULL in session'); setReferralId(NULL); setSession('refid', NULL); } else { // Set it from GLOBALS array in session //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Saving refid to session (' . getReferralId() . ') #2'); setSession('refid', getReferralId()); } // Run post validation filter chain runFilterChain('post_refid_validation'); // Return determined refid return getReferralId(); } // [EOF] ?>