]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
Referal system rewritten, ext-refback continued:
[mailer.git] / inc / mysql-manager.php
index 9ffbb0297cb7c67fdbc5d356251cdc2ab18c7919..2fd3d23d535598de2b0c1021b3bc4633719377a9 100644 (file)
@@ -155,9 +155,9 @@ function addYouAreHereLink ($accessLevel, $FQFN, $return = false) {
                $OUT = $prefix . '<strong><a class="you_are_here" href="{%url=modules.php?module=' . $modCheck . '&amp;' . $type . '=' . $search . $LINK_ADD . '%}">' . getTitleFromMenu($accessLevel, $search, $type, $ADD) . '</a></strong>';
 
                // Can we close the you-are-here navigation?
-               //* DEBUG: */ debugOutput(__LINE__.'*'.$type.'/'.getWhat().'*');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'type=' . $type . 'getWhat()=' . getWhat());
                if (($type == 'what') || (($type == 'action') && ((!isWhatSet()) || (getWhat() == 'overview')))) {
-                       //* DEBUG: */ debugOutput(__LINE__.'+'.$type.'+');
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'type=' . $type);
                        // Add closing div and br-tag
                        $OUT .= '</div>';
                        $GLOBALS['nav_depth'] = '0';
@@ -208,12 +208,12 @@ ORDER BY
        `sort` ASC",
                array($mode), __FUNCTION__, __LINE__);
 
-       //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . ':' . getWhat() . '*');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',getWhat()=' . getWhat());
        if (!SQL_HASZERONUMS($result_main)) {
                // There are menus available, so we simply display them... :)
                $GLOBALS['rows'] = '';
                while ($content = SQL_FETCHARRAY($result_main)) {
-                       //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . '/' . $content['action'] . ':' . getWhat() . '*');
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',action=' . $content['action'] . ',getWhat()=' . getWhat());
                        // Disable the block-mode
                        enableBlockMode(false);
 
@@ -251,7 +251,7 @@ ORDER BY
                                        $OUT = '';
 
                                        // Full file name for checking menu
-                                       //* DEBUG: */ debugOutput(__LINE__ . ':!!!!' . $content['sub_what'] . '!!!');
+                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sub_what=' . $content['sub_what']);
                                        $inc = sprintf("inc/modules/%s/what-%s.php", $mode, $content['sub_what']);
                                        if (isIncludeReadable($inc)) {
                                                // Mark currently selected menu - open
@@ -308,12 +308,12 @@ ORDER BY
                                if (isFileReadable($INC)) {
                                        // Load include file
                                        if ((!isExtensionActive($content['action'])) || ($content['action'] == 'online')) $GLOBALS['rows'] .= loadTemplate('menu_what_begin', true, $mode);
-                                       //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . '/' . $content['action'] . '/' . getWhat().'*');
+                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',action=' . $content['action'] . ',getWhat()=' . getWhat());
                                        loadInclude($INC);
-                                       //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . '/' . $content['action'] . '/' . getWhat() . '*');
+                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',action=' . $content['action'] . ',getWhat()=' . getWhat());
                                        if ((!isExtensionActive($content['action'])) || ($content['action'] == 'online')) $GLOBALS['rows'] .= loadTemplate('menu_what_end', true, $mode);
                                }
-                               //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . '/' . $content['action'] . '/' . $content['sub_what'] . ':' . getWhat() . '*');
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',action=' . $content['action'] . ',getWhat()=' . getWhat());
                        }
 
                        // Free result
@@ -322,7 +322,7 @@ ORDER BY
                        // Count one up
                        $main_cnt++;
 
-                       //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . ':' . getWhat() . '*');
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',getWhat()=' . getWhat());
                        if (SQL_NUMROWS($result_main) > $main_cnt) {
                                // Add seperator
                                $GLOBALS['rows'] .= loadTemplate('menu_seperator', true, $mode);
@@ -357,7 +357,7 @@ ORDER BY
                );
 
                // Load main template
-               //* DEBUG: */ debugOutput(__LINE__ . '/' . $main_cnt . '/' . $content['action'] . '/' . $content['sub_what'] . ':' . getWhat() . '*');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'main_cnt=' . $main_cnt . ',getWhat()=' . getWhat());
                loadTemplate('menu_table', false, $content);
        } // END - if
 }
@@ -540,7 +540,7 @@ function isAdmin () {
                $adminId    = getCurrentAdminId();
                $passCookie = getAdminMd5();
        } // END - if
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $adminId.'/'.$passCookie);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'mainId=' . $adminId . 'passCookie=' . $passCookie);
 
        // Abort if admin id is zero
        if ($adminId == '0') {
@@ -674,7 +674,7 @@ function isMenuActionValid ($mode, $action, $what, $updateEntry=false) {
        $add = '';
        if ((!isAdmin()) && ($mode != 'admin')) $add = " AND `locked`='N'";
 
-       //* DEBUG: */ debugOutput(__LINE__ . ':' . $mode . '/' . $action . '/' . $what . '*');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'mode=' . $mode . ',action=' . $action . ',what=' . $what);
        if (($mode != 'admin') && ($updateEntry === true)) {
                // Update guest or member menu
                $sql = SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_%s_menu` SET `counter`=`counter`+1 WHERE `action`='%s' AND `what`='%s'".$add." LIMIT 1",
@@ -724,10 +724,10 @@ function isMenuActionValid ($mode, $action, $what, $updateEntry=false) {
 
 // Get action value from mode (admin/guest/member) and what-value
 function getActionFromModuleWhat ($module, $what) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'module=' . $module . ',what=' . $what);
        // Init status
        $data['action'] = '';
 
-       //* DEBUG: */ debugOutput(__LINE__ . '=' . $module . '/'.$what . '/' . getAction() . '=');
        if (!isExtensionInstalledAndNewer('sql_patches', '0.0.5')) {
                // sql_patches is missing so choose depending on mode
                if (isWhatSet()) {
@@ -928,7 +928,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
        // Init count/sum
        $data['res'] = '0';
 
-       //* DEBUG: */ debugOutput($search.'/'.$tableName.'/'.$lookFor.'/'.$whereStatement.'/'.$add);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',tableName=' . $tableName . ',lookFor=' . $lookFor . ',whereStatement=' . $whereStatement . ',add=' . $add);
        if ((empty($search)) && ($search != '0')) {
                // Count or sum whole table?
                if ($countRows === true) {
@@ -948,7 +948,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
                }
        } elseif (($countRows === true) || ($lookFor == 'userid')) {
                // Count rows
-               //* DEBUG: */ debugOutput('COUNT!');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'COUNT!');
                $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'".$add,
                        array(
                                $lookFor,
@@ -959,7 +959,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
                        ), __FUNCTION__, __LINE__);
        } else {
                // Add all rows
-               //* DEBUG: */ debugOutput('SUM!');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SUM!');
                $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'".$add,
                        array(
                                $lookFor,
@@ -986,7 +986,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
        }
 
        // Return value
-       //* DEBUG: */ debugOutput('ret=' . $data['res']);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'res=' . $data['res']);
        return $data['res'];
 }
 // Getter fro ref level percents
@@ -1026,7 +1026,7 @@ function getReferalLevelPercents ($level) {
 
 // Initializes the referal system
 function initReferalSystem () {
-       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal system initialized!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal system initialized!');
        $GLOBALS['ref_level']  = NULL;
        $GLOBALS['ref_system'] = true;
 }
@@ -1040,24 +1040,14 @@ function initReferalSystem () {
  * 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?
- * paymentMethod = Deprecated parameter, the payment method
  */
-function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0', $paymentMethod = 'INVALID') {
-       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - ENTERED!');
+function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0') {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . $refid . ' - ENTERED!');
        // By default nothing has been added
        $added = false;
 
-       // Convert mode to upper-case
-       $paymentMethod = strtoupper($paymentMethod);
-
-       // Is it not invalid?
-       if ($paymentMethod != 'INVALID') {
-               // We are not using this anymore
-               debug_report_bug(__FUNCTION__, __LINE__, 'subject=' . $subject . ',paymentMethod=' . $paymentMethod . ' - deprecated parameter used.');
-       } // END - if
-
        // Determine payment method and notification
-       $paymentMethod = getPaymentMethodFromSubject($subject);
+       $paymentMethod = strtoupper(getPaymentMethodFromSubject($subject));
        $sendNotify    = isPaymentRecipientNotificationEnabled($subject);
 
        // When $userid = '0' add points to jackpot
@@ -1067,7 +1057,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0'
        } // END - if
 
        // 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);
@@ -1080,19 +1070,19 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0'
 
                // 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='.$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);
+                       //* 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!');
+                               //* 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...
@@ -1116,7 +1106,7 @@ 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='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
 
                        // No entry updated?
                        if (SQL_HASZEROAFFECTED()) {
@@ -1128,12 +1118,12 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0'
                                                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='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - 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(
@@ -1155,7 +1145,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));
 
                        // Points updated, maybe I shall send him an email?
                        if (($sendNotify === true) && (isValidUserId(getUserData('refid'))) && ($locked === false)) {
@@ -1192,82 +1182,83 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0'
 
                        // Increase referal level
                        $GLOBALS['ref_level']++;
-                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased, ref_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!');
+                               //* 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!');
+       //* 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
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
+       // Init referal id
+       $ref = NULL;
+
+       // Check for his referal
+       if (fetchUserData($userid)) {
+               // Get it
+               $ref = getUserData('refid');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ' - FETCHED!');
+       } // END - if
+
+       // Init entries
        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]);
+       if (empty($GLOBALS['cache_array']['ref_level'][$ref])) {
+               $GLOBALS['cache_array']['ref_level'][$ref] = NULL;
+       } // END - if
+
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref));
 
-       // Update counter
-       if (!is_null($GLOBALS['cache_array']['ref_level'][$userid])) {
-               // Level is > 0
+       // When he has a referal...
+       if (($ref > 0) && ($ref != $userid)) {
+               // Move to next referal level and count his counter one up
+               $GLOBALS['cache_array']['ref_level'][$ref] = $GLOBALS['cache_array']['ref_level'][$userid] + 1;
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref(' . $ref . ')=' . $GLOBALS['cache_array']['ref_level'][$ref] . ' - ADVANCED!');
+
+               // 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])
+                               bigintval($ref),
+                               bigintval($GLOBALS['cache_array']['ref_level'][$ref])
                        ), __FUNCTION__, __LINE__);
 
                // When no entry was updated then we have to create it here
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updated=' . SQL_AFFECTEDROWS());
+               //* 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)",
                                array(
-                                       bigintval($userid),
-                                       makeZeroToNull($GLOBALS['cache_array']['ref_level'][$userid])
+                                       bigintval($ref),
+                                       makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref])
                                ), __FUNCTION__, __LINE__);
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS());
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',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]++;
+               // Advance to next level
                updateReferalCounter($ref);
        } elseif ((($ref == $userid) || ($ref == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2'))) {
                // Remove cache here
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref='.$ref.' - CACHE!');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . makeZeroToNull($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
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ',level=' . makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref]) . ' - EXIT!');
 }
 
 // Sends out mail to all administrators. This function is no longer obsolete
@@ -1666,7 +1657,7 @@ function getWhatFromModule ($modCheck) {
        // Default is empty
        $what = '';
 
-       //* DEBUG: */ debugOutput(__LINE__.'!'.$modCheck.'!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'modCheck=' . $modCheck);
        switch ($modCheck) {
                case 'admin':
                        $what = 'overview';
@@ -1697,7 +1688,10 @@ function getWhatFromModule ($modCheck) {
 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__);
+               array(
+                       $points,
+                       bigintval($userid)
+               ), __FUNCTION__, __LINE__);
 
        // Prepare filter data
        $filterData = array(
@@ -1709,7 +1703,7 @@ function subtractPoints ($subject, $userid, $points) {
        );
 
        // Insert booking record
-       $filterData = runFilterChain('sub_points', $filterData);
+       $filterData = runFilterChain('post_sub_points', $filterData);
 
        // Return result
        return $filterData['added'];
@@ -1953,13 +1947,13 @@ ORDER BY
                        // Get unconfirmed mails
                        $row['unconfirmed']  = countSumTotalData($row['refid'], 'user_links', 'id', 'userid', true);
 
-                       // Init clickrate with zero
-                       $row['clickrate'] = '0';
+                       // Init click rate with zero
+                       $row['click_rate'] = '0';
 
                        // Is at least one mail received?
                        if ($row['emails_received'] > 0) {
-                               // Calculate clickrate
-                               $row['clickrate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
+                               // Calculate click rate
+                               $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
                        } // END - if
 
                        // Activity is 'active' by default because if autopurge is not installed