Refback will be payed now (user cannot setup currently)
authorRoland Häder <roland@mxchange.org>
Sun, 28 Sep 2008 01:30:00 +0000 (01:30 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 28 Sep 2008 01:30:00 +0000 (01:30 +0000)
13 files changed:
.gitattributes
inc/databases.php
inc/extensions.php
inc/extensions/ext-sql_patches.php
inc/gen_refback.php [new file with mode: 0644]
inc/language/de.php
inc/libs/bonus_functions.php
inc/libs/rallye_functions.php
inc/modules/admin/what-config_points.php
inc/modules/admin/what-config_rallye_prices.php
inc/modules/admin/what-list_refs.php
inc/modules/member/what-points.php
inc/mysql-manager.php

index 8782e9a..efd717c 100644 (file)
@@ -145,6 +145,7 @@ inc/fatal_errors.php -text
 inc/footer.php -text
 inc/functions.php -text
 inc/gen_mediadata.php -text
+inc/gen_refback.php -text
 inc/gen_sql_patches.php -text
 inc/header.php -text
 inc/install-inc.php -text
index 0507ac7..92a9536 100644 (file)
@@ -113,7 +113,7 @@ define('USAGE_BASE', "usage");
 define('SERVER_URL', "http://www.mxchange.org");
 
 // This current patch level
-define('CURR_SVN_REVISION', "408");
+define('CURR_SVN_REVISION', "409");
 
 // Take a prime number which is long (if you know a longer one please try it out!)
 define('_PRIME', 591623);
index 0e2b924..1851e5d 100644 (file)
@@ -401,7 +401,12 @@ function GET_EXT_VERSION ($ext_name) {
 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
 {
        // This shall never do a non-admin user!
-       global $cacheInstance, $_CONFIG, $NOTES; $SQLs = array();
+       global $cacheInstance, $_CONFIG, $NOTES;
+
+       // Init arrays
+       $SQLs = array(); $INC_POOL = array();
+
+       // Only admins are allowed to update extensions
        if ((!IS_ADMIN()) || (empty($ext))) return false;
 
        // Load extension in update mode
@@ -455,9 +460,16 @@ function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
                                }
                        }
 
+                       // In real-mode execute any existing includes
+                       if ((!$dry_run) && (count($INC_POOL) > 0)) {
+                               // Include all files
+                               foreach ($INC_POOL as $fqfn) {
+                                       require_once($fqfn);
+                               } // END - foreach
+                       } // END - if
+
                        // Run SQLs
-                       if ((is_array($SQLs)) && (!$dry_run))
-                       {
+                       if ((is_array($SQLs)) && (!$dry_run)) {
                                // Run SQL commands
                                foreach ($SQLs as $sql)
                                {
@@ -619,5 +631,6 @@ function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
        // Now compare both and return the result
        return ($currVersion < $ext_ver);
 }
+
 //
 ?>
index 7e99c8b..4517ec3 100644 (file)
@@ -38,13 +38,13 @@ if (!defined('__SECURITY')) {
 }
 
 // Version number
-$EXT_VERSION = "0.4.9";
+$EXT_VERSION = "0.5.0";
 
 // Auto-set extension version
 if (!isset($EXT_VER)) $EXT_VER = $EXT_VERSION;
 
 // Version history array (add more with , "0.1" and so on)
-$EXT_VER_HISTORY = array("0.0", "0.0.1", "0.0.2", "0.0.3", "0.0.4", "0.0.5", "0.0.6", "0.0.7", "0.0.8", "0.0.9", "0.1.0", "0.1.1", "0.1.2", "0.1.3", "0.1.4", "0.1.5", "0.1.6", "0.1.7", "0.1.8", "0.1.9", "0.2.0", "0.2.1", "0.2.2", "0.2.3", "0.2.4", "0.2.5", "0.2.6", "0.2.7", "0.2.8", "0.2.9", "0.3.0", "0.3.1", "0.3.2", "0.3.3", "0.3.4", "0.3.5", "0.3.6", "0.3.7", "0.3.8", "0.3.9", "0.4.0", "0.4.1", "0.4.2", "0.4.3", "0.4.4", "0.4.5", "0.4.6", "0.4.7", "0.4.8", "0.4.9");
+$EXT_VER_HISTORY = array("0.0", "0.0.1", "0.0.2", "0.0.3", "0.0.4", "0.0.5", "0.0.6", "0.0.7", "0.0.8", "0.0.9", "0.1.0", "0.1.1", "0.1.2", "0.1.3", "0.1.4", "0.1.5", "0.1.6", "0.1.7", "0.1.8", "0.1.9", "0.2.0", "0.2.1", "0.2.2", "0.2.3", "0.2.4", "0.2.5", "0.2.6", "0.2.7", "0.2.8", "0.2.9", "0.3.0", "0.3.1", "0.3.2", "0.3.3", "0.3.4", "0.3.5", "0.3.6", "0.3.7", "0.3.8", "0.3.9", "0.4.0", "0.4.1", "0.4.2", "0.4.3", "0.4.4", "0.4.5", "0.4.6", "0.4.7", "0.4.8", "0.4.9", "0.5.0");
 
 switch ($EXT_LOAD_MODE)
 {
@@ -85,6 +85,7 @@ action='account'";
        $SQLs[] = "DROP TABLE IF EXISTS "._MYSQL_PREFIX."_admin_menu_las";
        $SQLs[] = "DROP TABLE IF EXISTS "._MYSQL_PREFIX."_admin_menu_las_data";
        $SQLs[] = "DROP TABLE IF EXISTS "._MYSQL_PREFIX."_admin_menu_stats";
+       $SQLs[] = "DROP TABLE IF EXISTS "._MYSQL_PREFIX."_user_refs";
 
        // Drop indexes
        $SQLs[] = "ALTER TABLE "._MYSQL_PREFIX."_extensions DROP UNIQUE KEY (ext_name)";
@@ -147,7 +148,7 @@ case "update": // Update an extension
                $SQLs[] = "ALTER TABLE "._MYSQL_PREFIX."_config CHANGE auto_purge auto_purge BIGINT(20) UNSIGNED NOT NULL DEFAULT '1209600'";
 
                // Update notes (these will be set as task text!)
-               $UPDATE_NOTES = "In der Tabelle <STRONG>mxchange_config</STRONG> musste die Spalte <STRONG>auto_purge</STRONG> (autom. L&ouml;schen von Best&auml;tigungsmails angepasst werden (war auf dem Testsystem auf tiny(4) gesetzt.)";
+               $UPDATE_NOTES = "In der Tabelle <STRONG>"._MYSQL_PREFIX."_config</STRONG> musste die Spalte <STRONG>auto_purge</STRONG> (autom. L&ouml;schen von Best&auml;tigungsmails angepasst werden (war auf dem Testsystem auf tiny(4) gesetzt.)";
                break;
 
        case "0.0.3": // SQL queries for v0.0.3
@@ -632,13 +633,37 @@ PRIMARY KEY (id)
                // Update notes (these will be set as task text!)
                $UPDATE_NOTES = "User-Hash zum Best&auml;tigen wird auf NULL gesetzt, wenn Account best&auml;tigt wird.";
                break;
+
+       case "0.5.0": // SQL queries for v0.5.0
+               $SQLs[] = "ALTER TABLE `"._MYSQL_PREFIX."_refsystem` DROP INDEX `level`";
+               $SQLs[] = "ALTER TABLE `"._MYSQL_PREFIX."_refsystem` DROP INDEX `userid`, ADD UNIQUE `userid_level` (`userid`,`level`)";
+               $SQLs[] = "DROP TABLE IF EXISTS `"._MYSQL_PREFIX."_user_refs`";
+               $SQLs[] = "CREATE TABLE `"._MYSQL_PREFIX."_user_refs` (
+`id` bigint(20) NOT NULL auto_increment,
+`userid` bigint(20) NOT NULL DEFAULT 0,
+`level` smallint(6) NOT NULL DEFAULT 0,
+`refid` bigint(20) NOT NULL DEFAULT 0,
+`refback` float(4,1) NOT NULL DEFAULT 0.0,
+PRIMARY KEY (`id`),
+UNIQUE `user_refid` (`userid`,`level`,`refid`),
+KEY (`level`),
+KEY (`refid`),
+) ENGINE=MyISAM COMMENT='User Referals With Refback'";
+
+               // Update notes (these will be set as task text!)
+               $UPDATE_NOTES = "Referal-System unterst&uuml;tzt nun detailierte Referal-&Uuml;bersicht und vieles mehr.";
+
+               // Execute this special file on this update
+               $INC_POOL[] = PATH."inc/gen_refback.php";
+               break;
        }
        break;
 
 default: // Do stuff when extension is loaded
        // When the sql_patches is not installed we cannot load it's configuration... *sigh*
        if (GET_EXT_VERSION("sql_patches") != '') {
-               $_CONFIG['secret_key']        = "";
+               // Init key
+               $_CONFIG['secret_key'] = "";
 
                // Read key from secret file
                if ((empty($_CONFIG['file_hash'])) || (empty($_CONFIG['master_salt'])) || (empty($_CONFIG['pass_scramble']))) {
@@ -697,4 +722,4 @@ $EXT_LANG_PREFIX = "sql_patches";
 $EXT_ALWAYS_ACTIVE = "Y";
 
 //
-?>
\ No newline at end of file
+?>
diff --git a/inc/gen_refback.php b/inc/gen_refback.php
new file mode 100644 (file)
index 0000000..3e79ad1
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+/************************************************************************
+ * MXChange v0.2.1                                    Start: 09/27/2008 *
+ * ===============                              Last change: 09/27/2008 *
+ *                                                                      *
+ * -------------------------------------------------------------------- *
+ * File              : gen_refback.php                                  *
+ * -------------------------------------------------------------------- *
+ * Short description : Generate refback table including all refs        *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung  : Refback-Tabelle mit allen Refs generieren        *
+ * -------------------------------------------------------------------- *
+ *                                                                      *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2008 by Roland Haeder                           *
+ * For more information visit: http://www.mxchange.org                  *
+ *                                                                      *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or    *
+ * (at your option) any later version.                                  *
+ *                                                                      *
+ * This program is distributed in the hope that it will be useful,      *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+ * GNU General Public License for more details.                         *
+ *                                                                      *
+ * You should have received a copy of the GNU General Public License    *
+ * along with this program; if not, write to the Free Software          *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
+ * MA  02110-1301  USA                                                  *
+ ************************************************************************/
+
+// Some security stuff...
+if (!defined('__SECURITY')) {
+       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
+       require($INC);
+}
+
+// Sanity-check: Do we have ref level 0?
+$result = SQL_QUERY("SELECT id FROM "._MYSQL_PREFIX."_refsystem WHERE level=0", __FILE__, __LINE__);
+if (SQL_NUMROWS($result) > 0) {
+       // Upgrade refsystem, ref-level 0 is silly here
+       SQL_QUERY("UPDATE "._MYSQL_PREFIX."_refsystem SET level=level+1", __FILE__, __LINE__);
+} // END - if
+
+// Free result
+SQL_FREERESULT($result);
+
+// Reset the table
+SQL_QUERY("TRUNCATE TABLE "._MYSQL_PREFIX."_user_refs", __FILE__, __LINE__);
+
+// Begin with all users in level one
+$result_direct = SQL_QUERY("SELECT u.userid
+FROM "._MYSQL_PREFIX."_user_data AS u
+WHERE u.refid=0
+ORDER BY u.userid ASC", __FILE__, __LINE__);
+
+// Do we have entries?
+if (SQL_NUMROWS($result_direct) > 0) {
+       global $cacheArray;
+
+       // When "walk" through all users
+       while (list($uid) = SQL_FETCHROW($result_direct)) {
+               // Init level
+               $cacheArray['back_level'] = 1;
+
+               // Update refback table
+               /* DEBUG: */ echo "uid={$uid}<br />\n";
+               UPDATE_REFBACK_TABLE($uid);
+       } // END - while
+} // END - if
+
+// Free result
+SQL_FREERESULT($result_direct);
+
+//
+?>
index 1a19fe5..03305c6 100644 (file)
@@ -517,8 +517,7 @@ define('ADMIN_ALL_ACCOUNTS', "Alle Mitglieder-Accounts auflisten");
 define('ADMIN_LIST_LOCKED_ACCOUNTS', "Alle gesperrten Mitglieder-Accounts auflisten");
 define('ADMIN_LIST_CONFIRMED_ACCOUNTS', "Alle best&auml;tigten Mitglieder-Accounts auflisten");
 define('ADMIN_NO_NONE_REGISTERED', "Ihre Suchkriterieren ergaben keine Treffer in der Mitgliederliste. ");
-define('ADMIN_MEMBER_404_1', "Mitglieds-Account <strong>");
-define('ADMIN_MEMBER_404_2', "</strong> existiert nicht!");
+define('ADMIN_MEMBER_404', "Mitglieds-Account <strong>%s</strong> existiert nicht!");
 define('ADMIN_LAST_ONLINE', "Zuletzt aktiv");
 define('ADMIN_LAST_MODULE', "Letzte Aktion");
 define('RECEIVE_MAILS', "Kann Mails empfangen");
@@ -1045,8 +1044,10 @@ define('ADMIN_ENTER_BANNER_URL', "Banner-URL");
 define('ADMIN_ENTER_ALTERNATE_TEXT', "Alternativen Text");
 define('ADMIN_SELECT_BANNER_ACTIVE', "Ist der Banner aktiv?");
 define('ADMIN_SUBMIT_BANNER', "Banner hinzuf&uuml;gen");
-define('ADMIN_LIST_REFERRALS_1', "Referrals des Mitgliedes ");
-define('ADMIN_LIST_REFERRALS_2', " in erster Ebene");
+define('ADMIN_LIST_REFERRALS_1', "Referral-&Uuml;bersicht des Mitgliedes ");
+define('ADMIN_LIST_REFERRALS_2', " in allen Ebenen:");
+define('ADMIN_LIST_REFERRALS_LEVEL_1', "Referrals in Ebene ");
+define('ADMIN_LIST_REFERRALS_LEVEL_2', ":");
 define('ADMIN_LIST_REFERRALS_NOTE', "<strong>&Auml;nderung:</strong> Klicken Sie die User-ID an und gelangen Sie zum Mitgliederprofil. Klicken Sie Anzahl Referrals an, werden die Referrals des jeweiligen Mitgliedes aufgelistet.");
 define('ADMIN_NORMAL_MAIL_ALREADY_DELETED', "Mitglieder-Mail bereits gel&ouml;scht!");
 define('INITIAL_RELEASE', "Erste Alpha-Version");
@@ -1168,6 +1169,7 @@ define('MEMBER_ORDER_TEST_STARTED', "Framekiller-Test startet...");
 define('MEMBER_ORDER_PLEASE_WAIT_1', "Ihre Seite wird auf Framekiller getestet. Bitte noch ");
 define('MEMBER_ORDER_PLEASE_WAIT_2', " Sekunden warten.");
 define('ADMIN_LIST_NOREFS_ACCOUNTS', "Auflistung aller Accounts ohne Werber");
+define('ADMIN_NO_REF_LEVELS', "In Ihrem {!MT_WORD!} sind keine Referal-Ebenen eingerichtet! Ihre Mitglieder erhalten <u>keine</u> {!POINTS!} gutgeschrieben. Richten Sie mindestens Referal-Ebene 0 mit 100% Verg&uuml;tung ein.");
 
 //
 ?>
index 1404119..b132a99 100644 (file)
@@ -138,7 +138,7 @@ function BONUS_MAKE_RANK_ROWS($data, $type, $uid)
                for ($rank = 1; $rank <= $max; $rank++)
                {
                        // Load data
-                       $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s AND level='%s' LIMIT 1",
+                       $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s AND level=%s LIMIT 1",
                         array($type, $data, $rank), __FILE__, __LINE__);
                        list($userid, $points, $mark) = SQL_FETCHROW($result_users);
 
index 1b4798f..643c53f 100644 (file)
@@ -358,19 +358,13 @@ function RALLYE_EXPIRE_RALLYES($result)
        global $DATA, $_CONFIG;
 
        // Latest online time
-       $since = 0; $min_users = 0; $min_prices = 0;
+       $since = 0;
        if (EXT_IS_ACTIVE("autopurge")) {
                $since = $_CONFIG['ap_inactive_since'];
        } // END - if
 
        // Load rallye data
-       if (GET_EXT_VERSION("rallye") >= "0.2.0") {
-               list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
-       } else {
-               list($id, $title, $start, $end, $notify) = SQL_FETCHROW($result);
-       }
-
-       // Free result
+       list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
        SQL_FREERESULT($result);
 
        // Load users array (!) with assigned prices
@@ -759,7 +753,7 @@ function RALLYE_GET_REFCOUNT($uid, $old=0)
 {
        global $_CONFIG, $cacheArray;
        // Check current refs
-       if ((GET_EXT_VERSION("cache") >= "0.1.2") && (isset($cacheArray['ref_system'])))
+       if (GET_EXT_VERSION("cache") >= "0.1.2")
        {
                // Get refs from cache
                $cnt = 0;
@@ -808,7 +802,7 @@ function RALLYE_GET_REFCOUNT($uid, $old=0)
 FROM "._MYSQL_PREFIX."_refsystem AS s
 LEFT JOIN "._MYSQL_PREFIX."_refdepths AS d
 ON s.level=d.level
-WHERE s.userid=%s AND s.level=0", array(bigintval($uid)), __FILE__, __LINE__);
+WHERE s.userid=%s AND s.level=1", array(bigintval($uid)), __FILE__, __LINE__);
                list($cnt) = SQL_FETCHROW($result_ref);
                SQL_FREERESULT($result_ref);
                if (empty($cnt))
index 8307ed9..7241b97 100644 (file)
@@ -83,7 +83,7 @@ if (isset($_POST['ok'])) {
                                $id = bigintval($id);
 
                                // Update entry
-                               $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_refdepths SET level='%s', percents='%s' WHERE id=%s LIMIT 1",
+                               $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_refdepths SET level=%s, percents='%s' WHERE id=%s LIMIT 1",
                                 array(bigintval($value), $_POST['perc'][$id], $id), __FILE__, __LINE__);
                        }
                        $TEXT = REF_DEPTHS_SAVED;
@@ -134,7 +134,7 @@ WHERE mails_confirmed < %s", $REF, $REF);
        {
                if (strpos($SQL[0], "INSERT") > -1)
                {
-                       $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_refdepths WHERE level='%s' LIMIT 1",
+                       $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_refdepths WHERE level=%s LIMIT 1",
                         array(bigintval($_POST['lvl'])), __FILE__, __LINE__);
                        SQL_FREERESULT($result);
                }
index 463e7ed..6515750 100644 (file)
@@ -48,7 +48,7 @@ if (!empty($_GET['rallye']))
                if ((!empty($_POST['level'])) && ((!empty($_POST['points'])) || (!empty($_POST['info']))))
                {
                        // Submitted data is valid, but maybe we already have this price level?
-                       $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_rallye_prices WHERE rallye_id=%s AND price_level='%s' LIMIT 1",
+                       $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_rallye_prices WHERE rallye_id=%s AND price_level=%s LIMIT 1",
                         array(bigintval($_GET['rallye']), bigintval($_POST['level'])), __FILE__, __LINE__);
 
                        if (SQL_NUMROWS($result) == 0)
@@ -104,7 +104,7 @@ VALUES ('%s','%s','%s','%s')",
                        $id = bigintval($id);
 
                        // Update entry
-                       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_rallye_prices SET rallye_id=%s, price_level='%s', points='%s', info='%s' WHERE id=%s LIMIT 1",
+                       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_rallye_prices SET rallye_id=%s, price_level=%s, points='%s', info='%s' WHERE id=%s LIMIT 1",
                         array($_POST['rallye_id'][$id], bigintval($level), $_POST['points'][$id], $_POST['infos'][$id], $id), __FILE__, __LINE__);
                }
 
index 9cb4932..83b1760 100644 (file)
@@ -46,90 +46,61 @@ if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
 // Add description as navigation point
 ADD_DESCR("admin", basename(__FILE__));
 
-if (!empty($_GET['u_id']))
-{
+if (!empty($_GET['u_id'])) {
        // Check if the user already exists
        $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
         array(bigintval($_GET['u_id'])), __FILE__, __LINE__);
-       if (SQL_NUMROWS($result) == 1)
-       {
-               // Free memory
-               SQL_FREERESULT($result);
+       if (SQL_NUMROWS($result) == 1) {
+               // Load all referal levels
+               $result_levels = SQL_QUERY_ESC("SELECT d.level, d.percents, r.counter
+FROM "._MYSQL_PREFIX."_refdepths AS d
+LEFT JOIN "._MYSQL_PREFIX."_refsystem AS r
+ON d.level=r.level
+WHERE r.userid=%s
+ORDER BY d.level ASC",
+                       array(bigintval($_GET['u_id'])), __FILE__, __LINE__);
 
-               // Loads surname, family's name and the email address
-               $result     = SQL_QUERY_ESC("SELECT COUNT(*) FROM "._MYSQL_PREFIX."_user_data WHERE refid=%s",
-                array(bigintval($_GET['u_id'])), __FILE__, __LINE__);
-               $result_lck = SQL_QUERY_ESC("SELECT COUNT(*) FROM "._MYSQL_PREFIX."_user_data WHERE refid=%s AND status != 'CONFIRMED' ORDER BY userid",
-                array(bigintval($_GET['u_id'])), __FILE__, __LINE__);
-               $menge      = SQL_RESULT($result    , 0, 0);
-               $menge_lck  = SQL_RESULT($result_lck, 0, 0);
+               // Are there some levels (VERY BAD IF NONE!)
+               if (SQL_NUMROWS($result_levels) > 0) {
+                       // List all ref levels
+                       $OUT = ""; $SW = 2;
+                       while ($levels = SQL_FETCHARRAY($result_levels)) {
+                               // Insert more data
+                               $levels['sw']    = $SW;
+                               $levels['level'] = ($levels['level'] + 1);
 
-               // Free memory
-               SQL_FREERESULT($result);
-               SQL_FREERESULT($result_lck);
+                               // Load all refs of this user
+                               //$result_refs = SQL_QUERY_ESC("SELECT 
+                               // Load level template
+                               $OUT .= LOAD_TEMPLATE("admin_list_refs_level", true, $levels);
 
-               $result = SQL_QUERY_ESC("SELECT userid, gender, surname, family, email, status, joined FROM "._MYSQL_PREFIX."_user_data WHERE refid=%s ORDER BY userid",
-                array(bigintval($_GET['u_id'])), __FILE__, __LINE__);
+                               // Switch colors
+                               $SW = 3 - $SW;
+                       } // END - if
 
-               OUTPUT_HTML(ADMIN_TOTAL_REFS_1."".ADMIN_USER_PROFILE_LINK($_GET['u_id'])."".ADMIN_TOTAL_REFS_2.$menge.ADMIN_TOTAL_REFS_3.$menge_lck.ADMIN_TOTAL_REFS_4."<br /><br />");
-               if ($result)
-               {
-                       $rows = SQL_NUMROWS($result);
-                       if ($rows > 0)
-                       {
-                               // Load all referrals
-                               $SW = "2"; $OUT = "";
-                               while ($row = SQL_FETCHROW($result))
-                               {
-                                       // Check for referrals
-                                       $result_refs = SQL_QUERY_ESC("SELECT COUNT(userid) FROM "._MYSQL_PREFIX."_user_data WHERE refid=%s",
-                                        array(bigintval($row[0])), __FILE__, __LINE__);
-                                       $refs_cnt = SQL_RESULT($result_refs, 0, 0);
-                                       SQL_FREERESULT($result_refs);
+                       // Prepare content
+                       $content = array(
+                               'rows' => $OUT,
+                               'uid'  => ADMIN_USER_PROFILE_LINK($_GET['u_id'])
+                       );
 
-                                       // Prepare data for the template
-                                       $content = array(
-                                               'sw'         => $SW,
-                                               'u_link'     => ADMIN_USER_PROFILE_LINK($row[0]),
-                                               'gender'     => TRANSLATE_GENDER($row[1]),
-                                               'refs_link'  => "0",
-                                               'surname'    => $row[2],
-                                               'family'     => $row[3],
-                                               'email'      => "<A href=\"".CREATE_EMAIL_LINK($row[4], "user_data")."\">".$row[4]."</A>",
-                                               'status'     => TRANSLATE_STATUS($row[5]),
-                                               'registered' => MAKE_DATETIME($row[6], "3"),
-                                       );
-
-                                       // Check if referral count is larger 0 and update link
-                                       if ($refs_cnt > 0) $content['refs_link'] = ADMIN_USER_PROFILE_LINK($row[0], $refs_cnt, "list_refs");
-
-                                       // Load row template and switch color
-                                       $OUT .= LOAD_TEMPLATE("admin_list_refs_row", true, $content);
-                                       $SW = 3 - $SW;
-                               }
-
-                               // Free memory
-                               SQL_FREERESULT($result);
-                               define('__REF_ROWS', $OUT);
-
-                               // Load main template
-                               LOAD_TEMPLATE("admin_list_refs");
-                       }
-               }
-                else
-               {
-                       // No referrals made so far
-                       LOAD_TEMPLATE("admin_settings_saved", false, USER_REF_404);
+                       // Load mein template
+                       LOAD_TEMPLATE("admin_list_refs", false, $content);
+               } else {
+                       // No levels found, very bad!
+                       LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NO_REF_LEVELS);
                }
-       }
-        else
-       {
+
+               // Free result
+               SQL_FREERESULT($result_levels);
+       } else {
                // User not found
-               LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_MEMBER_404_1.$_GET['u_id'].ADMIN_MEMBER_404_2);
+               LOAD_TEMPLATE("admin_settings_saved", false, sprintf(ADMIN_MEMBER_404, $_GET['u_id']));
        }
-}
- else
-{
+
+       // Free memory
+       SQL_FREERESULT($result);
+} else {
        // Output selection form with all confirmed user accounts listed
        ADD_MEMBER_SELECTION_BOX();
 }
index d146a2c..0cb4d3c 100644 (file)
@@ -64,16 +64,17 @@ while (list($lvl, $per) = SQL_FETCHROW($result_depths)) {
        // Load referral points
        $result_points = SQL_QUERY_ESC("SELECT points, locked_points FROM "._MYSQL_PREFIX."_user_points WHERE userid=%s AND ref_depth=%d LIMIT 1", array($GLOBALS['userid'], bigintval($lvl)), __FILE__, __LINE__);
        if (SQL_NUMROWS($result_points) == 1) {
-               list($points, $LOCKED) = SQL_FETCHROW($result_points);
+               list($points, $locked) = SQL_FETCHROW($result_points);
                SQL_FREERESULT($result_points);
                // Also count locked points
-               $TPTS += $points; $TLOCK += $LOCKED;
+               $TPTS += $points; $TLOCK += $locked;
        } else {
-               $points = "0.00000"; $LOCKED = "0.00000";
+               $points = "0.00000"; $locked = "0.00000";
        }
 
        // Load referral counts
-       $result_refs = SQL_QUERY_ESC("SELECT counter FROM "._MYSQL_PREFIX."_refsystem WHERE userid=%s AND level='%s' LIMIT 1", array($GLOBALS['userid'], bigintval($lvl)), __FILE__, __LINE__);
+       $result_refs = SQL_QUERY_ESC("SELECT counter FROM "._MYSQL_PREFIX."_refsystem WHERE userid=%s AND level=%s LIMIT 1",
+               array($GLOBALS['userid'], bigintval($lvl)), __FILE__, __LINE__);
        if (SQL_NUMROWS($result_refs) == 1) {
                list($REFS) = SQL_FETCHROW($result_refs);
                SQL_FREERESULT($result_refs);
index a54357d..40cde51 100644 (file)
@@ -1086,6 +1086,42 @@ function GET_TOTAL_DATA($search, $tableName, $lookFor, $whereStatement="userid",
        // Return value
        return $ret;
 }
+// "Getter fro ref level percents
+function GET_REF_LEVEL_PERCENTS ($level) {
+       global $cacheInstance, $_CONFIG, $cacheArray;
+
+       // Default is zero
+       $per = 0;
+
+       // Do we have cache?
+       if ((isset($cacheArray['ref_depths']['level'])) && (EXT_IS_ACTIVE("cache"))) {
+               // First look for level
+               $key = array_search($level, $cacheArray['ref_depths']['level']);
+               if ($key !== false) {
+                       // Entry found!
+                       $per = $cacheArray['ref_depths']['percents'][$key];
+
+                       // Count cache hit
+                       $_CONFIG['cache_hits']++;
+               }
+       } else {
+               // Get referal data
+               $result_lvl = SQL_QUERY_ESC("SELECT percents FROM "._MYSQL_PREFIX."_refdepths WHERE level=%s LIMIT 1",
+                       array(bigintval($level)), __FILE__, __LINE__);
+
+               // Entry found?
+               if (SQL_NUMROWS($result_lvl) == 1) {
+                       // Get percents
+                       list($per) = SQL_FETCHROW($result_lvl);
+               } // END - if
+
+               // Free result
+               SQL_FREERESULT($result_lvl);
+       }
+
+       // Return percent
+       return $per;
+}
 /**
  *
  * Dynamic referral system, can also send mails!
@@ -1099,7 +1135,7 @@ function GET_TOTAL_DATA($search, $tableName, $lookFor, $whereStatement="userid",
  *               for default value will cause no referral will get points ever!!!)
  */
 function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $locked=false, $add_mode="ref") {
-       global $DEPTH, $_CONFIG, $DATA;
+       global $DEPTH, $_CONFIG, $DATA, $cacheArray;
 
        // Convert mode to lower-case
        $add_mode = strtolower($add_mode);
@@ -1115,7 +1151,7 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
        }
 
        // Count up referral depth
-       if (empty($DEPTH)) {
+       if (!isset($DEPTH)) {
                // Initialialize referral system
                $DEPTH = 0;
        } else {
@@ -1123,10 +1159,6 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
                $DEPTH++;
        }
 
-       // Percents and table
-       $percents = "percents"; if (isset($_CONFIG['db_percents'])) $percents = $_CONFIG['db_percents'];
-       $table = "refdepths";   if (isset($_CONFIG['db_table']))    $table    = $_CONFIG['db_table'];
-
        // Default is "normal" points
        $data = "points";
 
@@ -1140,26 +1172,33 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
        //* DEBUG */ echo "+".SQL_NUMROWS($result_user).":".$points."+<br />\n";
        if (SQL_NUMROWS($result_user) == 1) {
                // This is the user and his ref
-               list ($ref, $email) = SQL_FETCHROW($result_user);
+               list($ref, $email) = SQL_FETCHROW($result_user);
+               $cacheArray['add_uid'][$ref] = $uid;
 
                // Debug message
                //DEBUG_LOG(__FUNCTION__.": ref={$ref},email={$email},DEPTH={$DEPTH}");
 
-               // Get referal data
-               $result_lvl = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE level='%s' LIMIT 1",
-                array($percents, $table, bigintval($DEPTH)), __FILE__, __LINE__);
-               //* DEBUG */ echo "DEPTH:".$DEPTH."<br />\n";
-               if (SQL_NUMROWS($result_lvl) == 1) {
-                       // Get percents
-                       list($per) = SQL_FETCHROW($result_lvl);
+               // Get percents
+               $per = GET_REF_LEVEL_PERCENTS($DEPTH);
+               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},points={$points},depth={$DEPTH},per={$per},mode={$add_mode}<br />\n";
 
+               // Some percents found?
+               if ($per > 0) {
                        // Calculate new points
                        $ref_points = $points * $per / 100;
 
+                       // Pay refback here if level > 0 and in ref-mode
+                       if (($DEPTH > 0) && ($per < 100) && ($add_mode == "ref")) {
+                               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},data={$cacheArray['add_uid'][$uid]},ref_points={$ref_points},depth={$DEPTH} - BEFORE!<br />\n";
+                               $ref_points = ADD_REFBACK_POINTS($cacheArray['add_uid'][$uid], $uid, $ref_points, $DEPTH);
+                               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},data={$cacheArray['add_uid'][$uid]},ref_points={$ref_points},depth={$DEPTH} - AFTER!<br />\n";
+                       } // END - if
+
                        // Debug message
                        //DEBUG_LOG(__FUNCTION__.": percent={$per},ref_points={$ref_points}");
 
                        // Update points...
+                       //* DEBUG: */ echo __FUNCTION__.":data={$data},ref_points={$ref_points},uid={$uid},depth={$DEPTH} - UPDATE!<br />\n";
                        SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET %s=%s+%s WHERE userid=%s AND ref_depth=%d LIMIT 1",
                         array($data, $data, $ref_points, bigintval($uid), bigintval($DEPTH)), __FILE__, __LINE__);
 
@@ -1218,55 +1257,254 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
                        if (($ref > 0) && ($points > 0) && ($ref != $uid) && ($add_mode == "ref")) {
                                // Then let's credit him here...
                                ADD_POINTS_REFSYSTEM($ref, $points, $send_notify, $ref, $locked);
-                       }
-               }
+                       } // END - if
+               } // END - if
+       } // END - if
 
-               // Free result
-               SQL_FREERESULT($result_lvl);
+       // Free result
+       SQL_FREERESULT($result_user);
+}
+// Payback refback for refid and reduce it for current user
+function ADD_REFBACK_POINTS ($uid, $ref, $points) {
+       global $DEPTH, $cacheArray;
+
+       // Init points
+       $return = $points;
+
+       // "Walk through all refids
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},ref={$ref},points={$points}<br />\n";
+       foreach (GET_REFBACK_USERID_ARRAY($uid) as $refid) {
+               // Skip level zero or if both are the same
+               if ($uid == $refid) continue;
+
+               // Get refback percents
+               $percents = GET_REFBACK_PERCENTS($uid, $refid);
+               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />\n";
+
+               // Some percents given?
+               if ($percents > 0) {
+                       // Get points for refback
+                       $refback = $points * $percents / 100;
+       
+                       // Add points again, but only directly
+                       //* DEBUG: */ echo __FUNCTION__.":refback={$refback},depth={$DEPTH}<br />\n";
+                       $cacheArray['depth'][$uid][$ref] = $DEPTH; $DEPTH = -1;
+                       ADD_POINTS_REFSYSTEM($uid, $refback, false, "0", false, "direct");
+                       $DEPTH = $cacheArray['depth'][$uid][$ref];
+                       //* DEBUG: */ echo __FUNCTION__.":depth={$DEPTH} - RESTORE!<br />\n";
+
+                       // Reduce points if refid is found
+                       if ($refid == $ref) {
+                               // Reduce points here!
+                               $return = $points - $refback;
+                               //* DEBUG: */ echo __FUNCTION__.":points={$return} - REDUCED<br />\n";
+                       } // END - if
+               } // END - if
+       } // END foreach
+
+       // Return them
+       return $return;
+}
+// "Getter" for refback percents
+function GET_REFBACK_PERCENTS ($uid, $ref) {
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},ref={$ref}<br />\n";
+       // Skip identical ids
+       if ($uid == $ref) return 0;
+
+       // Default is zero
+       $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), __FILE__, __LINE__);
+
+       // Entry found? (Should be!)
+       if (SQL_NUMROWS($result) == 1) {
+               // Fetch percents
+               list($percents) = SQL_FETCHROW($result);
+       } else {
+               // Debug log
+               DEBUG_LOG(__FUNCTION__.": uid={$uid},ref={$ref} - No entry found! :-(");
        }
 
        // Free result
-       SQL_FREERESULT($result_user);
+       SQL_FREERESULT($result);
+
+       // Return percents
+       return $percents;
+}
+// "Getter" for userid array
+function GET_REFBACK_USERID_ARRAY ($rid) {
+       //* DEBUG: */ echo __FUNCTION__.":rid={$rid}<br />\n";
+       // Init userids
+       $userIds = array();
+
+       // Look for all
+       $result = SQL_QUERY_ESC("SELECT userid
+FROM "._MYSQL_PREFIX."_user_refs
+WHERE refid=%s
+GROUP BY level
+ORDER BY userid ASC",
+               array($rid), __FILE__, __LINE__);
+
+       // Entries found?
+       if (SQL_NUMROWS($result) > 0) {
+               // Add all
+               while (list($userid) = SQL_FETCHROW($result)) {
+                       //* DEBUG: */ echo __FUNCTION__.":rid={$rid},userid={$userid}<br />\n";
+                       $userIds[] = $userid;
+               } // END - while
+       } // END - if
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return array
+       //* DEBUG: */ echo __FUNCTION__.":rid={$rid},count()=".count($userIds)."<br />\n";
+       return $userIds;
 }
 //
-function UPDATE_REF_COUNTER($uid)
-{
-       global $REF_LVL, $cacheInstance;
+function UPDATE_REF_COUNTER ($uid) {
+       global $cacheArray, $cacheInstance;
 
        // Make it sure referral level zero (member him-/herself) is at least selected
-       if (empty($REF_LVL)) $REF_LVL = 0;
+       if (empty($cacheArray['ref_level'][$uid])) $cacheArray['ref_level'][$uid] = 1;
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level={$cacheArray['ref_level'][$uid]}<br />\n";
 
        // Update counter
-       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_refsystem SET counter=counter+1 WHERE userid=%s AND level='%s' LIMIT 1",
-        array(bigintval($uid), $REF_LVL), __FILE__, __LINE__);
+       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_refsystem SET counter=counter+1 WHERE userid=%s AND level=%s LIMIT 1",
+               array(bigintval($uid), $cacheArray['ref_level'][$uid]), __FILE__, __LINE__);
 
        // When no entry was updated then we have to create it here
-       if (SQL_AFFECTEDROWS() == 0)
-       {
+       //* DEBUG: */ echo __FUNCTION__.":updated=".SQL_AFFECTEDROWS()."<br />\n";
+       if (SQL_AFFECTEDROWS() == 0) {
                // First count!
-               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_refsystem (userid, level, counter) VALUES ('%s','%s','1')",
-                array(bigintval($uid), $REF_LVL), __FILE__, __LINE__);
-       }
+               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_refsystem (userid, level, counter) VALUES (%s,%s,1)",
+                       array(bigintval($uid), $cacheArray['ref_level'][$uid]), __FILE__, __LINE__);
+               //* DEBUG: */ echo __FUNCTION__.":uid={$uid}<br />\n";
+       } // END - if
 
        // Check for his referral
        $result = SQL_QUERY_ESC("SELECT refid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
-        array(bigintval($uid)), __FILE__, __LINE__);
+               array(bigintval($uid)), __FILE__, __LINE__);
+
+       // Load refid
        list($ref) = SQL_FETCHROW($result);
 
        // Free memory
        SQL_FREERESULT($result);
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},ref={$ref}<br />\n";
 
        // When he has a referral...
-       if (($ref > 0) && ($ref != $uid))
-       {
+       if (($ref > 0) && ($ref != $uid)) {
                // Move to next referral level and count his counter one up!
-               $REF_LVL++; UPDATE_REF_COUNTER($ref);
-       }
-        elseif ((($ref == $uid) || ($ref == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2"))
-       {
+               //* DEBUG: */ echo __FUNCTION__.":ref={$ref} - ADVANCE!<br />\n";
+               $cacheArray['ref_level'][$uid]++; UPDATE_REF_COUNTER($ref);
+       } elseif ((($ref == $uid) || ($ref == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2")) {
                // Remove cache here
+               //* DEBUG: */ echo __FUNCTION__.":ref={$ref} - CACHE!<br />\n";
                if ($cacheInstance->cache_file("refsystem", true)) $cacheInstance->cache_destroy();
        }
+
+       // "Walk" back here
+       $cacheArray['ref_level'][$uid]--;
+
+       // Handle refback here
+       UPDATE_REFBACK_TABLE($uid);
+}
+// Update "refback table"
+function UPDATE_REFBACK_TABLE($uid)
+{
+       global $cacheArray, $cacheInstance;
+
+       // Make it sure referral level zero (member him-/herself) is at least selected
+       if (empty($cacheArray['back_level'])) $cacheArray['back_level'] = 1;
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level={$cacheArray['back_level']}<br />\n";
+
+       // Init refid
+       $cacheArray['up_refid'][$cacheArray['back_level']] = 0;
+       $old = 0; $minus = 0;
+
+       // Check for his referral
+       $result_refid = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE refid=%s ORDER BY userid ASC",
+               array(bigintval($uid)), __FILE__, __LINE__);
+
+       // When no entry was updated then we have to create it here
+       //* DEBUG: */ echo __FUNCTION__.":found=".SQL_NUMROWS($result_refid)."<br />\n";
+       if (SQL_NUMROWS($result_refid) > 0) {
+               // Load all refids
+               while(list($cacheArray['up_refid'][$cacheArray['back_level']]) = SQL_FETCHROW($result_refid)) {
+                       // Remmber userid
+                       $cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']]] = $uid;
+
+                       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]}<br />\n";
+                       // Refid set?
+                       if (($cacheArray['up_refid'][$cacheArray['back_level']] > 0) && ($cacheArray['up_refid'][$cacheArray['back_level']] != $uid) && (!empty($cacheArray['up_refid'][$cacheArray['back_level']]))) {
+                               // New userid?
+                               if ((isset($cacheArray['up_refid'][$cacheArray['back_level']-1])) && (isset($cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']-1]]))) {
+                                       // New userid!
+                                       $old = $uid;
+                                       $uid = $cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']-1]];
+                                       $minus = 1;
+                                       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},old={$old},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - NEW UID!<br />\n";
+                               } // 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), $cacheArray['back_level'], bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
+
+                               // Do we have no entry?
+                               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />\n";
+                               if (SQL_NUMROWS($result) == 0) {
+                                       // Insert this level
+                                       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - ADD!<br />\n";
+                                       $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_refs (userid, level, refid) VALUES (%s,%s,%s)",
+                                               array(bigintval($uid), $cacheArray['back_level'], bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
+
+                                       // Move to next referral level and count his counter one up!
+                                       $cacheArray['back_level']++; UPDATE_REFBACK_TABLE($cacheArray['up_refid'][($cacheArray['back_level'] - 1)]);
+                               } // END - if
+
+                               // Do we have another level here?
+                               if ((($cacheArray['back_level']-$minus) > 0) && ($old > 0)) {
+                                       // Restore old one
+                                       $uid = $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), ($cacheArray['back_level']-$minus), bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
+
+                                       // Do we have no entry?
+                                       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level=".($cacheArray['back_level']-$minus).",ref={$cacheArray['up_refid'][$cacheArray['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />\n";
+                                       if (SQL_NUMROWS($result) == 0) {
+                                               // Insert this level
+                                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_refs (userid, level, refid) VALUES (%s,%s,%s)",
+                                                       array(bigintval($uid), ($cacheArray['back_level']-$minus), bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
+                                               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level=".($cacheArray['back_level']-$minus).",ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - RETURNED!<br />\n";
+                                       } // END - if
+                               } // END - if
+                       } // END - if
+               } // END - while
+
+               // Free memory
+               SQL_FREERESULT($result_refid);
+       } // END - if
+
+       // When he has a referral...
+       if ((($cacheArray['up_refid'][$cacheArray['back_level']] == $uid) || ($cacheArray['up_refid'][$cacheArray['back_level']] == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2") && (!isset($cacheArray['back_cached']))) {
+               // Remove cache here
+               //* DEBUG: */ echo __FUNCTION__.":uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - CACHE!<br />\n";
+               if ($cacheInstance->cache_file("refback", true)) $cacheInstance->cache_destroy();
+               $cacheArray['back_cached'] = 1;
+       } // END - if
+
+       // "Walk" back here
+       $cacheArray['back_level']--;
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level={$cacheArray['back_level']} - LEVEL!<br />\n";
+
+       // Fix empty refid
+       if (!isset($cacheArray['up_refid'][$cacheArray['back_level']-1])) $cacheArray['up_refid'][$cacheArray['back_level']-1] = 0;
+       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']-1]} - BACK!<br />\n";
 }
 // Updates/extends the online list
 function UPDATE_ONLINE_LIST($SID, $mod, $act, $wht) {
@@ -1298,7 +1536,7 @@ function UPDATE_ONLINE_LIST($SID, $mod, $act, $wht) {
                $ADMIN = "Y";
        } // END - if
 
-       if (isSessionVariableSet('refid')) {
+       if (isSessionVariableSet('up_refid')) {
                // Check cookie
                if (get_session('refid') > 0) $rid = bigintval($GLOBALS['refid']);
        } // END - if