New function isValidUserid() introduced, more rewrites to EL:
[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  * 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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } elseif (!isExtensionActive('doubler')) {
44         return;
45 }
46
47 // Do not execute when script is in CSS mode
48 if (getOutputMode() != 0) return;
49
50 // Initialize variables
51 $jackpotPoints = '0'; $userPoints = '0';
52 if (isExtensionActive('jackpot')) $jackpotPoints = getJackpotPoints();
53
54 // Get total points of the doubler itself
55 $DOUBLER_POINTS = DOUBLER_GET_TOTAL_POINTS_LEFT();
56 if ($DOUBLER_POINTS == '0') {
57         // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)
58         return;
59 } // END - if
60
61 // If not currently doubled set it to zero
62 unsetGetRequestParameter('DOUBLER_UID');
63 unsetPostRequestParameter('DOUBLER_UID');
64 setSession('DOUBLER_UID', '');
65 if (empty($GLOBALS['doubler_userid'])) $GLOBALS['doubler_userid'] = '0';
66
67 // Check for doubles which we can pay out
68 $result_total = SQL_QUERY_ESC("SELECT
69         d.id, d.userid, d.points, d.remote_ip, d.timemark
70 FROM
71         `{?_MYSQL_PREFIX?}_doubler` AS d
72 LEFT JOIN
73         `{?_MYSQL_PREFIX?}_user_data` AS u
74 ON
75         d.userid=u.userid
76 WHERE
77         u.`status`='CONFIRMED' AND
78         d.points <= %s AND
79         d.points >= ({?doubler_min?} * 2) AND
80         d.completed='N' AND
81         d.is_ref='N'
82 ORDER BY
83         d.timemark ASC",
84         array(
85                 $DOUBLER_POINTS
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 >= ({?doubler_min?} * 2) AND
101         d.completed='N' AND
102         d.is_ref='N'
103 ORDER BY
104         d.timemark ASC
105 LIMIT {?doubler_max_sent?}",
106         array(
107                 $DOUBLER_POINTS
108         ), __FILE__, __LINE__);
109
110 // Do we have entries found?
111 if (((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == 'Y')) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != 'Y'))) {
112         // Switch to matching SQL resource
113         $result_load = $result_main;
114         if ((SQL_NUMROWS($result_total) > 0) && (getConfig('doubler_sent_all') == 'Y')) $result_load = $result_total;
115
116         // At least one account was found
117         while ($content = SQL_FETCHARRAY($result_load)) {
118                 // Only double when points are enougth!
119                 if ($DOUBLER_POINTS >= $content['points']) {
120                         // Check for his ref points
121                         $ref = countSumTotalData($content['userid'], 'doubler', 'points', 'refid', false, " AND `completed`='N' AND `is_ref`='Y'");
122
123                         // Zero refid when empty (might be helpful!)
124                         if (empty($ref)) $ref = '0';
125
126                         if (($ref > 0) && ($GLOBALS['doubler_userid'] == $content['userid']) && (!empty($ref))) {
127                                 // Referal points found so add them and set line(s) to completed='Y'
128                                 $content['points'] += $ref;
129                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `refid`=%s AND `completed`='N' AND `is_ref`='Y'",
130                                         array(bigintval($content['userid'])), __FILE__, __LINE__);
131                         } else {
132                                 // No referal points found
133                                 $ref = '0';
134                         }
135
136                         // Exclude webmaster from doubling...
137                         if ($content['userid'] != getConfig('doubler_userid')) {
138                                 // Add points
139                                 addPointsDirectly(sprintf("doubler_%s", strtolower(getConfig('doubler_send_mode'))), $content['userid'], $content['points']);
140                         } // END - if
141
142                         // Set entry as "payed"
143                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `id`=%s LIMIT 1",
144                                 array(bigintval($content['id'])), __FILE__, __LINE__);
145
146                         $okay = false;
147
148                         // Check for jackpot inclusion in doubling process
149                         if (($jackpotPoints > 0) && ($jackpotPoints >= $content['points']) && (getConfig('doubler_jackpot') == 'Y')) {
150                                 // Subtract points from jackpot
151                                 subtractPointsFromJackpot($content['points']);
152                                 $jackpotPoints -= $content['points'];
153
154                                 // Okay, done!
155                                 $okay = true;
156                         } // END - if
157
158                         // Exclude also webmaster's id in taking points from webmaster's account
159                         if (($userPoints > 0) && ($userPoints >= $content['points']) && ($okay === false) && (getConfig('doubler_userid') > 0) && ($content['userid'] != getConfig('doubler_userid'))) {
160                                 // Add points to used points
161                                 subtractPoints('doubler_payout', getConfig('doubler_userid'), $content['points']);
162
163                                 // Okay, done!
164                                 $okay = true;
165                         } // END - if
166
167                         // Update doubler's account only when others are not updated
168                         if ($okay === false) {
169                                 // Add points to used doubler points
170                                 updateConfiguration('doubler_used', $content['points'], '+');
171                         } // END - if
172
173                         // Update variables to prevent errors
174                         incrementConfigEntry('doubler_used', $content['points']);
175                         $DOUBLER_POINTS -= $content['points'];
176
177                         // Prepare array
178                         $content = array(
179                                 // Doubler transmission id
180                                 'id'        => $content['id'],
181                                 // Doubled points
182                                 'points'    => $content['points'],
183                                 // Timemark
184                                 'timemark'  => generateDateTime($content['timemark'], 2),
185                                 // IP number when the member submitted the doubling form
186                                 'remote_ip' => $content['remote_ip'],
187                         );
188
189                         // Load mail template and send mail away...
190                         $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
191                         sendEmail($content['userid'], getMessage('DOUBLER_MEMBER_SUBJECT'), $message);
192                 } // END - if
193         } // END - while
194 } // END - if
195
196 // Free memory
197 SQL_FREERESULT($result_total);
198 SQL_FREERESULT($result_main);
199
200 // [EOF]
201 ?>