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