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