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