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