Renamed templates + added some filters for mail order
[mailer.git] / mailid.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/14/2003 *
4  * ===================                          Last change: 01/10/2013 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : mailid.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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Load security stuff here
39 require('inc/libs/security_functions.php');
40
41 // Init start time
42 $GLOBALS['__start_time'] = microtime(TRUE);
43
44 // Tell everyone we are in this module
45 $GLOBALS['__module']      = 'mailid';
46 $GLOBALS['__output_mode'] = '0';
47
48 // Disable copyright footer which would be to large for the upper frame
49 $GLOBALS['__copyright_enabled'] = 'N';
50
51 // Load the required file(s)
52 require('inc/config-global.php');
53
54 // Set content type and HTTP status
55 setContentType('text/html');
56 setHttpStatus('404 Not Found');
57
58 // Is ext-mailid active?
59 redirectOnUninstalledExtension('mailid');
60
61 // Is ext-other active?
62 redirectOnUninstalledExtension('other');
63
64 // Init data
65 $data = array(
66         'error_code' => '0',
67         'userid'     => '0',
68         'id'         => '0',
69         'code'       => '0',
70         'do'         => 'frames',
71         'type'       => '',
72 );
73
74 // Secure all data
75 if (isGetRequestElementSet('userid')) $data['userid'] = bigintval(getRequestElement('userid'));
76 if (isGetRequestElementSet('id'))     $data['id']     = bigintval(getRequestElement('id'));
77 if (isGetRequestElementSet('code'))   $data['code']   = bigintval(getRequestElement('code'));
78 if (isGetRequestElementSet('do'))     $data['do']     = getRequestElement('do');
79 if (isGetRequestElementSet('type'))   $data['type']   = getRequestElement('type');
80
81 // @TODO Improve check on $data['type'], empty() is not very much ...
82 if ((isValidId($data['userid'])) && (isValidId($data['id'])) && (!empty($data['type'])) && (!ifFatalErrorsDetected())) {
83         // Is 'do' still "frames"?
84         if ($data['do'] == 'frames') {
85                 // This is a frameset module
86                 $GLOBALS['frameset_mode'] = TRUE;
87         } // END - if
88
89         // No image?
90         if ($data['do'] != 'img') {
91                 // ... then output header
92                 loadIncludeOnce('inc/header.php');
93         } //¬†END - fi
94
95         // Init result for below sqlNumRows() function
96         $result_main = FALSE;
97
98         // Maybe he wants to confirm an email?
99         switch ($data['type']) {
100                 case 'normal':
101                         $result_main = sqlQueryEscaped("SELECT `id` AS `link_id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s AND `userid`=%s LIMIT 1",
102                                 array($data['id'], $data['userid']), __FILE__, __LINE__);
103                         break;
104
105                 case 'bonus':
106                         $result_main = sqlQueryEscaped("SELECT `id` AS `link_id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s AND `userid`=%s LIMIT 1",
107                                 array($data['id'], $data['userid']), __FILE__, __LINE__);
108                         break;
109
110                 default: // Not detected
111                         reportBug(__FILE__, __LINE__, 'No valid type=' . $data['type'] . ' set.');
112                         break;
113         } // END - switch
114
115         // Is an entry found?
116         if (sqlNumRows($result_main) == 1) {
117                 // Is the stats id valid?
118                 $data = merge_array($data, sqlFetchArray($result_main));
119
120                 // Init result here with invalid to avoid possible missing variable
121                 $result_mailid = FALSE;
122
123                 // @TODO Rewrite this to a filter/function
124                 switch ($data['link_type']) {
125                         case 'NORMAL':
126                                 $result_mailid = sqlQueryEscaped("SELECT `pool_id`, `userid` AS `sender`, `url` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `id`=%s LIMIT 1",
127                                         array($data['id']), __FILE__, __LINE__);
128                                 break;
129
130                         case 'BONUS':
131                                 $result_mailid = sqlQueryEscaped("SELECT `id` AS `pool_id`, `is_notify`, `url` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
132                                         array($data['id']), __FILE__, __LINE__);
133                                 break;
134
135                         default: // Unknown type
136                                 reportBug(__FILE__, __LINE__, 'Unknown mail type ' . $data['link_type'] . ' detected.');
137                                 break;
138                 } // END - switch
139
140                 // Entry found?
141                 if (sqlNumRows($result_mailid) == 1) {
142                         // Load data
143                         $data = merge_array($data, sqlFetchArray($result_mailid));
144
145                         // Correct notification switch in non-bonus mails
146                         if ((!isset($data['is_notify'])) || (!in_array($data['is_notify'], array('Y', 'N')))) {
147                                 $data['is_notify'] = 'N';
148                         } // END - if
149
150                         // Set sender to 0 when we have a bonus mail
151                         if ($data['link_type'] == 'BONUS') {
152                                 $data['sender'] = NULL;
153                         } // END - if
154
155                         // Is the user id valid?
156                         if (fetchUserData($data['userid']) === TRUE) {
157                                 // Is the user status CONFIRMED?
158                                 if (getUserData('status') == 'CONFIRMED') {
159                                         // User has confirmed his account so we can procede...
160                                         // @TODO Rewrite this to a filter
161                                         switch ($data['link_type']) {
162                                                 case 'NORMAL':
163                                                         $result = sqlQueryEscaped("SELECT `payment_id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s LIMIT 1",
164                                                                 array(bigintval($data['pool_id'])), __FILE__, __LINE__);
165
166                                                         // Entry found?
167                                                         if (sqlNumRows($result) == 1) {
168                                                                 $data = merge_array($data, sqlFetchArray($result));
169                                                                 $data['time']    = getPaymentTime($data['payment_id']);
170                                                                 $data['payment'] = getPaymentPayment($data['payment_id']);
171                                                                 $isValid   = TRUE;
172                                                         } // END - if
173
174                                                         // Free memory...
175                                                         sqlFreeResult($result);
176                                                         break;
177
178                                                 case 'BONUS':
179                                                         $result = sqlQueryEscaped("SELECT `time`, `points` AS `payment` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
180                                                                 array(bigintval($data['pool_id'])), __FILE__, __LINE__);
181
182                                                         // Entry found?
183                                                         if (sqlNumRows($result) == 1) {
184                                                                 $data = merge_array($data, sqlFetchArray($result));
185                                                                 $isValid = TRUE;
186                                                         } // END - if
187
188                                                         // Free memory...
189                                                         sqlFreeResult($result);
190                                                         break;
191
192                                                 default: // Unknown type
193                                                         reportBug(__FILE__, __LINE__, 'Unknown mail type ' . $data['link_type'] . ' detected.');
194                                                         break;
195                                         } // END - switch
196
197                                         // Is this entry valid?
198                                         if ($isValid === TRUE) {
199                                                 // Run at least one second
200                                                 if (($data['time'] < 1) && ($data['payment'] > 0)) {
201                                                         // Zero seconds is not supported
202                                                         $data['time'] = 1;
203                                                 } // END - if
204
205                                                 // Is time and payment set?
206                                                 if (($data['time'] > 0) && ($data['payment'] > 0)) {
207                                                         $data['real_code'] = '0';
208                                                         if (!empty($data['code'])) {
209                                                                 // Generate code (the user sees in the CAPTCHA)
210                                                                 $data['real_code'] = generateRandomCode(getCodeLength(), $data['code'], $data['userid'], $data['id']);
211                                                         } // END - if
212
213                                                         // Do the call-back by given data array
214                                                         doMailIdCallback($data);
215                                                 } else {
216                                                         $data['error_code'] = getCode('DATA_INVALID');
217                                                         $data['do'] = 'failed';
218                                                 }
219                                         } else {
220                                                 $data['error_code'] = getCode('POSSIBLE_INVALID');
221                                                 $data['do'] = 'failed';
222                                         }
223                                 } else {
224                                         $data['error_code'] = getCode('ACCOUNT_' . strtoupper(getUserData('status')));
225                                         $data['do'] = 'failed';
226                                 }
227                         } else {
228                                 $data['error_code'] = getCode('USER_404');
229                                 $data['do'] = 'failed';
230                         }
231                 } else {
232                         $data['error_code'] = getCode('STATS_404');
233                         $data['do'] = 'failed';
234                 }
235
236                 // Free result
237                 sqlFreeResult($result_mailid);
238         } else {
239                 $data['error_code'] = getCode('ALREADY_CONFIRMED');
240                 $data['do'] = 'failed';
241         }
242
243         // Free result
244         sqlFreeResult($result_main);
245 } else {
246         // Not all variables are set
247         $data['error_code'] = getCode('ERROR_MAILID');
248         $data['do'] = 'failed';
249 }
250
251 // Error code is set?
252 handleMailIdErrorCode($data['error_code']);
253
254 // Insert footer if no image
255 if ($data['do'] != 'img') {
256         // Write footer
257         loadIncludeOnce('inc/footer.php');
258 } // END - if
259
260 // Really all done here... ;-)
261 doShutdown();
262
263 // [EOF]
264 ?>