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