03a2e5872a946c1f5e2ea0bd1b8b9c3447f78d81
[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 - 2008 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['refid'] = 0;
48 $GLOBALS['output_mode'] = 0;
49
50 // Load the required file(s)
51 require('inc/config-global.php');
52
53 // Is the 'doubler' extension active?
54 redirectOnUninstalledExtension('doubler');
55
56 // Is the script installed?
57 if (!isInstalled()) {
58         // You have to install first!
59         redirectToUrl('install.php');
60 } // END - if
61
62 // Probe for referal ID
63 if (REQUEST_ISSET_GET('refid')) $GLOBALS['refid'] = REQUEST_GET('refid');
64
65 // Only check this if refid is provided!
66 if ($GLOBALS['refid'] > 0) {
67         // Do we have nickname or userid set?
68         if (isNicknameUsed($GLOBALS['refid'])) {
69                 // Nickname in URL, so load the ID
70                 $result = SQL_QUERY_ESC("SELECT `userid`, `status` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `nickname`='%s' LIMIT 1",
71                         array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
72         } else {
73                 // Direct userid entered
74                 $result = SQL_QUERY_ESC("SELECT userid, status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `userid`=%s LIMIT 1",
75                         array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
76         }
77
78         // Load data
79         list($rid, $status_ref) = SQL_FETCHROW($result);
80         $GLOBALS['refid'] = bigintval($rid);
81
82         // Free memory
83         SQL_FREERESULT($result);
84 } // END - if
85
86 // Init userid
87 $uid = 0;
88
89 // If no account was found set default refid and status to CONFIRMED
90 if (empty($GLOBALS['refid'])) {
91         $GLOBALS['refid'] = getConfig('def_refid');
92         $status = 'CONFIRMED';
93 } // END - if
94
95 // Begin with doubler script...
96 if (isFormSent()) {
97         // Secure points (so only integer/double values are allowed
98         REQUEST_SET_POST('points', bigintval(REQUEST_POST('points')));
99
100         // Begin with doubling process
101         if ((REQUEST_ISSET_POST('userid')) && (REQUEST_ISSET_POST('pass')) && (REQUEST_ISSET_POST('points'))) {
102                 // Probe for nickname extension and if a nickname was entered
103                 if (isNickNameUsed(REQUEST_POST('userid'))) {
104                         // Nickname in URL, so load the ID
105                         $result = SQL_QUERY_ESC("SELECT `userid`, `status`, `password` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `nickname`='%s' LIMIT 1",
106                                 array(REQUEST_POST('userid')), __FILE__, __LINE__);
107                 } else {
108                         // Direct userid entered
109                         $result = SQL_QUERY_ESC("SELECT `userid`, `status, `password` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `userid`=%s LIMIT 1",
110                                 array(bigintval(REQUEST_POST('userid'))), __FILE__, __LINE__);
111                 }
112
113                 // Load data
114                 list($uid, $status, $password) = SQL_FETCHROW($result);
115                 $uid = bigintval($uid);
116
117                 // Free result
118                 SQL_FREERESULT($result);
119
120                 // Remove any dots and unwanted chars from the points
121                 REQUEST_SET_POST('points', bigintval(round(convertCommaToDot(REQUEST_POST('points')))));
122
123                 // Probe for enough points
124                 $probe_points = ((REQUEST_POST('points') >= getConfig('doubler_min')) && (REQUEST_POST('points') <= getConfig('doubler_max')));
125
126                 // Check all together
127                 if ((!empty($uid)) && ($password == generateHash(REQUEST_POST('pass'), substr($password, 0, -40))) && ($status == 'CONFIRMED') && ($probe_points)) {
128                         // Nickname resolved to a unique userid or direct userid entered by the member
129                         $GLOBALS['doubler_uid'] = $uid;
130
131                         // Calulcate points
132                         $points = GET_TOTAL_DATA($uid, 'user_points', 'points') - GET_TOTAL_DATA($uid, 'user_data', 'used_points');
133
134                         // So let's continue with probing his points amount
135                         if (($points - getConfig('doubler_left') - REQUEST_POST('points') * getConfig('doubler_charge')) >= 0)
136                                 // Enough points are left so let's continue with the doubling process
137                                 // Create doubling "account" width *DOUBLED* points
138                                 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')",
139                                         array($uid, bigintval($GLOBALS['refid']), bigintval(REQUEST_POST('points') * 2)), __FILE__, __LINE__);
140
141                                 // Subtract entered points
142                                 SUB_POINTS('doubler', $uid, REQUEST_POST('points'));
143
144                                 // Add points to "total payed" including charge
145                                 $points = REQUEST_POST('points') - REQUEST_POST('points') * getConfig('doubler_charge');
146                                 updateConfiguration('doubler_points', $points, '+');
147                                 incrementConfigEntry('doubler_points', $points);
148
149                                 // Add second line for the referal but only when uid != refid
150                                 if (($GLOBALS['refid'] > 0) && ($GLOBALS['refid'] != $uid)) {
151                                         // Okay add a refid line and apply refid percents
152                                         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')",
153                                                 array(
154                                                         bigintval($GLOBALS['refid']),
155                                                         bigintval(REQUEST_POST('points') * 2 * getConfig('doubler_ref'))
156                                                 ), __FILE__, __LINE__);
157
158                                         // And that's why we don't want to you more than one referal level of doubler-points. ^^^
159                                 } // END - if
160
161                                 // Update usage counter
162                                 updateConfiguration('doubler_counter', 1, '+');
163
164                                 // Set constant
165                                 define('__DOUBLER_MSG', LOAD_TEMPLATE('doubler_reflink', true, REQUEST_POST('userid')));
166                         } else {
167                                 // Not enougth points left
168                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_NO_POINTS_LEFT'));
169                         }
170                 } elseif ($status == 'CONFIRMED') {
171                         // Account is unconfirmed!
172                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_WRONG_PASS'));
173                 } elseif ($status == 'UNCONFIRMED') {
174                         // Account is unconfirmed!
175                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_STATUS_UNCONFIRMED'));
176                 } elseif ($status == 'LOCKED') {
177                         // Account is locked by admin / holiday!
178                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_STATUS_LOCKED'));
179                 } elseif (REQUEST_POST('points') < getConfig('doubler_min')) {
180                         // Not enougth points entered
181                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_POINTS_MIN'));
182                 } elseif (REQUEST_POST('points') > getConfig('doubler_max')) {
183                         // Too much points entered
184                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_POINTS_MAX'));
185                 } elseif (isNickNameUsed(REQUEST_POST('userid'))) {
186                         // Cannot resolv nickname -> userid
187                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_NICKNAME'));
188                 } else {
189                         // Wrong password or account not found
190                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_MEMBER'));
191                 }
192         } elseif (!REQUEST_ISSET_POST('userid')) {
193                 // Login not entered
194                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_LOGIN'));
195         } elseif (!REQUEST_ISSET_POST('pass')) {
196                 // Password not entered
197                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_PASSWORD'));
198         } elseif (!REQUEST_ISSET_POST('points')) {
199                 // points not entered
200                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_POINTS'));
201         }
202 }
203
204 // Set messages to nothing
205 if (!defined('__DOUBLER_MSG')) define('__DOUBLER_MSG', '');
206 if (!defined('__ERROR_MSG'))   define('__ERROR_MSG'  , '');
207
208 // Shall I check for points immediately?
209 if (getConfig('doubler_send_mode') == 'DIRECT') loadInclude('inc/doubler_send.php');
210
211 // Output header
212 loadIncludeOnce('inc/header.php');
213
214 // Banner in text
215 define('__DOUBLER_BANNER', LOAD_TEMPLATE('doubler_banner', true));
216
217 // Load header/footer templates
218 define('__DOUBLER_HEADER', LOAD_TEMPLATE('doubler_header', true));
219 define('__DOUBLER_FOOTER', LOAD_TEMPLATE('doubler_footer', true));
220
221 if (!empty($uid)) {
222         // Transfer userid/nickname to constant
223         define('__REFID', $uid);
224 } else {
225         // Transfer userid/nickname to constant
226         define('__REFID', $GLOBALS['refid']);
227 }
228
229 // Percent values etc.
230 define('__CHARGE_VALUE', translateComma(getConfig('doubler_charge') * 100));
231 define('__REF_VALUE'   , translateComma(getConfig('doubler_ref') * 100));
232 define('__TOTAL_VALUE' , translateComma(getConfig('doubler_points')));
233 define('__MIN_VALUE'   , translateComma(getConfig('doubler_min')));
234 define('__MAX_VALUE'   , translateComma(getConfig('doubler_max')));
235
236 // Text "Enter login"
237 if (EXT_IS_ACTIVE('nickname')) {
238         // Choose login/nickname
239         define('DOUBLER_ENTER_LOGIN', getMessage('GUEST_ENTER_LOGIN_NICKNAME'));
240 } else {
241         // Simple login ID
242         define('DOUBLER_ENTER_LOGIN', getMessage('GUEST_ENTER_LOGIN'));
243 }
244
245 // Which mail-send-mode did the admin setup?
246 switch (getConfig('doubler_send_mode')) {
247         case 'DIRECT':
248                 define('DOUBLER_PAYOUT_TIME', getMessage('DOUBLER_PAYOUT_TIME_DIRECT'));
249                 break;
250
251         case 'RESET':
252                 define('DOUBLER_PAYOUT_TIME', getMessage('DOUBLER_PAYOUT_TIME_RESET'));
253                 break;
254 }
255
256 // Generate table with already payed out doubles
257 define('__DOUBLER_PAYOUT_HISTORY', DOUBLER_GENERATE_TABLE('0', 'Y', 'N', 'DESC'));
258
259 // Generate timemark
260 define('__TIMEOUT_MARK', createFancyTime(getConfig('doubler_timeout')));
261
262 // Usage counter
263 define('__DOUBLER_COUNTER', getConfig('doubler_counter'));
264
265 // Points left to doubler
266 define('__LEFT_VALUE', translateComma(DOUBLER_GET_TOTAL_POINTS_LEFT()));
267
268 // Output neccessary form for this
269 // @TODO Rewrite all constants
270 LOAD_TEMPLATE('doubler_index');
271
272 // Output footer
273 loadIncludeOnce('inc/footer.php');
274
275 // [EOF]
276 ?>