b88616de73d9260a7cfacbda1ad7d02c96937989
[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://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Load security stuff here
41 require('inc/libs/security_functions.php');
42
43 // Init start time
44 $GLOBALS['startTime'] = microtime(true);
45
46 // Tell everyone we are in this module
47 $GLOBALS['module'] = 'mailid';
48 $GLOBALS['output_mode'] = -1;
49
50 // Load the required file(s)
51 require('inc/config-global.php');
52
53 // Set content type 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 $mode = '';
69
70 // Secure all data
71 if (isGetRequestParameterSet('userid'))  $userId  = bigintval(getRequestParameter('userid'));
72 if (isGetRequestParameterSet('mailid'))  $mailId  = bigintval(getRequestParameter('mailid'));
73 if (isGetRequestParameterSet('bonusid')) $bonusId = bigintval(getRequestParameter('bonusid'));
74 if (isGetRequestParameterSet('code'))    $code    = bigintval(getRequestParameter('code'));
75 if (isGetRequestParameterSet('mode'))    $mode    = getRequestParameter('mode');
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 ($mode != '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')) $notify = 'N';
129
130                         // Set sender to 0 when we have a bonus mail
131                         if ($ltype == 'BONUS') $sender = '0';
132
133                         // Is the user id valid?
134                         if (fetchUserData($userId) === true) {
135                                 // Is the user status CONFIRMED?
136                                 if (getUserData('status') == 'CONFIRMED') {
137                                         // User has confirmed his account so we can procede...
138                                         // @TODO Rewrite this to a filter
139                                         switch ($ltype) {
140                                                 case 'NORMAL':
141                                                         $result = SQL_QUERY_ESC("SELECT `payment_id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s LIMIT 1",
142                                                                 array(bigintval($pool)), __FILE__, __LINE__);
143
144                                                         // Entry found?
145                                                         if (SQL_NUMROWS($result) == 1) {
146                                                                 list($pay) = SQL_FETCHROW($result);
147                                                                 $time      = getPaymentPoints($pay, 'time');
148                                                                 $payment   = getPaymentPoints($pay, 'payment');
149                                                                 $isValid   = true;
150                                                         } // END - if
151
152                                                         // Free memory...
153                                                         SQL_FREERESULT($result);
154                                                         break;
155
156                                                 case 'BONUS':
157                                                         $result = SQL_QUERY_ESC("SELECT `time`, `points` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
158                                                                 array(bigintval($pool)), __FILE__, __LINE__);
159
160                                                         // Entry found?
161                                                         if (SQL_NUMROWS($result) == 1) {
162                                                                 list($time, $payment) = SQL_FETCHROW($result);
163                                                                 $isValid = true;
164                                                         } // END - if
165
166                                                         // Free memory...
167                                                         SQL_FREERESULT($result);
168                                                         break;
169
170                                                 default: // Unknown type
171                                                         debug_report_bug(__FILE__, __LINE__, 'Unknown mail type ' . $ltype . ' detected.');
172                                                         break;
173                                         }
174
175                                         // Is this entry valid?
176                                         if ($isValid === true) {
177                                                 if (($time == '0') && ($payment > 0)) $time = 1;
178                                                 if (($time > 0) && ($payment > 0)) {
179                                                         $img_code = '0';
180                                                         if (!empty($code)) {
181                                                                 // Generate code
182                                                                 $img_code = generateRandomCode(getCodeLength(), $code, $userId, $urlId);
183                                                         } // END - if
184
185                                                         // @TODO Rewrite this to a filter
186                                                         switch ($mode) {
187                                                                 case 'add':
188                                                                         // Init stats data
189                                                                         $stats_data = '0';
190
191                                                                         // Count clicks
192                                                                         // @TODO Rewrite this to a filter
193                                                                         switch ($ltype) {
194                                                                                 case 'NORMAL':
195                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `clicks`=`clicks`+1 WHERE `id`=%s LIMIT 1",
196                                                                                                 array($mailId), __FILE__, __LINE__);
197
198                                                                                         // Update mediadata as well
199                                                                                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
200                                                                                                 // Update database
201                                                                                                 updateMediadataEntry(array('total_clicks', 'normal_clicks'), 'add', 1);
202                                                                                         } // END - if
203                                                                                         $stats_data = $mailId;
204                                                                                         break;
205
206                                                                                 case 'BONUS':
207                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `clicks`=`clicks`+1 WHERE `id`=%s LIMIT 1",
208                                                                                                 array($bonusId), __FILE__, __LINE__);
209
210                                                                                         // Update mediadata as well
211                                                                                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
212                                                                                                 // Update database
213                                                                                                 updateMediadataEntry(array('total_clicks', 'bonus_clicks'), 'add', 1);
214                                                                                         } // END - if
215                                                                                         $stats_data = $bonusId;
216                                                                                         break;
217
218                                                                                 default: // Unknown type
219                                                                                         debug_report_bug(__FILE__, __LINE__, 'Unknown mail type ' . $ltype . ' detected.');
220                                                                                         break;
221                                                                         } // END - switch
222
223                                                                         // Export data into constants for the template
224                                                                         $content['banner'] = loadTemplate('mailid_banner', true);
225
226                                                                         // Only when user extension = v0.1.2: Update mails-confirmed counter
227                                                                         // @TODO Rewrite these blocks to filter
228                                                                         if (isExtensionInstalledAndNewer('user', '0.1.2')) {
229                                                                                 // Update counter
230                                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET mails_confirmed=mails_confirmed + 1 WHERE `userid`=%s LIMIT 1",
231                                                                                         array($userId), __FILE__, __LINE__);
232
233                                                                                 // Update random confirmed as well?
234                                                                                 if (isExtensionInstalledAndNewer('user', '0.3.4')) {
235                                                                                         // Update second counter
236                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=`rand_confirmed` + 1 WHERE `userid`=%s LIMIT 1",
237                                                                                                 array($userId), __FILE__, __LINE__);
238                                                                                 } // END - if
239                                                                         } // END - if
240
241                                                                         // Insert stats record
242                                                                         insertUserStatsRecord($userId, $type, $stats_data);
243
244                                                                         // Right code entered?
245                                                                         if (bigintval(postRequestParameter('gfx_check')) == $img_code) {
246                                                                                 // Set HTTP status to okay
247                                                                                 setHttpStatus('200 OK');
248
249                                                                                 // Add points over referal system is the default
250                                                                                 $template = 'mailid_points_done';
251
252                                                                                 // Right code entered add points and remove entry
253                                                                                 if (ifUserPointsLocked($userId)) {
254                                                                                         // Don't add points over the referal system
255                                                                                         $template = 'mailid_points_locked';
256                                                                                 } // END - if
257
258                                                                                 // Count down ref_payout value
259                                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ref_payout`=`ref_payout`-1 WHERE `userid`=%s AND `ref_payout` > 0 LIMIT 1",
260                                                                                         array($userId), __FILE__, __LINE__);
261
262                                                                                 // Add points
263                                                                                 initReferalSystem();
264                                                                                 addPointsThroughReferalSystem('mailid_okay', $userId, $payment);
265
266                                                                                 // Shall I add bonus points for "turbo clickers" ?
267                                                                                 if (isExtensionInstalledAndNewer('bonus', '0.2.2')) {
268                                                                                         // Is an active-rallye running and this is not a notification mail?
269                                                                                         if ((isBonusRallyeActive()) && ($notify != 'Y')) {
270                                                                                                 // Shall I exclude the webmaster's own userid from the active-rallye?
271                                                                                                 if ((((getBonusUserid() == $userId) && (getConfig('bonus_include_own') == 'Y')) || (getBonusUserid() != $userId)) && (getDefRefid() != $userId)) {
272                                                                                                         // Add points and remember ranking are done in this function....
273                                                                                                         addTurboBonus($urlId, $userId, $type);
274
275                                                                                                         // Set template to mailid_points_done2 which contains a link to the ranking list
276                                                                                                         $template = 'mailid_points_done2';
277
278                                                                                                         // Different template if user has some mails to confirm
279                                                                                                         if (ifUserPointsLocked($userId)) {
280                                                                                                                 $template = 'mailid_points_locked2';
281                                                                                                         } // END - if
282
283                                                                                                         // Assign more data for the template
284                                                                                                         $content['userid']  = $userId;
285                                                                                                         $content['type']    = $type;
286                                                                                                         $content['data']    = $urlId;
287                                                                                                 } // END - if
288                                                                                         } // END - if
289                                                                                 } // END - if
290
291                                                                                 // Load total points
292                                                                                 $content['total_points']  = getTotalPoints($userId);
293
294                                                                                 // Add payment points
295                                                                                 $content['points'] = $payment;
296
297                                                                                 // Load template
298                                                                                 loadTemplate($template, false, $content);
299                                                                         } elseif (isValidUserId($sender)) {
300                                                                                 // Wrong image code! So add points to sender's account
301                                                                                 addPointsThroughReferalSystem('mailid_payback', $sender, $payment);
302
303                                                                                 // Load template
304                                                                                 loadTemplate('mailid_points_failed', false, $content);
305                                                                         }
306
307                                                                         // Remove link from table
308                                                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `id`=%s LIMIT 1",
309                                                                                 array(bigintval($lid)), __FILE__, __LINE__);
310                                                                         break;
311
312                                                                 case 'img':
313                                                                         generateImageOrCode($img_code);
314                                                                         break;
315
316                                                                 case 'confirm':
317                                                                         if ($code > 0) {
318                                                                                 // Export data into constants for the template
319                                                                                 $content['code']   = $code;
320                                                                                 $content['userid'] = $userId;
321                                                                                 $content['type']   = $type;
322                                                                                 $content['data']   = $urlId;
323                                                                                 $content['banner'] = loadTemplate('mailid_banner', true);
324                                                                                 if (getCodeLength() > 0) {
325                                                                                         // Generate Code
326                                                                                         $content['image'] = generateCaptchaCode($code, $type, $urlId, $userId);
327                                                                                         $templ = 'mailid_enter_code';
328                                                                                 } else {
329                                                                                         // Disabled code
330                                                                                         $content['gfx'] = $img_code;
331                                                                                         $templ = 'mailid_confirm_buttom';
332                                                                                 }
333
334                                                                                 // Load template
335                                                                                 loadTemplate($templ, false, $content);
336                                                                         } else {
337                                                                                 // Cannot confirm!
338                                                                                 debug_report_bug(__FILE__, __LINE__, 'No code given.');
339                                                                         }
340                                                                         break;
341
342                                                                 case '':
343                                                                         // Ok, all data is valid and loaded. Finally let's output the timer... :-)
344                                                                         // Export data into constants for the template
345                                                                         $content['time']   = $time;
346                                                                         $content['tim2']   = strlen($time);
347                                                                         $content['userid'] = $userId;
348                                                                         $content['type']   = $type;
349                                                                         $content['data']   = $urlId;
350                                                                         $content['rand']   = mt_rand(0, 99999);
351                                                                         $content['banner'] = loadTemplate('mailid_banner', true);
352
353                                                                         // Load template
354                                                                         loadTemplate('mailid_timer', false, $content);
355                                                                         break;
356
357                                                                 default: // Unknown mode
358                                                                         debug_report_bug(__FILE__, __LINE__, 'Unknown mode ' . $mode . ' detected.');
359                                                                         break;
360                                                         } // END - switch
361                                                 } else {
362                                                         loadTemplate('admin_settings_unsaved', false, '{--MAIL_ALREADY_CONFIRMED--} (6)');
363                                                         $mode = 'failed';
364                                                 }
365                                         } else {
366                                                 loadTemplate('admin_settings_unsaved', false, '{--MAIL_ALREADY_CONFIRMED--} (5)');
367                                                 $mode = 'failed';
368                                         }
369                                 } else {
370                                         loadTemplate('admin_settings_unsaved', false, '{--MAIL_ALREADY_CONFIRMED--} (4)');
371                                         $mode = 'failed';
372                                 }
373                         } else {
374                                 loadTemplate('admin_settings_unsaved', false, '{--MAIL_ALREADY_CONFIRMED--} (3)');
375                                 $mode = 'failed';
376                         }
377                 } else {
378                         loadTemplate('admin_settings_unsaved', false, '{--MAIL_ALREADY_CONFIRMED--} (2)');
379                         $mode = 'failed';
380                 }
381
382                 // Free result
383                 SQL_FREERESULT($result_mailid);
384         } else {
385                 loadTemplate('admin_settings_unsaved', false, '{--MAIL_ALREADY_CONFIRMED--} (1)');
386                 $mode = 'failed';
387         }
388
389         // Free result
390         SQL_FREERESULT($result_main);
391
392         // Insert footer if no image
393         if ($mode != 'img') {
394                 // Write footer
395                 loadIncludeOnce('inc/footer.php');
396         } // END - if
397 }
398
399 // Really all done here... ;-)
400 shutdown();
401
402 // [EOF]
403 ?>