]> git.mxchange.org Git - mailer.git/blobdiff - inc/libs/rallye_functions.php
Heacy rewrite/cleanup:
[mailer.git] / inc / libs / rallye_functions.php
index 3974a2df090c68a2f98f65f9afbc73b0323d70e7..5aa2cdb91fa3294872712c707269f852d4ffd4bf 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -43,13 +43,13 @@ if (!defined('__SECURITY')) {
 // Auto-start referral rallyes
 function autostartReferralRallyes ($result) {
        // Load all rallyes (usally we have only one rallye active per time!
-       $content = SQL_FETCHARRAY($result);
+       $content = sqlFetchArray($result);
 
        // Free result
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        // Set notified to Y
-       SQL_QUERY_ESC("UPDATE
+       sqlQueryEscaped("UPDATE
        `{?_MYSQL_PREFIX?}_rallye_data`
 SET
        `notified`='Y'
@@ -59,7 +59,7 @@ LIMIT 1",
                array(bigintval($content['id'])), __FUNCTION__, __LINE__);
 
        // Query all users
-       $result_user = SQL_QUERY("SELECT
+       $result_user = sqlQuery("SELECT
        `userid`
 FROM
        `{?_MYSQL_PREFIX?}_user_data`
@@ -70,18 +70,18 @@ ORDER BY
        `userid` ASC", __FUNCTION__, __LINE__);
 
        // Let's begin with the userids...
-       while ($row = SQL_FETCHARRAY($result_user)) {
+       while ($row = sqlFetchArray($result_user)) {
                // Merge both arrays
                $content = merge_array($content, $row);
 
-               $un = false;
+               $un = FALSE;
 
                // Get refs by userid
                $count = getReferralRallyeRefsCount($content['userid']);
                if (empty($count)) $count = '0'; // Added prevent some unknown troubles... :-?
 
                // Check if line is already included...
-               $result_ref = SQL_QUERY_ESC("SELECT
+               $result_ref = sqlQueryEscaped("SELECT
        `id`
 FROM
        `{?_MYSQL_PREFIX?}_rallye_users`
@@ -92,36 +92,36 @@ LIMIT 1",
                        array(bigintval($content['id']), bigintval($content['userid'])), __FUNCTION__, __LINE__);
 
                // Is there an entry?
-               if (SQL_HASZERONUMS($result_ref)) {
+               if (ifSqlHasZeroNums($result_ref)) {
                        // Add userid and his ref count to table
-                       $result_ref = SQL_QUERY_ESC("SELECT 
-       SUM(p.`points`) AS `points`
+                       $result_ref = sqlQueryEscaped("SELECT
+       SUM(`p`.`points`) AS `points`
 FROM
-       `{?_MYSQL_PREFIX?}_user_points` AS p
+       `{?_MYSQL_PREFIX?}_user_points` AS `p`
 LEFT JOIN
-       `{?_MYSQL_PREFIX?}_user_data` AS d
+       `{?_MYSQL_PREFIX?}_user_data` AS `d`
 ON
        p.userid=d.userid
 WHERE
-       d.`status`='CONFIRMED'
+       `d`.`status`='CONFIRMED'
        " . runFilterChain('user_exclusion_sql', ' ') . " AND
-       d.`max_mails` > 0 AND
-       d.`mails_confirmed` >= {?ref_payout?} AND
-       p.`ref_depth`=1 AND
-       p.`points` > 0 AND
-       d.`userid`=%s",
+       `d`.`max_mails` > 0 AND
+       `d`.`mails_confirmed` >= {?ref_payout?} AND
+       `p`.`ref_depth`=1 AND
+       `p`.`points` > 0 AND
+       `d`.`userid`=%s",
                                array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
 
                        // Get points
-                       list($cpoints) = SQL_FETCHROW($result_ref);
+                       list($cpoints) = sqlFetchRow($result_ref);
 
                        // Free result
-                       SQL_FREERESULT($result_ref);
+                       sqlFreeResult($result_ref);
 
                        if (empty($cpoints)) $cpoints = '0.00000';
 
                        // Add info line
-                       SQL_QUERY_ESC("INSERT INTO
+                       sqlQueryEscaped("INSERT INTO
        `{?_MYSQL_PREFIX?}_rallye_users`
 (
        `rallye_id`,
@@ -140,17 +140,17 @@ WHERE
                                        bigintval($count),
                                        $cpoints
                                ), __FUNCTION__, __LINE__);
-                       $un = true;
+                       $un = TRUE;
                } // END - if
 
                // Free memory
-               SQL_FREERESULT($result_ref);
+               sqlFreeResult($result_ref);
 
                // Ignored but for the template required refs (made before start of rallye)
                $content['refs']  = $count;
 
                // Shall I notify this member?
-               if (($content['send_notify'] == 'Y') && ($un === true)) {
+               if (($content['send_notify'] == 'Y') && ($un === TRUE)) {
                        // Load email template and send it to the user
                        $message = loadEmailTemplate('member_rallye_notify', $content, $content['userid']);
                        sendEmail($content['userid'], '{%message,MEMBER_RALLYE_NOTIFY_SUBJECT=' . $content['title'] . '%}', $message);
@@ -167,19 +167,19 @@ WHERE
        sendAdminNotification('{%message,ADMIN_RALLYE_NOTIFY_SUBJECT=' . $content['title'] . '%}', $templ, $content);
 
        // Free memory
-       SQL_FREERESULT($result_user);
+       sqlFreeResult($result_user);
 }
 
 //
 function addReferralRallyePrices ($rallye, $mode = 'email') {
        // Output mode
        switch($mode) {
-               case 'email': $mode = chr(10);  break;
+               case 'email': $mode = PHP_EOL;  break;
                case 'html' : $mode = '<br />'; break;
        } // END - switch
 
        // Load prices
-       $result_prices = SQL_QUERY_ESC("SELECT
+       $result_prices = sqlQueryEscaped("SELECT
        `price_level`,
        `points`,
        `info`
@@ -192,14 +192,14 @@ ORDER BY
                array($rallye), __FUNCTION__, __LINE__);
 
        // Entries found?
-       if (!SQL_HASZERONUMS($result_prices)) {
+       if (!ifSqlHasZeroNums($result_prices)) {
                // Load prices
                $prices = '';
-               if ($mode == chr(10)) {
+               if ($mode == PHP_EOL) {
                        $prices = '{--RALLYE_MEMBER_PRICES_ADDED--}:' . $mode . '------------------------------' . $mode;
                } // END - if
 
-               while ($content = SQL_FETCHARRAY($result_prices)) {
+               while ($content = sqlFetchArray($result_prices)) {
                        $prices .= $content['price_level']. '{--RALLYE_PRICE--}:';
                        if (!empty($content['info'])) {
                                $prices .= $content['info'];
@@ -210,14 +210,14 @@ ORDER BY
                } // END - if
 
                // Free memory
-               SQL_FREERESULT($result_prices);
+               sqlFreeResult($result_prices);
        } else {
                // No prices???
                $prices = '{%message,RALLYE_MEMBER_NO_PRICES=' . $mode . '%}';
        }
 
        // Add last line for email mode
-       if ($mode == chr(10)) {
+       if ($mode == PHP_EOL) {
                $prices .= '------------------------------';
        } // END - if
 
@@ -235,25 +235,25 @@ function addReferralRallyeTopUsers ($mode, $rallye, $default=0) {
        $prices = getTotalRallyePrices($rallye);
 
        // And load only limited users
-       $result = SQL_QUERY_ESC("SELECT DISTINCT
-       u.`userid`,
-       u.`refs`,
-       u.`curr_points`,
-       rd.`min_users`,
-       rd.`min_prices`
+       $result = sqlQueryEscaped("SELECT DISTINCT
+       `u`.`userid`,
+       `u`.`refs`,
+       `u`.`curr_points`,
+       `rd`.`min_users`,
+       `rd`.`min_prices`
 FROM
-       `{?_MYSQL_PREFIX?}_rallye_users` AS u
+       `{?_MYSQL_PREFIX?}_rallye_users` AS `u`
 INNER JOIN
-       `{?_MYSQL_PREFIX?}_refsystem` AS r
+       `{?_MYSQL_PREFIX?}_refsystem` AS `r`
 ON
        u.userid=r.userid
 INNER JOIN
-       `{?_MYSQL_PREFIX?}_rallye_data` AS rd
+       `{?_MYSQL_PREFIX?}_rallye_data` AS `rd`
 ON
-       u.`rallye_id`=rd.`id`
+       `u`.`rallye_id`=r`d`.`id`
 WHERE
-       u.`rallye_id`=%s AND
-       r.`counter` > 0
+       `u`.`rallye_id`=%s AND
+       `r`.`counter` > 0
 ORDER BY
        u.refs DESC",
                array(bigintval($rallye)), __FUNCTION__, __LINE__);
@@ -266,35 +266,35 @@ ORDER BY
        );
 
        // Load all users
-       while ($row = SQL_FETCHARRAY($result)) {
+       while ($row = sqlFetchArray($result)) {
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $row['userid'] . ',refs=' . $row['refs']);
                // Get current refs
                $count = getReferralRallyeRefsCount($row['userid'], $row['refs']);
 
                // Points of ref's
-               $result_ref = SQL_QUERY_ESC("SELECT
-       p.`points`
+               $result_ref = sqlQueryEscaped("SELECT
+       `p`.`points`
 FROM
-       `{?_MYSQL_PREFIX?}_user_points` AS p
+       `{?_MYSQL_PREFIX?}_user_points` AS `p`
 LEFT JOIN
-       `{?_MYSQL_PREFIX?}_user_data` AS d
+       `{?_MYSQL_PREFIX?}_user_data` AS `d`
 ON
-       p.`userid`=d.`userid`
+       `p`.`userid`=`d`.`userid`
 WHERE
-       d.`userid`=%s AND
-       d.`status`='CONFIRMED'
+       `d`.`userid`=%s AND
+       `d`.`status`='CONFIRMED'
        " . runFilterChain('user_exclusion_sql', ' ') . " AND
-       p.`ref_depth`=1 AND
-       d.`max_mails` > 0 AND
-       d.`mails_confirmed` >= {?ref_payout?}
+       `p`.`ref_depth`=1 AND
+       `d`.`max_mails` > 0 AND
+       `d`.`mails_confirmed` >= {?ref_payout?}
 LIMIT 1",
                        array(bigintval($row['userid'])), __FUNCTION__, __LINE__);
 
                // Get points
-               list($refpoints) = SQL_FETCHROW($result_ref);
+               list($refpoints) = sqlFetchRow($result_ref);
 
                // Free result
-               SQL_FREERESULT($result_ref);
+               sqlFreeResult($result_ref);
 
                // Fix empty values (ugly code again)
                if (empty($refpoints)) {
@@ -321,10 +321,10 @@ LIMIT 1",
        } // END - while
 
        // Free memory
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        // Sort whole array
-       array_pk_sort($content, array('cpoints', 'ref'), 0, 1, true);
+       array_pk_sort($content, array('cpoints', 'ref'), 0, 1, TRUE);
 
        // Generate table
        $OUT = '';
@@ -346,7 +346,7 @@ LIMIT 1",
                );
 
                // Load row template and switch color
-               $OUT .= loadTemplate($mode . '_list_rallye_row', true, $row);
+               $OUT .= loadTemplate($mode . '_list_rallye_row', TRUE, $row);
        } // END - for
 
        // Add min_prices/users
@@ -355,7 +355,7 @@ LIMIT 1",
        $content['min_users']  = bigintval($min_users);
 
        // And finnally return the output
-       return loadTemplate($mode . '_list_rallye', true, $content);
+       return loadTemplate($mode . '_list_rallye', TRUE, $content);
 }
 
 // Sends out a notification about the rallye
@@ -378,11 +378,11 @@ function addUserToReferralRallye ($userid, $content = array()) {
 
        // Updated extension?
        if (isExtensionInstalledAndNewer('rallye', '0.2.0')) {
-               $add .= ',`min_users`,`min_prices`';
+               $add .= ',`min_users`, `min_prices`';
        } // END - if
 
        // Check for an auto-add rallye
-       $result = SQL_QUERY("SELECT
+       $result = sqlQuery("SELECT
        `id`,
        `title`,
        `start_time`,
@@ -397,16 +397,16 @@ WHERE
        `auto_add_new_user`='Y' AND
        `expired`='N'
 LIMIT 1", __FUNCTION__, __LINE__);
-       if (SQL_NUMROWS($result) == 1) {
+       if (sqlNumRows($result) == 1) {
                // Init variables
                $min_users = '0';
                $min_prices = '0';
 
                // Load and merge data
-               $content = merge_array($content, SQL_FETCHARRAY($result));
+               $content = merge_array($content, sqlFetchArray($result));
 
                // Free result
-               SQL_FREERESULT($result);
+               sqlFreeResult($result);
 
                // Notify the user?
                if ($content['send_notify'] == 'Y') {
@@ -415,7 +415,7 @@ LIMIT 1", __FUNCTION__, __LINE__);
                } // END - if
 
                // Check if line is already included...
-               $result = SQL_QUERY_ESC("SELECT
+               $result = sqlQueryEscaped("SELECT
        `id`
 FROM
        `{?_MYSQL_PREFIX?}_rallye_users`
@@ -429,9 +429,9 @@ LIMIT 1",
                        ), __FUNCTION__, __LINE__);
 
                // Is this user added?
-               if (SQL_HASZERONUMS($result)) {
+               if (ifSqlHasZeroNums($result)) {
                        // Add userid and his ref count to table
-                       SQL_QUERY_ESC("INSERT INTO
+                       sqlQueryEscaped("INSERT INTO
        `{?_MYSQL_PREFIX?}_rallye_users`
 (
        `rallye_id`,
@@ -449,7 +449,7 @@ LIMIT 1",
                } // END - if
 
                // Free memory
-               SQL_FREERESULT($result);
+               sqlFreeResult($result);
        } // END - if
 }
 
@@ -466,11 +466,11 @@ function getTotalRallyeWinners ($rallyeId) {
                // "Walk" through all entries
                foreach ($prices['userid'] as $key => $userid) {
                        // Check for userid
-                       $active = countSumTotalData($userid, 'user_data', 'userid', 'userid', true, runFilterChain('user_exclusion_sql', " AND `status`='CONFIRMED'"));
+                       $active = countSumTotalData($userid, 'user_data', 'userid', 'userid', TRUE, runFilterChain('user_exclusion_sql', " AND `status`='CONFIRMED'"));
 
                        // Allow valid and active users with at least one ref
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'rallyeId=' . $rallyeId . ',userid=' . $userid . ',ref=' . $prices['ref'][$key] . ',cpoints=' . $prices['cpoints'][$key]);
-                       if ((isValidUserId($userid)) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
+                       if ((isValidId($userid)) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
                                $GLOBALS[__FUNCTION__][$rallyeId]++;
                        } // END - if
                } // END - foreach
@@ -483,10 +483,10 @@ function getTotalRallyeWinners ($rallyeId) {
 // Stops the given rallye result (from SQL layer) by marking it as expirced
 function stopRallyeByResult ($result) {
        // Load rallye data
-       $content = SQL_FETCHARRAY($result);
+       $content = sqlFetchArray($result);
 
        // Free result
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        // Init array
        $count = '0';
@@ -506,12 +506,12 @@ function stopRallyeByResult ($result) {
                 *   active = 1: account is still confirmed
                 *   active = 0: account is deleted or locked
                 */
-               $active = countSumTotalData($userid, 'user_data', 'userid', 'userid', true, runFilterChain('user_exclusion_sql', " AND `status`='CONFIRMED'"));
+               $active = countSumTotalData($userid, 'user_data', 'userid', 'userid', TRUE, runFilterChain('user_exclusion_sql', " AND `status`='CONFIRMED'"));
 
                $prices['active'][$key] = $active;
 
                // Allow valid and active users with at least one ref to get points
-               if ((isValidUserId($userid)) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
+               if ((isValidId($userid)) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
                        $total++;
                } // END - if
        } // END - foreach
@@ -523,13 +523,13 @@ function stopRallyeByResult ($result) {
        } // END - if
 
        // Expire rallye
-       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `expired`='Y' WHERE `id`=%s LIMIT 1",
+       sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `expired`='Y' WHERE `id`=%s LIMIT 1",
                array(bigintval($content['id'])), __FUNCTION__, __LINE__);
 
        // Run array through (by userid is the most important 2nd-level-array)
        foreach ($prices['userid'] as $key => $userid) {
                // Allow valid and active users with at least one ref to get points
-               if ((isValidUserId($userid)) && ($prices['ref'][$key] > 0) && ($prices['active'][$key] == 1) && ($prices['cpoints'][$key] > 0)) {
+               if ((isValidId($userid)) && ($prices['ref'][$key] > 0) && ($prices['active'][$key] == 1) && ($prices['cpoints'][$key] > 0)) {
                        // Transfer data to array for the mail template
                        $content['level']  = $prices['level'][$key];
                        $content['points'] = $prices['points'][$key];
@@ -609,8 +609,8 @@ function getReferralRallyeUserDataFromArray ($userIds) {
        $ret = '';
 
        // Load users
-       $result = SQL_QUERY_ESC("SELECT
-       `userid`,`gender`,`surname`,`family`,`email`
+       $result = sqlQueryEscaped("SELECT
+       `userid`, `gender`, `surname`, `family`, `email`
 FROM
        `{?_MYSQL_PREFIX?}_user_data`
 WHERE
@@ -624,7 +624,7 @@ LIMIT %s",
                        $userid_string,
                        count($userIds)
                ), __FUNCTION__, __LINE__);
-       while ($content = SQL_FETCHARRAY($result)) {
+       while ($content = sqlFetchArray($result)) {
                // Construct the message masked and add it
                $ret .= sprintf("{%%pipe,translateGender=%s%} %s %s (%s) - %s\n",
                        $content['gender'],
@@ -649,8 +649,10 @@ function getArrayFromReferralRallyePrices ($rallye) {
        );
 
        // Load prices
-       $result = SQL_QUERY_ESC("SELECT
-       `price_level`,`points`,`info`
+       $result = sqlQueryEscaped("SELECT
+       `price_level`,
+       `points`,
+       `info`
 FROM
        `{?_MYSQL_PREFIX?}_rallye_prices`
 WHERE
@@ -660,14 +662,14 @@ ORDER BY
                array(bigintval($rallye)), __FUNCTION__, __LINE__);
 
        // Transfer elements
-       while ($content = SQL_FETCHARRAY($result)) {
+       while ($content = sqlFetchArray($result)) {
                $prices['level'][]  = $content['price_level'];
                array_push($prices['points'], $content['points']);
                array_push($prices['info'], $content['info']);
        } // END - while
 
        // Free memory
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        // Return array
        return $prices;
@@ -693,7 +695,7 @@ function getArrayFromReferralRallyeUsers ($rallye) {
         *  - refs        = Old amount of referrals (before the rallye has started)
         *  - curr_points = Current amount of total points
         */
-       $result_user = SQL_QUERY_ESC("SELECT
+       $result_user = sqlQueryEscaped("SELECT
        `userid`,
        `refs`,
        `curr_points`
@@ -704,35 +706,35 @@ WHERE
 ORDER BY
        `userid` ASC",
                array(bigintval($rallye)), __FUNCTION__, __LINE__);
-       while ($content = SQL_FETCHARRAY($result_user)) {
+       while ($content = sqlFetchArray($result_user)) {
                // Load current ref count
                $count = getReferralRallyeRefsCount($content['userid'], $content['refs']);
 
                // Points of ref's
-               $result_ref = SQL_QUERY_ESC("SELECT
-       SUM(p.`points`) AS `points`
+               $result_ref = sqlQueryEscaped("SELECT
+       SUM(`p`.`points`) AS `points`
 FROM
-       `{?_MYSQL_PREFIX?}_user_points` AS p
+       `{?_MYSQL_PREFIX?}_user_points` AS `p`
 LEFT JOIN
-       `{?_MYSQL_PREFIX?}_user_data` AS d
+       `{?_MYSQL_PREFIX?}_user_data` AS `d`
 ON
-       p.`userid`=d.`userid`
+       `p`.`userid`=`d`.`userid`
 WHERE
-       d.`status`='CONFIRMED'
+       `d`.`status`='CONFIRMED'
        " . runFilterChain('user_exclusion_sql', ' ' ) . " AND
-       d.`max_mails` > 0 AND
-       d.`mails_confirmed` >= {?ref_payout?} AND
-       p.`ref_depth`=1 AND
-       p.`points` > 0 AND
-       d.`userid`=%s
+       `d`.`max_mails` > 0 AND
+       `d`.`mails_confirmed` >= {?ref_payout?} AND
+       `p`.`ref_depth`=1 AND
+       `p`.`points` > 0 AND
+       `d`.`userid`=%s
 LIMIT 1",
                        array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
 
                // Get points
-               list($refpoints) = SQL_FETCHROW($result_ref);
+               list($refpoints) = sqlFetchRow($result_ref);
 
                // Free result
-               SQL_FREERESULT($result_ref);
+               sqlFreeResult($result_ref);
 
                // Fix empty refpoints
                if (empty($refpoints)) $refpoints = '0';
@@ -744,10 +746,10 @@ LIMIT 1",
        } // END - while
 
        // Free memory
-       SQL_FREERESULT($result_user);
+       sqlFreeResult($result_user);
 
        // Sort array for refs (descending)
-       array_pk_sort($users, array('cpoints', 'ref'), 0, 1, true);
+       array_pk_sort($users, array('cpoints', 'ref'), 0, 1, TRUE);
 
        // Load prices array (!)
        $prices = getArrayFromReferralRallyePrices($rallye);
@@ -782,7 +784,7 @@ function addReferralRallyeWinners ($mode, $rallye, $default=0) {
                 *   active = 1: account is still confirmed
                 *   active = 0: account is deleted or locked
                 */
-               $active = countSumTotalData($content['userid'][$idx], 'user_data', 'userid', 'userid', true, runFilterChain('user_exclusion_sql', " AND `status`='CONFIRMED'"));
+               $active = countSumTotalData($content['userid'][$idx], 'user_data', 'userid', 'userid', TRUE, runFilterChain('user_exclusion_sql', " AND `status`='CONFIRMED'"));
 
                if ((empty($content['ref'][$idx])) || ($content['ref'][$idx] == '0') || ($active == '0') || ('' . round($content['cpoints'][$idx]) . '' == '0') || (empty($content['cpoints'][$idx]))) {
                        // Allow valid and active users with at least one ref to get points
@@ -816,7 +818,7 @@ function addReferralRallyeWinners ($mode, $rallye, $default=0) {
                );
 
                // Load row template
-               $OUT .= loadTemplate($mode . '_list_rallye_row', true, $row);
+               $OUT .= loadTemplate($mode . '_list_rallye_row', TRUE, $row);
        } // END - for
 
        // Prepare content
@@ -827,14 +829,14 @@ function addReferralRallyeWinners ($mode, $rallye, $default=0) {
        );
 
        // Load main template and finnally return the output
-       return loadTemplate($mode . '_list_rallye', true, $content);
+       return loadTemplate($mode . '_list_rallye', TRUE, $content);
 }
 
 //
 function purgeExpiredReferralRallyes () {
        // Check for expired rallyes
        $EXPIRE = 3; // @TODO The hard-coded value...
-       $result_rallye = SQL_QUERY_ESC("SELECT
+       $result_rallye = sqlQueryEscaped("SELECT
        `id`,
        `title`,
        `start_time`,
@@ -846,12 +848,12 @@ WHERE
        `expired`='Y'",
                array($EXPIRE), __FUNCTION__, __LINE__);
 
-       if (!SQL_HASZERONUMS($result_rallye)) {
+       if (!ifSqlHasZeroNums($result_rallye)) {
                // Init SQLs
                initSqls();
 
                // Expire found rallyes and notify admin
-               while ($content = SQL_FETCHARRAY($result_rallye)) {
+               while ($content = sqlFetchArray($result_rallye)) {
                        // Prepare data for mail template
                        $content['start_time'] = generateDateTime($content['start_time'], 1);
                        $content['end_time']   = generateDateTime($content['end_time']  , 1);
@@ -861,12 +863,12 @@ WHERE
                        sendAdminNotification('{%message,ADMIN_RALLYE_PURGED_SUBJECT=' . $content['title'] . '%}', 'admin_rallye_purged', $content);
 
                        // Purge whole rallye
-                       addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_data` WHERE `id`=%s LIMIT 1",
-                               array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
-                       addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s LIMIT 1",
-                               array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
-                       addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s LIMIT 1",
-                               array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
+                       addSql(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_data` WHERE `id`=%s LIMIT 1",
+                               array(bigintval($content['id'])), __FUNCTION__, __LINE__, FALSE));
+                       addSql(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s LIMIT 1",
+                               array(bigintval($content['id'])), __FUNCTION__, __LINE__, FALSE));
+                       addSql(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s LIMIT 1",
+                               array(bigintval($content['id'])), __FUNCTION__, __LINE__, FALSE));
 
                        // Add task (we ignore the task id here
                        createNewTask('{--ADMIN_RALLYE_PURGED_TASK--}: ' . $content['title'], '{--ADMIN_RALLYE_PURGED_TEXT--}', 'RALLYE_PURGED');
@@ -877,7 +879,7 @@ WHERE
        } // END - if
 
        // Free memory
-       SQL_FREERESULT($result_rallye);
+       sqlFreeResult($result_rallye);
 }
 
 //
@@ -887,10 +889,10 @@ function addReferralRallyeTemplateSelection ($name = 'template', $default = '')
        $ral = array();
 
        // Check templates directory
-       $basePath = sprintf("templates/%s/html/rallye/", getLanguage());
+       $basePath = sprintf('templates/%s/html/rallye/', getLanguage());
 
        // Read the directory
-       $templates = getArrayFromDirectory($basePath, 'rallye_', false, true, array(), '.tpl');
+       $templates = getArrayFromDirectory($basePath, 'rallye_', FALSE, TRUE, array(), '.tpl');
 
        // And walk through
        foreach ($templates as $read) {
@@ -903,7 +905,7 @@ function addReferralRallyeTemplateSelection ($name = 'template', $default = '')
                        $ral[$read] = $read;
                } else {
                        // Log invalid
-                       logDebugMessage(__FUNCTION__, __LINE__, sprintf("WARNING: Template %s not used.", $read));
+                       logDebugMessage(__FUNCTION__, __LINE__, sprintf('WARNING: Template %s not used.', $read));
                }
        } // END - foreach
 
@@ -950,7 +952,7 @@ function getReferralRallyeRefsCount ($currUserid, $oldReferralCount = '0') {
                        } // END - if
                } // END - foreach
 
-               //* DEBUG: */ debugOutput('<pre>'.print_r($GLOBALS['cache_array']['refsystem'], true).'</pre>');
+               //* DEBUG: */ debugOutput('<pre>'.print_r($GLOBALS['cache_array']['refsystem'], TRUE).'</pre>');
                //* DEBUG: */ doShutdown();
 
                if ($count > 0) {
@@ -963,26 +965,26 @@ function getReferralRallyeRefsCount ($currUserid, $oldReferralCount = '0') {
                } // END - if
        } else {
                // Load current refs from database
-               $result_ref = SQL_QUERY_ESC("SELECT
-       SUM(s.counter) AS cnt
+               $result_ref = sqlQueryEscaped("SELECT
+       SUM(s.counter) AS `cnt`
 FROM
-       `{?_MYSQL_PREFIX?}_refsystem` AS s
+       `{?_MYSQL_PREFIX?}_refsystem` AS `s`
 LEFT JOIN
-       `{?_MYSQL_PREFIX?}_refdepths` AS d
+       `{?_MYSQL_PREFIX?}_refdepths` AS `d`
 ON
-       s.level=d.level
+       `s`.`level`=`d`.`level`
 WHERE
-       s.userid=%s AND
-       s.level=1",
+       `s`.`userid`=%s AND
+       `s`.`level`=1",
                        array(
                                bigintval($userid)
                        ), __FUNCTION__, __LINE__);
 
                // Load count @TODO Can't we rewrite this to our API?
-               list($count) = SQL_FETCHROW($result_ref);
+               list($count) = sqlFetchRow($result_ref);
 
                // Free result
-               SQL_FREERESULT($result_ref);
+               sqlFreeResult($result_ref);
                if (empty($count)) {
                        $count = '0';
                } else {
@@ -1028,7 +1030,7 @@ function getTotalRallyePrices ($rallyeId) {
        // Is there cache?
        if (!isset($GLOBALS[__FUNCTION__][$rallyeId])) {
                // Determine it
-               $GLOBALS[__FUNCTION__][$rallyeId] = countSumTotalData($rallyeId, 'rallye_prices', 'id', 'rallye_id', true);
+               $GLOBALS[__FUNCTION__][$rallyeId] = countSumTotalData($rallyeId, 'rallye_prices', 'id', 'rallye_id', TRUE);
        } // END - if
 
        // Return it