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