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