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