Huge rewrite of default parameters, ext-network continued:
[mailer.git] / inc / libs / refback_functions.php
index 426dabc..48ddd1a 100644 (file)
@@ -17,7 +17,7 @@
  * Needs to be in all Files and every File needs "svn propset           *
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
-       require($INC);
+       die();
 }
 
 // Payback refback for refid and reduce it for current user
-function ADD_REFBACK_POINTS ($uid, $ref, $points, $ref_points) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
+function addRefbackPoints ($userid, $ref, $points, $ref_points) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
        // Back ref depths
-       $GLOBALS['cache_array']['depth'][$uid][$ref] = $GLOBALS['ref_level']; 
+       $GLOBALS['refback_depth'][$userid][$ref] = $GLOBALS['ref_level'];
 
        // Init points
        $return = $points;
 
        // Get all ref levels
-       $result_refs = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level > 0 ORDER BY level ASC",
+       $result_refs = SQL_QUERY("SELECT `level`, `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level` > 0 ORDER BY `level` ASC",
                __FUNCTION__, __LINE__);
 
        // "Walk" through all level
        while ($content = SQL_FETCHARRAY($result_refs)) {
                // Reset ref depths
+               // @TODO Try to rewrite the following unset()
                unset($GLOBALS['ref_level']);
 
                // "Walk" through all refids
-               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},level={$content['level']},points={$points}<br />\n";
-               foreach (GET_REFBACK_USERID_ARRAY($uid, $content['level']) as $refid) {
+               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},ref={$ref},level={$content['level']},points={$points}<br />");
+               foreach (getArrayFromRefbackLevel($userid, $content['level']) as $refid) {
                        // Skip level zero or if both are the same
-                       if ($uid == $refid) continue;
+                       if ($userid == $refid) continue;
 
                        // Get refback percents
-                       $percents = GET_REFBACK_PERCENTS($uid, $refid);
-                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />\n";
+                       $percents = getRefbackPercents($userid, $refid);
+                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />");
 
                        // Some percents given?
                        if ($percents > 0) {
-                               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$points},perc={$content['percents']},percents={$percents},level={$content['level']}<br />\n";
+                               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$points},perc={$content['percents']},percents={$percents},level={$content['level']}<br />");
                                // Calculate points for refback
                                $refback = $points * ($content['percents'] / 100) * ($percents / 100);
 
-                               // Update refback table ("refid" and "uid" must be exchanged!)
-                               SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET points=points+%s WHERE userid=%s AND refid=%s LIMIT 1",
-                                       array($refback, $refid, $uid), __FUNCTION__, __LINE__);
-                               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
+                               // Update refback table ("refid" and "userid" must be exchanged!)
+                               SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `points`=`points`+%s WHERE `userid`=%s AND `refid`=%s LIMIT 1",
+                                       array($refback, $refid, $userid), __FUNCTION__, __LINE__);
+                               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},userid={$userid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />");
 
                                // Add points again, but only directly
-                               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback}<br />\n";
-                               ADD_POINTS_REFSYSTEM(sprintf("refback:%s", $refid), $uid, $refback, false, "0", false, "direct");
+                               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback}<br />");
+                               addPointsDirectly(sprintf("refback:%s", $refid), $userid, $refback);
 
                                // Reduce points if refid is found
                                if ($refid == $ref) {
                                        // Reduce¬†points here!
                                        $return = $points * ($content['percents'] / 100) - $refback;
-                                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$return} - REDUCED<br />\n";
+                                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$return} - REDUCED<br />");
                                } // END - if
                        } // END - if
                } // END foreach
@@ -99,66 +99,70 @@ function ADD_REFBACK_POINTS ($uid, $ref, $points, $ref_points) {
        SQL_FREERESULT($result_refs);
 
        // Restore ref depth
-       $GLOBALS['ref_level'] = $GLOBALS['cache_array']['depth'][$uid][$ref];
+       $GLOBALS['ref_level'] = $GLOBALS['refback_depth'][$userid][$ref];
 
        // Return them
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
        return $return;
 }
 
 // "Getter" for refback percents
-function GET_REFBACK_PERCENTS ($uid, $ref) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref}<br />\n";
+function getRefbackPercents ($userid, $ref) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},ref={$ref}<br />");
        // Skip identical ids
-       if ($uid == $ref) return 0;
+       if ($userid == $ref) return 0;
 
        // Default is zero
-       $percents = 0;
+       $percents = '0';
 
        // Get percents from database
-       $result = SQL_QUERY_ESC("SELECT refback FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE userid=%s AND refid=%s LIMIT 1",
-               array($ref, $uid), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT refback FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `refid`=%s LIMIT 1",
+       array($ref, $userid), __FUNCTION__, __LINE__);
 
        // Entry found? (Should be!)
        if (SQL_NUMROWS($result) == 1) {
                // Fetch percents
                list($percents) = SQL_FETCHROW($result);
-               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):percents={$percents}<br />\n";
+               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):percents={$percents}<br />");
        } else {
                // Debug log
-               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): NO ENTRY FOUND! :-(<br />\n";
-               DEBUG_LOG(__FUNCTION__, __LINE__, " uid={$uid},ref={$ref} - No entry found! :-(");
+               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): NO ENTRY FOUND! :-(<br />");
+               logDebugMessage(__FUNCTION__, __LINE__, "userid={$userid},ref={$ref} - No entry found! :-(");
        }
 
        // Free result
        SQL_FREERESULT($result);
 
        // Return percents
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
        return $percents;
 }
 
 // "Getter" for userid array which will return only one entry
-function GET_REFBACK_USERID_ARRAY ($rid, $level) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level}<br />\n";
+function getArrayFromRefbackLevel ($rid, $level) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level}<br />");
        // Init userids
        $userIds = array();
 
        // Look for all
-       $result = SQL_QUERY_ESC("SELECT userid
-FROM `{!_MYSQL_PREFIX!}_user_refs`
-WHERE refid=%s AND level=%s
-ORDER BY userid ASC",
-               array($rid,$level), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT
+       `userid`
+FROM
+       `{?_MYSQL_PREFIX?}_user_refs`
+WHERE
+       `refid`=%s AND `level`=%s
+ORDER BY
+       `userid` ASC",
+               array($rid, $level), __FUNCTION__, __LINE__);
 
        // Entries found?
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />");
        if (SQL_NUMROWS($result) > 0) {
                // Add all
                while ($content = SQL_FETCHARRAY($result)) {
-                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level},userid={$content['userid']}<br />\n";
+                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level},userid={$content['userid']}<br />");
                        $userIds[] = $content['userid'];
                } // END - while
        } // END - if
@@ -167,78 +171,78 @@ ORDER BY userid ASC",
        SQL_FREERESULT($result);
 
        // Return array
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},count()=".count($userIds)."<br />\n";
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},count()=".count($userIds)."<br />");
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
        return $userIds;
 }
 
 // Update "refback table"
-function UPDATE_REFBACK_TABLE ($uid) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
+function updateRefbackTable ($userid) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
        // Make it sure referal level zero (member him-/herself) is at least selected
-       if (empty($GLOBALS['cache_array']['back_level'])) $GLOBALS['cache_array']['back_level'] = 1;
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']}<br />\n";
+       if (empty($GLOBALS['refback_level'])) $GLOBALS['refback_level'] = 1;
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']}<br />");
 
        // Init refid
-       $GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] = 0;
-       $old = 0; $minus = 0;
+       $GLOBALS['refback_refid'][$GLOBALS['refback_level']] = '0';
+       $old = '0'; $minus = '0';
 
        // Check for his referal
-       $result_refid = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE refid=%s ORDER BY userid ASC",
-               array(bigintval($uid)), __FUNCTION__, __LINE__);
+       $result_refid = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC",
+               array(bigintval($userid)), __FUNCTION__, __LINE__);
 
        // When no entry was updated then we have to create it here
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />\n";
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />");
        if (SQL_NUMROWS($result_refid) > 0) {
                // Load all refids
-               while (list($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]) = SQL_FETCHROW($result_refid)) {
+               while (list($GLOBALS['refback_refid'][$GLOBALS['refback_level']]) = SQL_FETCHROW($result_refid)) {
                        // Remmber userid
-                       $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]] = $uid;
+                       $GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']]] = $userid;
 
-                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]}<br />\n";
                        // Refid set?
-                       if (($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] > 0) && ($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] != $uid) && (isset($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]))) {
+                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]}<br />");
+                       if (($GLOBALS['refback_refid'][$GLOBALS['refback_level']] > 0) && ($GLOBALS['refback_refid'][$GLOBALS['refback_level']] != $userid) && (isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']]))) {
                                // New userid?
-                               if ((isset($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1])) && (isset($GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]]))) {
+                               if ((isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']-1])) && (isset($GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]]))) {
                                        // New userid!
-                                       $old = $uid;
-                                       $uid = $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]];
+                                       $old = $userid;
+                                       $userid = $GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]];
                                        $minus = 1;
-                                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},old={$old},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - NEW UID!<br />\n";
+                                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},old={$old},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - NEW UID!<br />");
                                } // END - if
 
                                // Check existence
-                               $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE userid=%s AND level='%s' AND refid=%s LIMIT 1",
-                                       array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                               $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`='%s' AND `refid`=%s LIMIT 1",
+                                       array(bigintval($userid), $GLOBALS['refback_level'], bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
 
                                // Do we have no entry?
-                               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />\n";
-                               if (SQL_NUMROWS($result) == 0) {
+                               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />");
+                               if (SQL_NUMROWS($result) == '0') {
                                        // Insert this level
-                                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - ADD!<br />\n";
-                                       SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
-                                               array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - ADD!<br />");
+                                       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
+                                               array(bigintval($userid), $GLOBALS['refback_level'], bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
 
                                        // Move to next referal level and count his counter one up!
-                                       $GLOBALS['cache_array']['back_level']++; UPDATE_REFBACK_TABLE($GLOBALS['cache_array']['up_refid'][($GLOBALS['cache_array']['back_level'] - 1)]);
+                                       $GLOBALS['refback_level']++; updateRefbackTable($GLOBALS['refback_refid'][($GLOBALS['refback_level'] - 1)]);
                                } // END - if
 
                                // Do we have another level here?
-                               if ((($GLOBALS['cache_array']['back_level']-$minus) > 0) && ($old > 0)) {
+                               if ((($GLOBALS['refback_level']-$minus) > 0) && ($old > 0)) {
                                        // Restore old one
-                                       $uid = $old;
+                                       $userid = $old;
 
                                        // Shall we add this as well?
-                                       $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE userid=%s AND level='%s' AND refid=%s LIMIT 1",
-                                               array(bigintval($uid), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                                       $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`='%s' AND `refid`=%s LIMIT 1",
+                                               array(bigintval($userid), ($GLOBALS['refback_level']-$minus), bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
 
                                        // Do we have no entry?
-                                       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />\n";
-                                       if (SQL_NUMROWS($result) == 0) {
+                                       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level=".($GLOBALS['refback_level']-$minus).",ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />");
+                                       if (SQL_NUMROWS($result) == '0') {
                                                // Insert this level
-                                               SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
-                                                       array(bigintval($uid), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
-                                               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - RETURNED!<br />\n";
+                                               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
+                                                       array(bigintval($userid), ($GLOBALS['refback_level']-$minus), bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
+                                               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level=".($GLOBALS['refback_level']-$minus).",ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - RETURNED!<br />");
                                        } // END - if
                                } // END - if
                        } // END - if
@@ -249,43 +253,53 @@ function UPDATE_REFBACK_TABLE ($uid) {
        } // END - if
 
        // When he has a referal...
-       if ((($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] == $uid) || ($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2") && (!isset($GLOBALS['cache_array']['back_cached']))) {
+       if ((($GLOBALS['refback_refid'][$GLOBALS['refback_level']] == $userid) || ($GLOBALS['refback_refid'][$GLOBALS['refback_level']] == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2')) && (!isset($GLOBALS['refback_cached']))) {
                // Remove cache here
-               //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - CACHE!<br />\n";
-               if ($GLOBALS['cache_instance']->loadCacheFile("refback")) $GLOBALS['cache_instance']->destroyCacheFile();
-               $GLOBALS['cache_array']['back_cached'] = 1;
+               //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - CACHE!<br />");
+               if ($GLOBALS['cache_instance']->loadCacheFile('refback')) $GLOBALS['cache_instance']->removeCacheFile();
+               $GLOBALS['refback_cached'] = 1;
        } // END - if
 
        // "Walk" back here
-       $GLOBALS['cache_array']['back_level']--;
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']} - LEVEL!<br />\n";
+       $GLOBALS['refback_level']--;
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']} - LEVEL!<br />");
 
        // Fix empty refid
-       if (!isset($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1])) $GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1] = 0;
-       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]} - BACK!<br />\n";
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       if (!isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']-1])) $GLOBALS['refback_refid'][$GLOBALS['refback_level']-1] = '0';
+       //* DEBUG: */ print(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]} - BACK!<br />");
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
 }
 
 // "Getter" for array for user refs in given level
-function GET_USER_REFS ($uid, $level) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
+function getArrayFromRefbackUserRefs ($userid, $level) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
        // Default is no refs and no nickname
-       $ADD = "";
+       $add = '';
        $refs = array();
 
        // Do we have nickname extension installed?
-       if (EXT_IS_ACTIVE("nickname")) {
-               $ADD = ", d.nickname";
+       if (isExtensionActive('nickname')) {
+               $add = ", d.nickname";
        } // END - if
 
        // Get refs from database
-       $result = SQL_QUERY_ESC("SELECT r.id, r.refid, r.refback, r.points, d.status".$ADD."
-FROM `{!_MYSQL_PREFIX!}_user_refs` AS r
-LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS d
-ON r.refid=d.userid
-WHERE r.userid=%s AND r.level=%s
-ORDER BY r.refid ASC",
-               array(bigintval($uid), bigintval($level)), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT
+       r.id,
+       r.refid,
+       r.refback,
+       r.points,
+       d.status".$add."
+FROM
+       `{?_MYSQL_PREFIX?}_user_refs` AS r
+LEFT JOIN
+       `{?_MYSQL_PREFIX?}_user_data` AS d
+ON
+       r.refid=d.userid
+WHERE
+       r.userid=%s AND r.level=%s
+ORDER BY
+       r.refid ASC",
+               array(bigintval($userid), bigintval($level)), __FUNCTION__, __LINE__);
 
        // Are there some entries?
        if (SQL_NUMROWS($result) > 0) {
@@ -300,13 +314,13 @@ ORDER BY r.refid ASC",
        SQL_FREERESULT($result);
 
        // Return result
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
        return $refs;
 }
 
 // "Getter" for refback entry
-function GET_USER_REF_ENTRY ($id) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
+function getArrayFromUserRefbackData ($id) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
        // Default data is nothing
        $data = array();
 
@@ -314,8 +328,8 @@ function GET_USER_REF_ENTRY ($id) {
        $id = bigintval($id);
 
        // Get entry from database
-       $result = SQL_QUERY_ESC("SELECT id, refid, refback, level FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE id=%s AND userid=%s LIMIT 1",
-               array($id, getUserId()), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT `id`, `refid`, `refback`, `level` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1",
+               array($id, getMemberId()), __FUNCTION__, __LINE__);
 
        // Is there an entry?
        if (SQL_NUMROWS($result) == 1) {
@@ -327,22 +341,22 @@ function GET_USER_REF_ENTRY ($id) {
        SQL_FREERESULT($result);
 
        // Return entry
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
        return $data;
 }
 
 // Update refback percents (but with some sanity-checks!
-function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
-       //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
+function updateMemberRefbackPercents ($id, $percents) {
+       //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
        // Default status is failed
        $status['message'] = getMessage('MEMBER_REFBACK_ERROR_GENERAL');
 
        // Do some sanity-checks
-       if (!IS_MEMBER()) {
+       if (!isMember()) {
                // No member!
                $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NO_MEMBER');
                return $status;
-       } elseif ("".$id."" != "".($id + 0)."") {
+       } elseif (''.$id.'' != ''.($id + 0).'') {
                // No number!
                $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER');
                return $status;
@@ -353,21 +367,21 @@ function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
        }
 
        // Get ref entry
-       $dummy = GET_USER_REF_ENTRY($id);
+       $dummy = getArrayFromUserRefbackData($id);
 
        // Is this valid?
-       if (count($dummy) == 0) {
-               // ID does not belong to user!
+       if (count($dummy) == '0') {
+               // id does not belong to user!
                $status['message'] = getMessage('MEMBER_REFBACK_ERROR_ID_MISMATCH');
                return $status;
        } // END - if
 
        // Revert german commata
-       $percents = REVERT_COMMA($percents);
+       $percents = convertCommaToDot($percents);
 
        // Update entry
-       SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET refback='%s' WHERE id=%s AND userid=%s LIMIT 1",
-               array($percents, $id, getUserId()), __FUNCTION__, __LINE__);
+       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `refback`='%s' WHERE `id`=%s AND `userid`=%s LIMIT 1",
+               array($percents, $id, getMemberId()), __FUNCTION__, __LINE__);
 
        // Entry updated?
        if (SQL_AFFECTEDROWS() < 1) {
@@ -378,25 +392,25 @@ function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
 
        // Prepare email content
        $content = array(
-               'percents' => TRANSLATE_COMMA($percents),
+               'percents' => translateComma($percents),
                'refid'    => $dummy['refid'],
                'level'    => $dummy['level']
        );
 
        // Load member email template
-       $mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, getUserId());
+       $mail = loadEmailTemplate('member_refback', $content, getMemberId());
 
        // Send email to user
-       SEND_EMAIL($dummy['refid'], getMessage('MEMBER_REFBACK_SUBJECT'), $mail);
+       sendEmail($dummy['refid'], getMessage('MEMBER_REFBACK_SUBJECT'), $mail);
 
        // Send admin notification
-       SEND_ADMIN_NOTIFICATION(getMessage('ADMIN_REFBACK_SUBJECT'), "admin_refback", $content, getUserId());
+       sendAdminNotification(getMessage('ADMIN_REFBACK_SUBJECT'), 'admin_refback', $content, getMemberId());
 
        // All fine!
        $status['ok'] = true;
 
        // Return status array
-       //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
+       //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
        return $status;
 }
 //