Possible fix for non-working nickname referal link
[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 - 2009 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 start time
43 $GLOBALS['startTime'] = microtime(true);
44
45 // Set module
46 $GLOBALS['module'] = 'beg';
47 $GLOBALS['output_mode'] = -1;
48 $errorCode = null;
49
50 // Load the required file(s)
51 require('inc/config-global.php');
52
53 // Set content type
54 setContentType('text/html');
55
56 // Is the 'beg' extension active?
57 redirectOnUninstalledExtension('beg');
58
59 // Check for userid
60 if (isGetRequestElementSet('userid')) {
61         // Init variables
62         $points = '0';
63
64         // Don't pay is the default...
65         $pay = false;
66
67         // Validate if it is not a number
68         if (isNicknameUsed(getRequestElement('userid'))) {
69                 // Is the nickname extension there?
70                 if (isExtensionActive('nickname')) {
71                         // Maybe we have found a nickname?
72                         fetchUserData(getRequestElement('userid'), 'nickname');
73                 } else {
74                         // Nickname entered but nickname is not active
75                         $errorCode = getCode('EXTENSION_PROBLEM');
76                 }
77         } else {
78                 // Direct userid
79                 fetchUserData(getRequestElement('userid'));
80         }
81
82         // Check if locked in so don't pay points
83         $status = 'failed';
84
85         // Check if account was found
86         if ((isUserDataValid()) && (getUserData('status') == 'CONFIRMED')) {
87                 // Multiply configured values with 100000 and divide with 100000 so we can also handle small values
88                 // If we need more number behind the decimal dot then we just need to increase all these three
89                 // numbers matching to the numbers behind the decimal dot. Simple! ;-)
90                 $points = mt_rand((getConfig('beg_points') * 100000), (getConfig('beg_points_max') * 100000)) / 100000;
91
92                 // Set nickname / userid for the template(s
93                 $content['userid']      = getRequestElement('userid');
94                 $content['clicks']      = (getUserData('beg_clicks') + 1);
95                 $content['banner']      = loadTemplate('beg_banner', true);
96                 $content['points']      = translateComma($points);
97         } // END - if
98
99         // User id valid and not webmaster's id?
100         if ((getUserData('userid') > 0) && (getConfig('beg_userid') != getUserData('userid'))) {
101                 // Update counter
102                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `beg_clicks`=`beg_clicks`+1 WHERE `userid`=%s LIMIT 1",
103                         array(getUserData('userid')), __FILE__, __LINE__);
104
105                 // Check for last entry for userid w/o IP number
106                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_beg_ips` WHERE (`timeout` > (UNIX_TIMESTAMP() - {?beg_timeout?}) OR (timeout > (UNIX_TIMESTAMP() - {?beg_userid_timeout?}) AND `userid`=%s)) AND (`remote_ip`='%s' OR `sid`='%s') LIMIT 1",
107                         array(getUserData('userid'), detectRemoteAddr(), session_id()), __FILE__, __LINE__);
108
109                 // Entry not found, points set and not logged in?
110                 if (((SQL_NUMROWS($result) == '0') || (isAdmin())) && ($points > 0) && (!isMember()) && (getConfig('beg_pay_mode') == 'NONE')) {
111
112                         // Admin is testing?
113                         if (!isAdmin()) {
114                                 // Remember remote address, userid and timestamp for next click
115                                 // but only when there is no admin begging.
116                                 // Admins shall be able to test it!
117                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_beg_ips` (`userid`, `remote_ip`,`sid`, `timeout`) VALUES ('%s','%s','%s', UNIX_TIMESTAMP())",
118                                         array(getUserData('userid'), detectRemoteAddr(), session_id()), __FILE__, __LINE__);
119
120                                 // Was is successfull?
121                                 $pay (SQL_AFFECTEDROWS() == 1);
122                         } else {
123                                 // Is admin!
124                                 $pay = true;
125                         }
126
127                         // Pay points?
128                         if ($pay === true) {
129                                 // Add points to user or begging rallye account
130                                 if (addPointsBeg(getUserData('userid'), $points)) {
131                                         // Set 'done' message
132                                         $content['message'] = loadTemplate('beg_done', true);
133                                 } else {
134                                         // Error!
135                                         $content['message'] = loadTemplate('beg_failed', true);
136                                 }
137                         } else {
138                                 // Error!
139                                 $content['message'] = loadTemplate('beg_failed', true);
140                         }
141                 } elseif (isMember()) {
142                         // Logged in user found!
143                         $content['message'] = loadTemplate('beg_login', true);
144                 } elseif (getConfig('beg_pay_mode') != 'NONE') { // Other pay-mode active!
145                         // Prepare content for template
146                         $content = array(
147                                 'clicks' => $content['clicks'],
148                                 'points' => $content['points'],
149                                 'userid' => $content['userid']
150                         );
151
152                         // Load message template depending on pay-mode
153                         $content['message'] = loadTemplate('beg_pay_mode_'.strtolower(getConfig('beg_pay_mode')), true, $content);
154                         $pay = true;
155                 } else {
156                         // Clicked received while reload lock is active
157                         $content['message'] = loadTemplate('beg_failed', true, $content);
158                 }
159
160                 // Free memory
161                 SQL_FREERESULT($result);
162
163                 // Include header
164                 loadIncludeOnce('inc/header.php');
165
166                 // Load final template
167                 loadTemplate('beg_link', false, $content);
168
169                 // Tracker code enabled? (We don't track users here!
170                 if ((getConfig('beg_pay_mode') != 'NONE') && ($pay === true)) {
171                         // Prepare content for template
172                         // @TODO Opps, what is missing here???
173                         $content = array(
174                         );
175
176                         // Include config-depending template
177                         loadTemplate('beg_pay_code_'.strtolower(getConfig('beg_pay_mode')), false, $content);
178                 } elseif (($pay === false) && (!isset($content['message']))) {
179                         // Cannot pay! :-(
180                         $content['message'] = loadTemplate('beg_failed', true);
181                 }
182
183                 // Include footer
184                 loadIncludeOnce('inc/footer.php');
185         } elseif ((getUserData('status') != 'CONFIRMED') && (getUserData('status') != 'failed')) {
186                 // Maybe locked/unconfirmed account?
187                 $errorCode = generateErrorCodeFromUserStatus();
188         } elseif ((getUserData('userid') == '0') || (getUserData('status') == 'failed')) {
189                 // Inalid or locked account, so let's find out
190                 if (fetchUserData(getRequestElement('userid'), 'nickname')) {
191                         // Locked account
192                         $errorCode = getCode('ACCOUNT_LOCKED');
193                 } else {
194                         // Invalid nickname! (404)
195                         $errorCode = getCode('USER_404');
196                 }
197         } elseif (getUserData('userid') == getConfig('beg_userid')) {
198                 // Webmaster's id cannot beg for points!
199                 $errorCode = getCode('BEG_SAME_AS_OWN');
200         }
201
202         // Reload to index module
203         if ((!empty($errorCode)) && (!empty($errorCode))) redirectToUrl('modules.php?module=index&amp;code=' . $errorCode . '&amp;ext=beg');
204 } else {
205         // No userid entered
206         redirectToUrl('modules.php?module=index');
207 }
208
209 // Really all done here... ;-)
210 shutdown();
211
212 // [EOF]
213 ?>