c10ece39d9376b992c9fc1ce4163c676fafc303c
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 // Initialize variables
42 $jackpot = "0"; $user = "0";
43
44 // Get total points of the doubler itself
45 $DOUBLER_POINTS = DOUBLER_GET_TOTAL_POINTS_LEFT();
46 if ($DOUBLER_POINTS == 0)
47 {
48         // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)
49         return;
50 }
51
52 // If not currently doubled set it to zero
53 unset($_GET['DOUBLER_UID']);
54 unset($_POST['DOUBLER_UID']);
55 unset($_COOKIE['DOUBLER_UID']);
56 if (empty($DOUBLER_UID)) $DOUBLER_UID = "0";
57
58 // Check for doubles which we can pay out
59 $min = bigintval($CONFIG['doubler_min'] * 2);
60 $result_total = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark
61 FROM "._MYSQL_PREFIX."_doubler AS d
62 LEFT JOIN "._MYSQL_PREFIX."_user_data AS u
63 ON d.userid=u.userid
64 WHERE u.status='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
65 ORDER BY d.timemark", array($DOUBLER_POINTS, $min), __FILE__, __LINE__);
66
67 // Check for accounts with limitation
68 $result_main = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark
69 FROM "._MYSQL_PREFIX."_doubler AS d
70 LEFT JOIN "._MYSQL_PREFIX."_user_data AS u
71 ON d.userid=u.userid
72 WHERE u.status='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
73 ORDER BY d.timemark
74 LIMIT %s", array($DOUBLER_POINTS, $min, $CONFIG['doubler_max_sent']), __FILE__, __LINE__);
75
76 if (((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == 'Y')) || ((SQL_NUMROWS($result_main) == $CONFIG['doubler_group_sent']) && ($CONFIG['doubler_sent_all'] == 'N')))
77 {
78         // Switch to matching SQL resource
79         $result_load = $result_main;
80         if ((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == 'Y')) $result_load = $result_total;
81
82         // At least one account was found
83         while(list($id, $uid, $points, $ip, $time) = SQL_FETCHROW($result_load))
84         {
85                 // Only double when points are enougth!
86                 if ($DOUBLER_POINTS >= $points)
87                 {
88                         // Check for his ref points
89                         $result_ref = SQL_QUERY_ESC("SELECT SUM(points) FROM "._MYSQL_PREFIX."_doubler WHERE refid=%d AND completed='N' AND is_ref='Y'",
90                          array(bigintval($uid)), __FILE__, __LINE__);
91                         list($ref) = SQL_FETCHROW($result_ref);
92
93                         // Free memory
94                         SQL_FREERESULT($result_ref);
95
96                         // Zero refid when empty (might be helpful!)
97                         if (empty($ref)) $ref = 0;
98                         if (($ref > 0) && ($DOUBLER_UID == $uid) && (!empty($ref)))
99                         {
100                                 // Referral points found so add them and set line(s) to completed='Y'
101                                 $points += $ref;
102                                 $result_ref = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE refid=%d AND completed='N' AND is_ref='Y'",
103                                  array(bigintval($uid)), __FILE__, __LINE__);
104                         }
105                          else
106                         {
107                                 // No referral points found
108                                 $ref = "0";
109                         }
110
111                         // Exclude webmaster from doubling...
112                         if ($uid != $CONFIG['doubler_uid'])
113                         {
114                                 // Add points
115                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET points=points+%s WHERE userid=%d AND ref_depth='0' LIMIT 1",
116                                  array($points, bigintval($uid)), __FILE__, __LINE__);
117
118                                 // Update mediadata as well
119                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
120                                 {
121                                         // Update database
122                                         MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);
123                                 }
124                         }
125
126                         // Set entry as "payed"
127                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE id=%d LIMIT 1",
128                          array(bigintval($id)), __FILE__, __LINE__);
129
130                         $OK = false;
131                         // Check for jackpot inclusion in doubling process
132                         if (($jackpot > 0) && ($jackpot >= $points) && ($CONFIG['doubler_jackpot'] == 'Y'))
133                         {
134                                 // Subtract points from jackpot
135                                 SUB_JACKPOT($points);
136                                 $jackpot -= $points;
137
138                                 // Okay, done!
139                                 $OK = true;
140                         }
141
142                         // Exclude also webmaster's ID in taking points from webmaster's account
143                         if (($user > 0) && ($user >= $points) && (!$OK) && ($CONFIG['doubler_uid'] > 0) && ($uid != $CONFIG['doubler_uid']))
144                         {
145                                 // Add points to used points
146                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET used_points=used_points+%s WHERE userid='' LIMIT 1",
147                                  array($points, $CONFIG['doubler_uid']), __FILE__, __LINE__);
148
149                                 // Update mediadata as well
150                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
151                                 {
152                                         // Update database
153                                         MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
154                                 }
155
156                                 // Okay, done!
157                                 $OK = true;
158                         }
159
160                         // Update doubler's account only when others are not updated
161                         if (!$OK)
162                         {
163                                 // Add points to used doubler points
164                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET doubler_used=doubler_used+%s WHERE config='0' LIMIT 1",
165                                  array($points), __FILE__, __LINE__);
166
167                                 // Destroy cache
168                                 if (GET_EXT_VERSION("cache") >= "0.1.2")
169                                 {
170                                         if ($CACHE->cache_file("config", true)) $CACHE->cache_destroy();
171                                 }
172                         }
173
174                         // Update variables to prevent errors
175                         $CONFIG['doubler_used'] += $points;
176                         $DOUBLER_POINTS -= $points;
177
178                         // Prepare array
179                         $content = array(
180                                 // Doubler transmission ID
181                                 'id'     => $id,
182                                 // Doubled points
183                                 'points' => TRANSLATE_COMMA($points),
184                                 // Timemark
185                                 'when'   => MAKE_DATETIME($time, "2"),
186                                 // IP number when the member submitted the doubling form
187                                 'ip'     => $ip,
188                         );
189
190                         // Load mail template and send mail away...
191                         $msg = LOAD_EMAIL_TEMPLATE("member_doubler", $content, $uid);
192                         SEND_EMAIL($uid, DOUBLER_MEMBER_SUBJECT, $msg);
193                 }
194         }
195 }
196
197 // Free memory
198 SQL_FREERESULT($result_total);
199 SQL_FREERESULT($result_main);
200
201 //
202 ?>