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