]> git.mxchange.org Git - mailer.git/blobdiff - inc/referral-functions.php
Code style changed, ext-user continued:
[mailer.git] / inc / referral-functions.php
index 667a25e0d4725677bc84bd09f0a38c5e6f548569..26700a53061e710b70659489379f8742bb51b0d5 100644 (file)
@@ -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])
@@ -388,8 +388,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 +411,7 @@ WHERE
        ur.`userid`=%s AND
        ur.`level`=%s
 ORDER BY
-       ur.`refid` ASC",
+       ur.`refid` ASC',
                array(
                        bigintval($userid),
                        bigintval($level)
@@ -475,10 +481,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 +495,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 +518,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 +579,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 +594,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 +666,7 @@ function addMissingReferralLevels ($userid) {
                        } // 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!');
 }
 
@@ -682,8 +688,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 +705,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 +721,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
 
@@ -808,16 +814,14 @@ function getReferralId () {
 
 // Determines referral id and sets it
 function determineReferralId () {
-       // Do we have it already detected?
+       // Is it already detected?
        if (isReferralIdValid()) {
                // Do not determine it, just return it
                return getReferralId();
-       } // END - if
-
-       // Skip this in non-html-mode and outside ref.php
-       if ((!isHtmlOutputMode()) && (basename($_SERVER['PHP_SELF']) != 'ref.php')) {
-               return false;
-       } // END - if
+       } 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()) {
@@ -833,7 +837,7 @@ function determineReferralId () {
                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('refid') . ')');
+               //* 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
@@ -860,15 +864,15 @@ function determineReferralId () {
        // 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;
+               $found = FALSE;
 
-               // Do we have nickname or userid set?
+               // 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) {
+                       if ($found === TRUE) {
                                // Set the userid as 'refid'
                                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from user account by nickname (' . getUserData('userid') . ')');
                                setReferralId(getUserData('userid'));
@@ -880,7 +884,7 @@ function determineReferralId () {
                }
 
                // Is the record valid?
-               if ((($found === false) || (!isUserDataValid())) && (isExtensionInstalledAndNewer('sql_patches', '0.1.2'))) {
+               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());
@@ -900,6 +904,9 @@ function determineReferralId () {
                setSession('refid', getReferralId());
        }
 
+       // Run post validation filter chain
+       runFilterChain('post_refid_validation');
+
        // Return determined refid
        return getReferralId();
 }