Better RNG choosen (mt_rand()), writing initial secret file fixed, thanks to profi...
[mailer.git] / beg.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 01/09/2005 *
4  * ===============                              Last change: 01/09/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : beg.php                                          *
8  * -------------------------------------------------------------------- *
9  * Short description : Beg link for members                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Bettel-Link fuer Mitglieder                      *
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'] = "beg";
44 $GLOBALS['refid']  = 0;
45 $CSS = -1;
46 $msg = null;
47
48 // Load the required file(s)
49 require ("inc/config.php");
50
51 // Is the "beg" extension active?
52 if (!EXT_IS_ACTIVE("beg")) {
53         // Redirect to index
54         LOAD_URL("modules.php?module=index&amp;msg=".CODE_EXTENSION_PROBLEM."&amp;ext=beg");
55 } // END - if
56
57 // Is the script installed?
58 if (isBooleanConstantAndTrue('mxchange_installed')) {
59         // Check for userid
60         if (!empty($_GET['uid'])) {
61                 // Init user ID
62                 $uid = 0;
63                 $result = false;
64
65                 // Validate if it is not a number
66                 if ("".($_GET['uid'] + 0)."" !== "".$_GET['uid']."") {
67                         if (EXT_IS_ACTIVE("nickname")) {
68                                 // Maybe we have found a nickname?
69                                 $result = SQL_QUERY_ESC("SELECT userid, beg_clicks, ref_payout, status, last_online FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' LIMIT 1",
70                                         array($_GET['uid']), __FILE__, __LINE__);
71                         } else {
72                                 // Nickname entered but nickname is not active
73                                 $msg = CODE_EXTENSION_PROBLEM;
74                                 $uid = -1;
75                         }
76                 } else {
77                         // Direct userid
78                         $result = SQL_QUERY_ESC("SELECT userid, beg_clicks, ref_payout, status, last_online FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
79                                 array(bigintval($_GET['uid'])), __FILE__, __LINE__);
80                 }
81
82                 // Check if locked in so don't pay points
83                 $login = IS_MEMBER(); $status = "failed";
84
85                 // Check if account was found
86                 if ((SQL_NUMROWS($result) == 1) && ($result != false)) {
87                         // Found an ID so we simply set it
88                         list($uid, $clicks, $ref_payout, $status, $last) = SQL_FETCHROW($result);
89
90                         // Account confirmed?
91                         if ($status == "CONFIRMED") {
92                                 // Secure userid
93                                 $uid = bigintval($uid);
94
95                                 // Calculate beg points
96                                 mt_srand((double)microtime() * 10000000000 / time());
97
98                                 // Multiply configured values with 100000 and divide with 100000 so we can also handle small values
99                                 // If we need more number behind the decimal dot then we just need to increase all these three
100                                 // numbers matching to the numbers behind the decimal dot. Simple! ;-)
101                                 $points = mt_rand(($_CONFIG['beg_points'] * 100000), ($_CONFIG['beg_points_max'] * 100000)) / 100000;
102
103                                 // Set nickname / userid for the template(s
104                                 define('__BEG_UID'   , $_GET['uid']);
105                                 define('__BEG_CLICKS', ($clicks + 1));
106                                 define('__BEG_BANNER', LOAD_TEMPLATE("beg_banner", true));
107                                 define('__BEG_POINTS', TRANSLATE_COMMA($points));
108                         } else {
109                                 // Other status
110                                 $uid = 0;
111                         }
112                 }
113
114                 // Free memory
115                 SQL_FREERESULT($result);
116
117                 // User id valid and not webmaster's id?
118                 if (($uid > 0) && ($_CONFIG['beg_uid'] != $uid)) {
119                         // Update counter
120                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET beg_clicks=beg_clicks+1 WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
121                                 array($uid), __FILE__, __LINE__);
122
123                         // Check for last entry for userid w/o IP number
124                         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_beg_ips WHERE (timeout > (UNIX_TIMESTAMP() - ".$_CONFIG['beg_timeout'].") OR (timeout > (UNIX_TIMESTAMP() - ".$_CONFIG['beg_uid_timeout'].") AND userid=%s)) AND (remote_ip='%s' OR sid='%s') LIMIT 1",
125                                 array($uid, GET_REMOTE_ADDR(), session_id()), __FILE__, __LINE__);
126
127                         // Entry not found, points set and not logged in?
128                         if (((SQL_NUMROWS($result) == 0) || (IS_ADMIN())) && ($points > 0) && (!$login) && ($_CONFIG['beg_pay_mode'] == "NONE")) {
129                                 // Don't pay is the default...
130                                 $pay = false;
131
132                                 // Admin is testing?
133                                 if (!IS_ADMIN()) {
134                                         // Remember remote address, userid and timestamp for next click
135                                         // but only when there is no admin begging.
136                                         // Admins shall be able to test it!
137                                         SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_beg_ips (userid, remote_ip,sid, timeout) VALUES ('%s','%s','%s', UNIX_TIMESTAMP())",
138                                                 array($uid, GET_REMOTE_ADDR(), session_id()), __FILE__, __LINE__);
139
140                                         // Was is successfull?
141                                         if (SQL_AFFECTEDROWS() == 1) {
142                                                 // Okay!
143                                                 $pay = true;
144                                         } // END - if
145                                 } else {
146                                         // Is admin!
147                                         $pay = true;
148                                 }
149
150                                 // Pay points?
151                                 if ($pay) {
152                                         // Set mode depending on how many mails the member has to confirm
153                                         $locked = false;
154                                         if (($ref_payout > 0) && ($_CONFIG['allow_direct_pay'] == "N")) $locked = true;
155
156                                         // Is begging rallye active?
157                                         if ($_CONFIG['beg_rallye'] == "Y") {
158                                                 // Add points to rallye account
159                                                 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET beg_points=beg_points+%s WHERE userid=%s LIMIT 1",
160                                                         array($points, $uid), __FILE__, __LINE__);
161                                         } else {
162                                                 // Add points to account
163                                                 unset($DEPTH);
164                                                 ADD_POINTS_REFSYSTEM($uid, $points, false, "0", $locked, strtolower($_CONFIG['beg_mode']));
165                                         }
166
167                                         // Subtract begged points from member account if the admin has selected one
168                                         if ($_CONFIG['beg_uid'] > 0) {
169                                                 // Subtract from this account
170                                                 SUB_POINTS($_CONFIG['beg_uid'], $points);
171                                         } // END - if
172
173                                         // Set message
174                                         define('__BEG_MSG', LOAD_TEMPLATE("beg_done", true));
175                                 } else {
176                                         // Error!
177                                         define('__BEG_MSG', LOAD_TEMPLATE("beg_failed", true));
178                                 }
179                         } elseif ($login) {
180                                 // Logged in user found!
181                                 define('__BEG_MSG', LOAD_TEMPLATE("beg_login", true));
182                         } elseif ($_CONFIG['beg_pay_mode'] != "NONE") {
183                                 // Other pay-mode active!
184                                 define('__BEG_MSG', LOAD_TEMPLATE("beg_pay_mode_".strtolower($_CONFIG['beg_pay_mode']), true));
185                         } else {
186                                 // Clicked received while reload lock is active
187                                 define('__BEG_MSG', LOAD_TEMPLATE("beg_failed", true));
188                         }
189
190                         // Free memory
191                         SQL_FREERESULT($result);
192
193                         // Include header
194                         require_once(PATH."inc/header.php");
195
196                         // Load final template
197                         LOAD_TEMPLATE("beg_link");
198
199                         // Tracker code enabled? (We don't track users here!
200                         if ($_CONFIG['beg_pay_mode'] != "NONE") {
201                                 // Include config-depending template
202                                 LOAD_TEMPLATE("beg_pay_code_".strtolower($_CONFIG['beg_pay_mode']));
203                         } // END - if
204
205                         // Include footer
206                         require_once(PATH."inc/footer.php");
207                 } elseif (($status != "CONFIRMED") && ($status != "failed")) {
208                         // Maybe locked/unconfirmed account?
209                         switch ($status) {
210                                 case "LOCKED"     : $msg = CODE_ID_LOCKED     ; break; // Locked account
211                                 case "UNCONFIRMED": $msg = CODE_ID_UNCONFIRMED; break; // Unconfirmed account
212                         }
213                 } elseif (($uid == "0") || ($status == "failed")) {
214                         // Inalid or locked account, so let's find out
215                         $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' LIMIT 1",
216                                 array($_GET['uid']), __FILE__, __LINE__);
217                         if (SQL_NUMROWS($result) == 1) {
218                                 // Locked account
219                                 $msg = CODE_ACCOUNT_LOCKED;
220                         } else {
221                                 // Invalid nickname! (404)
222                                 $msg = CODE_USER_404;
223                         }
224
225                         // Free memory
226                         SQL_FREERESULT($result);
227                 } elseif ($uid == $_CONFIG['beg_uid']) {
228                         // Webmaster's ID cannot beg for points!
229                         $msg = CODE_BEG_SAME_AS_OWN;
230                 }
231
232                 // Reload to index module
233                 if ((!empty($msg)) && (!empty($msg))) LOAD_URL("modules.php?module=index&amp;msg=".$msg."&amp;ext=beg");
234         } else {
235                 // No userid entered
236                 LOAD_URL("modules.php?module=index");
237         }
238 } else {
239         // You have to configure first!
240         LOAD_URL("install.php");
241 }
242
243 // Really all done here... ;-)
244 ?>