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