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