Renamed more:
[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 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://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['__start_time'] = microtime(TRUE);
43
44 // Set module and output mode (HTML)
45 $GLOBALS['__module']      = 'beg';
46 $GLOBALS['__output_mode'] = '0';
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 (isGetRequestElementSet('userid')) {
60         // Init variables
61         $points = '0';
62
63         // Don't pay is the default...
64         $isPayed = FALSE;
65
66         // Validate if it is not a number
67         if ((isExtensionActive('nickname')) && (isNicknameUsed(getRequestElement('userid')))) {
68                 // Maybe a nickname is provided?
69                 fetchUserData(getRequestElement('userid'), 'nickname');
70         } elseif (isNicknameUsed(getRequestElement('userid'))) {
71                 // Nickname entered but nickname is not active
72                 $errorCode = getCode('EXTENSION_PROBLEM');
73         } else {
74                 // Direct userid
75                 fetchUserData(getRequestElement('userid'));
76         }
77
78         // Check if locked in so don't pay points
79         $status = 'failed';
80
81         // Check if account was found
82         if ((isUserDataValid()) && (getUserData('status') == 'CONFIRMED')) {
83                 /*
84                  * Multiply configured values with 100000 and divide with 100000 so we can also handle small values
85                  * If we need more number behind the decimal dot then we just need to increase all these three
86                  * numbers matching to the numbers behind the decimal dot. Simple! ;-)
87                  */
88                 $points = mt_rand((getBegPoints() * 100000), (getBegPointsMax() * 100000)) / 100000;
89
90                 // Set nickname / userid for template
91                 $content['userid']        = getRequestElement('userid');
92                 $content['clicks']        = (getUserData('beg_clicks') + 1);
93                 $content['points']        = $points;
94         } // END - if
95
96         // User id valid and not webmaster's id?
97         if ((isValidId(getUserData('userid'))) && (getBegUserid() != getUserData('userid'))) {
98                 // Update counter
99                 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `beg_clicks`=`beg_clicks`+1 WHERE `userid`=%s LIMIT 1",
100                         array(getUserData('userid')), __FILE__, __LINE__);
101
102                 // Check for last entry for userid w/o IP number                            12              33                               2    23              44            3                                          21     1                              1
103                 $result = sqlQueryEscaped("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",
104                         array(
105                                 getUserData('userid'),
106                                 determineRealRemoteAddress(),
107                                 session_id()
108                         ), __FILE__, __LINE__);
109
110                 // Entry not found, points set and not logged in?
111                 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'SQL_HASZERONUMS()=' . intval(SQL_HASZERONUMS($result)) . ',isAdmin()=' . intval(isAdmin()) . ',points=' . $points . ',isMember()=' . intval(isMember()) . ',getBegPayMode()=' . getBegPayMode());
112                 if ((ifSqlHasZeroNums($result)) && ($points > 0) && (getBegPayMode() == 'NONE') && ((!isMember()) || (isAdmin()))) {
113                         // Default is result from isAdmin(), mostly false
114                         $isPayed = isAdmin();
115
116                         // Admin is testing?
117                         if (!isAdmin()) {
118                                 /*
119                                  * Remember remote address, userid and timestamp for next click
120                                  * but only when there is no admin begging.
121                                  * Admins shall be able to test it!
122                                  */
123                                 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_beg_ips` (`userid`, `remote_ip`, `sid`, `timeout`) VALUES ('%s','%s','%s', UNIX_TIMESTAMP())",
124                                         array(
125                                                 getUserData('userid'),
126                                                 determineRealRemoteAddress(),
127                                                 session_id()
128                                         ), __FILE__, __LINE__);
129
130                                 // Was is successfull?
131                                 $isPayed = (!ifSqlHasZeroAffectedRows());
132                         } // END - if
133
134                         // Pay points?
135                         //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'isPayed=' . intval($isPayed));
136                         if ($isPayed === TRUE) {
137                                 // Add points to user or begging rallye account
138                                 if (addPointsBeg(getUserData('userid'), $points)) {
139                                         // Set 'done' message
140                                         $content['message'] = loadTemplate('beg_done', TRUE, $content);
141                                 } else {
142                                         // Error!
143                                         $content['message'] = loadTemplate('beg_failed', TRUE, $content);
144                                 }
145                         } else {
146                                 // Error!
147                                 $content['message'] = loadTemplate('beg_failed', TRUE, $content);
148                         }
149                 } elseif (isMember()) {
150                         // Logged in user found
151                         $content['message'] = loadTemplate('beg_login', TRUE, $content);
152                 } elseif (getBegPayMode() != 'NONE') { // Other pay-mode active!
153                         // Load message template depending on pay-mode
154                         $content['message'] = loadTemplate('beg_pay_mode_' . strtolower(getBegPayMode()), TRUE, $content);
155                         $isPayed = TRUE;
156                 } else {
157                         // Clicked received while reload lock is active
158                         $content['message'] = loadTemplate('beg_failed', TRUE, $content);
159                 }
160
161                 // Free memory
162                 sqlFreeResult($result);
163
164                 // Include header
165                 loadIncludeOnce('inc/header.php');
166
167                 // Load final template
168                 loadTemplate('beg_link', FALSE, $content);
169
170                 // Tracker code enabled? (We don't track users here!
171                 if ((getBegPayMode() != 'NONE') && ($isPayed === TRUE)) {
172                         // Prepare content for template
173                         // @TODO Opps, what is missing here???
174                         $content = array(
175                         );
176
177                         // Include config-depending template
178                         loadTemplate('beg_pay_code_' . strtolower(getBegPayMode()), FALSE, $content);
179                 } elseif (($isPayed === FALSE) && (!isset($content['message']))) {
180                         // Cannot pay! :-(
181                         $content['message'] = loadTemplate('beg_failed', TRUE);
182                 }
183
184                 // Include footer
185                 loadIncludeOnce('inc/footer.php');
186         } elseif ((getUserData('status') != 'CONFIRMED') && (getUserData('status') != 'failed')) {
187                 // Maybe locked/unconfirmed account?
188                 $errorCode = generateErrorCodeFromUserStatus();
189         } elseif ((!isValidId(getUserData('userid'))) || (getUserData('status') == 'failed')) {
190                 // Inalid or locked account, so let's find out
191                 if (fetchUserData(getRequestElement('userid'), 'nickname')) {
192                         // Locked account
193                         $errorCode = getCode('ACCOUNT_LOCKED');
194                 } else {
195                         // Invalid nickname! (404)
196                         $errorCode = getCode('USER_404');
197                 }
198         } elseif (getUserData('userid') == getBegUserid()) {
199                 // Webmaster's id cannot beg for points!
200                 $errorCode = getCode('BEG_SAME_AS_OWN');
201         }
202
203         // Reload to index module if an error happens
204         if (!is_null($errorCode)) {
205                 redirectToUrl('modules.php?module=index&amp;code=' . $errorCode . '&amp;ext=beg');
206         } // END - if
207 } else {
208         // No userid entered
209         redirectToUrl('modules.php?module=index');
210 }
211
212 // Really all done here... ;-)
213 doShutdown();
214
215 // [EOF]
216 ?>