791dc3e025881aefc00a0cef03c5d71397b34c3e
[mailer.git] / doubler.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 02/13/2005 *
4  * ===============                              Last change: 02/13/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : doubler.php                                      *
8  * -------------------------------------------------------------------- *
9  * Short description : Points doubler                                   *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Punkteverdoppler                                 *
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 // Load security stuff here
40 require('inc/libs/security_functions.php');
41
42 // Init start time
43 $GLOBALS['startTime'] = microtime(true);
44
45 // Set module
46 $GLOBALS['module'] = 'doubler';
47 $GLOBALS['output_mode'] = 0;
48
49 // Load the required file(s)
50 require('inc/config-global.php');
51
52 // Set content type
53 setContentType('text/html');
54
55 // Is the 'doubler' extension active?
56 redirectOnUninstalledExtension('doubler');
57
58 // Probe for referal ID
59 if (isGetRequestElementSet('refid')) $GLOBALS['refid'] = getRequestElement('refid');
60
61 // Only check this if refid is provided!
62 if (determineReferalId() > 0) {
63         // Do we have nickname or userid set?
64         if (isNicknameUsed(determineReferalId())) {
65                 // Nickname in URL, so load the ID
66                 $result = SQL_QUERY_ESC("SELECT `userid`, `status` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `nickname`='%s' LIMIT 1",
67                         array(determineReferalId()), __FILE__, __LINE__);
68         } else {
69                 // Direct userid entered
70                 $result = SQL_QUERY_ESC("SELECT `userid`, `status` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
71                         array(determineReferalId()), __FILE__, __LINE__);
72         }
73
74         // Load data
75         list($rid, $status_ref) = SQL_FETCHROW($result);
76         $GLOBALS['refid'] = bigintval($rid);
77
78         // Free memory
79         SQL_FREERESULT($result);
80 } // END - if
81
82 // Init userid
83 $userid = 0;
84
85 // If no account was found set default refid and status to CONFIRMED
86 if (empty($GLOBALS['refid'])) {
87         // Determine referal id again
88         $GLOBALS['refid'] = determineReferalId();
89         $status = 'CONFIRMED';
90 } // END - if
91
92 // Init content array
93 $content = array(
94         'message' => '',
95 );
96
97 // Begin with doubler script...
98 if (isFormSent()) {
99         // Secure points (so only integer/double values are allowed
100         setRequestPostElement('points', bigintval(postRequestElement('points')));
101
102         // Begin with doubling process
103         if ((isPostRequestElementSet('userid')) && (isPostRequestElementSet('pass')) && (isPostRequestElementSet('points'))) {
104                 // Probe for nickname extension and if a nickname was entered
105                 if (isNickNameUsed(postRequestElement('userid'))) {
106                         // Nickname in URL, so load the ID
107                         $result = SQL_QUERY_ESC("SELECT `userid`, `status`, `password` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `nickname`='%s' LIMIT 1",
108                                 array(postRequestElement('userid')), __FILE__, __LINE__);
109                 } else {
110                         // Direct userid entered
111                         $result = SQL_QUERY_ESC("SELECT `userid`, `status, `password` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
112                                 array(bigintval(postRequestElement('userid'))), __FILE__, __LINE__);
113                 }
114
115                 // Load data
116                 list($userid, $status, $password) = SQL_FETCHROW($result);
117                 $userid = bigintval($userid);
118
119                 // Free result
120                 SQL_FREERESULT($result);
121
122                 // Remove any dots and unwanted chars from the points
123                 setRequestPostElement('points', bigintval(round(convertCommaToDot(postRequestElement('points')))));
124
125                 // Probe for enough points
126                 $probe_points = ((postRequestElement('points') >= getConfig('doubler_min')) && (postRequestElement('points') <= getConfig('doubler_max')));
127
128                 // Check all together
129                 if ((!empty($userid)) && ($password == generateHash(postRequestElement('pass'), substr($password, 0, -40))) && ($status == 'CONFIRMED') && ($probe_points)) {
130                         // Nickname resolved to a unique userid or direct userid entered by the member
131                         $GLOBALS['doubler_userid'] = $userid;
132
133                         // Calulcate points
134                         $points = countSumTotalData($userid, 'user_points', 'points') - countSumTotalData($userid, 'user_data', 'used_points');
135
136                         // So let's continue with probing his points amount
137                         if (($points - getConfig('doubler_left') - postRequestElement('points') * getConfig('doubler_charge')) >= 0) {
138                                 // Enough points are left so let's continue with the doubling process
139                                 // Create doubling "account" width *DOUBLED* points
140                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_doubler` (`userid`, `refid`, `points`, `remote_ip`, `timemark`, `completed`, `is_ref`) VALUES ('%s','%s','%s','".detectRemoteAddr()."', UNIX_TIMESTAMP(), 'N','N')",
141                                         array($userid, determineReferalId(), bigintval(postRequestElement('points') * 2)), __FILE__, __LINE__);
142
143                                 // Subtract entered points
144                                 subtractPoints('doubler', $userid, postRequestElement('points'));
145
146                                 // Add points to "total payed" including charge
147                                 $points = postRequestElement('points') - postRequestElement('points') * getConfig('doubler_charge');
148                                 updateConfiguration('doubler_points', $points, '+');
149                                 incrementConfigEntry('doubler_points', $points);
150
151                                 // Add second line for the referal but only when userid != refid
152                                 if ((determineReferalId() > 0) && (determineReferalId() != $userid)) {
153                                         // Okay add a refid line and apply refid percents
154                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_doubler` (`userid`, `refid`, `points`, `remote_ip`, `timemark`, `completed`, `is_ref`) VALUES ('%s',0,'%s','".detectRemoteAddr()."',UNIX_TIMESTAMP(),'N','Y')",
155                                                 array(
156                                                         determineReferalId(),
157                                                         bigintval(postRequestElement('points') * 2 * getConfig('doubler_ref'))
158                                                 ), __FILE__, __LINE__);
159
160                                         // And that's why we don't want to you more than one referal level of doubler-points. ^^^
161                                 } // END - if
162
163                                 // Update usage counter
164                                 updateConfiguration('doubler_counter', 1, '+');
165
166                                 // Set constant
167                                 $content['message'] = loadTemplate('doubler_reflink', true, postRequestElement('userid'));
168                         } else {
169                                 // Not enougth points left
170                                 $content['message'] = getMessage('DOUBLER_FORM_NO_POINTS_LEFT');
171                         }
172                 } elseif ($status == 'CONFIRMED') {
173                         // Account is unconfirmed!
174                         $content['message'] = getMessage('DOUBLER_FORM_WRONG_PASS');
175                 } elseif ($status == 'UNCONFIRMED') {
176                         // Account is unconfirmed!
177                         $content['message'] = getMessage('DOUBLER_FORM_STATUS_UNCONFIRMED');
178                 } elseif ($status == 'LOCKED') {
179                         // Account is locked by admin / holiday!
180                         $content['message'] = getMessage('DOUBLER_FORM_STATUS_LOCKED');
181                 } elseif (postRequestElement('points') < getConfig('doubler_min')) {
182                         // Not enougth points entered
183                         $content['message'] = getMessage('DOUBLER_FORM_POINTS_MIN');
184                 } elseif (postRequestElement('points') > getConfig('doubler_max')) {
185                         // Too much points entered
186                         $content['message'] = getMessage('DOUBLER_FORM_POINTS_MAX');
187                 } elseif (isNickNameUsed(postRequestElement('userid'))) {
188                         // Cannot resolv nickname -> userid
189                         $content['message'] = getMessage('DOUBLER_FORM_404_NICKNAME');
190                 } else {
191                         // Wrong password or account not found
192                         $content['message'] = getMessage('DOUBLER_FORM_404_MEMBER');
193                 }
194         } elseif (!isPostRequestElementSet('userid')) {
195                 // Login not entered
196                 $content['message'] = getMessage('DOUBLER_FORM_404_LOGIN');
197         } elseif (!isPostRequestElementSet('pass')) {
198                 // Password not entered
199                 $content['message'] = getMessage('DOUBLER_FORM_404_PASSWORD');
200         } elseif (!isPostRequestElementSet('points')) {
201                 // points not entered
202                 $content['message'] = getMessage('DOUBLER_FORM_404_POINTS');
203         }
204 }
205
206 // Shall I check for points immediately?
207 if (getConfig('doubler_send_mode') == 'DIRECT') loadInclude('inc/mails/doubler_mails.php');
208
209 // Output header
210 loadIncludeOnce('inc/header.php');
211
212 // Banner in text
213 $content['banner'] = loadTemplate('doubler_banner', true);
214
215 // Load header/footer templates
216 $content['header'] = loadTemplate('doubler_header', true);
217 $content['footer'] = loadTemplate('doubler_footer', true);
218
219 if (!empty($userid)) {
220         // Transfer userid/nickname to constant
221         $content['refid'] = $userid;
222 } else {
223         // Transfer userid/nickname to constant
224         $content['refid'] = determineReferalId();
225 }
226
227 // Percent values etc.
228 $content['charge'] = translateComma(getConfig('doubler_charge') * 100);
229 $content['ref']    = translateComma(getConfig('doubler_ref') * 100);
230 $content['total']  = translateComma(getConfig('doubler_points'));
231 $content['min']    = translateComma(getConfig('doubler_min'));
232 $content['max']    = translateComma(getConfig('doubler_max'));
233
234 // Text "Enter login"
235 if (isExtensionActive('nickname')) {
236         // Choose login/nickname
237         $content['enter_login'] = getMessage('GUEST_ENTER_LOGIN_NICKNAME');
238 } else {
239         // Simple login ID
240         $content['enter_login'] = getMessage('GUEST_ENTER_LOGIN');
241 }
242
243 // Which mail-send-mode did the admin setup?
244 $content['payout_time'] = getMessage('DOUBLER_PAYOUT_TIME_' . getConfig('doubler_send_mode'));
245
246 // Generate table with already payed out doubles
247 $content['payout_history'] = generateDoublerTable(0, 'Y', 'N', 'DESC');
248
249 // Generate timemark
250 $content['timeout_mark'] = createFancyTime(getConfig('doubler_timeout'));
251
252 // Points left to doubler
253 $content['left'] = translateComma(DOUBLER_GET_TOTAL_POINTS_LEFT());
254
255 // Output neccessary form for this
256 loadTemplate('doubler_index', false, $content);
257
258 // Output footer
259 loadIncludeOnce('inc/footer.php');
260
261 // [EOF]
262 ?>