function initReferralSystem () {
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referral system initialized!');
$GLOBALS['ref_level'] = NULL;
- $GLOBALS['ref_system'] = true;
+ $GLOBALS['ref_system'] = TRUE;
}
// Getter fro ref level percents
// Default is zero
$data['percents'] = '0';
- // Do we have cache?
+ // Is there cache?
if ((isset($GLOBALS['cache_array']['refdepths']['level'])) && (isExtensionActive('cache'))) {
// First look for level
$key = array_search($level, $GLOBALS['cache_array']['refdepths']['level']);
- if ($key !== false) {
+ if ($key !== FALSE) {
// Entry found
$data['percents'] = $GLOBALS['cache_array']['refdepths']['percents'][$key];
function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NULL) {
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . convertNullToZero($refid) . ' - ENTERED!');
// By default nothing has been added
- $added = false;
+ $added = FALSE;
// Determine payment method and notification
$paymentMethod = strtoupper(getPaymentMethodFromSubject($subject));
// No entry updated?
if (SQL_HASZEROAFFECTED()) {
// First ref in this level! :-)
- SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`,`ref_depth`,`%s`) VALUES (%s, %s, %s)",
+ SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `%s`) VALUES (%s, %s, %s)",
array(
$pointsColumn,
bigintval($userid),
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ',sendNotify=' . intval($sendNotify) . ',isLocked=' . intval($isLocked));
// Send "referral confirmed" mails out?
- if ((isValidUserid($refid)) && ($refid != $userid) && ($sendNotify === true)) {
+ if ((isValidUserid($refid)) && ($refid != $userid) && ($sendNotify === TRUE)) {
// Calculate the referral's points and percents
$percentsReferral = getReferralLevelPercents($GLOBALS['ref_level'] + 1);
} // END - if
// Points updated, maybe I shall send him an email?
- if (($sendNotify === true) && ($isLocked === false)) {
+ if (($sendNotify === TRUE) && ($isLocked === FALSE)) {
// "Explode" subject
$subjectArray = explode(':', $subject);
$subjectUserid = (isset($subjectArray[1])) ? $subjectArray[1] : '0';
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',updated=' . SQL_AFFECTEDROWS());
if (SQL_HASZEROAFFECTED()) {
// First count!
- SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`,`level`,`counter`) VALUES (%s,%s,1)",
+ SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
array(
bigintval($ref),
convertZeroToNull($GLOBALS['cache_array']['ref_level'][$ref])
'userid' => $userid,
'points' => $points,
'points_mode' => 'sub',
+ 'column' => 'used_points',
'added' => (!SQL_HASZEROAFFECTED())
);
$refs = array();
// Get refs from database
- $result = SQL_QUERY_ESC("SELECT
- ur.`id`, ur.`refid`, ud.`status`, ud.`last_online`, ud.`mails_confirmed`, ud.`emails_received`
+ $result = SQL_QUERY_ESC('SELECT
+ ur.`id`,
+ ur.`refid`,
+ ud.`status`,
+ ud.`last_online`,
+ ud.`mails_confirmed`,
+ ud.`emails_received`,
+ ud.`subid`
FROM
`{?_MYSQL_PREFIX?}_user_refs` AS `ur`
LEFT JOIN
ur.`userid`=%s AND
ur.`level`=%s
ORDER BY
- ur.`refid` ASC",
+ ur.`refid` ASC',
array(
bigintval($userid),
bigintval($level)
} // END - if
// Now checkout the entry in database table
- $result = SQL_QUERY_ESC("SELECT `id`,`subject`,`column_name`,`locked_mode`,`payment_method`,`notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
+ $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
array($subject), __FUNCTION__, __LINE__);
- // Do we have an entry?
+ // Is there an entry?
if (SQL_NUMROWS($result) == 1) {
// Then load it
$pointsData = SQL_FETCHARRAY($result);
} // END - foreach
} else {
// Register this automatically
- SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`,`column_name`,`locked_mode`,`payment_method`,`notify_recipient`) VALUES ('%s','points','LOCKED','REFERRAL','N')",
+ SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient`) VALUES ('%s','points','LOCKED','REFERRAL','N')",
array($subject), __FUNCTION__, __LINE__);
// Re-request it
$columnName = $pointsData['column_name'];
// Are the points locked?
- if (($isLocked === true) && ($pointsData['locked_mode'] == 'LOCKED')) {
+ if (($isLocked === TRUE) && ($pointsData['locked_mode'] == 'LOCKED')) {
// Locked points, so prefix it
$columnName = 'locked_' . $pointsData['column_name'];
} // END - if
$GLOBALS['referral_result'][$userid] = SQL_QUERY_ESC('SELECT `level` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `userid`=%s ORDER BY `level` ASC',
array($userid), __FUNCTION__, __LINE__);
- // Do we have entries?
+ // Are there entries?
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_NUMROWS()=' . SQL_NUMROWS($GLOBALS['referral_result'][$userid]));
if (SQL_NUMROWS($GLOBALS['referral_result'][$userid]) > 0) {
// Then walk through all levels
$GLOBALS['referral_result_refs'][$userid] = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC',
array($userid), __FUNCTION__, __LINE__);
- // Do we have entries?
+ // Are there entries?
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',SQL_NUMROWS()=' . SQL_NUMROWS($GLOBALS['referral_result_refs'][$userid]));
if (SQL_NUMROWS($GLOBALS['referral_result_refs'][$userid]) > 0) {
// Then again walk through all
} // END - foreach
} // END - foreach
} // END - foreach
- //die('<pre>'.print_r($GLOBALS['referral_refid'][$userid],true).'</pre>');
+ //die('<pre>'.print_r($GLOBALS['referral_refid'][$userid],TRUE).'</pre>');
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!');
}
} // END - if
// Prepare SQL
- $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES ';
- $executeSql = false;
+ $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`, `level`, `refid`) VALUES ';
+ $executeSql = FALSE;
// Now walk through the array, first levels
foreach ($GLOBALS['referral_refid'][$userid] as $level => $levelArray) {
$refid
), __FUNCTION__, __LINE__));
- // Do we have no entry?
+ // Is there no entry?
if ($count == 0) {
// Then add it to the SQL
$SQL .= '(' . $userid . ',' . $level . ',' . $refid . '),';
// Some has been added, so execute the query
- $executeSql = true;
+ $executeSql = TRUE;
} // END - if
} // END - foreach
} // END - foreach
// And run it
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL);
- if ($executeSql === true) {
+ if ($executeSql === TRUE) {
SQL_QUERY($SQL, __FUNCTION__, __LINE__);
} // END - if
return $GLOBALS['__refid'];
}
+// Determines referral id and sets it
+function determineReferralId () {
+ // Is it already detected?
+ if (isReferralIdValid()) {
+ // Do not determine it, just return it
+ return getReferralId();
+ } elseif ((!isHtmlOutputMode()) && (basename($_SERVER['PHP_SELF']) != 'ref.php')) {
+ // Skip this in non-html-mode and outside ref.php
+ return FALSE;
+ }
+
+ // Check if refid is set
+ if (isReferralIdValid()) {
+ // This is fine...
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from GLOBALS (' . getReferralId() . ')');
+ } elseif (isPostRequestElementSet('refid')) {
+ // Get referral id from POST element refid
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from POST data (' . postRequestElement('refid') . ')');
+ setReferralId(secureString(postRequestElement('refid')));
+ } elseif (isGetRequestElementSet('refid')) {
+ // Get referral id from GET parameter refid
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from GET data (' . getRequestElement('refid') . ')');
+ setReferralId(secureString(getRequestElement('refid')));
+ } elseif (isGetRequestElementSet('ref')) {
+ // Set refid=ref (the referral link uses such variable)
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using ref from GET data (' . getRequestElement('ref') . ')');
+ setReferralId(secureString(getRequestElement('ref')));
+ } elseif ((isGetRequestElementSet('user')) && (basename($_SERVER['PHP_SELF']) == 'click.php')) {
+ // The variable user comes from click.php
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using user from GET data (' . getRequestElement('user') . ')');
+ setReferralId(bigintval(getRequestElement('user')));
+ } elseif ((isSessionVariableSet('refid')) && (isValidUserId(getSession('refid')))) {
+ // Set session refid as global
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from SESSION data (' . getSession('refid') . ')');
+ setReferralId(bigintval(getSession('refid')));
+ } elseif ((isExtensionInstalledAndNewer('user', '0.3.4')) && (isRandomReferralIdEnabled())) {
+ // Select a random user which has confirmed enougth mails
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Checking random referral id');
+ setReferralId(determineRandomReferralId());
+ } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.1.2')) && (isValidUserId(getDefRefid()))) {
+ // Set default refid as refid in URL
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using default refid (' . getDefRefid() . ')');
+ setReferralId(getDefRefid());
+ } else {
+ // No default id when ext-sql_patches is not installed or none set
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using NULL as refid');
+ setReferralId(NULL);
+ }
+
+ // Set cookie when default refid > 0
+ if ((!isSessionVariableSet('refid')) || (!isValidUserId(getReferralId())) || ((!isValidUserId(getSession('refid'))) && (isExtensionInstalledAndNewer('sql_patches', '0.1.2')) && (isValidUserId(getDefRefid())))) {
+ // Default is not found
+ $found = FALSE;
+
+ // Is there nickname or userid set?
+ if ((isExtensionActive('nickname')) && (isNicknameUsed(getReferralId()))) {
+ // Nickname in URL, so load the id
+ $found = fetchUserData(getReferralId(), 'nickname');
+
+ // If we found it, use the userid as referral id
+ if ($found === TRUE) {
+ // Set the userid as 'refid'
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from user account by nickname (' . getUserData('userid') . ')');
+ setReferralId(getUserData('userid'));
+ } // END - if
+ } elseif (isValidUserId(getReferralId())) {
+ // Direct userid entered
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using direct userid (' . getReferralId() . ')');
+ $found = fetchUserData(getReferralId());
+ }
+
+ // Is the record valid?
+ if ((($found === FALSE) || (!isUserDataValid())) && (isExtensionInstalledAndNewer('sql_patches', '0.1.2'))) {
+ // No, then reset referral id
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using default refid (' . getDefRefid() . ')');
+ setReferralId(getDefRefid());
+ } // END - if
+
+ // Set cookie
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Saving refid to session (' . getReferralId() . ') #1');
+ setSession('refid', getReferralId());
+ } elseif ((!isReferralIdValid()) || (!fetchUserData(getReferralId()))) {
+ // Not valid!
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not valid referral id (' . getReferralId() . '), setting NULL in session');
+ setReferralId(NULL);
+ setSession('refid', NULL);
+ } else {
+ // Set it from GLOBALS array in session
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Saving refid to session (' . getReferralId() . ') #2');
+ setSession('refid', getReferralId());
+ }
+
+ // Run post validation filter chain
+ runFilterChain('post_refid_validation');
+
+ // Return determined refid
+ return getReferralId();
+}
+
// [EOF]
?>