More misc fixes and rewrites (sorry, lame description)
[mailer.git] / inc / libs / refback_functions.php
index 7106e79..1ec422b 100644 (file)
@@ -52,8 +52,8 @@ function ADD_REFBACK_POINTS ($uid, $ref, $points, $ref_points) {
        $return = $points;
 
        // Get all ref levels
        $return = $points;
 
        // Get all ref levels
-       $result_refs = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level > 0 ORDER BY level ASC",
-       __FUNCTION__, __LINE__);
+       $result_refs = SQL_QUERY("SELECT `level`, `percents` FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE `level` > 0 ORDER BY `level` ASC",
+               __FUNCTION__, __LINE__);
 
        // "Walk" through all level
        while ($content = SQL_FETCHARRAY($result_refs)) {
 
        // "Walk" through all level
        while ($content = SQL_FETCHARRAY($result_refs)) {
@@ -77,7 +77,7 @@ function ADD_REFBACK_POINTS ($uid, $ref, $points, $ref_points) {
                                $refback = $points * ($content['percents'] / 100) * ($percents / 100);
 
                                // Update refback table ("refid" and "uid" must be exchanged!)
                                $refback = $points * ($content['percents'] / 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",
+                               SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET points=points+%s WHERE `userid`=%s AND refid=%s LIMIT 1",
                                array($refback, $refid, $uid), __FUNCTION__, __LINE__);
                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
 
                                array($refback, $refid, $uid), __FUNCTION__, __LINE__);
                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
 
@@ -117,7 +117,7 @@ function GET_REFBACK_PERCENTS ($uid, $ref) {
        $percents = 0;
 
        // Get percents from database
        $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",
+       $result = SQL_QUERY_ESC("SELECT refback FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE `userid`=%s AND refid=%s LIMIT 1",
        array($ref, $uid), __FUNCTION__, __LINE__);
 
        // Entry found? (Should be!)
        array($ref, $uid), __FUNCTION__, __LINE__);
 
        // Entry found? (Should be!)
@@ -147,11 +147,15 @@ function GET_REFBACK_USERID_ARRAY ($rid, $level) {
        $userIds = array();
 
        // Look for all
        $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), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT
+       `userid`
+FROM
+       `{!_MYSQL_PREFIX!}_user_refs`
+WHERE
+       `refid=`%s AND `level`=%s
+ORDER BY
+       `userid` ASC",
+               array($rid,$level), __FUNCTION__, __LINE__);
 
        // Entries found?
        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
 
        // Entries found?
        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
@@ -184,8 +188,8 @@ function updateRefbackTable ($uid) {
        $old = 0; $minus = 0;
 
        // Check for his referal
        $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)), __FUNCTION__, __LINE__);
+       $result_refid = SQL_QUERY_ESC("SELECT `userid` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC",
+               array(bigintval($uid)), __FUNCTION__, __LINE__);
 
        // When no entry was updated then we have to create it here
        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />\n";
 
        // When no entry was updated then we have to create it here
        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />\n";
@@ -208,16 +212,16 @@ function updateRefbackTable ($uid) {
                                } // END - if
 
                                // Check existence
                                } // 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), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                               $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), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
 
                                // Do we have no entry?
                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />\n";
                                if (SQL_NUMROWS($result) == 0) {
                                        // Insert this level
                                        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - ADD!<br />\n";
 
                                // Do we have no entry?
                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />\n";
                                if (SQL_NUMROWS($result) == 0) {
                                        // Insert this level
                                        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - ADD!<br />\n";
-                                       SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
-                                       array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                                       SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
+                                               array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
 
                                        // Move to next referal level and count his counter one up!
                                        $GLOBALS['cache_array']['back_level']++; updateRefbackTable($GLOBALS['cache_array']['up_refid'][($GLOBALS['cache_array']['back_level'] - 1)]);
 
                                        // Move to next referal level and count his counter one up!
                                        $GLOBALS['cache_array']['back_level']++; updateRefbackTable($GLOBALS['cache_array']['up_refid'][($GLOBALS['cache_array']['back_level'] - 1)]);
@@ -229,15 +233,15 @@ function updateRefbackTable ($uid) {
                                        $uid = $old;
 
                                        // Shall we add this as well?
                                        $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), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                                       $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), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
 
                                        // Do we have no entry?
                                        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />\n";
                                        if (SQL_NUMROWS($result) == 0) {
                                                // Insert this level
 
                                        // Do we have no entry?
                                        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />\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), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
+                                               SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
+                                                       array(bigintval($uid), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
                                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - RETURNED!<br />\n";
                                        } // END - if
                                } // END - if
                                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - RETURNED!<br />\n";
                                        } // END - if
                                } // END - if
@@ -279,13 +283,23 @@ function GET_USER_REFS ($uid, $level) {
        } // END - if
 
        // Get refs from database
        } // 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)), __FUNCTION__, __LINE__);
+       $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)), __FUNCTION__, __LINE__);
 
        // Are there some entries?
        if (SQL_NUMROWS($result) > 0) {
 
        // Are there some entries?
        if (SQL_NUMROWS($result) > 0) {
@@ -314,8 +328,8 @@ function GET_USER_REF_ENTRY ($id) {
        $id = bigintval($id);
 
        // Get entry from database
        $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, getUserId()), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT `id`, `refid`, `refback`, `level` FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1",
+               array($id, getUserId()), __FUNCTION__, __LINE__);
 
        // Is there an entry?
        if (SQL_NUMROWS($result) == 1) {
 
        // Is there an entry?
        if (SQL_NUMROWS($result) == 1) {