More improved SQL queries
[mailer.git] / inc / doubler_send.php
index c3ccc8dcf3c07df1a54d5371544a8e251acc7c93..bdf6d7b2826641db4636cdd686d0eca4c1e4bbfc 100644 (file)
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
+if (!defined('__SECURITY')) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
        require($INC);
 }
 
 // Initialize variables
-$jackpot = "0"; $user = "0";
+$jackpot = 0; $user = 0;
 
 // Get total points of the doubler itself
 $DOUBLER_POINTS = DOUBLER_GET_TOTAL_POINTS_LEFT();
-if ($DOUBLER_POINTS == 0)
-{
+if ($DOUBLER_POINTS == 0) {
        // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)
        return;
-}
+} // END - if
 
 // If not currently doubled set it to zero
 unset($_GET['DOUBLER_UID']);
 unset($_POST['DOUBLER_UID']);
-unset($_COOKIE['DOUBLER_UID']);
-if (empty($DOUBLER_UID)) $DOUBLER_UID = "0";
+set_session('DOUBLER_UID', "");
+if (empty($DOUBLER_UID)) $DOUBLER_UID = 0;
 
 // Check for doubles which we can pay out
-$min = bigintval($CONFIG['doubler_min'] * 2);
+$min = bigintval(getConfig('doubler_min') * 2);
 $result_total = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark
-FROM "._MYSQL_PREFIX."_doubler AS d
-LEFT JOIN "._MYSQL_PREFIX."_user_data AS u
+FROM `{!_MYSQL_PREFIX!}_doubler` AS d
+LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS u
 ON d.userid=u.userid
-WHERE u.status='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
+WHERE u.`status`='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
 ORDER BY d.timemark", array($DOUBLER_POINTS, $min), __FILE__, __LINE__);
 
 // Check for accounts with limitation
 $result_main = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark
-FROM "._MYSQL_PREFIX."_doubler AS d
-LEFT JOIN "._MYSQL_PREFIX."_user_data AS u
+FROM `{!_MYSQL_PREFIX!}_doubler` AS d
+LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS u
 ON d.userid=u.userid
-WHERE u.status='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
+WHERE u.`status`='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
 ORDER BY d.timemark
-LIMIT %s", array($DOUBLER_POINTS, $min, $CONFIG['doubler_max_sent']), __FILE__, __LINE__);
+LIMIT %d", array($DOUBLER_POINTS, $min, getConfig('doubler_max_sent')), __FILE__, __LINE__);
 
-if (((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == 'Y')) || ((SQL_NUMROWS($result_main) == $CONFIG['doubler_group_sent']) && ($CONFIG['doubler_sent_all'] == 'N')))
-{
+// Do we have entries found?
+if (((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == "Y")) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') == "N"))) {
        // Switch to matching SQL resource
        $result_load = $result_main;
-       if ((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == 'Y')) $result_load = $result_total;
+       if ((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == "Y")) $result_load = $result_total;
 
        // At least one account was found
-       while(list($id, $uid, $points, $ip, $time) = SQL_FETCHROW($result_load))
-       {
+       while (list($id, $uid, $points, $ip, $time) = SQL_FETCHROW($result_load)) {
                // Only double when points are enougth!
-               if ($DOUBLER_POINTS >= $points)
-               {
+               if ($DOUBLER_POINTS >= $points) {
                        // Check for his ref points
-                       $result_ref = SQL_QUERY_ESC("SELECT SUM(points) FROM "._MYSQL_PREFIX."_doubler WHERE refid=%d AND completed='N' AND is_ref='Y'",
-                        array(bigintval($uid)), __FILE__, __LINE__);
-                       list($ref) = SQL_FETCHROW($result_ref);
-
-                       // Free memory
-                       SQL_FREERESULT($result_ref);
+                       $ref = GET_TOTAL_DATA($uid, "doubler", "points", "refid", false, " AND completed='N' AND is_ref='Y'");
 
                        // Zero refid when empty (might be helpful!)
                        if (empty($ref)) $ref = 0;
-                       if (($ref > 0) && ($DOUBLER_UID == $uid) && (!empty($ref)))
-                       {
-                               // Referral points found so add them and set line(s) to completed='Y'
+                       if (($ref > 0) && ($DOUBLER_UID == $uid) && (!empty($ref))) {
+                               // Referal points found so add them and set line(s) to completed='Y'
                                $points += $ref;
-                               $result_ref = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE refid=%d AND completed='N' AND is_ref='Y'",
-                                array(bigintval($uid)), __FILE__, __LINE__);
-                       }
-                        else
-                       {
-                               // No referral points found
-                               $ref = "0";
+                               SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_doubler` SET completed='Y' WHERE refid=%s AND completed='N' AND is_ref='Y'",
+                                       array(bigintval($uid)), __FILE__, __LINE__);
+                       } else {
+                               // No referal points found
+                               $ref = 0;
                        }
 
                        // Exclude webmaster from doubling...
-                       if ($uid != $CONFIG['doubler_uid'])
-                       {
+                       if ($uid != getConfig('doubler_uid')) {
                                // Add points
-                               $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET points=points+%s WHERE userid=%d AND ref_depth='0' LIMIT 1",
-                                array($points, bigintval($uid)), __FILE__, __LINE__);
-
-                               // Update mediadata as well
-                               if (GET_EXT_VERSION("mediadata") >= "0.0.4")
-                               {
-                                       // Update database
-                                       MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);
-                               }
-                       }
+                               ADD_POINTS_REFSYSTEM(sprintf("doubler_%s", strtolower(getConfig('doubler_send_mode'))), $uid, $points, false, "0", false, "direct");
+                       } // END - if
 
                        // Set entry as "payed"
-                       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE id=%d LIMIT 1",
-                        array(bigintval($id)), __FILE__, __LINE__);
+                       SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_doubler` SET completed='Y' WHERE id=%s LIMIT 1",
+                               array(bigintval($id)), __FILE__, __LINE__);
 
                        $OK = false;
                        // Check for jackpot inclusion in doubling process
-                       if (($jackpot > 0) && ($jackpot >= $points) && ($CONFIG['doubler_jackpot'] == 'Y'))
-                       {
+                       if (($jackpot > 0) && ($jackpot >= $points) && (getConfig('doubler_jackpot') == "Y")) {
                                // Subtract points from jackpot
-                               $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_jackpot SET points=points-%s WHERE ok='ok' LIMIT 1",
-                                array($points), __FILE__, __LINE__);
-                               $jackpot -= $jackpot;
+                               SUB_JACKPOT($points);
+                               $jackpot -= $points;
 
                                // Okay, done!
                                $OK = true;
-                       }
+                       } // END - if
 
                        // Exclude also webmaster's ID in taking points from webmaster's account
-                       if (($user > 0) && ($user >= $points) && (!$OK) && ($CONFIG['doubler_uid'] > 0) && ($uid != $CONFIG['doubler_uid']))
-                       {
+                       if (($user > 0) && ($user >= $points) && (!$OK) && (getConfig('doubler_uid') > 0) && ($uid != getConfig('doubler_uid'))) {
                                // Add points to used points
-                               $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET used_points=used_points+%s WHERE userid='' LIMIT 1",
-                                array($points, $CONFIG['doubler_uid']), __FILE__, __LINE__);
-
-                               // Update mediadata as well
-                               if (GET_EXT_VERSION("mediadata") >= "0.0.4")
-                               {
-                                       // Update database
-                                       MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
-                               }
+                               SUB_POINTS("doubler_payout", getConfig('doubler_uid'), $points);
 
                                // Okay, done!
                                $OK = true;
-                       }
+                       } // END - if
 
                        // Update doubler's account only when others are not updated
-                       if (!$OK)
-                       {
+                       if (!$OK) {
                                // Add points to used doubler points
-                               $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET doubler_used=doubler_used+%s WHERE config='0' LIMIT 1",
-                                array($points), __FILE__, __LINE__);
-
-                               // Destroy cache
-                               if (GET_EXT_VERSION("cache") >= "0.1.2")
-                               {
-                                       if ($CACHE->cache_file("config", true)) $CACHE->cache_destroy();
-                               }
-                       }
+                               UPDATE_CONFIG("doubler_used", $points, "+");
+                       } // END - if
 
                        // Update variables to prevent errors
-                       $CONFIG['doubler_used'] += $points;
+                       $_CONFIG['doubler_used'] += $points;
                        $DOUBLER_POINTS -= $points;
 
                        // Prepare array
@@ -191,9 +151,9 @@ if (((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == 'Y')) |
                        // Load mail template and send mail away...
                        $msg = LOAD_EMAIL_TEMPLATE("member_doubler", $content, $uid);
                        SEND_EMAIL($uid, DOUBLER_MEMBER_SUBJECT, $msg);
-               }
-       }
-}
+               } // END - if
+       } // END - while
+} // END - if
 
 // Free memory
 SQL_FREERESULT($result_total);