d71492ded8cc6a9d363473d7f42b08466ff30734
[mailer.git] / inc / doubler_send.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 02/16/2005 *
4  * ===============                              Last change: 01/21/2006 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : doubler_send.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Send's out mails for doubled points              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sendet Mails bei vergueteter Verdoppelung aus    *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Initialize variables
41 $jackpot = 0; $user = 0;
42
43 // Get total points of the doubler itself
44 $DOUBLER_POINTS = DOUBLER_GET_TOTAL_POINTS_LEFT();
45 if ($DOUBLER_POINTS == 0) {
46         // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)
47         return;
48 } // END - if
49
50 // If not currently doubled set it to zero
51 unset($_GET['DOUBLER_UID']);
52 unset($_POST['DOUBLER_UID']);
53 set_session('DOUBLER_UID', "");
54 if (empty($DOUBLER_UID)) $DOUBLER_UID = 0;
55
56 // Check for doubles which we can pay out
57 $min = getConfig(('doubler_min') * 2);
58 $result_total = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark
59 FROM `{!_MYSQL_PREFIX!}_doubler` AS d
60 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS u
61 ON d.userid=u.userid
62 WHERE u.`status`='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
63 ORDER BY d.timemark", array($DOUBLER_POINTS, $min), __FILE__, __LINE__);
64
65 // Check for accounts with limitation
66 $result_main = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark
67 FROM `{!_MYSQL_PREFIX!}_doubler` AS d
68 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS u
69 ON d.userid=u.userid
70 WHERE u.`status`='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
71 ORDER BY d.timemark
72 LIMIT %d", array($DOUBLER_POINTS, $min, getConfig('doubler_max_sent')), __FILE__, __LINE__);
73
74 // Do we have entries found?
75 if (((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == "Y")) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != "Y"))) {
76         // Switch to matching SQL resource
77         $result_load = $result_main;
78         if ((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == "Y")) $result_load = $result_total;
79
80         // At least one account was found
81         while (list($id, $uid, $points, $ip, $time) = SQL_FETCHROW($result_load)) {
82                 // Only double when points are enougth!
83                 if ($DOUBLER_POINTS >= $points) {
84                         // Check for his ref points
85                         $ref = GET_TOTAL_DATA($uid, "doubler", "points", "refid", false, " AND completed='N' AND is_ref='Y'");
86
87                         // Zero refid when empty (might be helpful!)
88                         if (empty($ref)) $ref = 0;
89                         if (($ref > 0) && ($DOUBLER_UID == $uid) && (!empty($ref))) {
90                                 // Referal points found so add them and set line(s) to completed='Y'
91                                 $points += $ref;
92                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_doubler` SET completed='Y' WHERE refid=%s AND completed='N' AND is_ref='Y'",
93                                         array(bigintval($uid)), __FILE__, __LINE__);
94                         } else {
95                                 // No referal points found
96                                 $ref = 0;
97                         }
98
99                         // Exclude webmaster from doubling...
100                         if ($uid != getConfig('doubler_uid')) {
101                                 // Add points
102                                 ADD_POINTS_REFSYSTEM(sprintf("doubler_%s", strtolower(getConfig('doubler_send_mode'))), $uid, $points, false, "0", false, "direct");
103                         } // END - if
104
105                         // Set entry as "payed"
106                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_doubler` SET completed='Y' WHERE id=%s LIMIT 1",
107                                 array(bigintval($id)), __FILE__, __LINE__);
108
109                         $okay = false;
110                         // Check for jackpot inclusion in doubling process
111                         if (($jackpot > 0) && ($jackpot >= $points) && (getConfig('doubler_jackpot') == "Y")) {
112                                 // Subtract points from jackpot
113                                 SUB_JACKPOT($points);
114                                 $jackpot -= $points;
115
116                                 // Okay, done!
117                                 $okay = true;
118                         } // END - if
119
120                         // Exclude also webmaster's ID in taking points from webmaster's account
121                         if (($user > 0) && ($user >= $points) && (!$okay) && (getConfig('doubler_uid') > 0) && ($uid != getConfig('doubler_uid'))) {
122                                 // Add points to used points
123                                 SUB_POINTS("doubler_payout", getConfig('doubler_uid'), $points);
124
125                                 // Okay, done!
126                                 $okay = true;
127                         } // END - if
128
129                         // Update doubler's account only when others are not updated
130                         if (!$okay) {
131                                 // Add points to used doubler points
132                                 UPDATE_CONFIG("doubler_used", $points, "+");
133                         } // END - if
134
135                         // Update variables to prevent errors
136                         incrementConfigEntry('doubler_used', $points);
137                         $DOUBLER_POINTS -= $points;
138
139                         // Prepare array
140                         $content = array(
141                                 // Doubler transmission ID
142                                 'id'     => $id,
143                                 // Doubled points
144                                 'points' => TRANSLATE_COMMA($points),
145                                 // Timemark
146                                 'when'   => MAKE_DATETIME($time, "2"),
147                                 // IP number when the member submitted the doubling form
148                                 'ip'     => $ip,
149                         );
150
151                         // Load mail template and send mail away...
152                         $msg = LOAD_EMAIL_TEMPLATE("member_doubler", $content, $uid);
153                         SEND_EMAIL($uid, getMessage('DOUBLER_MEMBER_SUBJECT'), $msg);
154                 } // END - if
155         } // END - while
156 } // END - if
157
158 // Free memory
159 SQL_FREERESULT($result_total);
160 SQL_FREERESULT($result_main);
161
162 //
163 ?>