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