Heavy rewrite:
[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 - 2008 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'] = 0;
48
49 // Load the required file(s)
50 require('inc/config-global.php');
51
52 if (!isInstalled()) {
53         // You have to install first!
54         redirectToUrl('install.php');
55 } // END - if
56
57 // Is the extension active
58 redirectOnUninstalledExtension('mailid');
59
60 // Secure all data
61 $url_uid = 0; $url_bid = 0; $url_mid = 0; $code = 0; $mode = '';
62 if (REQUEST_ISSET_GET('uid'))     $url_uid = bigintval(REQUEST_GET('uid'));
63 if (REQUEST_ISSET_GET('mailid'))  $url_mid = bigintval(REQUEST_GET('mailid'));
64 if (REQUEST_ISSET_GET('bonusid')) $url_bid = bigintval(REQUEST_GET('bonusid'));
65 if (REQUEST_ISSET_GET('code'))    $code    = bigintval(REQUEST_GET('code'));
66 if (REQUEST_ISSET_GET('mode'))    $mode    = REQUEST_GET('mode');
67
68 // 01        1        12            2    2            21    1                   22     10
69 if (($url_uid) > 0 && (($url_mid > 0) || ($url_bid > 0)) && (getTotalFatalErrors() == 0)) {
70         // No image? Then output header
71         if ($mode != 'img') loadIncludeOnce('inc/header.php');
72
73         // Maybe he wants to confirm an email?
74         if ($url_mid > 0) {
75                 $result = SQL_QUERY_ESC("SELECT id, link_type FROM `{!_MYSQL_PREFIX!}_user_links` WHERE stats_id=%s AND `userid`=%s LIMIT 1",
76                 array($url_mid, $url_uid), __FILE__, __LINE__);
77                 $type = 'mailid'; $urlId = $url_mid;
78         } elseif ($url_bid > 0) {
79                 $result = SQL_QUERY_ESC("SELECT id, link_type FROM `{!_MYSQL_PREFIX!}_user_links` WHERE bonus_id=%s AND `userid`=%s LIMIT 1",
80                 array($url_bid, $url_uid), __FILE__, __LINE__);
81                 $type = 'bonusid'; $urlId = $url_bid;
82         }
83
84         if (SQL_NUMROWS($result) == 1) {
85                 // Is the stats ID valid?
86                 list($lid, $ltype) = SQL_FETCHROW($result);
87                 SQL_FREERESULT($result);
88
89                 // @TODO Rewrite this to a filter
90                 switch ($ltype) {
91                         case 'NORMAL':
92                                 $result_mailid = SQL_QUERY_ESC("SELECT pool_id, userid, id FROM `{!_MYSQL_PREFIX!}_user_stats` WHERE `id`=%s LIMIT 1",
93                                 array($url_mid), __FILE__, __LINE__);
94                                 break;
95
96                         case 'BONUS':
97                                 $result_mailid = SQL_QUERY_ESC("SELECT id, id, is_notify FROM `{!_MYSQL_PREFIX!}_bonus` WHERE `id`=%s LIMIT 1",
98                                 array($url_bid), __FILE__, __LINE__);
99                                 break;
100
101                         default: // Unknown type
102                                 debug_report_bug('Unknown mail type ' . $ltype . ' detected.');
103                                 break;
104                 }
105
106                 // Entry found?
107                 if (SQL_NUMROWS($result_mailid) == 1) {
108                         // Load data
109                         list($pool, $sender, $notify) = SQL_FETCHROW($result_mailid);
110
111                         // Correct notification switch in non-bonus mails
112                         if (($notify != 'Y') && ($notify != 'N')) $notify = 'N';
113
114                         // Free some memory
115                         SQL_FREERESULT($result_mailid);
116
117                         // Set sender to 0 when we have a bonus mail
118                         if ($ltype == 'BONUS') $sender = 0;
119
120                         // Is the user's ID unlocked?
121                         $result = SQL_QUERY_ESC("SELECT status, gender, surname, family, ref_payout FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `userid`=%s LIMIT 1",
122                         array($url_uid), __FILE__, __LINE__);
123                         if (SQL_NUMROWS($result) == 1) {
124                                 // Load data
125                                 list($status, $gender, $sname, $fname, $ref_pay) = SQL_FETCHROW($result);
126
127                                 // Free some memory
128                                 SQL_FREERESULT($result);
129
130                                 if ($status == 'CONFIRMED') {
131                                         // Update last activity
132                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET last_online=UNIX_TIMESTAMP(), `last_module`='mailid_top' WHERE `userid`=%s LIMIT 1",
133                                         array($url_uid), __FILE__, __LINE__);
134
135                                         // User has confirmed his account so we can procede...
136                                         // @TODO Rewrite this to a filter
137                                         switch ($ltype)
138                                         {
139                                                 case 'NORMAL':
140                                                         $result = SQL_QUERY_ESC("SELECT payment_id FROM `{!_MYSQL_PREFIX!}_user_stats` WHERE pool_id=%s LIMIT 1",
141                                                         array(bigintval($pool)), __FILE__, __LINE__);
142
143                                                         // Entry found?
144                                                         if (SQL_NUMROWS($result) == 1) {
145                                                                 list($pay) = SQL_FETCHROW($result);
146                                                                 $time      = getPaymentPoints($pay, 'time');
147                                                                 $payment   = getPaymentPoints($pay, 'payment');
148                                                                 $isValid   = true;
149                                                         } // END - if
150
151                                                         // Free memory...
152                                                         SQL_FREERESULT($result);
153                                                         break;
154
155                                                 case 'BONUS':
156                                                         $result = SQL_QUERY_ESC("SELECT time, points FROM `{!_MYSQL_PREFIX!}_bonus` WHERE `id`=%s LIMIT 1",
157                                                         array(bigintval($pool)), __FILE__, __LINE__);
158
159                                                         // Entry found?
160                                                         if (SQL_NUMROWS($result) == 1) {
161                                                                 list($time, $payment) = SQL_FETCHROW($result);
162                                                                 $isValid = true;
163                                                         } // END - if
164
165                                                         // Free memory...
166                                                         SQL_FREERESULT($result);
167                                                         break;
168
169                                                 default: // Unknown type
170                                                         debug_report_bug('Unknown mail type ' . $ltype . ' detected.');
171                                                         break;
172                                         }
173
174                                         // Is this entry valid?
175                                         if ($isValid) {
176                                                 if (($time == '0') && ($payment > 0)) $time = '1';
177                                                 if (($time > 0) && ($payment > 0)) {
178                                                         if (!empty($code)) {
179                                                                 // Generate code
180                                                                 $img_code = generateRandomCode(getConfig('code_length'), $code, $url_uid, $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                                                                         {
193                                                                                 case 'NORMAL':
194                                                                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_stats` SET clicks=clicks + 1 WHERE `id`=%s LIMIT 1",
195                                                                                         array($url_mid), __FILE__, __LINE__);
196
197                                                                                         // Update mediadata as well
198                                                                                         if (GET_EXT_VERSION('mediadata') >= '0.0.4') {
199                                                                                                 // Update database
200                                                                                                 MEDIA_UPDATE_ENTRY(array('total_clicks', 'normal_clicks'), 'add', 1);
201                                                                                         }
202                                                                                         $stats_data = $url_mid;
203                                                                                         break;
204
205                                                                                 case 'BONUS':
206                                                                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_bonus` SET clicks=clicks + 1 WHERE `id`=%s LIMIT 1",
207                                                                                         array($url_bid), __FILE__, __LINE__);
208
209                                                                                         // Update mediadata as well
210                                                                                         if (GET_EXT_VERSION('mediadata') >= '0.0.4') {
211                                                                                                 // Update database
212                                                                                                 MEDIA_UPDATE_ENTRY(array('total_clicks', 'bonus_clicks'), 'add', 1);
213                                                                                         }
214                                                                                         $stats_data = $url_bid;
215                                                                                         break;
216
217                                                                                 default: // Unknown type
218                                                                                         debug_report_bug('Unknown mail type ' . $ltype . ' detected.');
219                                                                                         break;
220                                                                         }
221
222                                                                         // Export data into constants for the template
223                                                                         // @TODO Rewrite these constants
224                                                                         define('_POINTS_VALUE'   , translateComma($payment));
225                                                                         define('_TEMPLATE_BANNER', LOAD_TEMPLATE('mailid_banner', true));
226
227                                                                         // Only when user extension = v0.1.2: Update mails-confirmed counter
228                                                                         // @TODO Rewrite these blocks to filter
229                                                                         if (GET_EXT_VERSION('user') >= '0.1.2') {
230                                                                                 // Update counter
231                                                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET mails_confirmed=mails_confirmed + 1 WHERE `userid`=%s LIMIT 1",
232                                                                                 array($url_uid), __FILE__, __LINE__);
233
234                                                                                 // Update random confirmed as well?
235                                                                                 if (GET_EXT_VERSION('user') >= '0.3.4') {
236                                                                                         // Update second counter
237                                                                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET rand_confirmed=rand_confirmed + 1 WHERE `userid`=%s LIMIT 1",
238                                                                                         array($url_uid), __FILE__, __LINE__);
239                                                                                 } // END - if
240                                                                         } // END - if
241
242                                                                         // Insert stats record
243                                                                         insertUserStatsRecord($url_uid, $type, $stats_data);
244
245                                                                         // Right code entered?
246                                                                         if (bigintval(REQUEST_POST('gfx_check')) == $img_code) {
247                                                                                 // Add points over referal system is the default
248                                                                                 $locked = false;
249                                                                                 $template = 'mailid_points_done';
250
251                                                                                 // Right code entered add points and remove entry
252                                                                                 if (($ref_pay > 0) && (getConfig('allow_direct_pay') != 'Y')) {
253                                                                                         // Don't add points over the referal system
254                                                                                         $locked = true;
255                                                                                         $template = 'mailid_points_locked';
256                                                                                 } // END - if
257
258                                                                                 // Count down ref_payout value
259                                                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `ref_payout`=`ref_payout`-1 WHERE `userid`=%s AND `ref_payout` > 0 LIMIT 1",
260                                                                                         array($url_uid), __FILE__, __LINE__);
261
262                                                                                 // Add points
263                                                                                 unset($GLOBALS['ref_level']);
264                                                                                 ADD_POINTS_REFSYSTEM('mailid_okay', $url_uid, $payment, false, '0', $locked);
265
266                                                                                 // Shall I add bonus points for "turbo clickers" ?
267                                                                                 if (GET_EXT_VERSION('bonus') >= '0.2.2') {
268                                                                                         // Is an active-rallye running and this is not a notification mail?
269                                                                                         if ((getConfig('bonus_active') == 'Y') && ($notify == 'N')) {
270                                                                                                 // Shall I exclude the webmaster's own userid from the active-rallye?
271                                                                                                 if ((((getConfig('bonus_uid') == $url_uid) && (getConfig('bonus_include_own') == 'Y')) || (getConfig('bonus_uid') != $url_uid)) && (getConfig('def_refid') != $url_uid)) {
272                                                                                                         // Add points and remember ranking are done in this function....
273                                                                                                         BONUS_ADD_TURBO_POINTS($urlId, $url_uid, $type);
274
275                                                                                                         // Set template to mailid_points_done2 which contains a link to the ranking list
276                                                                                                         $template = 'mailid_points_done2';
277                                                                                                         if ($locked) $template = 'mailid_points_locked2';
278                                                                                                         define('_UID_VALUE' , $url_uid);
279                                                                                                         define('_TYPE_VALUE', $type);
280                                                                                                         define('_DATA_VALUE', translateComma($urlId));
281                                                                                                 } // END - if
282                                                                                         } // END - if
283                                                                                 } // END - if
284
285                                                                                 // Load total points
286                                                                                 define('__TOTAL_POINTS', translateComma(
287                                                                                 GET_TOTAL_DATA($url_uid, 'user_points', 'points') -
288                                                                                 GET_TOTAL_DATA($url_uid, 'user_data', 'used_points'))
289                                                                                 );
290
291                                                                                 // Load template
292                                                                                 LOAD_TEMPLATE($template);
293                                                                         } else {
294                                                                                 // Wrong image code! So add points to sender's account
295                                                                                 unset($GLOBALS['ref_level']);
296                                                                                 ADD_POINTS_REFSYSTEM_DIRECT('mailid_payback', $sender, $payment);
297
298                                                                                 // Load template
299                                                                                 LOAD_TEMPLATE('mailid_points_failed');
300                                                                         }
301
302                                                                         // Remove link from table
303                                                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_links` WHERE `id`=%s LIMIT 1",
304                                                                         array(bigintval($lid)), __FILE__, __LINE__);
305                                                                         break;
306
307                                                                 case 'img':
308                                                                         GENERATE_IMAGE($img_code);
309                                                                         break;
310
311                                                                 case 'confirm':
312                                                                         if ($code > 0) {
313                                                                                 // Export data into constants for the template
314                                                                                 define('_CODE_VALUE', $code);
315                                                                                 define('_UID_VALUE' , $url_uid );
316                                                                                 define('_TYPE_VALUE', $type);
317                                                                                 define('_DATA_VALUE', $urlId);
318                                                                                 define('_TEMPLATE_BANNER', LOAD_TEMPLATE('mailid_banner', true));
319                                                                                 if (getConfig('code_length') > 0) {
320                                                                                         // Generate Code
321                                                                                         define('_IMAGE_CODE', generateCaptchaCode($code, $type, $urlId, $url_uid));
322                                                                                         $templ = 'mailid_enter_code';
323                                                                                 } else {
324                                                                                         // Disabled code
325                                                                                         define('__GFX_CODE', $img_code);
326                                                                                         $templ = 'mailid_confirm_buttom';
327                                                                                 }
328
329                                                                                 // Load template
330                                                                                 LOAD_TEMPLATE($templ);
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                                                                         define('_TIME_VALUE', $time);
338                                                                         define('_TIM2_VALUE', strlen($time));
339                                                                         define('_UID_VALUE' , $url_uid );
340                                                                         define('_TYPE_VALUE', $type);
341                                                                         define('_DATA_VALUE', $urlId);
342                                                                         define('_RAND_VALUE', mt_rand(0, 99999));
343                                                                         define('_TEMPLATE_BANNER', LOAD_TEMPLATE('mailid_banner', true));
344
345                                                                         // Load template
346                                                                         LOAD_TEMPLATE('mailid_timer');
347                                                                         break;
348
349                                                                 default: // Unknown mode
350                                                                         debug_report_bug('Unknown mode ' . $mode . ' detected.');
351                                                                         break;
352                                                         } // END - switch
353                                                 } else {
354                                                         LOAD_TEMPLATE('admin_settings_saved', false, "<div class=\"member_failed\">{--MAIL_ALREADY_CONFIRMED--} (6)</div>");
355                                                         $mode = 'failed';
356                                                 }
357                                         } else {
358                                                 LOAD_TEMPLATE('admin_settings_saved', false, "<div class=\"member_failed\">{--MAIL_ALREADY_CONFIRMED--} (5)</div>");
359                                                 $mode = 'failed';
360                                         }
361                                 } else {
362                                         LOAD_TEMPLATE('admin_settings_saved', false, "<div class=\"member_failed\">{--MAIL_ALREADY_CONFIRMED--} (4)</div>");
363                                         $mode = 'failed';
364                                 }
365                         } else {
366                                 SQL_FREERESULT($result);
367                                 LOAD_TEMPLATE('admin_settings_saved', false, "<div class=\"member_failed\">{--MAIL_ALREADY_CONFIRMED--} (3)</div>");
368                                 $mode = 'failed';
369                         }
370                 } else {
371                         SQL_FREERESULT($result);
372                         LOAD_TEMPLATE('admin_settings_saved', false, "<div class=\"member_failed\">{--MAIL_ALREADY_CONFIRMED--} (2)</div>");
373                         $mode = 'failed';
374                 }
375         } else {
376                 SQL_FREERESULT($result);
377                 LOAD_TEMPLATE('admin_settings_saved', false, "<div class=\"member_failed\">{--MAIL_ALREADY_CONFIRMED--} (1)</div>");
378                 $mode = 'failed';
379         }
380
381         // Insert footer if no image
382         if ($mode != 'img') {
383                 // Write footer
384                 loadIncludeOnce('inc/footer.php');
385         } // END - if
386 }
387
388 // Really all done here... ;-)
389 shutdown();
390
391 // [EOF]
392 ?>