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