More rewrites to make use of (cached) wrapper functions
[mailer.git] / mailid_top.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/14/2003 *
4  * ===================                          Last change: 11/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : mailid_top.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Confirmation file for emails                     *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Bestaetigung von Mails                           *
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 // Tell everyone we are in this module
47 $GLOBALS['module'] = 'mailid';
48 $GLOBALS['output_mode'] = -1;
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 extension mailid active?
57 redirectOnUninstalledExtension('mailid');
58
59 // Is the extension other active?
60 redirectOnUninstalledExtension('other');
61
62 // Init variables
63 $url_userid = '0';
64 $url_bid = '0';
65 $url_mid = '0';
66 $code = '0';
67 $mode = '';
68
69 // Secure all data
70 if (isGetRequestParameterSet('userid'))  $url_userid = bigintval(getRequestParameter('userid'));
71 if (isGetRequestParameterSet('mailid'))  $url_mid    = bigintval(getRequestParameter('mailid'));
72 if (isGetRequestParameterSet('bonusid')) $url_bid    = bigintval(getRequestParameter('bonusid'));
73 if (isGetRequestParameterSet('code'))    $code       = bigintval(getRequestParameter('code'));
74 if (isGetRequestParameterSet('mode'))    $mode       = getRequestParameter('mode');
75
76 // 01           1        12            2    2            21    1                   22     10
77 if ((isValidUserId($url_userid)) && (($url_mid > 0) || ($url_bid > 0)) && (!ifFatalErrorsDetected())) {
78         // No image? Then output header
79         if ($mode != 'img') loadIncludeOnce('inc/header.php');
80
81         // Maybe he wants to confirm an email?
82         if ($url_mid > 0) {
83                 $result_main = SQL_QUERY_ESC("SELECT `id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s AND `userid`=%s LIMIT 1",
84                         array($url_mid, $url_userid), __FILE__, __LINE__);
85                 $type = 'mailid'; $urlId = $url_mid;
86         } elseif ($url_bid > 0) {
87                 $result_main = SQL_QUERY_ESC("SELECT `id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s AND `userid`=%s LIMIT 1",
88                         array($url_bid, $url_userid), __FILE__, __LINE__);
89                 $type = 'bonusid'; $urlId = $url_bid;
90         }
91
92         if (SQL_NUMROWS($result_main) == 1) {
93                 // Is the stats id valid?
94                 list($lid, $ltype) = SQL_FETCHROW($result_main);
95
96                 // Init result here with invalid to avoid possible missing variable
97                 $result_mailid = false;
98
99                 // @TODO Rewrite this to a filter
100                 switch ($ltype) {
101                         case 'NORMAL':
102                                 $result_mailid = SQL_QUERY_ESC("SELECT `pool_id`, `userid`, `id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `id`=%s LIMIT 1",
103                                         array($url_mid), __FILE__, __LINE__);
104                                 break;
105
106                         case 'BONUS':
107                                 $result_mailid = SQL_QUERY_ESC("SELECT `id`, `id`, `is_notify` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
108                                         array($url_bid), __FILE__, __LINE__);
109                                 break;
110
111                         default: // Unknown type
112                                 debug_report_bug(__FILE__, __LINE__, 'Unknown mail type ' . $ltype . ' detected.');
113                                 break;
114                 }
115
116                 // Entry found?
117                 if (SQL_NUMROWS($result_mailid) == 1) {
118                         // Load data
119                         list($pool, $sender, $notify) = SQL_FETCHROW($result_mailid);
120
121                         // Correct notification switch in non-bonus mails
122                         if (($notify != 'Y') && ($notify != 'N')) $notify = 'N';
123
124                         // Set sender to 0 when we have a bonus mail
125                         if ($ltype == 'BONUS') $sender = '0';
126
127                         // Is the user id valid?
128                         if (fetchUserData($url_userid) === true) {
129                                 // Is the user status CONFIRMED?
130                                 if (getUserData('status') == 'CONFIRMED') {
131                                         // User has confirmed his account so we can procede...
132                                         // @TODO Rewrite this to a filter
133                                         switch ($ltype) {
134                                                 case 'NORMAL':
135                                                         $result = SQL_QUERY_ESC("SELECT `payment_id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s LIMIT 1",
136                                                                 array(bigintval($pool)), __FILE__, __LINE__);
137
138                                                         // Entry found?
139                                                         if (SQL_NUMROWS($result) == 1) {
140                                                                 list($pay) = SQL_FETCHROW($result);
141                                                                 $time      = getPaymentPoints($pay, 'time');
142                                                                 $payment   = getPaymentPoints($pay, 'payment');
143                                                                 $isValid   = true;
144                                                         } // END - if
145
146                                                         // Free memory...
147                                                         SQL_FREERESULT($result);
148                                                         break;
149
150                                                 case 'BONUS':
151                                                         $result = SQL_QUERY_ESC("SELECT `time`, `points` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
152                                                                 array(bigintval($pool)), __FILE__, __LINE__);
153
154                                                         // Entry found?
155                                                         if (SQL_NUMROWS($result) == 1) {
156                                                                 list($time, $payment) = SQL_FETCHROW($result);
157                                                                 $isValid = true;
158                                                         } // END - if
159
160                                                         // Free memory...
161                                                         SQL_FREERESULT($result);
162                                                         break;
163
164                                                 default: // Unknown type
165                                                         debug_report_bug(__FILE__, __LINE__, 'Unknown mail type ' . $ltype . ' detected.');
166                                                         break;
167                                         }
168
169                                         // Is this entry valid?
170                                         if ($isValid === true) {
171                                                 if (($time == '0') && ($payment > 0)) $time = 1;
172                                                 if (($time > 0) && ($payment > 0)) {
173                                                         $img_code = '0';
174                                                         if (!empty($code)) {
175                                                                 // Generate code
176                                                                 $img_code = generateRandomCode(getConfig('code_length'), $code, $url_userid, $urlId);
177                                                         } // END - if
178
179                                                         // @TODO Rewrite this to a filter
180                                                         switch ($mode) {
181                                                                 case 'add':
182                                                                         // Init stats data
183                                                                         $stats_data = '0';
184
185                                                                         // Count clicks
186                                                                         // @TODO Rewrite this to a filter
187                                                                         switch ($ltype) {
188                                                                                 case 'NORMAL':
189                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `clicks`=`clicks`+1 WHERE `id`=%s LIMIT 1",
190                                                                                                 array($url_mid), __FILE__, __LINE__);
191
192                                                                                         // Update mediadata as well
193                                                                                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
194                                                                                                 // Update database
195                                                                                                 updateMediadataEntry(array('total_clicks', 'normal_clicks'), 'add', 1);
196                                                                                         } // END - if
197                                                                                         $stats_data = $url_mid;
198                                                                                         break;
199
200                                                                                 case 'BONUS':
201                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `clicks`=`clicks`+1 WHERE `id`=%s LIMIT 1",
202                                                                                                 array($url_bid), __FILE__, __LINE__);
203
204                                                                                         // Update mediadata as well
205                                                                                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
206                                                                                                 // Update database
207                                                                                                 updateMediadataEntry(array('total_clicks', 'bonus_clicks'), 'add', 1);
208                                                                                         } // END - if
209                                                                                         $stats_data = $url_bid;
210                                                                                         break;
211
212                                                                                 default: // Unknown type
213                                                                                         debug_report_bug(__FILE__, __LINE__, 'Unknown mail type ' . $ltype . ' detected.');
214                                                                                         break;
215                                                                         } // END - switch
216
217                                                                         // Export data into constants for the template
218                                                                         $content['points'] = translateComma($payment);
219                                                                         $content['banner'] = loadTemplate('mailid_banner', true);
220
221                                                                         // Only when user extension = v0.1.2: Update mails-confirmed counter
222                                                                         // @TODO Rewrite these blocks to filter
223                                                                         if (isExtensionInstalledAndNewer('user', '0.1.2')) {
224                                                                                 // Update counter
225                                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET mails_confirmed=mails_confirmed + 1 WHERE `userid`=%s LIMIT 1",
226                                                                                         array($url_userid), __FILE__, __LINE__);
227
228                                                                                 // Update random confirmed as well?
229                                                                                 if (isExtensionInstalledAndNewer('user', '0.3.4')) {
230                                                                                         // Update second counter
231                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=`rand_confirmed` + 1 WHERE `userid`=%s LIMIT 1",
232                                                                                                 array($url_userid), __FILE__, __LINE__);
233                                                                                 } // END - if
234                                                                         } // END - if
235
236                                                                         // Insert stats record
237                                                                         insertUserStatsRecord($url_userid, $type, $stats_data);
238
239                                                                         // Right code entered?
240                                                                         if (bigintval(postRequestParameter('gfx_check')) == $img_code) {
241                                                                                 // Add points over referal system is the default
242                                                                                 $locked = false;
243                                                                                 $template = 'mailid_points_done';
244
245                                                                                 // Right code entered add points and remove entry
246                                                                                 if ((getUserData('ref_payout') > 0) && (getConfig('allow_direct_pay') != 'Y')) {
247                                                                                         // Don't add points over the referal system
248                                                                                         $locked = true;
249                                                                                         $template = 'mailid_points_locked';
250                                                                                 } // END - if
251
252                                                                                 // Count down ref_payout value
253                                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ref_payout`=`ref_payout`-1 WHERE `userid`=%s AND `ref_payout` > 0 LIMIT 1",
254                                                                                         array($url_userid), __FILE__, __LINE__);
255
256                                                                                 // Add points
257                                                                                 // @TODO Try to rewrite the following unset()
258                                                                                 unset($GLOBALS['ref_level']);
259                                                                                 addPointsThroughReferalSystem('mailid_okay', $url_userid, $payment, false, 0, $locked);
260
261                                                                                 // Shall I add bonus points for "turbo clickers" ?
262                                                                                 if (isExtensionInstalledAndNewer('bonus', '0.2.2')) {
263                                                                                         // Is an active-rallye running and this is not a notification mail?
264                                                                                         if ((ifBonusRallyeActive()) && ($notify != 'Y')) {
265                                                                                                 // Shall I exclude the webmaster's own userid from the active-rallye?
266                                                                                                 if ((((getConfig('bonus_userid') == $url_userid) && (getConfig('bonus_include_own') == 'Y')) || (getConfig('bonus_userid') != $url_userid)) && (getConfig('def_refid') != $url_userid)) {
267                                                                                                         // Add points and remember ranking are done in this function....
268                                                                                                         addTurboBonus($urlId, $url_userid, $type);
269
270                                                                                                         // Set template to mailid_points_done2 which contains a link to the ranking list
271                                                                                                         $template = 'mailid_points_done2';
272                                                                                                         if ($locked) $template = 'mailid_points_locked2';
273                                                                                                         $content['userid']  = $url_userid;
274                                                                                                         $content['type']    = $type;
275                                                                                                         $content['data']    = $urlId;
276                                                                                                 } // END - if
277                                                                                         } // END - if
278                                                                                 } // END - if
279
280                                                                                 // Load total points
281                                                                                 $content['total'] = translateComma(
282                                                                                         countSumTotalData($url_userid, 'user_points', 'points') -
283                                                                                         countSumTotalData($url_userid, 'user_data', 'used_points')
284                                                                                 );
285
286                                                                                 // Load template
287                                                                                 loadTemplate($template, false, $content);
288                                                                         } elseif (isValidUserId($sender)) {
289                                                                                 // Wrong image code! So add points to sender's account
290                                                                                 addPointsDirectly('mailid_payback', $sender, $payment);
291
292                                                                                 // Load template
293                                                                                 loadTemplate('mailid_points_failed', false, $content);
294                                                                         }
295
296                                                                         // Remove link from table
297                                                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `id`=%s LIMIT 1",
298                                                                                 array(bigintval($lid)), __FILE__, __LINE__);
299                                                                         break;
300
301                                                                 case 'img':
302                                                                         generateImageOrCode($img_code);
303                                                                         break;
304
305                                                                 case 'confirm':
306                                                                         if ($code > 0) {
307                                                                                 // Export data into constants for the template
308                                                                                 $content['code']   = $code;
309                                                                                 $content['userid'] = $url_userid;
310                                                                                 $content['type']   = $type;
311                                                                                 $content['data']   = $urlId;
312                                                                                 $content['banner'] = loadTemplate('mailid_banner', true);
313                                                                                 if (getConfig('code_length') > 0) {
314                                                                                         // Generate Code
315                                                                                         $content['image'] = generateCaptchaCode($code, $type, $urlId, $url_userid);
316                                                                                         $templ = 'mailid_enter_code';
317                                                                                 } else {
318                                                                                         // Disabled code
319                                                                                         $content['gfx'] = $img_code;
320                                                                                         $templ = 'mailid_confirm_buttom';
321                                                                                 }
322
323                                                                                 // Load template
324                                                                                 loadTemplate($templ, false, $content);
325                                                                         } else {
326                                                                                 // Cannot confirm!
327                                                                                 debug_report_bug(__FILE__, __LINE__, 'No code given.');
328                                                                         }
329                                                                         break;
330
331                                                                 case '':
332                                                                         // Ok, all data is valid and loaded. Finally let's output the timer... :-)
333                                                                         // Export data into constants for the template
334                                                                         $content['time']   = $time;
335                                                                         $content['tim2']   = strlen($time);
336                                                                         $content['userid'] = $url_userid;
337                                                                         $content['type']   = $type;
338                                                                         $content['data']   = $urlId;
339                                                                         $content['rand']   = mt_rand(0, 99999);
340                                                                         $content['banner'] = loadTemplate('mailid_banner', true);
341
342                                                                         // Load template
343                                                                         loadTemplate('mailid_timer', false, $content);
344                                                                         break;
345
346                                                                 default: // Unknown mode
347                                                                         debug_report_bug(__FILE__, __LINE__, 'Unknown mode ' . $mode . ' detected.');
348                                                                         break;
349                                                         } // END - switch
350                                                 } else {
351                                                         loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (6)</div>');
352                                                         $mode = 'failed';
353                                                 }
354                                         } else {
355                                                 loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (5)</div>');
356                                                 $mode = 'failed';
357                                         }
358                                 } else {
359                                         loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (4)</div>');
360                                         $mode = 'failed';
361                                 }
362                         } else {
363                                 loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (3)</div>');
364                                 $mode = 'failed';
365                         }
366                 } else {
367                         loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (2)</div>');
368                         $mode = 'failed';
369                 }
370
371                 // Free result
372                 SQL_FREERESULT($result_mailid);
373         } else {
374                 loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (1)</div>');
375                 $mode = 'failed';
376         }
377
378         // Free result
379         SQL_FREERESULT($result_main);
380
381         // Insert footer if no image
382         if ($mode != 'img') {
383                 // Write footer
384                 loadIncludeOnce('inc/footer.php');
385         } // END - if
386 }
387
388 // Really all done here... ;-)
389 shutdown();
390
391 // [EOF]
392 ?>