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