Fixed logfile writing in installation phase, .revision is now ignored
[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  * $Revision:: 856                                                    $ *
14  * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009)              $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author:: stelzi                                                   $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Load security stuff here (Oh, I hope this is not unsecure? Am I paranoia??? ;-) )
40 require("inc/libs/security_functions.php");
41
42 // Init "action" and "what"
43 $GLOBALS['startTime'] = microtime(true);
44 $GLOBALS['what'] = "";
45 $GLOBALS['action'] = "";
46
47 // Set module
48 $GLOBALS['module'] = "doubler";
49 $GLOBALS['refid'] = 0;
50 $GLOBALS['output_mode'] = 0;
51
52 // Load the required file(s)
53 require("inc/config.php");
54
55 // Is the "doubler" extension active?
56 REDIRECT_ON_UNINSTALLED_EXTENSION("doubler");
57
58 // Is the script installed?
59 if (isInstalled()) {
60         // Probe for referal ID
61         if (REQUEST_ISSET_GET(('refid'))) $GLOBALS['refid'] = REQUEST_GET(('refid'));
62
63         // Only check this if refid is provided!
64         if ($GLOBALS['refid'] > 0) {
65                 // Probe for nickname extension and if a nickname was supplied by URL
66                 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($GLOBALS['refid'])."") != $GLOBALS['refid']));
67
68                 // Do we have nickname or userid set?
69                 if ($probe_nickname) {
70                         // Nickname in URL, so load the ID
71                         $result = SQL_QUERY_ESC("SELECT userid, status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE nickname='%s' LIMIT 1",
72                                 array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
73                 } else {
74                         // Direct userid entered
75                         $result = SQL_QUERY_ESC("SELECT userid, status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
76                                 array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
77                 }
78
79                 // Load data
80                 list($rid, $status_ref) = SQL_FETCHROW($result);
81                 $GLOBALS['refid'] = bigintval($rid);
82
83                 // Free memory
84                 SQL_FREERESULT($result);
85         } // END - if
86
87         // Init userid
88         $uid = 0;
89
90         // If no account was found set default refid and status to CONFIRMED
91         if (empty($GLOBALS['refid'])) {
92                 $GLOBALS['refid'] = getConfig('def_refid');
93                 $status = "CONFIRMED";
94         } // END - if
95
96         // Begin with doubler script...
97         if (IS_FORM_SENT()) {
98                 // Secure points (so only integer/double values are allowed
99                 REQUEST_SET_POST('points', bigintval(REQUEST_POST('points')));
100
101                 // Begin with doubling process
102                 if ((REQUEST_ISSET_POST(('userid'))) && (REQUEST_ISSET_POST(('pass'))) && (REQUEST_ISSET_POST(('points')))) {
103                         // Probe for nickname extension and if a nickname was entered
104                         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round(REQUEST_POST('userid'))."") != REQUEST_POST('userid')));
105                         if ($probe_nickname) {
106                                 // Nickname in URL, so load the ID
107                                 $result = SQL_QUERY_ESC("SELECT userid, status, password FROM `{!_MYSQL_PREFIX!}_user_data` WHERE nickname='%s' LIMIT 1",
108                                         array(REQUEST_POST('userid')), __FILE__, __LINE__);
109                         } else {
110                                 // Direct userid entered
111                                 $result = SQL_QUERY_ESC("SELECT userid, status, password FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
112                                         array(bigintval(REQUEST_POST('userid'))), __FILE__, __LINE__);
113                         }
114
115                         // Load data
116                         list($uid, $status, $password) = SQL_FETCHROW($result);
117                         $uid = bigintval($uid);
118
119                         // Free result
120                         SQL_FREERESULT($result);
121
122                         // Remove any dots and unwanted chars from the points
123                         REQUEST_SET_POST('points', bigintval(round(REVERT_COMMA(REQUEST_POST('points')))));
124
125                         // Probe for enough points
126                         $probe_points = ((REQUEST_POST('points') >= getConfig('doubler_min')) && (REQUEST_POST('points') <= getConfig('doubler_max')));
127
128                         // Check all together
129                         if ((!empty($uid)) && ($password == generateHash(REQUEST_POST('pass'), substr($password, 0, -40))) && ($status == "CONFIRMED") && ($probe_points)) {
130                                 // Nickname resolved to a unique userid or direct userid entered by the member
131                                 $GLOBALS['doubler_uid'] = $uid;
132
133                                 // Calulcate points
134                                 $points = GET_TOTAL_DATA($uid, "user_points", "points") - GET_TOTAL_DATA($uid, "user_data", "used_points");
135
136                                 // So let's continue with probing his points amount
137                                 if (($points - getConfig('doubler_left') - REQUEST_POST('points') * getConfig('doubler_charge')) >= 0) 
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(REQUEST_POST('points') * 2)), __FILE__, __LINE__);
142
143                                         // Subtract entered points
144                                         SUB_POINTS("doubler", $uid, REQUEST_POST('points'));
145
146                                         // Add points to "total payed" including charge
147                                         $points = REQUEST_POST('points') - REQUEST_POST('points') * getConfig('doubler_charge');
148                                         UPDATE_CONFIG("doubler_points", $points, "+");
149                                         incrementConfigEntry('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(
156                                                                 bigintval($GLOBALS['refid']),
157                                                                 bigintval(REQUEST_POST('points') * 2 * getConfig('doubler_ref'))
158                                                         ), __FILE__, __LINE__);
159
160                                                 // And that's why we don't want to you more than one referal level of doubler-points. ^^^
161                                         } // END - if
162
163                                         // Update usage counter
164                                         UPDATE_CONFIG("doubler_counter", 1, "+");
165
166                                         // Set constant
167                                         define('__DOUBLER_MSG', LOAD_TEMPLATE("doubler_reflink", true, REQUEST_POST('userid')));
168                                 } else {
169                                         // Not enougth points left
170                                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_NO_POINTS_LEFT'));
171                                 }
172                         } elseif ($status == "CONFIRMED") {
173                                 // Account is unconfirmed!
174                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_WRONG_PASS'));
175                         } elseif ($status == "UNCONFIRMED") {
176                                 // Account is unconfirmed!
177                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_STATUS_UNCONFIRMED'));
178                         } elseif ($status == "LOCKED") {
179                                 // Account is locked by admin / holiday!
180                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_STATUS_LOCKED'));
181                         } elseif (REQUEST_POST('points') < getConfig('doubler_min')) {
182                                 // Not enougth points entered
183                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_POINTS_MIN'));
184                         } elseif (REQUEST_POST('points') > getConfig('doubler_max')) {
185                                 // Too much points entered
186                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_POINTS_MAX'));
187                         } elseif ($probe_nickname) {
188                                 // Cannot resolv nickname -> userid
189                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_NICKNAME'));
190                         } else {
191                                 // Wrong password or account not found
192                                 define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_MEMBER'));
193                         }
194                 } elseif (!REQUEST_ISSET_POST(('userid'))) {
195                         // Login not entered
196                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_LOGIN'));
197                 } elseif (!REQUEST_ISSET_POST(('pass'))) {
198                         // Password not entered
199                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_PASSWORD'));
200                 } elseif (!REQUEST_ISSET_POST(('points'))) {
201                         // points not entered
202                         define('__ERROR_MSG', getMessage('DOUBLER_FORM_404_POINTS'));
203                 }
204         }
205
206         // Set messages to nothing
207         if (!defined('__DOUBLER_MSG')) define('__DOUBLER_MSG', "");
208         if (!defined('__ERROR_MSG'))   define('__ERROR_MSG'  , "");
209
210         // Shall I check for points immediately?
211         if (getConfig('doubler_send_mode') == "DIRECT") LOAD_INC("inc/doubler_send.php");
212
213         // Output header
214         LOAD_INC("inc/header.php");
215
216         // Banner in text
217         define('__DOUBLER_BANNER', LOAD_TEMPLATE("doubler_banner", true));
218
219         // Load header/footer templates
220         define('__DOUBLER_HEADER', LOAD_TEMPLATE("doubler_header", true));
221         define('__DOUBLER_FOOTER', LOAD_TEMPLATE("doubler_footer", true));
222
223         if (!empty($uid)) {
224                 // Transfer userid/nickname to constant
225                 define('__REFID', $uid);
226         } else {
227                 // Transfer userid/nickname to constant
228                 define('__REFID', $GLOBALS['refid']);
229         }
230
231         // Percent values etc.
232         define('__CHARGE_VALUE', TRANSLATE_COMMA(getConfig('doubler_charge') * 100));
233         define('__REF_VALUE'   , TRANSLATE_COMMA(getConfig('doubler_ref') * 100));
234         define('__TOTAL_VALUE' , TRANSLATE_COMMA(getConfig('doubler_points')));
235         define('__MIN_VALUE'   , TRANSLATE_COMMA(getConfig('doubler_min')));
236         define('__MAX_VALUE'   , TRANSLATE_COMMA(getConfig('doubler_max')));
237
238         // Text "Enter login"
239         if (EXT_IS_ACTIVE("nickname")) {
240                 // Choose login/nickname
241                 define('DOUBLER_ENTER_LOGIN', getMessage('GUEST_ENTER_LOGIN_NICKNAME'));
242         } else {
243                 // Simple login ID
244                 define('DOUBLER_ENTER_LOGIN', getMessage('GUEST_ENTER_LOGIN'));
245         }
246
247         // Which mail-send-mode did the admin setup?
248         switch (getConfig('doubler_send_mode'))
249         {
250         case "DIRECT":
251                 define('DOUBLER_PAYOUT_TIME', getMessage('DOUBLER_PAYOUT_TIME_DIRECT'));
252                 break;
253
254         case "RESET":
255                 define('DOUBLER_PAYOUT_TIME', getMessage('DOUBLER_PAYOUT_TIME_RESET'));
256                 break;
257         }
258
259         // Generate table with already payed out doubles
260         define('__DOUBLER_PAYOUT_HISTORY', DOUBLER_GENERATE_TABLE("0", "Y", "N", "DESC"));
261
262         // Generate timemark
263         define('__TIMEOUT_MARK', CREATE_FANCY_TIME(getConfig('doubler_timeout')));
264
265         // Usage counter
266         define('__DOUBLER_COUNTER', getConfig('doubler_counter'));
267
268         // Points left to doubler
269         define('__LEFT_VALUE', TRANSLATE_COMMA(DOUBLER_GET_TOTAL_POINTS_LEFT()));
270
271         // Output neccessary form for this
272         LOAD_TEMPLATE("doubler_index");
273
274         // Output footer
275         LOAD_INC("inc/footer.php");
276 } else {
277         // You have to install first!
278         LOAD_URL("install.php");
279 }
280
281 // Really all done here... ;-)
282 shutdown();
283
284 //
285 ?>