A lot fixes to templates and missing functions added, more rewrites
[mailer.git] / inc / modules / guest / what-sponsor_reg.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 06/10/2005 *
4  * ===============                              Last change: 05/18/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-sponsor_reg.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Sponsor's registration form                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Anmeldeformular fuer Sponsoren                   *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software. You can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License.       *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
36         require($INC);
37 } elseif ((!EXT_IS_ACTIVE("sponsor"))) {
38         addFatalMessage(__FILE__, __LINE__, getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), "sponsor");
39         return;
40 }
41
42 // Add description as navigation point
43 ADD_DESCR("guest", __FILE__);
44
45 // Create array for form errors (= missing data)
46 $FORM_ERRORS = array();
47
48 if (IS_FORM_SENT()) {
49         //
50         // Check submitted form data
51         //
52         // 1. Salutation / Surname / family name
53         if (!REQUEST_ISSET_POST(('gender'))) {
54                 // Surname is empty
55                 $FORM_ERRORS[] = getMessage('SPONSOR_NO_GENDER_SELECTED');
56         }
57
58         if (!REQUEST_ISSET_POST(('surname'))) {
59                 // Surname is empty
60                 $FORM_ERRORS[] = getMessage('SPONSOR_SURNAME_IS_EMPTY');
61         }
62
63         if (!REQUEST_ISSET_POST(('family'))) {
64                 // Surname is empty
65                 $FORM_ERRORS[] = getMessage('SPONSOR_FAMILY_IS_EMPTY');
66         }
67
68         // 2. Company name
69         // 012     3                 32    23      4                   43    3      4                  4321    12      3                 32    2     3                   3210
70         if (((!REQUEST_ISSET_POST(('company'))) && ((REQUEST_ISSET_POST(('tax_ident'))) || (REQUEST_ISSET_POST(('position'))))) || ((REQUEST_ISSET_POST(('company'))) && (!REQUEST_ISSET_POST(('tax_ident'))))) {
71                 if (!REQUEST_ISSET_POST(('company'))) {
72                         // Company name is empty
73                         $FORM_ERRORS[] = getMessage('SPONSOR_COMPANY_IS_EMPTY');
74                 } elseif (!REQUEST_ISSET_POST(('tax_ident'))) {
75                         // Tax ident number name is empty
76                         $FORM_ERRORS[] = getMessage('SPONSOR_TAX_IDENT_IS_EMPTY');
77                 }
78
79                 if (!REQUEST_ISSET_POST(('position'))) {
80                         // Not fatal but not nice: position in company is empty
81                         $FORM_ERRORS[] = getMessage('SPONSOR_POSITION_IS_EMPTY');
82                 }
83         }
84
85         // 3. Street and number
86         if (!REQUEST_ISSET_POST(('street_nr1'))) {
87                 // Street name and house number are empty
88                 $FORM_ERRORS[] = getMessage('SPONSOR_STREET_NR1_IS_EMPTY');
89         } elseif ((REQUEST_ISSET_POST(('street_nr2'))) && (!REQUEST_ISSET_POST(('street_nr1')))) {
90                 // 1st line for street is empty, but 2nd line not
91                 $FORM_ERRORS[] = getMessage('SPONSOR_STREET_NR1_IS_EMPTY_2_NOT');
92         }
93
94         // 4. Country code
95         if (!REQUEST_ISSET_POST(('country'))) {
96                 // Country code is empty
97                 $FORM_ERRORS[] = getMessage('SPONSOR_COUNTRY_IS_EMPTY');
98         } elseif (strlen(REQUEST_POST('country')) != 2) {
99                 // Country code is invalid
100                 $FORM_ERRORS[] = getMessage('SPONSOR_COUNTRY_IS_INVALID');
101         }
102
103         // 3. ZIP code
104         if (!REQUEST_ISSET_POST(('zip'))) {
105                 // ZIP code is empty
106                 $FORM_ERRORS[] = getMessage('SPONSOR_ZIP_IS_EMPTY');
107         } elseif (bigintval(REQUEST_POST('zip')) != REQUEST_POST('zip')) {
108                 // ZIP is invalid
109                 $FORM_ERRORS[] = getMessage('SPONSOR_ZIP_IS_INVALID');
110                 REQUEST_SET_POST('zip', "");
111         }
112
113         // 4. City
114         if (!REQUEST_ISSET_POST(('city'))) {
115                 // City is empty
116                 $FORM_ERRORS[] = getMessage('SPONSOR_CITY_IS_EMPTY');
117         }
118
119         // 5. Phone number
120         if (!REQUEST_ISSET_POST(('phone'))) {
121                 // City is empty
122                 $FORM_ERRORS[] = getMessage('SPONSOR_PHONE_IS_EMPTY');
123         }
124
125         // 6. Homepage URL
126         if (!REQUEST_ISSET_POST(('url'))) {
127                 // Homepage URL is empty
128                 $FORM_ERRORS[] = getMessage('SPONSOR_URL_IS_EMPTY');
129         } elseif (!VALIDATE_URL(REQUEST_POST('url'))) {
130                 // Homepage URL is invalid
131                 $FORM_ERRORS[] = getMessage('SPONSOR_URL_IS_INVALID');
132                 REQUEST_SET_POST('url', "");
133         }
134
135         // 7. Light validation of email address
136         if ((!REQUEST_ISSET_POST(('email'))) || (REQUEST_POST('email') == "@")) {
137                 // Email is invalid/empty
138                 $FORM_ERRORS[] = getMessage('SPONSOR_EMAIL_IS_INVALID');
139         } elseif (SPONSOR_FOUND_EMAIL_DB(REQUEST_POST('email'))) {
140                 // Email already found in database!
141                 $FORM_ERRORS[] = getMessage('SPONSOR_EMAIL_IS_ALREADY_REGISTERED');
142                 REQUEST_SET_POST('email', "");
143         }
144
145         // 8. Pay type selected?
146         if (!REQUEST_ISSET_POST(('pay_type'))) {
147                 // Not pay type selected
148                 $FORM_ERRORS[] = getMessage('SPONSOR_NO_PAYTYPE_SELECTED');
149         }
150
151         // 9. Interval of mails
152         if (!REQUEST_ISSET_POST(('warning_interval'))) {
153                 // No warning interval selected
154                 $FORM_ERRORS[] = getMessage('SPONSOR_NO_WARNING_INTERVAL_SELECTED');
155         }
156
157         // 10. Mail notifications disabled/enabled
158         if (!REQUEST_ISSET_POST(('receive_warnings'))) {
159                 // Option not selected!
160                 $FORM_ERRORS[] = getMessage('SPONSOR_NO_RECEIVE_WARNINGS_SELECTED');
161         }
162
163         // Did he enter his password twice?
164         if (((!REQUEST_ISSET_POST(('pass1'))) || (!REQUEST_ISSET_POST(('pass2')))) || ((REQUEST_POST('pass1') != REQUEST_POST('pass2')) && (REQUEST_ISSET_POST(('pass1'))) && (REQUEST_ISSET_POST(('pass2'))))) {
165                 if ((REQUEST_POST('pass1') != REQUEST_POST('pass2')) && (REQUEST_ISSET_POST(('pass1'))) && (REQUEST_ISSET_POST(('pass2')))) {
166                         // Passwords missmatch
167                         $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORDS_MISMATCH');
168                 } else {
169                         if (!REQUEST_ISSET_POST(('pass1'))) {
170                                 // Password 1 is empty
171                                 $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD1_EMPTY');
172                         }
173
174                         if (!REQUEST_ISSET_POST(('pass2'))) {
175                                 // Password 2 is empty
176                                 $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD2_EMPTY');
177                         }
178                 }
179         } elseif (strlen(REQUEST_POST('pass1')) < getConfig('pass_len')) {
180                 // Password is to short!
181                 $FORM_ERRORS[] = sprintf(getMessage('SPONSOR_PASSWORD_TOO_SHORT'), getConfig('pass_len'));
182         }
183
184         // Check if he has accepted the terms&conditions
185         if (!REQUEST_ISSET_POST(('terms'))) {
186                 // Homepage URL is empty
187                 $FORM_ERRORS[] = getMessage('SPONSOR_TERMS_NOT_ACCEPTED');
188         }
189         
190         // If there is something wrong/missing stop registration
191         if (count($FORM_ERRORS) > 0) REQUEST_UNSET_POST('ok');
192 }
193
194 if ((IS_FORM_SENT()) && (count($FORM_ERRORS) == 0)) {
195         // Generate message array
196         $MSGs = array(
197                 'failed' => getMessage('SPONSOR_REGISTRATION_FAILED'),
198                 'added'  => getMessage('SPONSOR_REGISTRATION_COMPLETED'),
199         );
200
201         // Calulate points
202         $result = SQL_QUERY_ESC("SELECT (pay_rate * pay_min_count) AS points, pay_min_count AS pay, pay_currency AS curr
203 FROM `{!_MYSQL_PREFIX!}_sponsor_paytypes`
204 WHERE id='%s' LIMIT 1", array(REQUEST_POST('pay_type')), __FILE__, __LINE__);
205         list($points, $pay, $curr) = SQL_FETCHROW($result);
206
207         // Free memory
208         SQL_FREERESULT($result);
209
210         // Add points to array
211         REQUEST_POST('points_amount', $points);
212         REQUEST_POST('points_used'  , "0.00000");
213         REQUEST_POST('last_pay'     , $pay);
214         REQUEST_POST('last_curr'    , $curr);
215
216         // Register sponsor but never ever update here!
217         $STATUS = SPONSOR_HANDLE_SPONSOR(REQUEST_POST_ARRAY(), true, $MSGs, true);
218
219         // Check the status of the registration process
220         switch ($STATUS)
221         {
222         case "added": // Sponsor successfully added with account status = UNCONFIRMED!
223                 // Check for his ID number
224                 $result = SQL_QUERY_ESC("SELECT id, hash FROM `{!_MYSQL_PREFIX!}_sponsor_data` WHERE email='%s' LIMIT 1",
225                         array(REQUEST_POST('email')), __FILE__, __LINE__);
226                 if (SQL_NUMROWS($result) == 1) {
227                         // ID found so let's load it for the confirmation email
228                         list($id, $hash) = SQL_FETCHROW($result);
229
230                         // Prepare data for the email template
231                         define('__ID'       , $id);
232                         define('__HASH'     , $hash);
233                         define('__EMAIL'    , REQUEST_POST('email'));
234                         define('__SURNAME'  , REQUEST_POST('surname'));
235                         define('__FAMILY'   , REQUEST_POST('family'));
236                         define('__GENDER'    , TRANSLATE_GENDER(REQUEST_POST('gender')));
237                         define('__TIMESTAMP', MAKE_DATETIME(time(), 0));
238                         define('__PASSWORD' , REQUEST_POST('pass1'));
239
240                         // Generate email and send it to the new sponsor
241                         $EMAIL_MSG = LOAD_EMAIL_TEMPLATE("sponsor_confirm", $hash);
242                         SEND_EMAIL(REQUEST_POST('email'), getMessage('SPONSOR_PLEASE_CONFIRM_SUBJ'), $EMAIL_MSG);
243
244                         // Send mail to admin
245                         SEND_ADMIN_NOTIFICATION(getMessage('ADMIN_NEW_SPONSOR'), "admin_sponsor_reg", $hash);
246
247                         // Output message: DONE
248                         $MSG = $MSGs['added'];
249                 } else {
250                         // Sponsor account not found???
251                         $MSG = sprintf(getMessage('SPONSOR_EMAIL_404'), REQUEST_POST('email'));
252                 }
253
254                 // Free memory
255                 SQL_FREERESULT($result);
256                 break;
257
258         default:
259                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown status %s detected.", $STATUS));
260                 if (!IS_ADMIN()) {
261                         // Message for testing admin
262                         $MSG = sprintf(getMessage('ADMIN_SPONSOR_UNKNOWN_STATUS'), $STATUS);
263                 } else {
264                         // Message for the guest
265                         $MSG = sprintf(getMessage('SPONSOR_UNKNOWN_STATUS'), $STATUS);
266                 }
267                 break;
268         }
269
270         // Display message
271         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
272 } else {
273         // Check for payment types
274         $result = SQL_QUERY("SELECT id, pay_name, pay_rate, pay_currency, pay_min_count
275 FROM `{!_MYSQL_PREFIX!}_sponsor_paytypes`
276 ORDER BY pay_name", __FILE__, __LINE__);
277         if (SQL_NUMROWS($result) > 0) {
278                 // Load all types...
279                 // @TODO Swap this HTML code to templates
280                 $OUT = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left2 right2 top2\">\n";
281                 $SW = 2;
282                 while (list($id, $name, $rate, $curr, $min) = SQL_FETCHROW($result)) {
283                         $OUT .= "<tr>
284   <td class=\"bottom2 switch_sw".$SW."\" height=\"50\" style=\"padding-left: 2px; padding-right: 2px\">
285     <input type=\"radio\" name=\"pay_type\" class=\"guest_normal\" value=\"".bigintval($id)."\" />&nbsp;".$name."<br />
286     <div align=\"right\">(".$min." ".$curr." = ".TRANSLATE_COMMA($rate * $min)." {!POINTS!})</div>
287   </td>
288 </tr>\n";
289                         $SW = 3 - $SW;
290                 }
291                 $OUT .= "</table>\n";
292                 define('__PAYMENT_LIST', $OUT);
293                 SQL_FREERESULT($result);
294
295                 // Check for invalid entries
296                 if (count($FORM_ERRORS) > 0)
297                 {
298                         // Some found... :-(
299                         define('__COMPANY'  , COMPILE_CODE(REQUEST_POST('company')));
300                         define('__POSITION' , COMPILE_CODE(REQUEST_POST('position')));
301                         define('__TAX_IDENT', COMPILE_CODE(REQUEST_POST('tax_ident')));
302                         define('__SURNAME'  , COMPILE_CODE(REQUEST_POST('surname')));
303                         define('__FAMILY'   , COMPILE_CODE(REQUEST_POST('family')));
304                         define('__STREET1'  , COMPILE_CODE(REQUEST_POST('street_nr1')));
305                         define('__STREET2'  , COMPILE_CODE(REQUEST_POST('street_nr2')));
306                         define('__COUNTRY'  , COMPILE_CODE(REQUEST_POST('country')));
307                         define('__ZIP'      , COMPILE_CODE(REQUEST_POST('zip')));
308                         define('__CITY'     , COMPILE_CODE(REQUEST_POST('city')));
309                         define('__PHONE'    , COMPILE_CODE(REQUEST_POST('phone')));
310                         define('__FAX'      , COMPILE_CODE(REQUEST_POST('fax')));
311                         define('__CELL'     , COMPILE_CODE(REQUEST_POST('cell')));
312                         define('__EMAIL'    , COMPILE_CODE(REQUEST_POST('email')));
313                         define('__URL'      , COMPILE_CODE(REQUEST_POST('url')));
314
315                         // Check for gender selection
316                         switch (REQUEST_POST('gender'))
317                         {
318                         case "M": // Male
319                                 define('__GENDER_M'  , " selected=\"selected\"");
320                                 define('__GENDER_F'  , "");
321                                 define('__GENDER_C'  , "");
322                                 break;
323
324                         case "F": // Female
325                                 define('__GENDER_M'  , "");
326                                 define('__GENDER_F'  , " selected=\"selected\"");
327                                 define('__GENDER_C'  , "");
328                                 break;
329
330                         case "C": // Company
331                                 define('__GENDER_M'  , "");
332                                 define('__GENDER_F'  , "");
333                                 define('__GENDER_C'  , " selected=\"selected\"");
334                                 break;
335                         }
336
337                         // Check for receive_warnings
338                         switch (REQUEST_POST('receive_warnings'))
339                         {
340                         case "Y":
341                                 define('__REC_Y'    , " selected=\"selected\"");
342                                 define('__REC_N'    , "");
343                                 break;
344
345                         case "N":
346                                 define('__REC_Y'    , "");
347                                 define('__REC_N'    , " selected=\"selected\"");
348                                 break;
349                         }
350
351                         // Errors found
352                         $OUT = "<P align=\"center\">
353   <strong><div class=\"guest_failed\">".SPONSOR_REGISTER_SOMETHING_MISSING.":</div></strong><br />
354 </P>
355 <ol>\n";
356                         foreach ($FORM_ERRORS as $error) {
357                                 $OUT .= "<li>".$error."</li>\n";
358                         } // END - if
359
360                         $OUT .= "</ol><br />\n";
361                         define('__SPONSOR_FORM_ERRORS', $OUT);
362                         define('__SPONSOR_REFID', REQUEST_POST('refid'));
363                 } else {
364                         // None found, first call
365                         define('__COMPANY'  , "");
366                         define('__POSITION' , "");
367                         define('__TAX_IDENT', "");
368                         define('__GENDER_M'  , "");
369                         define('__GENDER_F'  , "");
370                         define('__GENDER_C'  , "");
371                         define('__SURNAME'  , "");
372                         define('__FAMILY'   , "");
373                         define('__STREET1'  , "");
374                         define('__STREET2'  , "");
375                         define('__COUNTRY'  , "");
376                         define('__ZIP'      , "");
377                         define('__CITY'     , "");
378                         define('__PHONE'    , "");
379                         define('__FAX'      , "");
380                         define('__CELL'     , "");
381                         define('__EMAIL'    , "");
382                         define('__URL'      , "");
383                         define('__REC_Y'    , "");
384                         define('__REC_N'    , "");
385
386                         // No errors found so far
387                         define('__SPONSOR_FORM_ERRORS', "");
388                 }
389
390                 // Prepare referal things
391                 if (!REQUEST_ISSET_GET(('refid'))) {
392                         // No referal link
393                         define('__SPONSOR_REFID', "0");
394                 } else  {
395                         // Referal ID transmitted, we don't care here if it is right or not
396                         define('__SPONSOR_REFID', bigintval(REQUEST_GET('refid')));
397                 }
398
399                 // Display registration form
400                 LOAD_TEMPLATE("guest_sponsor_reg");
401         } else {
402                 // Nothing added so far
403                 LOAD_TEMPLATE("admin_settings_saved", false, SPONSOR_PAYMENT_LIST_IS_EMPTY);
404         }
405
406         // Free memory
407         SQL_FREERESULT($result);
408 }
409
410 //
411 ?>