X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Freferral-functions.php;h=a60a8a2424b7c988f39fa8cf93ce4793e4d70258;hb=eafccb2f191b89aafda0b227238e0d3f18011a5b;hp=a0261d9293830f9d8eb5a97652a2168d7c35c40f;hpb=5a295e2c5ceaf111dc6a6d7cea5c4a7ebaab6480;p=mailer.git diff --git a/inc/referral-functions.php b/inc/referral-functions.php index a0261d9293..a60a8a2424 100644 --- a/inc/referral-functions.php +++ b/inc/referral-functions.php @@ -44,7 +44,7 @@ if (!defined('__SECURITY')) { 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 @@ -52,11 +52,11 @@ function getReferralLevelPercents ($level) { // 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]; @@ -93,7 +93,7 @@ function getReferralLevelPercents ($level) { 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)); @@ -160,7 +160,7 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU // 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), @@ -200,7 +200,7 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU //* 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); @@ -226,7 +226,7 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU } // 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'; @@ -335,7 +335,7 @@ function updateReferralCounter ($userid) { //* 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]) @@ -372,6 +372,7 @@ function subtractPoints ($subject, $userid, $points) { 'userid' => $userid, 'points' => $points, 'points_mode' => 'sub', + 'column' => 'used_points', 'added' => (!SQL_HASZEROAFFECTED()) ); @@ -388,8 +389,14 @@ function getUserReferralPoints ($userid, $level) { $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 @@ -405,7 +412,7 @@ WHERE ur.`userid`=%s AND ur.`level`=%s ORDER BY - ur.`refid` ASC", + ur.`refid` ASC', array( bigintval($userid), bigintval($level) @@ -475,10 +482,10 @@ function getPointsDataArrayFromSubject ($subject) { } // 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); @@ -489,7 +496,7 @@ function getPointsDataArrayFromSubject ($subject) { } // 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 @@ -512,7 +519,7 @@ function getPointsColumnNameFromSubjectLocked ($subject, $isLocked) { $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 @@ -573,7 +580,7 @@ function loadReferralTable ($userid) { $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 @@ -588,7 +595,7 @@ function loadReferralTable ($userid) { $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 @@ -660,7 +667,7 @@ function addMissingReferralLevels ($userid) { } // END - foreach } // END - foreach } // END - foreach - //die('
'.print_r($GLOBALS['referral_refid'][$userid],true).'
'); + //die('
'.print_r($GLOBALS['referral_refid'][$userid],TRUE).'
'); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); } @@ -682,8 +689,8 @@ function flushReferralTableToDatabase ($userid) { } // 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) { @@ -699,13 +706,13 @@ function flushReferralTableToDatabase ($userid) { $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 @@ -715,7 +722,7 @@ function flushReferralTableToDatabase ($userid) { // And run it //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL); - if ($executeSql === true) { + if ($executeSql === TRUE) { SQL_QUERY($SQL, __FUNCTION__, __LINE__); } // END - if @@ -806,5 +813,104 @@ function getReferralId () { 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(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(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] ?>