75d4b46c5cdca4b5a91f6aef6362333ba7e14eee
[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 d.id, d.userid, d.points, d.remote_ip, d.timemark
68 FROM `{?_MYSQL_PREFIX?}_doubler` AS d
69 LEFT JOIN `{?_MYSQL_PREFIX?}_user_data` AS u
70 ON d.userid=u.userid
71 WHERE u.`status`='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
72 ORDER BY d.timemark",
73         array(
74                 $DOUBLER_POINTS,
75                 getConfig(('doubler_min') * 2)
76         ), __FILE__, __LINE__);
77
78 // Check for accounts with limitation
79 $result_main = SQL_QUERY_ESC("SELECT d.id, d.userid, d.points, d.remote_ip, d.timemark
80 FROM `{?_MYSQL_PREFIX?}_doubler` AS d
81 LEFT JOIN `{?_MYSQL_PREFIX?}_user_data` AS u
82 ON d.userid=u.userid
83 WHERE u.`status`='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'
84 ORDER BY d.timemark
85 LIMIT %d",
86         array(
87                 $DOUBLER_POINTS,
88                 getConfig(('doubler_min') * 2),
89                 getConfig('doubler_max_sent')
90         ), __FILE__, __LINE__);
91
92 // Do we have entries found?
93 if (((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == 'Y')) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != 'Y'))) {
94         // Switch to matching SQL resource
95         $result_load = $result_main;
96         if ((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == 'Y')) $result_load = $result_total;
97
98         // At least one account was found
99         while ($content = SQL_FETCHARRAY($result_load)) {
100                 // Only double when points are enougth!
101                 if ($DOUBLER_POINTS >= $content['points']) {
102                         // Check for his ref points
103                         $ref = countSumTotalData($content['userid'], 'doubler', 'points', 'refid', false, " AND `completed`='N' AND `is_ref`='Y'");
104
105                         // Zero refid when empty (might be helpful!)
106                         if (empty($ref)) $ref = 0;
107
108                         if (($ref > 0) && ($GLOBALS['doubler_userid'] == $content['userid']) && (!empty($ref))) {
109                                 // Referal points found so add them and set line(s) to completed='Y'
110                                 $content['points'] += $ref;
111                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `refid`=%s AND `completed`='N' AND `is_ref`='Y'",
112                                         array(bigintval($content['userid'])), __FILE__, __LINE__);
113                         } else {
114                                 // No referal points found
115                                 $ref = 0;
116                         }
117
118                         // Exclude webmaster from doubling...
119                         if ($content['userid'] != getConfig('doubler_userid')) {
120                                 // Add points
121                                 addPointsDirectly(sprintf("doubler_%s", strtolower(getConfig('doubler_send_mode'))), $content['userid'], $content['points']);
122                         } // END - if
123
124                         // Set entry as "payed"
125                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `id`=%s LIMIT 1",
126                                 array(bigintval($content['id'])), __FILE__, __LINE__);
127
128                         $okay = false;
129
130                         // Check for jackpot inclusion in doubling process
131                         if (($jackpot > 0) && ($jackpot >= $content['points']) && (getConfig('doubler_jackpot') == 'Y')) {
132                                 // Subtract points from jackpot
133                                 subtractPointsFromJackpot($content['points']);
134                                 $jackpot -= $content['points'];
135
136                                 // Okay, done!
137                                 $okay = true;
138                         } // END - if
139
140                         // Exclude also webmaster's id in taking points from webmaster's account
141                         if (($user > 0) && ($user >= $content['points']) && ($okay === false) && (getConfig('doubler_userid') > 0) && ($content['userid'] != getConfig('doubler_userid'))) {
142                                 // Add points to used points
143                                 subtractPoints('doubler_payout', getConfig('doubler_userid'), $content['points']);
144
145                                 // Okay, done!
146                                 $okay = true;
147                         } // END - if
148
149                         // Update doubler's account only when others are not updated
150                         if ($okay === false) {
151                                 // Add points to used doubler points
152                                 updateConfiguration('doubler_used', $content['points'], '+');
153                         } // END - if
154
155                         // Update variables to prevent errors
156                         incrementConfigEntry('doubler_used', $content['points']);
157                         $DOUBLER_POINTS -= $content['points'];
158
159                         // Prepare array
160                         $content = array(
161                                 // Doubler transmission id
162                                 'id'     => $content['id'],
163                                 // Doubled points
164                                 'points' => translateComma($content['points']),
165                                 // Timemark
166                                 'when'   => generateDateTime($content['timemark'], 2),
167                                 // IP number when the member submitted the doubling form
168                                 'ip'     => $content['remote_ip'],
169                         );
170
171                         // Load mail template and send mail away...
172                         $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
173                         sendEmail($content['userid'], getMessage('DOUBLER_MEMBER_SUBJECT'), $message);
174                 } // END - if
175         } // END - while
176 } // END - if
177
178 // Free memory
179 SQL_FREERESULT($result_total);
180 SQL_FREERESULT($result_main);
181
182 // [EOF]
183 ?>