Fixes for doubler (not fully fixed) and rewrites for beg/doubler:
[mailer.git] / beg.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Load security stuff here
41 require('inc/libs/security_functions.php');
42
43 // Init start time
44 $GLOBALS['startTime'] = microtime(true);
45
46 // Set module
47 $GLOBALS['module'] = 'beg';
48 $GLOBALS['output_mode'] = -1;
49 $errorCode = null;
50
51 // Load the required file(s)
52 require('inc/config-global.php');
53
54 // Set content type
55 setContentType('text/html');
56
57 // Is the 'beg' extension active?
58 redirectOnUninstalledExtension('beg');
59
60 // Check for userid
61 if (isGetRequestParameterSet('userid')) {
62         // Init variables
63         $points = '0';
64
65         // Don't pay is the default...
66         $pay = false;
67
68         // Validate if it is not a number
69         if (isNicknameUsed(getRequestParameter('userid'))) {
70                 // Is the nickname extension there?
71                 if (isExtensionActive('nickname')) {
72                         // Maybe we have found a nickname?
73                         fetchUserData(getRequestParameter('userid'), 'nickname');
74                 } else {
75                         // Nickname entered but nickname is not active
76                         $errorCode = getCode('EXTENSION_PROBLEM');
77                 }
78         } else {
79                 // Direct userid
80                 fetchUserData(getRequestParameter('userid'));
81         }
82
83         // Check if locked in so don't pay points
84         $status = 'failed';
85
86         // Check if account was found
87         if ((isUserDataValid()) && (getUserData('status') == 'CONFIRMED')) {
88                 // Multiply configured values with 100000 and divide with 100000 so we can also handle small values
89                 // If we need more number behind the decimal dot then we just need to increase all these three
90                 // numbers matching to the numbers behind the decimal dot. Simple! ;-)
91                 $points = mt_rand((getConfig('beg_points') * 100000), (getConfig('beg_points_max') * 100000)) / 100000;
92
93                 // Set nickname / userid for template
94                 $content['userid'] = getRequestParameter('userid');
95                 $content['clicks'] = (getUserData('beg_clicks') + 1);
96                 $content['banner'] = loadTemplate('beg_banner', true);
97                 $content['points'] = $points;
98         } // END - if
99
100         // User id valid and not webmaster's id?
101         if ((isValidUserId(getUserData('userid'))) && (getConfig('beg_userid') != getUserData('userid'))) {
102                 // Update counter
103                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `beg_clicks`=`beg_clicks`+1 WHERE `userid`=%s LIMIT 1",
104                         array(getUserData('userid')), __FILE__, __LINE__);
105
106                 // Check for last entry for userid w/o IP number
107                 $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",
108                         array(getUserData('userid'), detectRemoteAddr(), session_id()), __FILE__, __LINE__);
109
110                 // Entry not found, points set and not logged in?
111                 if (((SQL_HASZERONUMS($result)) || (isAdmin())) && ($points > 0) && (!isMember()) && (getConfig('beg_pay_mode') == 'NONE')) {
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, $content);
133                                 } else {
134                                         // Error!
135                                         $content['message'] = loadTemplate('beg_failed', true, $content);
136                                 }
137                         } else {
138                                 // Error!
139                                 $content['message'] = loadTemplate('beg_failed', true, $content);
140                         }
141                 } elseif (isMember()) {
142                         // Logged in user found!
143                         $content['message'] = loadTemplate('beg_login', true, $content);
144                 } elseif (getConfig('beg_pay_mode') != 'NONE') { // Other pay-mode active!
145                         // Load message template depending on pay-mode
146                         $content['message'] = loadTemplate('beg_pay_mode_'.strtolower(getConfig('beg_pay_mode')), true, $content);
147                         $pay = true;
148                 } else {
149                         // Clicked received while reload lock is active
150                         $content['message'] = loadTemplate('beg_failed', true, $content);
151                 }
152
153                 // Free memory
154                 SQL_FREERESULT($result);
155
156                 // Include header
157                 loadIncludeOnce('inc/header.php');
158
159                 // Load final template
160                 loadTemplate('beg_link', false, $content);
161
162                 // Tracker code enabled? (We don't track users here!
163                 if ((getConfig('beg_pay_mode') != 'NONE') && ($pay === true)) {
164                         // Prepare content for template
165                         // @TODO Opps, what is missing here???
166                         $content = array(
167                         );
168
169                         // Include config-depending template
170                         loadTemplate('beg_pay_code_'.strtolower(getConfig('beg_pay_mode')), false, $content);
171                 } elseif (($pay === false) && (!isset($content['message']))) {
172                         // Cannot pay! :-(
173                         $content['message'] = loadTemplate('beg_failed', true);
174                 }
175
176                 // Include footer
177                 loadIncludeOnce('inc/footer.php');
178         } elseif ((getUserData('status') != 'CONFIRMED') && (getUserData('status') != 'failed')) {
179                 // Maybe locked/unconfirmed account?
180                 $errorCode = generateErrorCodeFromUserStatus();
181         } elseif ((getUserData('userid') == '0') || (getUserData('status') == 'failed')) {
182                 // Inalid or locked account, so let's find out
183                 if (fetchUserData(getRequestParameter('userid'), 'nickname')) {
184                         // Locked account
185                         $errorCode = getCode('ACCOUNT_LOCKED');
186                 } else {
187                         // Invalid nickname! (404)
188                         $errorCode = getCode('USER_404');
189                 }
190         } elseif (getUserData('userid') == getConfig('beg_userid')) {
191                 // Webmaster's id cannot beg for points!
192                 $errorCode = getCode('BEG_SAME_AS_OWN');
193         }
194
195         // Reload to index module
196         if ((!empty($errorCode)) && (!empty($errorCode))) redirectToUrl('modules.php?module=index&amp;code=' . $errorCode . '&amp;ext=beg');
197 } else {
198         // No userid entered
199         redirectToUrl('modules.php?module=index');
200 }
201
202 // Really all done here... ;-)
203 shutdown();
204
205 // [EOF]
206 ?>