Extension ext-coupon continued, a lot improvements applied:
[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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Load security stuff here
39 require('inc/libs/security_functions.php');
40
41 // Init start time
42 $GLOBALS['startTime'] = microtime(true);
43
44 // Set module
45 $GLOBALS['module'] = 'beg';
46 $GLOBALS['output_mode'] = -1;
47 $errorCode = null;
48
49 // Load the required file(s)
50 require('inc/config-global.php');
51
52 // Set content type
53 setContentType('text/html');
54
55 // Is the 'beg' extension active?
56 redirectOnUninstalledExtension('beg');
57
58 // Check for userid
59 if (isGetRequestParameterSet('userid')) {
60         // Init variables
61         $points = '0';
62
63         // Don't pay is the default...
64         $pay = false;
65
66         // Validate if it is not a number
67         if (isNicknameUsed(getRequestParameter('userid'))) {
68                 // Is the nickname extension there?
69                 if (isExtensionActive('nickname')) {
70                         // Maybe we have found a nickname?
71                         fetchUserData(getRequestParameter('userid'), 'nickname');
72                 } else {
73                         // Nickname entered but nickname is not active
74                         $errorCode = getCode('EXTENSION_PROBLEM');
75                 }
76         } else {
77                 // Direct userid
78                 fetchUserData(getRequestParameter('userid'));
79         }
80
81         // Check if locked in so don't pay points
82         $status = 'failed';
83
84         // Check if account was found
85         if ((isUserDataValid()) && (getUserData('status') == 'CONFIRMED')) {
86                 /*
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                  */
91                 $points = mt_rand((getBegPoints() * 100000), (getBegPointsMax() * 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'))) && (getBegUserid() != 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                            12              33                               2    23              44            3                                          21     1                              1
107                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_beg_ips` WHERE ((UNIX_TIMESTAMP() - `timeout`) >= {?beg_timeout?} OR ((UNIX_TIMESTAMP() - `timeout`) >= {?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()) && (getBegPayMode() == 'NONE')) {
112                         // Admin is testing?
113                         if (!isAdmin()) {
114                                 /*
115                                  * Remember remote address, userid and timestamp for next click
116                                  * but only when there is no admin begging.
117                                  * Admins shall be able to test it!
118                                  */
119                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_beg_ips` (`userid`, `remote_ip`, `sid`, `timeout`) VALUES ('%s','%s','%s', UNIX_TIMESTAMP())",
120                                         array(
121                                                 getUserData('userid'),
122                                                 detectRemoteAddr(),
123                                                 session_id()
124                                         ), __FILE__, __LINE__);
125
126                                 // Was is successfull?
127                                 $pay (!SQL_HASZEROAFFECTED());
128                         } else {
129                                 // Is admin!
130                                 $pay = true;
131                         }
132
133                         // Pay points?
134                         if ($pay === true) {
135                                 // Add points to user or begging rallye account
136                                 if (addPointsBeg(getUserData('userid'), $points)) {
137                                         // Set 'done' message
138                                         $content['message'] = loadTemplate('beg_done', true, $content);
139                                 } else {
140                                         // Error!
141                                         $content['message'] = loadTemplate('beg_failed', true, $content);
142                                 }
143                         } else {
144                                 // Error!
145                                 $content['message'] = loadTemplate('beg_failed', true, $content);
146                         }
147                 } elseif (isMember()) {
148                         // Logged in user found
149                         $content['message'] = loadTemplate('beg_login', true, $content);
150                 } elseif (getBegPayMode() != 'NONE') { // Other pay-mode active!
151                         // Load message template depending on pay-mode
152                         $content['message'] = loadTemplate('beg_pay_mode_'.strtolower(getBegPayMode()), true, $content);
153                         $pay = true;
154                 } else {
155                         // Clicked received while reload lock is active
156                         $content['message'] = loadTemplate('beg_failed', true, $content);
157                 }
158
159                 // Free memory
160                 SQL_FREERESULT($result);
161
162                 // Include header
163                 loadIncludeOnce('inc/header.php');
164
165                 // Load final template
166                 loadTemplate('beg_link', false, $content);
167
168                 // Tracker code enabled? (We don't track users here!
169                 if ((getBegPayMode() != 'NONE') && ($pay === true)) {
170                         // Prepare content for template
171                         // @TODO Opps, what is missing here???
172                         $content = array(
173                         );
174
175                         // Include config-depending template
176                         loadTemplate('beg_pay_code_'.strtolower(getBegPayMode()), false, $content);
177                 } elseif (($pay === false) && (!isset($content['message']))) {
178                         // Cannot pay! :-(
179                         $content['message'] = loadTemplate('beg_failed', true);
180                 }
181
182                 // Include footer
183                 loadIncludeOnce('inc/footer.php');
184         } elseif ((getUserData('status') != 'CONFIRMED') && (getUserData('status') != 'failed')) {
185                 // Maybe locked/unconfirmed account?
186                 $errorCode = generateErrorCodeFromUserStatus();
187         } elseif ((getUserData('userid') == '0') || (getUserData('status') == 'failed')) {
188                 // Inalid or locked account, so let's find out
189                 if (fetchUserData(getRequestParameter('userid'), 'nickname')) {
190                         // Locked account
191                         $errorCode = getCode('ACCOUNT_LOCKED');
192                 } else {
193                         // Invalid nickname! (404)
194                         $errorCode = getCode('USER_404');
195                 }
196         } elseif (getUserData('userid') == getBegUserid()) {
197                 // Webmaster's id cannot beg for points!
198                 $errorCode = getCode('BEG_SAME_AS_OWN');
199         }
200
201         // Reload to index module if an error happens
202         if (!empty($errorCode)) {
203                 redirectToUrl('modules.php?module=index&amp;code=' . $errorCode . '&amp;ext=beg');
204         } // END - if
205 } else {
206         // No userid entered
207         redirectToUrl('modules.php?module=index');
208 }
209
210 // Really all done here... ;-)
211 shutdown();
212
213 // [EOF]
214 ?>