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