Unknown module in non-HTML mode, debug comments removed :(
[mailer.git] / inc / mails / doubler_mails.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 02/16/2005 *
4  * ===============                              Last change: 01/21/2006 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : doubler_mails.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 - 2009 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         die();
42 } elseif (!isExtensionActive('doubler')) {
43         return;
44 }
45
46 // Do not execute when script is in CSS mode
47 if (getOutputMode() != 0) return;
48
49 // Initialize variables
50 $jackpot = '0'; $user = '0';
51 if (isExtensionActive('jackpot')) $jackpot = getJackpotPoints();
52
53 // Get total points of the doubler itself
54 $DOUBLER_POINTS = DOUBLER_GET_TOTAL_POINTS_LEFT();
55 if ($DOUBLER_POINTS == '0') {
56         // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)
57         return;
58 } // END - if
59
60 // If not currently doubled set it to zero
61 unsetGetRequestElement('DOUBLER_UID');
62 unsetPostRequestElement('DOUBLER_UID');
63 setSession('DOUBLER_UID', '');
64 if (empty($GLOBALS['doubler_userid'])) $GLOBALS['doubler_userid'] = '0';
65
66 // Check for doubles which we can pay out
67 $result_total = SQL_QUERY_ESC("SELECT
68         d.id, d.userid, d.points, d.remote_ip, d.timemark
69 FROM
70         `{?_MYSQL_PREFIX?}_doubler` AS d
71 LEFT JOIN
72         `{?_MYSQL_PREFIX?}_user_data` AS u
73 ON
74         d.userid=u.userid
75 WHERE
76         u.`status`='CONFIRMED' AND
77         d.points <= %s AND
78         d.points >= %s AND
79         d.completed='N' AND
80         d.is_ref='N'
81 ORDER BY
82         d.timemark ASC",
83         array(
84                 $DOUBLER_POINTS,
85                 getConfig(('doubler_min') * 2)
86         ), __FILE__, __LINE__);
87
88 // Check for accounts with limitation
89 $result_main = SQL_QUERY_ESC("SELECT
90         d.id, d.userid, d.points, d.remote_ip, d.timemark
91 FROM
92         `{?_MYSQL_PREFIX?}_doubler` AS d
93 LEFT JOIN
94         `{?_MYSQL_PREFIX?}_user_data` AS u
95 ON
96         d.userid=u.userid
97 WHERE
98         u.`status`='CONFIRMED' AND
99         d.points <= %s AND
100         d.points >= %s AND
101         d.completed='N' AND
102         d.is_ref='N'
103 ORDER BY
104         d.timemark ASC
105 LIMIT %d",
106         array(
107                 $DOUBLER_POINTS,
108                 getConfig(('doubler_min') * 2),
109                 getConfig('doubler_max_sent')
110         ), __FILE__, __LINE__);
111
112 // Do we have entries found?
113 if (((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == 'Y')) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != 'Y'))) {
114         // Switch to matching SQL resource
115         $result_load = $result_main;
116         if ((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == 'Y')) $result_load = $result_total;
117
118         // At least one account was found
119         while ($content = SQL_FETCHARRAY($result_load)) {
120                 // Only double when points are enougth!
121                 if ($DOUBLER_POINTS >= $content['points']) {
122                         // Check for his ref points
123                         $ref = countSumTotalData($content['userid'], 'doubler', 'points', 'refid', false, " AND `completed`='N' AND `is_ref`='Y'");
124
125                         // Zero refid when empty (might be helpful!)
126                         if (empty($ref)) $ref = '0';
127
128                         if (($ref > 0) && ($GLOBALS['doubler_userid'] == $content['userid']) && (!empty($ref))) {
129                                 // Referal points found so add them and set line(s) to completed='Y'
130                                 $content['points'] += $ref;
131                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `refid`=%s AND `completed`='N' AND `is_ref`='Y'",
132                                         array(bigintval($content['userid'])), __FILE__, __LINE__);
133                         } else {
134                                 // No referal points found
135                                 $ref = '0';
136                         }
137
138                         // Exclude webmaster from doubling...
139                         if ($content['userid'] != getConfig('doubler_userid')) {
140                                 // Add points
141                                 addPointsDirectly(sprintf("doubler_%s", strtolower(getConfig('doubler_send_mode'))), $content['userid'], $content['points']);
142                         } // END - if
143
144                         // Set entry as "payed"
145                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `id`=%s LIMIT 1",
146                                 array(bigintval($content['id'])), __FILE__, __LINE__);
147
148                         $okay = false;
149
150                         // Check for jackpot inclusion in doubling process
151                         if (($jackpot > 0) && ($jackpot >= $content['points']) && (getConfig('doubler_jackpot') == 'Y')) {
152                                 // Subtract points from jackpot
153                                 subtractPointsFromJackpot($content['points']);
154                                 $jackpot -= $content['points'];
155
156                                 // Okay, done!
157                                 $okay = true;
158                         } // END - if
159
160                         // Exclude also webmaster's id in taking points from webmaster's account
161                         if (($user > 0) && ($user >= $content['points']) && ($okay === false) && (getConfig('doubler_userid') > 0) && ($content['userid'] != getConfig('doubler_userid'))) {
162                                 // Add points to used points
163                                 subtractPoints('doubler_payout', getConfig('doubler_userid'), $content['points']);
164
165                                 // Okay, done!
166                                 $okay = true;
167                         } // END - if
168
169                         // Update doubler's account only when others are not updated
170                         if ($okay === false) {
171                                 // Add points to used doubler points
172                                 updateConfiguration('doubler_used', $content['points'], '+');
173                         } // END - if
174
175                         // Update variables to prevent errors
176                         incrementConfigEntry('doubler_used', $content['points']);
177                         $DOUBLER_POINTS -= $content['points'];
178
179                         // Prepare array
180                         $content = array(
181                                 // Doubler transmission id
182                                 'id'     => $content['id'],
183                                 // Doubled points
184                                 'points' => translateComma($content['points']),
185                                 // Timemark
186                                 'when'   => generateDateTime($content['timemark'], 2),
187                                 // IP number when the member submitted the doubling form
188                                 'ip'     => $content['remote_ip'],
189                         );
190
191                         // Load mail template and send mail away...
192                         $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
193                         sendEmail($content['userid'], getMessage('DOUBLER_MEMBER_SUBJECT'), $message);
194                 } // END - if
195         } // END - while
196 } // END - if
197
198 // Free memory
199 SQL_FREERESULT($result_total);
200 SQL_FREERESULT($result_main);
201
202 // [EOF]
203 ?>