f2fc13ccebd338f1d2e73e625de0b30ff14b2af6
[mailer.git] / inc / mails / doubler_mails.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } elseif (!isExtensionActive('doubler')) {
42         // Do not execute when extension is absent
43         return;
44 } elseif (!isHtmlOutputMode()) {
45         // Do not execute when script is in CSS mode
46         return;
47 }
48
49 // Initialize variables
50 $jackpotPoints = '0';
51 $userPoints = '0';
52
53 // @TODO Can this be rewritten to a filter?
54 if (isExtensionActive('jackpot')) {
55         $jackpotPoints = getJackpotPoints();
56 } // END - if
57
58 // Get total points of the doubler itself
59 $totalPoints = getDoublerTotalPointsLeft();
60 if ($totalPoints == '0') {
61         // Exit here to prevent some SQL errors (sqlQueryEscaped doen't insert zeros! We need to fix this...)
62         return;
63 } // END - if
64
65 // If not currently doubled set it to zero
66 unsetGetRequestElement('DOUBLER_USERID');
67 unsetPostRequestElement('DOUBLER_USERID');
68 setSession('DOUBLER_USERID', '');
69 if (empty($GLOBALS['local_doubler_userid'])) $GLOBALS['local_doubler_userid'] = '0';
70
71 // Check for doubles which we can pay out
72 $result_total = sqlQueryEscaped("SELECT
73         COUNT(`do`.`id`) AS `cnt`,
74 FROM
75         `{?_MYSQL_PREFIX?}_doubler` AS `do`
76 INNER JOIN
77         `{?_MYSQL_PREFIX?}_user_data` AS `d`
78 ON
79         `do`.`userid`=`d`.`userid`
80 WHERE
81         `d`.`status`='CONFIRMED'
82         " . runFilterChain('user_exclusion_sql', ' ' . $whereStatement) . " AND
83         `do`.`points` <= %s AND
84         `do`.`points` >= ({?doubler_min?} * 2) AND
85         `do`.`completed`='N' AND
86         `do`.`is_ref`='N'
87 ORDER BY
88         `do`.`timemark` ASC",
89         array(
90                 $totalPoints
91         ), __FILE__, __LINE__);
92
93 // Get total count
94 list($totalCount) = sqlFetchRow($result_total);
95
96 // Free memory
97 sqlFreeResult($result_total);
98
99 // Check for accounts with limitation
100 $result_main = sqlQueryEscaped("SELECT
101         `do`.`id`,
102         `do`.`userid`,
103         `do`.`points`,
104         `do`.`remote_ip`,
105         `do`.`timemark`
106 FROM
107         `{?_MYSQL_PREFIX?}_doubler` AS `do`
108 INNER JOIN
109         `{?_MYSQL_PREFIX?}_user_data` AS `d`
110 ON
111         `do`.`userid`=`d`.`userid`
112 WHERE
113         `d`.`status`='CONFIRMED'
114         " . runFilterChain('user_exclusion_sql', ' ' . $whereStatement) . " AND
115         `do`.`points` <= %s AND
116         `do`.`points` >= ({?doubler_min?} * 2) AND
117         `do`.`completed`='N' AND
118         `do`.`is_ref`='N'
119 ORDER BY
120         `do`.`timemark` ASC
121 LIMIT {?doubler_max_sent?}",
122         array(
123                 $totalPoints
124         ), __FILE__, __LINE__);
125
126 // Are there entries found?
127 if ((($totalCount > 0) && (getConfig('doubler_sent_all') == 'Y')) || ((sqlNumRows($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != 'Y'))) {
128         // Switch to matching SQL resource
129         $result_load = $result_main;
130         if (($totalCount > 0) && (getConfig('doubler_sent_all') == 'Y')) {
131                 $result_load = $result_total;
132         } // END - if
133
134         // At least one account was found
135         while ($content = sqlFetchArray($result_load)) {
136                 // Only double when points are enougth!
137                 if ($totalPoints >= $content['points']) {
138                         // Check for his ref points
139                         $refPoints = countSumTotalData($content['userid'], 'doubler', 'points', 'refid', FALSE, " AND `completed`='N' AND `is_ref`='Y'");
140
141                         // Zero refid when empty (might be helpful!)
142                         if (empty($refPoints)) {
143                                 $refPoints = '0';
144                         } // END - if
145
146                         if (($refPoints > 0) && ($GLOBALS['local_doubler_userid'] == $content['userid']) && (!empty($refPoints))) {
147                                 // Referral points found so add them and set line(s) to completed='Y'
148                                 $content['points'] += $refPoints;
149                                 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `refid`=%s AND `completed`='N' AND `is_ref`='Y'",
150                                         array(bigintval($content['userid'])), __FILE__, __LINE__);
151                         } else {
152                                 // No referral points found
153                                 $refPoints = '0';
154                         }
155
156                         // Exclude webmaster from doubling...
157                         if ($content['userid'] != getDoublerUserid()) {
158                                 // Add points
159                                 initReferralSystem();
160                                 addPointsThroughReferralSystem(sprintf('doubler_%s', strtolower(getDoublerSendMode())), $content['userid'], $content['points']);
161                         } // END - if
162
163                         // Set entry as "payed"
164                         sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `id`=%s LIMIT 1",
165                                 array(bigintval($content['id'])), __FILE__, __LINE__);
166
167                         $okay = FALSE;
168
169                         // Check for jackpot inclusion in doubling process
170                         if (($jackpotPoints > 0) && ($jackpotPoints >= $content['points']) && (getConfig('doubler_jackpot') == 'Y')) {
171                                 // Subtract points from jackpot
172                                 $okay = subtractPointsFromJackpot($content['points']);
173                                 $jackpotPoints -= $content['points'];
174                         } // END - if
175
176                         // Exclude also webmaster's id in taking points from webmaster's account
177                         if (($userPoints > 0) && ($userPoints >= $content['points']) && ($okay === FALSE) && (isValidId(getDoublerUserid())) && ($content['userid'] != getDoublerUserid())) {
178                                 // Subtract points
179                                 $okay = subtractPoints('doubler_payout', getDoublerUserid(), $content['points']);
180                         } // END - if
181
182                         // Update doubler's account only when others are not updated
183                         if ($okay === FALSE) {
184                                 // Add points to used doubler points
185                                 updateConfiguration('doubler_used', $content['points'], '+');
186                         } // END - if
187
188                         // Update variables to prevent errors
189                         incrementConfigEntry('doubler_used', $content['points']);
190                         $totalPoints -= $content['points'];
191
192                         // Prepare array
193                         $content['timemark'] = generateDateTime($content['timemark'], 2);
194
195                         // Load mail template and send mail away...
196                         $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
197                         sendEmail($content['userid'], '{--MEMBER_DOUBLER_SUBJECT--}', $message);
198                 } // END - if
199         } // END - while
200 } // END - if
201
202 // Free memory
203 sqlFreeResult($result_total);
204 sqlFreeResult($result_main);
205
206 // [EOF]
207 ?>