2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 02/16/2005 *
4 * =================== Last change: 01/21/2006 *
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 - 2013 by Mailer Developer Team *
15 * For more information visit: http://mxchange.org *
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. *
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. *
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, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
36 } elseif (!isExtensionActive('doubler')) {
37 // Do not execute when extension is absent
39 } elseif (!isHtmlOutputMode()) {
40 // Do not execute when script is in CSS mode
44 // Initialize variables
48 // @TODO Can this be rewritten to a filter?
49 if (isExtensionActive('jackpot')) {
50 $jackpotPoints = getJackpotPoints();
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...)
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';
66 // Check for doubles which we can pay out
67 $result_total = sqlQueryEscaped("SELECT
68 COUNT(`do`.`id`) AS `cnt`,
70 `{?_MYSQL_PREFIX?}_doubler` AS `do`
72 `{?_MYSQL_PREFIX?}_user_data` AS `d`
74 `do`.`userid`=`d`.`userid`
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
86 ), __FILE__, __LINE__);
89 list($totalCount) = sqlFetchRow($result_total);
92 sqlFreeResult($result_total);
94 // Check for accounts with limitation
95 $result_main = sqlQueryEscaped("SELECT
102 `{?_MYSQL_PREFIX?}_doubler` AS `do`
104 `{?_MYSQL_PREFIX?}_user_data` AS `d`
106 `do`.`userid`=`d`.`userid`
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
116 LIMIT {?doubler_max_sent?}",
119 ), __FILE__, __LINE__);
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;
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'");
136 // Zero refid when empty (might be helpful!)
137 if (empty($refPoints)) {
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__);
147 // No referral points found
151 // Exclude webmaster from doubling...
152 if ($content['userid'] != getDoublerUserid()) {
154 initReferralSystem();
155 addPointsThroughReferralSystem(sprintf('doubler_%s', strtolower(getDoublerSendMode())), $content['userid'], $content['points']);
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__);
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'];
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())) {
174 $okay = subtractPoints('doubler_payout', getDoublerUserid(), $content['points']);
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'], '+');
183 // Update variables to prevent errors
184 incrementConfigEntry('doubler_used', $content['points']);
185 $totalPoints -= $content['points'];
188 $content['timemark'] = generateDateTime($content['timemark'], 2);
190 // Load mail template and send mail away...
191 $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
192 sendEmail($content['userid'], '{--MEMBER_DOUBLER_SUBJECT--}', $message);
198 sqlFreeResult($result_total);
199 sqlFreeResult($result_main);