".__FUNCTION__." - ENTRY ------------------------
- \n";
global $cacheArray;
// Back ref depths
$cacheArray['depth'][$uid][$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", __FILE__, __LINE__);
// "Walk" through all level
while (list($level, $perc) = SQL_FETCHROW($result_refs)) {
// Reset ref depths
unset($GLOBALS['ref_level']);
// "Walk" through all refids
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},ref={$ref},level={$level},points={$points}
\n";
foreach (GET_REFBACK_USERID_ARRAY($uid, $level) 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: */ print __FUNCTION__."(".__LINE__."):uid={$uid},ref={$ref},refid={$refid},points={$points},percents={$percents}
\n";
// Some percents given?
if ($percents > 0) {
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):points={$points},perc={$perc},percents={$percents},level={$level}
\n";
// Calculate points for refback
$refback = $points * ($perc / 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), __FILE__, __LINE__);
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")
\n";
// Add points again, but only directly
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):refback={$refback}
\n";
ADD_POINTS_REFSYSTEM(sprintf("refback:%s", $refid), $uid, $refback, false, "0", false, "direct");
// Reduce points if refid is found
if ($refid == $ref) {
// Reduce points here!
$return = $points * ($perc / 100) - $refback;
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):points={$return} - REDUCED
\n";
} // END - if
} // END - if
} // END foreach
} // END - while
// Free result
SQL_FREERESULT($result_refs);
// Restore ref depth
$GLOBALS['ref_level'] = $cacheArray['depth'][$uid][$ref];
// Return them
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
return $return;
}
// "Getter" for refback percents
function GET_REFBACK_PERCENTS ($uid, $ref) {
//* DEBUG: */ print "----------------------- ".__FUNCTION__." - ENTRY ------------------------- \n";
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},ref={$ref}
\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);
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):percents={$percents}
\n";
} else {
// Debug log
//* DEBUG: */ print __FUNCTION__."(".__LINE__."): NO ENTRY FOUND! :-(
\n";
DEBUG_LOG(__FUNCTION__, __LINE__, " uid={$uid},ref={$ref} - No entry found! :-(");
}
// Free result
SQL_FREERESULT($result);
// Return percents
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
return $percents;
}
// "Getter" for userid array which will return only one entry
function GET_REFBACK_USERID_ARRAY ($rid, $level) {
//* DEBUG: */ print "----------------------- ".__FUNCTION__." - ENTRY ------------------------- \n";
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):rid={$rid},level={$level}
\n";
// 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), __FILE__, __LINE__);
// Entries found?
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):numRows=".SQL_NUMROWS($result)."
\n";
if (SQL_NUMROWS($result) > 0) {
// Add all
while (list($userid) = SQL_FETCHROW($result)) {
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):rid={$rid},level={$level},userid={$userid}
\n";
$userIds[] = $userid;
} // END - while
} // END - if
// Free result
SQL_FREERESULT($result);
// Return array
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):rid={$rid},count()=".count($userIds)."
\n";
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
return $userIds;
}
// Update "refback table"
function UPDATE_REFBACK_TABLE($uid) {
//* DEBUG: */ print "----------------------- ".__FUNCTION__." - ENTRY ------------------------- \n";
global $cacheArray, $cacheInstance;
// Make it sure referal level zero (member him-/herself) is at least selected
if (empty($cacheArray['back_level'])) $cacheArray['back_level'] = 1;
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level={$cacheArray['back_level']}
\n";
// Init refid
$cacheArray['up_refid'][$cacheArray['back_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)), __FILE__, __LINE__);
// When no entry was updated then we have to create it here
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):found=".SQL_NUMROWS($result_refid)."
\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: */ print __FUNCTION__."(".__LINE__."):uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]}
\n";
// Refid set?
if (($cacheArray['up_refid'][$cacheArray['back_level']] > 0) && ($cacheArray['up_refid'][$cacheArray['back_level']] != $uid) && (isset($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: */ print __FUNCTION__."(".__LINE__."):uid={$uid},old={$old},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - NEW UID!
\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: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!
\n";
if (SQL_NUMROWS($result) == 0) {
// Insert this level
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - ADD!
\n";
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 referal 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: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level=".($cacheArray['back_level']-$minus).",ref={$cacheArray['up_refid'][$cacheArray['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!
\n";
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), ($cacheArray['back_level']-$minus), bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level=".($cacheArray['back_level']-$minus).",ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - RETURNED!
\n";
} // END - if
} // END - if
} // END - if
} // END - while
// Free memory
SQL_FREERESULT($result_refid);
} // END - if
// When he has a referal...
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: */ print __FUNCTION__."(".__LINE__."):uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - CACHE!
\n";
if ($cacheInstance->loadCacheFile("refback")) $cacheInstance->destroyCacheFile();
$cacheArray['back_cached'] = 1;
} // END - if
// "Walk" back here
$cacheArray['back_level']--;
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level={$cacheArray['back_level']} - LEVEL!
\n";
// Fix empty refid
if (!isset($cacheArray['up_refid'][$cacheArray['back_level']-1])) $cacheArray['up_refid'][$cacheArray['back_level']-1] = 0;
//* DEBUG: */ print __FUNCTION__."(".__LINE__."):uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']-1]} - BACK!
\n";
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
}
// "Getter" for array for user refs in given level
function GET_USER_REFS ($uid, $level) {
//* DEBUG: */ print "----------------------- ".__FUNCTION__." - ENTRY ------------------------- \n";
// Default is no refs and no nickname
$ADD = "";
$refs = array();
// Do we have nickname extension installed?
if (EXT_IS_ACTIVE("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)), __FILE__, __LINE__);
// Are there some entries?
if (SQL_NUMROWS($result) > 0) {
// Fetch all entries
while ($row = SQL_FETCHARRAY($result)) {
// Add row
$refs[$row['id']] = $row;
} // END - while
} // END - if
// Free result
SQL_FREERESULT($result);
// Return result
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
return $refs;
}
// "Getter" for refback entry
function GET_USER_REF_ENTRY ($id) {
//* DEBUG: */ print "----------------------- ".__FUNCTION__." - ENTRY ------------------------- \n";
// Default data is nothing
$data = array();
// Secure id number
$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, $GLOBALS['userid']), __FILE__, __LINE__);
// Is there an entry?
if (SQL_NUMROWS($result) == 1) {
// Fetch data
$data = SQL_FETCHARRAY($result);
} // END - if
// Free result
SQL_FREERESULT($result);
// Return entry
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
return $data;
}
// Update refback percents (but with some sanity-checks!
function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
//* DEBUG: */ print "----------------------- ".__FUNCTION__." - ENTRY ------------------------- \n";
// Default status is failed
$status['message'] = MEMBER_REFBACK_ERROR_GENERAL;
// Do some sanity-checks
if (!IS_MEMBER()) {
// No member!
$status['message'] = MEMBER_REFBACK_ERROR_NO_MEMBER;
return $status;
} elseif ("".$id."" != "".($id + 0)."") {
// No number!
$status['message'] = MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER;
return $status;
} elseif (($percents < 0) || ($percents > 100)) {
// Percentage is not valid!
$status['message'] = MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE;
return $status;
}
// Get ref entry
$dummy = GET_USER_REF_ENTRY($id);
// Is this valid?
if (count($dummy) == 0) {
// ID does not belong to user!
$status['message'] = MEMBER_REFBACK_ERROR_ID_MISMATCH;
return $status;
} // END - if
// Revert german commata
$percents = REVERT_COMMA($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, $GLOBALS['userid']), __FILE__, __LINE__);
// Entry updated?
if (SQL_AFFECTEDROWS() < 1) {
// Entry not updated!
$status['message'] = MEMBER_REFBACK_ERROR_NOT_UPDATED;;
return $status;
} // END - if
// Prepare email content
$content = array(
'percents' => TRANSLATE_COMMA($percents),
'refid' => $dummy['refid'],
'level' => $dummy['level']
);
// Load member email template
$mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, $GLOBALS['userid']);
// Send email to user
SEND_EMAIL($dummy['refid'], MEMBER_REFBACK_SUBJECT, $mail);
// Send admin notification
SEND_ADMIN_NOTIFICATION(ADMIN_REFBACK_SUBJECT, "admin_refback", $content, $GLOBALS['userid']);
// All fine!
$status['ok'] = true;
// Return status array
//* DEBUG: */ print "
----------------------- ".__FUNCTION__." - EXIT ------------------------
\n";
return $status;
}
//
?>