(Possible) fixes for illegal array indexes
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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
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'] = 'beg';
49 $GLOBALS['refid']  = 0;
50 $GLOBALS['output_mode'] = -1;
51 $msg = null;
52
53 // Load the required file(s)
54 require('inc/config-global.php');
55
56 // Is the 'beg' extension active?
57 REDIRCT_ON_UNINSTALLED_EXTENSION('beg');
58
59 // Is the script installed?
60 if (!isInstalled()) {
61         // You have to install first!
62         redirectToUrl('install.php');
63 } // END - if
64
65 // Check for userid
66 if (REQUEST_ISSET_GET('uid')) {
67         // Init variables
68         $uid = 0;
69         $result = false;
70         $points = 0;
71
72         // Don't pay is the default...
73         $pay = false;
74
75         // Validate if it is not a number
76         if (''.(REQUEST_GET('uid') + 0).'' !== ''.REQUEST_GET('uid').'') {
77                 if (EXT_IS_ACTIVE('nickname')) {
78                         // Maybe we have found a nickname?
79                         $result = SQL_QUERY_ESC("SELECT userid, beg_clicks, ref_payout, status, last_online FROM `{!_MYSQL_PREFIX!}_user_data` WHERE nickname='%s' LIMIT 1",
80                         array(REQUEST_GET('uid')), __FILE__, __LINE__);
81                 } else {
82                         // Nickname entered but nickname is not active
83                         $msg = getCode('EXTENSION_PROBLEM');
84                         $uid = -1;
85                 }
86         } else {
87                 // Direct userid
88                 $result = SQL_QUERY_ESC("SELECT userid, beg_clicks, ref_payout, status, last_online FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
89                 array(bigintval(REQUEST_GET('uid'))), __FILE__, __LINE__);
90         }
91
92         // Check if locked in so don't pay points
93         $status = 'failed';
94
95         // Check if account was found
96         if (SQL_NUMROWS($result) == 1) {
97                 // Found an ID so we simply set it
98                 list($uid, $clicks, $ref_payout, $status, $last) = SQL_FETCHROW($result);
99
100                 // Account confirmed?
101                 if ($status == 'CONFIRMED') {
102                         // Secure userid
103                         $uid = bigintval($uid);
104
105                         // Multiply configured values with 100000 and divide with 100000 so we can also handle small values
106                         // If we need more number behind the decimal dot then we just need to increase all these three
107                         // numbers matching to the numbers behind the decimal dot. Simple! ;-)
108                         $points = mt_rand((getConfig('beg_points') * 100000), (getConfig('beg_points_max') * 100000)) / 100000;
109
110                         // Set nickname / userid for the template(s
111                         define('__BEG_UID'   , REQUEST_GET('uid'));
112                         define('__BEG_CLICKS', ($clicks + 1));
113                         define('__BEG_BANNER', LOAD_TEMPLATE('beg_banner', true));
114                         define('__BEG_POINTS', translateComma($points));
115                 } else {
116                         // Other status
117                         $uid = 0;
118                 }
119         } // END - if
120
121         // Free memory
122         SQL_FREERESULT($result);
123
124         // User id valid and not webmaster's id?
125         if (($uid > 0) && (getConfig('beg_uid') != $uid)) {
126                 // Update counter
127                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET beg_clicks=beg_clicks+1 WHERE userid=%s AND `status`='CONFIRMED' LIMIT 1",
128                 array($uid), __FILE__, __LINE__);
129
130                 // Check for last entry for userid w/o IP number
131                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_beg_ips` WHERE (timeout > (UNIX_TIMESTAMP() - ".getConfig('beg_timeout').") OR (timeout > (UNIX_TIMESTAMP() - ".getConfig('beg_uid_timeout').") AND `userid`=%s)) AND (remote_ip='%s' OR sid='%s') LIMIT 1",
132                 array($uid, detectRemoteAddr(), session_id()), __FILE__, __LINE__);
133
134                 // Entry not found, points set and not logged in?
135                 if (((SQL_NUMROWS($result) == 0) || (IS_ADMIN())) && ($points > 0) && (!IS_MEMBER()) && (getConfig('beg_pay_mode') == 'NONE')) {
136
137                         // Admin is testing?
138                         if (!IS_ADMIN()) {
139                                 // Remember remote address, userid and timestamp for next click
140                                 // but only when there is no admin begging.
141                                 // Admins shall be able to test it!
142                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_beg_ips` (userid, remote_ip,sid, timeout) VALUES ('%s','%s','%s', UNIX_TIMESTAMP())",
143                                 array($uid, detectRemoteAddr(), session_id()), __FILE__, __LINE__);
144
145                                 // Was is successfull?
146                                 $pay (SQL_AFFECTEDROWS() == 1);
147                         } else {
148                                 // Is admin!
149                                 $pay = true;
150                         }
151
152                         // Pay points?
153                         if ($pay === true) {
154                                 // Add points to user or begging rallye account
155                                 if (BEG_ADD_POINTS($uid, $points)) {
156                                         // Set 'done' message
157                                         $content['msg'] = LOAD_TEMPLATE('beg_done', true);
158                                 } else {
159                                         // Error!
160                                         $content['msg'] = LOAD_TEMPLATE('beg_failed', true);
161                                 }
162                         } else {
163                                 // Error!
164                                 $content['msg'] = LOAD_TEMPLATE('beg_failed', true);
165                         }
166                 } elseif (IS_MEMBER()) {
167                         // Logged in user found!
168                         $content['msg'] = LOAD_TEMPLATE('beg_login', true);
169                 } elseif (getConfig('beg_pay_mode') != 'NONE') { // Other pay-mode active!
170                         // Prepare content for template
171                         $content = array(
172                                 'clicks' => constant('__BEG_CLICKS'),
173                                 'points' => constant('__BEG_POINTS'),
174                                 'uid'    => constant('__BEG_UID')
175                         );
176
177                         // Load message template depending on pay-mode
178                         $content['msg'] = LOAD_TEMPLATE('beg_pay_mode_'.strtolower(getConfig('beg_pay_mode')), true, $content);
179                         $pay = true;
180                 } else {
181                         // Clicked received while reload lock is active
182                         $content['msg'] = LOAD_TEMPLATE('beg_failed', true);
183                 }
184
185                 // Free memory
186                 SQL_FREERESULT($result);
187
188                 // Include header
189                 loadIncludeOnce('inc/header.php');
190
191                 // Load final template
192                 LOAD_TEMPLATE('beg_link', false, $content);
193
194                 // Tracker code enabled? (We don't track users here!
195                 if ((getConfig('beg_pay_mode') != 'NONE') && ($pay === true)) {
196                         // Prepare content for template
197                         // @TODO Opps, what is missing here???
198                         $content = array(
199                         );
200
201                         // Include config-depending template
202                         LOAD_TEMPLATE('beg_pay_code_'.strtolower(getConfig('beg_pay_mode')), false, $content);
203                 } elseif ((!$pay) && (!isset($content['msg']))) {
204                         // Cannot pay! :-(
205                         $content['msg'] = LOAD_TEMPLATE('beg_failed', true);
206                 }
207
208                 // Include footer
209                 loadIncludeOnce('inc/footer.php');
210         } elseif (($status != 'CONFIRMED') && ($status != 'failed')) {
211                 // Maybe locked/unconfirmed account?
212                 $msg = generateErrorCodeFromUserStatus($status);
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(REQUEST_GET('uid')), __FILE__, __LINE__);
217                 if (SQL_NUMROWS($result) == 1) {
218                         // Locked account
219                         $msg = getCode('ACCOUNT_LOCKED');
220                 } else {
221                         // Invalid nickname! (404)
222                         $msg = getCode('USER_404');
223                 }
224
225                 // Free memory
226                 SQL_FREERESULT($result);
227         } elseif ($uid == getConfig('beg_uid')) {
228                 // Webmaster's ID cannot beg for points!
229                 $msg = getCode('BEG_SAME_AS_OWN');
230         }
231
232         // Reload to index module
233         if ((!empty($msg)) && (!empty($msg))) redirectToUrl('modules.php?module=index&amp;msg='.$msg.'&amp;ext=beg');
234 } else {
235         // No userid entered
236         redirectToUrl('modules.php?module=index');
237 }
238
239 // Really all done here... ;-)
240 shutdown();
241
242 //
243 ?>