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