]> git.mxchange.org Git - mailer.git/blobdiff - inc/referral-functions.php
Code style changed, ext-user continued:
[mailer.git] / inc / referral-functions.php
index 6078e378fa309f9a80c15a0ec5968dc2a60aa8cd..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];
 
@@ -83,7 +83,7 @@ function getReferralLevelPercents ($level) {
 }
 
 /**
- * Dynamic referral and points system, can also send mails!
+ * Dynamic Referral and points system, can also send mails!
  *
  * subject       = Subject line, write in lower-case letters and underscore is allowed
  * userid        = Referral id wich should receive...
@@ -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);
 
@@ -222,11 +222,11 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU
                                $message = loadEmailTemplate('guest_user_confirmed_referral', $content, bigintval($userid));
 
                                // Send email
-                               sendEmail($userid, '{--THANX_REFERRAL_ONE_SUBJECT--}', $message);
+                               sendEmail($userid, '{--THANKS_REFERRAL_ONE_SUBJECT--}', $message);
                        } // 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';
@@ -262,7 +262,7 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU
                                );
                        } // END - if
 
-                       // Increase referral level, if payment method is REFERRAL
+                       // Increase referral level, if payment method is 'REFERRAL'
                        if ($paymentMethod == 'REFERRAL') {
                                // Increase it
                                $GLOBALS['ref_level']++;
@@ -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
 
@@ -806,5 +812,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(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 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]
 ?>