Extra new-line removed
[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 $url_userid = '0';
64 $url_bid = '0';
65 $url_mid = '0';
66 $code = '0';
67 $mode = '';
68
69 // Secure all data
70 if (isGetRequestParameterSet('userid'))  $url_userid = bigintval(getRequestParameter('userid'));
71 if (isGetRequestParameterSet('mailid'))  $url_mid    = bigintval(getRequestParameter('mailid'));
72 if (isGetRequestParameterSet('bonusid')) $url_bid    = 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 (($url_userid) > 0 && (($url_mid > 0) || ($url_bid > 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 ($url_mid > 0) {
83                 $result = SQL_QUERY_ESC("SELECT `id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s AND `userid`=%s LIMIT 1",
84                         array($url_mid, $url_userid), __FILE__, __LINE__);
85                 $type = 'mailid'; $urlId = $url_mid;
86         } elseif ($url_bid > 0) {
87                 $result = SQL_QUERY_ESC("SELECT `id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s AND `userid`=%s LIMIT 1",
88                         array($url_bid, $url_userid), __FILE__, __LINE__);
89                 $type = 'bonusid'; $urlId = $url_bid;
90         }
91
92         if (SQL_NUMROWS($result) == 1) {
93                 // Is the stats id valid?
94                 list($lid, $ltype) = SQL_FETCHROW($result);
95                 SQL_FREERESULT($result);
96
97                 // Init result here with invalid to avoid possible missing variable
98                 $result_mailid = false;
99
100                 // @TODO Rewrite this to a filter
101                 switch ($ltype) {
102                         case 'NORMAL':
103                                 $result_mailid = SQL_QUERY_ESC("SELECT `pool_id`, `userid`, `id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `id`=%s LIMIT 1",
104                                         array($url_mid), __FILE__, __LINE__);
105                                 break;
106
107                         case 'BONUS':
108                                 $result_mailid = SQL_QUERY_ESC("SELECT `id`, `id`, `is_notify` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
109                                         array($url_bid), __FILE__, __LINE__);
110                                 break;
111
112                         default: // Unknown type
113                                 debug_report_bug('Unknown mail type ' . $ltype . ' detected.');
114                                 break;
115                 }
116
117                 // Entry found?
118                 if (SQL_NUMROWS($result_mailid) == 1) {
119                         // Load data
120                         list($pool, $sender, $notify) = SQL_FETCHROW($result_mailid);
121
122                         // Correct notification switch in non-bonus mails
123                         if (($notify != 'Y') && ($notify != 'N')) $notify = 'N';
124
125                         // Free some memory
126                         SQL_FREERESULT($result_mailid);
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($url_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('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(getConfig('code_length'), $code, $url_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($url_mid), __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 = $url_mid;
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($url_bid), __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 = $url_bid;
214                                                                                         break;
215
216                                                                                 default: // Unknown type
217                                                                                         debug_report_bug('Unknown mail type ' . $ltype . ' detected.');
218                                                                                         break;
219                                                                         } // END - switch
220
221                                                                         // Export data into constants for the template
222                                                                         $content['points'] = translateComma($payment);
223                                                                         $content['banner'] = loadTemplate('mailid_banner', true);
224
225                                                                         // Only when user extension = v0.1.2: Update mails-confirmed counter
226                                                                         // @TODO Rewrite these blocks to filter
227                                                                         if (isExtensionInstalledAndNewer('user', '0.1.2')) {
228                                                                                 // Update counter
229                                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET mails_confirmed=mails_confirmed + 1 WHERE `userid`=%s LIMIT 1",
230                                                                                         array($url_userid), __FILE__, __LINE__);
231
232                                                                                 // Update random confirmed as well?
233                                                                                 if (isExtensionInstalledAndNewer('user', '0.3.4')) {
234                                                                                         // Update second counter
235                                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET rand_confirmed=rand_confirmed + 1 WHERE `userid`=%s LIMIT 1",
236                                                                                                 array($url_userid), __FILE__, __LINE__);
237                                                                                 } // END - if
238                                                                         } // END - if
239
240                                                                         // Insert stats record
241                                                                         insertUserStatsRecord($url_userid, $type, $stats_data);
242
243                                                                         // Right code entered?
244                                                                         if (bigintval(postRequestParameter('gfx_check')) == $img_code) {
245                                                                                 // Add points over referal system is the default
246                                                                                 $locked = false;
247                                                                                 $template = 'mailid_points_done';
248
249                                                                                 // Right code entered add points and remove entry
250                                                                                 if ((getUserData('ref_payout') > 0) && (getConfig('allow_direct_pay') != 'Y')) {
251                                                                                         // Don't add points over the referal system
252                                                                                         $locked = true;
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($url_userid), __FILE__, __LINE__);
259
260                                                                                 // Add points
261                                                                                 // @TODO Try to rewrite the following unset()
262                                                                                 unset($GLOBALS['ref_level']);
263                                                                                 addPointsThroughReferalSystem('mailid_okay', $url_userid, $payment, false, 0, $locked);
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 ((getConfig('bonus_active') == 'Y') && ($notify != 'Y')) {
269                                                                                                 // Shall I exclude the webmaster's own userid from the active-rallye?
270                                                                                                 if ((((getConfig('bonus_userid') == $url_userid) && (getConfig('bonus_include_own') == 'Y')) || (getConfig('bonus_userid') != $url_userid)) && (getConfig('def_refid') != $url_userid)) {
271                                                                                                         // Add points and remember ranking are done in this function....
272                                                                                                         addTurboBonus($urlId, $url_userid, $type);
273
274                                                                                                         // Set template to mailid_points_done2 which contains a link to the ranking list
275                                                                                                         $template = 'mailid_points_done2';
276                                                                                                         if ($locked) $template = 'mailid_points_locked2';
277                                                                                                         $content['userid']  = $url_userid;
278                                                                                                         $content['type']    = $type;
279                                                                                                         $content['data']    = $urlId;
280                                                                                                 } // END - if
281                                                                                         } // END - if
282                                                                                 } // END - if
283
284                                                                                 // Load total points
285                                                                                 $content['total'] = translateComma(
286                                                                                         countSumTotalData($url_userid, 'user_points', 'points') -
287                                                                                         countSumTotalData($url_userid, 'user_data', 'used_points')
288                                                                                 );
289
290                                                                                 // Load template
291                                                                                 loadTemplate($template, false, $content);
292                                                                         } elseif ($sender > 0) {
293                                                                                 // Wrong image code! So add points to sender's account
294                                                                                 addPointsDirectly('mailid_payback', $sender, $payment);
295
296                                                                                 // Load template
297                                                                                 loadTemplate('mailid_points_failed', false, $content);
298                                                                         }
299
300                                                                         // Remove link from table
301                                                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `id`=%s LIMIT 1",
302                                                                                 array(bigintval($lid)), __FILE__, __LINE__);
303                                                                         break;
304
305                                                                 case 'img':
306                                                                         generateImageOrCode($img_code);
307                                                                         break;
308
309                                                                 case 'confirm':
310                                                                         if ($code > 0) {
311                                                                                 // Export data into constants for the template
312                                                                                 $content['code']   = $code;
313                                                                                 $content['userid'] = $url_userid;
314                                                                                 $content['type']   = $type;
315                                                                                 $content['data']   = $urlId;
316                                                                                 $content['banner'] = loadTemplate('mailid_banner', true);
317                                                                                 if (getConfig('code_length') > 0) {
318                                                                                         // Generate Code
319                                                                                         $content['image'] = generateCaptchaCode($code, $type, $urlId, $url_userid);
320                                                                                         $templ = 'mailid_enter_code';
321                                                                                 } else {
322                                                                                         // Disabled code
323                                                                                         $content['gfx'] = $img_code;
324                                                                                         $templ = 'mailid_confirm_buttom';
325                                                                                 }
326
327                                                                                 // Load template
328                                                                                 loadTemplate($templ, false, $content);
329                                                                         } else {
330                                                                                 // Cannot confirm!
331                                                                                 debug_report_bug('No code given.');
332                                                                         }
333                                                                         break;
334
335                                                                 case '':
336                                                                         // Ok, all data is valid and loaded. Finally let's output the timer... :-)
337                                                                         // Export data into constants for the template
338                                                                         $content['time']   = $time;
339                                                                         $content['tim2']   = strlen($time);
340                                                                         $content['userid'] = $url_userid;
341                                                                         $content['type']   = $type;
342                                                                         $content['data']   = $urlId;
343                                                                         $content['rand']   = mt_rand(0, 99999);
344                                                                         $content['banner'] = loadTemplate('mailid_banner', true);
345
346                                                                         // Load template
347                                                                         loadTemplate('mailid_timer', false, $content);
348                                                                         break;
349
350                                                                 default: // Unknown mode
351                                                                         debug_report_bug('Unknown mode ' . $mode . ' detected.');
352                                                                         break;
353                                                         } // END - switch
354                                                 } else {
355                                                         loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (6)</div>');
356                                                         $mode = 'failed';
357                                                 }
358                                         } else {
359                                                 loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (5)</div>');
360                                                 $mode = 'failed';
361                                         }
362                                 } else {
363                                         loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (4)</div>');
364                                         $mode = 'failed';
365                                 }
366                         } else {
367                                 SQL_FREERESULT($result);
368                                 loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (3)</div>');
369                                 $mode = 'failed';
370                         }
371                 } else {
372                         SQL_FREERESULT($result);
373                         loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (2)</div>');
374                         $mode = 'failed';
375                 }
376         } else {
377                 SQL_FREERESULT($result);
378                 loadTemplate('admin_settings_saved', false, '<div class="member_failed">{--MAIL_ALREADY_CONFIRMED--} (1)</div>');
379                 $mode = 'failed';
380         }
381
382         // Insert footer if no image
383         if ($mode != 'img') {
384                 // Write footer
385                 loadIncludeOnce('inc/footer.php');
386         } // END - if
387 }
388
389 // Really all done here... ;-)
390 shutdown();
391
392 // [EOF]
393 ?>