Fixed endless loop, if no sql_patches is installed and modules.php?module=index was...
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Load security stuff here (Oh, I hope this is not unsecure? Am I paranoia??? ;-) )
35 require_once("inc/libs/security_functions.php");
36
37 // Init "action" and "what"
38 global $what, $action, $startTime;
39 $GLOBALS['startTime'] = microtime(true);
40 $GLOBALS['what'] = "";
41 $GLOBALS['action'] = "";
42
43 // Set module
44 $GLOBALS['module'] = "doubler";
45 $GLOBALS['refid'] = 0;
46 $CSS = 0;
47
48 // Load the required file(s)
49 require("inc/config.php");
50
51 // Is the "doubler" extension active?
52 if (!EXT_IS_ACTIVE("doubler")) {
53         // Redirect to index
54         LOAD_URL("modules.php?module=index&amp;msg=".CODE_EXTENSION_PROBLEM."&amp;ext=doubler");
55 } // END - if
56
57 // Is the script installed?
58 if (isBooleanConstantAndTrue('mxchange_installed')) {
59         // Probe for referal ID
60         if (!empty($_GET['refid'])) $GLOBALS['refid'] = SQL_ESCAPE($_GET['refid']);
61
62         // Only check this if refid is provided!
63         if ($GLOBALS['refid'] > 0) {
64                 // Probe for nickname extension and if a nickname was supplied by URL
65                 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($GLOBALS['refid'])."") != $GLOBALS['refid']));
66
67                 // Do we have nickname or userid set?
68                 if ($probe_nickname) {
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 (isset($_POST['ok'])) {
97                 // Secure points (so only integer/double values are allowed
98                 $_POST['points'] = bigintval($_POST['points']);
99
100                 // Begin with doubling process
101                 if ((!empty($_POST['userid'])) && (!empty($_POST['pass'])) && (!empty($_POST['points']))) {
102                         // Probe for nickname extension and if a nickname was entered
103                         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['userid'])."") != $_POST['userid']));
104                         if ($probe_nickname) {
105                                 // Nickname in URL, so load the ID
106                                 $result = SQL_QUERY_ESC("SELECT userid, status, password FROM `"._MYSQL_PREFIX."_user_data` WHERE nickname='%s' LIMIT 1",
107                                         array($_POST['userid']), __FILE__, __LINE__);
108                         } else {
109                                 // Direct userid entered
110                                 $result = SQL_QUERY_ESC("SELECT userid, status, password FROM `"._MYSQL_PREFIX."_user_data` WHERE userid=%s LIMIT 1",
111                                         array(bigintval($_POST['userid'])), __FILE__, __LINE__);
112                         }
113
114                         // Load data
115                         list($uid, $status, $password) = SQL_FETCHROW($result);
116                         $uid = bigintval($uid);
117
118                         // Free result
119                         SQL_FREERESULT($result);
120
121                         // Remove any dots and unwanted chars from the points
122                         $_POST['points'] = bigintval(round(REVERT_COMMA($_POST['points'])));
123
124                         // Probe for enough points
125                         $probe_points = (($_POST['points'] >= getConfig('doubler_min')) && ($_POST['points'] <= getConfig('doubler_max')));
126
127                         // Check all together
128                         if ((!empty($uid)) && ($password == generateHash($_POST['pass'], substr($password, 0, -40))) && ($status == "CONFIRMED") && ($probe_points)) {
129                                 // Nickname resolved to a unique userid or direct userid entered by the member
130                                 $DOUBLER_UID = $uid;
131
132                                 // Calulcate points
133                                 $points = GET_TOTAL_DATA($uid, "user_points", "points") - GET_TOTAL_DATA($uid, "user_data", "used_points");
134
135                                 // So let's continue with probing his points amount
136                                 if (($points - getConfig('doubler_left') - $_POST['points'] * getConfig('doubler_charge')) >= 0)
137                                 {
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','".GET_REMOTE_ADDR()."', UNIX_TIMESTAMP(), 'N','N')",
141                                                 array($uid, bigintval($GLOBALS['refid']), bigintval($_POST['points'] * 2)), __FILE__, __LINE__);
142
143                                         // Subtract entered points
144                                         SUB_POINTS("doubler", $uid, $_POST['points']);
145
146                                         // Add points to "total payed" including charge
147                                         $points = $_POST['points'] - $_POST['points'] * getConfig('doubler_charge');
148                                         UPDATE_CONFIG("doubler_points", $points, "+");
149                                         getConfig('doubler_points') += $points;
150
151                                         // Add second line for the referal but only when uid != refid
152                                         if (($GLOBALS['refid'] > 0) && ($GLOBALS['refid'] != $uid)) {
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','".GET_REMOTE_ADDR()."',UNIX_TIMESTAMP(),'N','Y')",
155                                                         array(bigintval($GLOBALS['refid']), bigintval($_POST['points'] * 2 * getConfig('doubler_ref'))), __FILE__, __LINE__);
156
157                                                 // And that's why we dont't want to you more than one referal level of doubler-points. ^^^
158                                         } // END - if
159
160                                         // Update usage counter
161                                         UPDATE_CONFIG("doubler_counter", 1, "+");
162                                         incrementCondigEntry('doubler_counter');
163
164                                         // Set constant
165                                         define('__DOUBLER_MSG', LOAD_TEMPLATE("doubler_reflink", true, $_POST['userid']));
166                                 } else {
167                                         // Not enougth points left
168                                         define('__ERROR_MSG', DOUBLER_FORM_NO_POINTS_LEFT);
169                                 }
170                         } elseif ($status == "CONFIRMED") {
171                                 // Account is unconfirmed!
172                                 define('__ERROR_MSG', DOUBLER_FORM_WRONG_PASS);
173                         } elseif ($status == "UNCONFIRMED") {
174                                 // Account is unconfirmed!
175                                 define('__ERROR_MSG', DOUBLER_FORM_STATUS_UNCONFIRMED);
176                         } elseif ($status == "LOCKED") {
177                                 // Account is locked by admin / holiday!
178                                 define('__ERROR_MSG', DOUBLER_FORM_STATUS_LOCKED);
179                         } elseif ($_POST['points'] < getConfig('doubler_min')) {
180                                 // Not enougth points entered
181                                 define('__ERROR_MSG', DOUBLER_FORM_POINTS_MIN);
182                         } elseif ($_POST['points'] > getConfig('doubler_max')) {
183                                 // Too much points entered
184                                 define('__ERROR_MSG', DOUBLER_FORM_POINTS_MAX);
185                         } elseif ($probe_nickname) {
186                                 // Cannot resolv nickname -> userid
187                                 define('__ERROR_MSG', DOUBLER_FORM_404_NICKNAME);
188                         } else {
189                                 // Wrong password or account not found
190                                 define('__ERROR_MSG', DOUBLER_FORM_404_MEMBER);
191                         }
192                 } elseif (empty($_POST['userid'])) {
193                         // Login not entered
194                         define('__ERROR_MSG', DOUBLER_FORM_404_LOGIN);
195                 } elseif (empty($_POST['pass'])) {
196                         // Password not entered
197                         define('__ERROR_MSG', DOUBLER_FORM_404_PASSWORD);
198                 } elseif (empty($_POST['points'])) {
199                         // points not entered
200                         define('__ERROR_MSG', 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") require(PATH."inc/doubler_send.php");
210
211         // Output header
212         include(PATH."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', TRANSLATE_COMMA(getConfig('doubler_charge') * 100));
231         define('__REF_VALUE'   , TRANSLATE_COMMA(getConfig('doubler_ref') * 100));
232         define('__TOTAL_VALUE' , TRANSLATE_COMMA(getConfig('doubler_points')));
233         define('__MIN_VALUE'   , TRANSLATE_COMMA(getConfig('doubler_min')));
234         define('__MAX_VALUE'   , TRANSLATE_COMMA(getConfig('doubler_max')));
235
236         // Text "Enter login"
237         if (EXT_IS_ACTIVE("nickname")) {
238                 // Choose login/nickname
239                 define('DOUBLER_ENTER_LOGIN', GUEST_ENTER_LOGIN_NICKNAME);
240         } else {
241                 // Simple login ID
242                 define('DOUBLER_ENTER_LOGIN', GUEST_ENTER_LOGIN);
243         }
244
245         // Which mail-send-mode did the admin setup?
246         switch (getConfig('doubler_send_mode'))
247         {
248         case "DIRECT":
249                 define('DOUBLER_PAYOUT_TIME', DOUBLER_PAYOUT_TIME_DIRECT);
250                 break;
251
252         case "RESET":
253                 define('DOUBLER_PAYOUT_TIME', DOUBLER_PAYOUT_TIME_RESET);
254                 break;
255         }
256
257         // Generate table with already payed out doubles
258         define('__DOUBLER_PAYOUT_HISTORY', DOUBLER_GENERATE_TABLE("0", "Y", "N", "DESC"));
259
260         // Generate timemark
261         define('__TIMEOUT_MARK', CREATE_FANCY_TIME(getConfig('doubler_timeout')));
262
263         // Usage counter
264         define('__DOUBLER_COUNTER', getConfig('doubler_counter'));
265
266         // Points left to doubler
267         define('__LEFT_VALUE', TRANSLATE_COMMA(DOUBLER_GET_TOTAL_POINTS_LEFT()));
268
269         // Output neccessary form for this
270         LOAD_TEMPLATE("doubler_index");
271
272         // Output footer
273         include(PATH."inc/footer.php");
274 } else {
275         // You have to configure first!
276         LOAD_URL("install.php");
277 }
278
279 // Really all done here... ;-)
280 ?>