8aab915b5fae453da849a917739a0f9a832f68a4
[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         $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 }
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 = bigintval($_CONFIG['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, $_CONFIG['doubler_max_sent']), __FILE__, __LINE__);
73
74 if (((SQL_NUMROWS($result_total) > 0) && ($_CONFIG['doubler_sent_all'] == 'Y')) || ((SQL_NUMROWS($result_main) == $_CONFIG['doubler_group_sent']) && ($_CONFIG['doubler_sent_all'] == 'N')))
75 {
76         // Switch to matching SQL resource
77         $result_load = $result_main;
78         if ((SQL_NUMROWS($result_total) > 0) && ($_CONFIG['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         {
83                 // Only double when points are enougth!
84                 if ($DOUBLER_POINTS >= $points)
85                 {
86                         // Check for his ref points
87                         $result_ref = SQL_QUERY_ESC("SELECT SUM(points) FROM "._MYSQL_PREFIX."_doubler WHERE refid=%d AND completed='N' AND is_ref='Y'",
88                          array(bigintval($uid)), __FILE__, __LINE__);
89                         list($ref) = SQL_FETCHROW($result_ref);
90
91                         // Free memory
92                         SQL_FREERESULT($result_ref);
93
94                         // Zero refid when empty (might be helpful!)
95                         if (empty($ref)) $ref = 0;
96                         if (($ref > 0) && ($DOUBLER_UID == $uid) && (!empty($ref)))
97                         {
98                                 // Referral points found so add them and set line(s) to completed='Y'
99                                 $points += $ref;
100                                 $result_ref = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE refid=%d AND completed='N' AND is_ref='Y'",
101                                  array(bigintval($uid)), __FILE__, __LINE__);
102                         }
103                          else
104                         {
105                                 // No referral points found
106                                 $ref = "0";
107                         }
108
109                         // Exclude webmaster from doubling...
110                         if ($uid != $_CONFIG['doubler_uid'])
111                         {
112                                 // Add points
113                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET points=points+%s WHERE userid=%d AND ref_depth=0 LIMIT 1",
114                                  array($points, bigintval($uid)), __FILE__, __LINE__);
115
116                                 // Update mediadata as well
117                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
118                                 {
119                                         // Update database
120                                         MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);
121                                 }
122                         }
123
124                         // Set entry as "payed"
125                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE id=%d LIMIT 1",
126                          array(bigintval($id)), __FILE__, __LINE__);
127
128                         $OK = false;
129                         // Check for jackpot inclusion in doubling process
130                         if (($jackpot > 0) && ($jackpot >= $points) && ($_CONFIG['doubler_jackpot'] == 'Y'))
131                         {
132                                 // Subtract points from jackpot
133                                 SUB_JACKPOT($points);
134                                 $jackpot -= $points;
135
136                                 // Okay, done!
137                                 $OK = true;
138                         }
139
140                         // Exclude also webmaster's ID in taking points from webmaster's account
141                         if (($user > 0) && ($user >= $points) && (!$OK) && ($_CONFIG['doubler_uid'] > 0) && ($uid != $_CONFIG['doubler_uid']))
142                         {
143                                 // Add points to used points
144                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET used_points=used_points+%s WHERE userid='%d' LIMIT 1",
145                                  array($points, $_CONFIG['doubler_uid']), __FILE__, __LINE__);
146
147                                 // Update mediadata as well
148                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
149                                 {
150                                         // Update database
151                                         MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
152                                 }
153
154                                 // Okay, done!
155                                 $OK = true;
156                         }
157
158                         // Update doubler's account only when others are not updated
159                         if (!$OK)
160                         {
161                                 // Add points to used doubler points
162                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET doubler_used=doubler_used+%s WHERE config=0 LIMIT 1",
163                                  array($points), __FILE__, __LINE__);
164
165                                 // Destroy cache
166                                 if (GET_EXT_VERSION("cache") >= "0.1.2")
167                                 {
168                                         if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
169                                 }
170                         }
171
172                         // Update variables to prevent errors
173                         $_CONFIG['doubler_used'] += $points;
174                         $DOUBLER_POINTS -= $points;
175
176                         // Prepare array
177                         $content = array(
178                                 // Doubler transmission ID
179                                 'id'     => $id,
180                                 // Doubled points
181                                 'points' => TRANSLATE_COMMA($points),
182                                 // Timemark
183                                 'when'   => MAKE_DATETIME($time, "2"),
184                                 // IP number when the member submitted the doubling form
185                                 'ip'     => $ip,
186                         );
187
188                         // Load mail template and send mail away...
189                         $msg = LOAD_EMAIL_TEMPLATE("member_doubler", $content, $uid);
190                         SEND_EMAIL($uid, DOUBLER_MEMBER_SUBJECT, $msg);
191                 }
192         }
193 }
194
195 // Free memory
196 SQL_FREERESULT($result_total);
197 SQL_FREERESULT($result_main);
198
199 //
200 ?>