- // Check user account
- /* 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);
-
- // 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);
-
- // 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!');
- } // 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='.$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),
- 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().')');
- } // END - if
-
- // Check affected rows
- $added = SQL_AFFECTEDROWS();
- /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
-
- // Prepare data for the filter
- $filterData = array(
- 'subject' => $subject,
- 'userid' => $userid,
- 'points' => $points,
- 'ref_points' => $ref_points,
- 'column' => $pointsColumn,
- 'notify' => $sendNotify,
- 'refid' => $refid,
- 'locked' => $locked,
- '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));
-
- // Points updated, maybe I shall send him an email?
- if (($sendNotify === true) && (isValidUserId(getUserData('refid'))) && ($locked === false)) {
- // Prepare content
- $content = array(
- 'percents' => $per,
- 'level' => bigintval($GLOBALS['ref_level']),
- 'points' => $ref_points,
- );
-
- // Load email template
- $message = loadEmailTemplate('guest_user_confirmed_referal', $content, bigintval($userid));
-
- // Send email
- sendEmail($userid, '{--THANX_REFERAL_ONE_SUBJECT--}', $message);
- } elseif (($sendNotify === true) && (!isValidUserId(getUserData('refid'))) && ($locked === false) && ($paymentMethod == 'DIRECT')) {
- // Prepare content
- $content = array(
- 'reason' => '{--REASON_DIRECT_PAYMENT--}',
- 'subject' => $subject,
- 'points' => $ref_points
- );
-
- // Load message
- $message = loadEmailTemplate('member_add_points', $content, $userid);
-
- // And sent it away
- sendEmail($userid, '{--DIRECT_PAYMENT_SUBJECT--}', $message);
- if (!isGetRequestParameterSet('mid')) {
- // Output message to admin
- displayMessage('{--ADMIN_POINTS_ADDED--}');
- } // 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) && ($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')));
- } // END - if
- } // END - if
- } // END - if
-
- /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - 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] = NULL;
- } // END - if
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',level='.$GLOBALS['cache_array']['ref_level'][$userid]);
-
- // Update counter
- 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),
- 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)",
- 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
- $ref = '0';
-
- // Check for his referal
- if (fetchUserData($userid)) {
- // Get it
- $ref = getUserData('refid');
- } // END - if
-
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',ref='.$ref);
-
- // When he has a referal...
- if (($ref > 0) && ($ref != $userid)) {
- // Move to next referal level and count his counter one up!
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref='.$ref.' - ADVANCE!');
- $GLOBALS['cache_array']['ref_level'][$userid]++;
- updateReferalCounter($ref);
- } elseif ((($ref == $userid) || ($ref == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2'))) {
- // Remove cache here
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref='.$ref.' - CACHE!');
- rebuildCache('refsystem', 'refsystem');
- }
-
- // "Walk" back here
- $GLOBALS['cache_array']['ref_level'][$userid]--;
-
- // Handle refback here if extension is installed
- // @TODO Rewrite this to a filter
- if (isExtensionActive('refback')) {
- updateRefbackTable($userid);
- } // END - if
-}
-
-// Sends out mail to all administrators. This function is no longer obsolete
-// because we need it when there is no ext-admins installed
-function sendAdminEmails ($subj, $message) {