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