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