a101f17ae3425e8aea0b11c905d276f06e5141f2
[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         if (IS_ADMIN()) {
39                 ADD_FATAL(sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "sponsor"));
40         } else {
41                 ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "sponsor");
42         }
43         return;
44 }
45
46 // Add description as navigation point
47 ADD_DESCR("guest", __FILE__);
48
49 // Create array for form errors (= missing data)
50 $FORM_ERRORS = array();
51
52 if (isset($_POST['ok'])) {
53         //
54         // Check submitted form data
55         //
56         // 1. Salutation / Surname / family name
57         if (empty($_POST['gender'])) {
58                 // Surname is empty
59                 $FORM_ERRORS[] = SPONSOR_NO_GENDER_SELECTED;
60         }
61
62         if (empty($_POST['surname'])) {
63                 // Surname is empty
64                 $FORM_ERRORS[] = SPONSOR_SURNAME_IS_EMPTY;
65         }
66
67         if (empty($_POST['family'])) {
68                 // Surname is empty
69                 $FORM_ERRORS[] = SPONSOR_FAMILY_IS_EMPTY;
70         }
71
72         // 2. Company name
73         // 012     3                 32    23      4                   43    3      4                  4321    12      3                 32    2     3                   3210
74         if (((empty($_POST['company'])) && ((!empty($_POST['tax_ident'])) || (!empty($_POST['position'])))) || ((!empty($_POST['company'])) && (empty($_POST['tax_ident'])))) {
75                 if (empty($_POST['company'])) {
76                         // Company name is empty
77                         $FORM_ERRORS[] = SPONSOR_COMPANY_IS_EMPTY;
78                 } elseif (empty($_POST['tax_ident'])) {
79                         // Tax ident number name is empty
80                         $FORM_ERRORS[] = SPONSOR_TAX_IDENT_IS_EMPTY;
81                 }
82
83                 if (empty($_POST['position'])) {
84                         // Not fatal but not nice: position in company is empty
85                         $FORM_ERRORS[] = SPONSOR_POSITION_IS_EMPTY;
86                 }
87         }
88
89         // 3. Street and number
90         if (empty($_POST['street_nr1'])) {
91                 // Street name and house number are empty
92                 $FORM_ERRORS[] = SPONSOR_STREET_NR1_IS_EMPTY;
93         } elseif ((!empty($_POST['street_nr2'])) && (empty($_POST['street_nr1']))) {
94                 // 1st line for street is empty, but 2nd line not
95                 $FORM_ERRORS[] = SPONSOR_STREET_NR1_IS_EMPTY_2_NOT;
96         }
97
98         // 4. Country code
99         if (empty($_POST['country'])) {
100                 // Country code is empty
101                 $FORM_ERRORS[] = SPONSOR_COUNTRY_IS_EMPTY;
102         } elseif (strlen($_POST['country']) != 2) {
103                 // Country code is invalid
104                 $FORM_ERRORS[] = SPONSOR_COUNTRY_IS_INVALID;
105         }
106
107         // 3. ZIP code
108         if (empty($_POST['zip'])) {
109                 // ZIP code is empty
110                 $FORM_ERRORS[] = SPONSOR_ZIP_IS_EMPTY;
111         } elseif (bigintval($_POST['zip']) != $_POST['zip']) {
112                 // ZIP is invalid
113                 $FORM_ERRORS[] = SPONSOR_ZIP_IS_INVALID;
114                 $_POST['zip'] = "";
115         }
116
117         // 4. City
118         if (empty($_POST['city'])) {
119                 // City is empty
120                 $FORM_ERRORS[] = SPONSOR_CITY_IS_EMPTY;
121         }
122
123         // 5. Phone number
124         if (empty($_POST['phone'])) {
125                 // City is empty
126                 $FORM_ERRORS[] = SPONSOR_PHONE_IS_EMPTY;
127         }
128
129         // 6. Homepage URL
130         if (empty($_POST['url'])) {
131                 // Homepage URL is empty
132                 $FORM_ERRORS[] = SPONSOR_URL_IS_EMPTY;
133         } elseif (!VALIDATE_URL($_POST['url'])) {
134                 // Homepage URL is invalid
135                 $FORM_ERRORS[] = SPONSOR_URL_IS_INVALID;
136                 $_POST['url'] = "";
137         }
138
139         // 7. Light validation of email address
140         if ((empty($_POST['email'])) || ($_POST['email'] == "@")) {
141                 // Email is invalid/empty
142                 $FORM_ERRORS[] = SPONSOR_EMAIL_IS_INVALID;
143         } elseif (SPONSOR_FOUND_EMAIL_DB($_POST['email'])) {
144                 // Email already found in database!
145                 $FORM_ERRORS[] = SPONSOR_EMAIL_IS_ALREADY_REGISTERED;
146                 $_POST['email'] = "";
147         }
148         // 8. Pay type selected?
149         if (empty($_POST['pay_type']))
150         {
151                 // Not pay type selected
152                 $FORM_ERRORS[] = SPONSOR_NO_PAYTYPE_SELECTED;
153         }
154         // 9. Interval of mails
155         if (empty($_POST['warning_interval']))
156         {
157                 // No warning interval selected
158                 $FORM_ERRORS[] = SPONSOR_NO_WARNING_INTERVAL_SELECTED;
159         }
160         // 10. Mail notifications disabled/enabled
161         if (empty($_POST['receive_warnings']))
162         {
163                 // Option not selected!
164                 $FORM_ERRORS[] = SPONSOR_NO_RECEIVE_WARNINGS_SELECTED;
165         }
166         // Did he enter his password twice?
167         if (((empty($_POST['pass1'])) || (empty($_POST['pass2']))) || (($_POST['pass1'] != $_POST['pass2']) && (!empty($_POST['pass1'])) && (!empty($_POST['pass2']))))
168         {
169                 if (($_POST['pass1'] != $_POST['pass2']) && (!empty($_POST['pass1'])) && (!empty($_POST['pass2'])))
170                 {
171                         // Passwords missmatch
172                         $FORM_ERRORS[] = SPONSOR_PASSWORDS_MISMATCH;
173                 }
174                  else
175                 {
176                         if (empty($_POST['pass1']))
177                         {
178                                 // Password 1 is empty
179                                 $FORM_ERRORS[] = SPONSOR_PASSWORD1_EMPTY;
180                         }
181                         if (empty($_POST['pass2']))
182                         {
183                                 // Password 2 is empty
184                                 $FORM_ERRORS[] = SPONSOR_PASSWORD2_EMPTY;
185                         }
186                 }
187         }
188         // Is the password long enough?
189          elseif (strlen($_POST['pass1']) < $_CONFIG['pass_len'])
190         {
191                 // Too short!
192                 $FORM_ERRORS[] = SPONSOR_PASSWORD_TOO_SHORT;
193         }
194
195         // Check if he has accepted the terms&conditions
196         if (empty($_POST['terms']))
197         {
198                 // Homepage URL is empty
199                 $FORM_ERRORS[] = SPONSOR_TERMS_NOT_ACCEPTED;
200         }
201         
202         // If there is something wrong/missing stop registration
203         if (count($FORM_ERRORS) > 0) unset($_POST['ok']);
204 }
205
206 if ((isset($_POST['ok'])) && (count($FORM_ERRORS) == 0))
207 {
208         // Generate message array
209         $MSGs = array(
210                 'failed' => SPONSOR_REGISTRATION_FAILED,
211                 'added'  => SPONSOR_REGISTRATION_COMPLETED,
212         );
213
214         // Calulate points
215         $result = SQL_QUERY_ESC("SELECT (pay_rate * pay_min_count) AS points, pay_min_count AS pay, pay_currency AS curr
216 FROM "._MYSQL_PREFIX."_sponsor_paytypes
217 WHERE id='%s' LIMIT 1", array($_POST['pay_type']), __FILE__, __LINE__);
218         list($points, $pay, $curr) = SQL_FETCHROW($result);
219
220         // Free memory
221         SQL_FREERESULT($result);
222
223         // Add points to array
224         $_POST['points_amount'] = $points;
225         $_POST['points_used']   = "0.00000";
226         $_POST['last_pay']      = $pay;
227         $_POST['last_curr']     = $curr;
228
229         // Register sponsor but never ever update here!
230         $STATUS = SPONSOR_HANDLE_SPONSOR($_POST, true, $MSGs, true);
231
232         // Check the status of the registration process
233         switch ($STATUS)
234         {
235         case "added": // Sponsor successfully added with account status = UNCONFIRMED!
236                 // Check for his ID number
237                 $result = SQL_QUERY_ESC("SELECT id, hash FROM "._MYSQL_PREFIX."_sponsor_data WHERE email='%s' LIMIT 1",
238                  array($_POST['email']), __FILE__, __LINE__);
239                 if (SQL_NUMROWS($result) == 1)
240                 {
241                         // ID found so let's load it for the confirmation email
242                         list($id, $hash) = SQL_FETCHROW($result);
243
244                         // Prepare data for the email template
245                         define('__ID'       , $id);
246                         define('__HASH'     , $hash);
247                         define('__EMAIL'    , $_POST['email']);
248                         define('__SURNAME'  , $_POST['surname']);
249                         define('__FAMILY'   , $_POST['family']);
250                         define('__GENDER'    , TRANSLATE_GENDER($_POST['gender']));
251                         define('__TIMESTAMP', MAKE_DATETIME(time(), 0));
252                         define('__PASSWORD' , $_POST['pass1']);
253
254                         // Generate email and send it to the new sponsor
255                         $EMAIL_MSG = LOAD_EMAIL_TEMPLATE("sponsor_confirm", $hash);
256                         SEND_EMAIL($_POST['email'], SPONSOR_PLEASE_CONFIRM_SUBJ, $EMAIL_MSG);
257
258                         // Send mail to admin
259                         SEND_ADMIN_NOTIFICATION(ADMIN_NEW_SPONSOR, "admin_sponsor_reg", $hash);
260
261                         // Output message: DONE
262                         $MSG = $MSGs['added'];
263                 }
264                  else
265                 {
266                         // Sponsor account not found???
267                         $MSG = SPONSOR_EMAIL_404_1.$_POST['email'].SPONSOR_EMAIL_404_2;
268                 }
269
270                 // Free memory
271                 SQL_FREERESULT($result);
272                 break;
273
274         default:
275                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown status %s detected.", $STATUS));
276                 if (!IS_ADMIN()) {
277                         // Message for testing admin
278                         $MSG = ADMIN_SPONSOR_UNKOWN_STATUS_1.$STATUS.ADMIN_SPONSOR_UNKOWN_STATUS_2;
279                 } else {
280                         // Message for the guest
281                         $MSG = SPONSOR_UNKOWN_STATUS_1.$STATUS.SPONSOR_UNKOWN_STATUS_2;
282                 }
283                 break;
284         }
285
286         // Display message
287         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
288 } else {
289         // Check for payment types
290         $result = SQL_QUERY("SELECT id, pay_name, pay_rate, pay_currency, pay_min_count
291 FROM "._MYSQL_PREFIX."_sponsor_paytypes
292 ORDER BY pay_name", __FILE__, __LINE__);
293         if (SQL_NUMROWS($result) > 0) {
294                 // Load all types...
295                 $OUT = "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left2 right2 top2\">\n";
296                 $SW = 2;
297                 while(list($id, $name, $rate, $curr, $min) = SQL_FETCHROW($result))
298                 {
299                         $OUT .= "<TR>
300   <TD class=\"bottom2 switch_sw".$SW."\" height=\"50\" style=\"padding-left: 2px; padding-right: 2px\">
301     <INPUT type=\"radio\" name=\"pay_type\" class=\"guest_normal\" value=\"".bigintval($id)."\">&nbsp;".$name."<br />
302     <DIV align=\"right\">(".$min." ".$curr." = ".TRANSLATE_COMMA($rate * $min)." ".POINTS.")</DIV>
303   </TD>
304 </TR>\n";
305                         $SW = 3 - $SW;
306                 }
307                 $OUT .= "</TABLE>\n";
308                 define('__PAYMENT_LIST', $OUT);
309                 SQL_FREERESULT($result);
310
311                 // Check for invalid entries
312                 if (count($FORM_ERRORS) > 0)
313                 {
314                         // Some found... :-(
315                         define('__COMPANY'  , COMPILE_CODE($_POST['company']));
316                         define('__POSITION' , COMPILE_CODE($_POST['position']));
317                         define('__TAX_IDENT', COMPILE_CODE($_POST['tax_ident']));
318                         define('__SURNAME'  , COMPILE_CODE($_POST['surname']));
319                         define('__FAMILY'   , COMPILE_CODE($_POST['family']));
320                         define('__STREET1'  , COMPILE_CODE($_POST['street_nr1']));
321                         define('__STREET2'  , COMPILE_CODE($_POST['street_nr2']));
322                         define('__COUNTRY'  , COMPILE_CODE($_POST['country']));
323                         define('__ZIP'      , COMPILE_CODE($_POST['zip']));
324                         define('__CITY'     , COMPILE_CODE($_POST['city']));
325                         define('__PHONE'    , COMPILE_CODE($_POST['phone']));
326                         define('__FAX'      , COMPILE_CODE($_POST['fax']));
327                         define('__CELL'     , COMPILE_CODE($_POST['cell']));
328                         define('__EMAIL'    , COMPILE_CODE($_POST['email']));
329                         define('__URL'      , COMPILE_CODE($_POST['url']));
330
331                         // Check for gender selection
332                         switch ($_POST['gender'])
333                         {
334                         case "M": // Male
335                                 define('__GENDER_M'  , " selected=\"selected\"");
336                                 define('__GENDER_F'  , "");
337                                 define('__GENDER_C'  , "");
338                                 break;
339
340                         case "F": // Female
341                                 define('__GENDER_M'  , "");
342                                 define('__GENDER_F'  , " selected=\"selected\"");
343                                 define('__GENDER_C'  , "");
344                                 break;
345
346                         case "C": // Company
347                                 define('__GENDER_M'  , "");
348                                 define('__GENDER_F'  , "");
349                                 define('__GENDER_C'  , " selected=\"selected\"");
350                                 break;
351                         }
352
353                         // Check for receive_warnings
354                         switch ($_POST['receive_warnings'])
355                         {
356                         case "Y":
357                                 define('__REC_Y'    , " selected=\"selected\"");
358                                 define('__REC_N'    , "");
359                                 break;
360
361                         case "N":
362                                 define('__REC_Y'    , "");
363                                 define('__REC_N'    , " selected=\"selected\"");
364                                 break;
365                         }
366
367                         // Errors found
368                         $OUT = "<P align=\"center\">
369   <STRONG><FONT class=\"guest_failed\">".SPONSOR_REGISTER_SOMETHING_MISSING.":</FONT></STRONG><br />
370 </P>
371 <OL>\n";
372                         foreach ($FORM_ERRORS as $error) {
373                                 $OUT .= "<LI>".$error."</LI>\n";
374                         } // END - if
375
376                         $OUT .= "</OL><br />\n";
377                         define('__SPONSOR_FORM_ERRORS', $OUT);
378                         define('__SPONSOR_REFID', $_POST['refid']);
379                 } else {
380                         // None found, first call
381                         define('__COMPANY'  , "");
382                         define('__POSITION' , "");
383                         define('__TAX_IDENT', "");
384                         define('__GENDER_M'  , "");
385                         define('__GENDER_F'  , "");
386                         define('__GENDER_C'  , "");
387                         define('__SURNAME'  , "");
388                         define('__FAMILY'   , "");
389                         define('__STREET1'  , "");
390                         define('__STREET2'  , "");
391                         define('__COUNTRY'  , "");
392                         define('__ZIP'      , "");
393                         define('__CITY'     , "");
394                         define('__PHONE'    , "");
395                         define('__FAX'      , "");
396                         define('__CELL'     , "");
397                         define('__EMAIL'    , "");
398                         define('__URL'      , "");
399                         define('__REC_Y'    , "");
400                         define('__REC_N'    , "");
401
402                         // No errors found so far
403                         define('__SPONSOR_FORM_ERRORS', "");
404                 }
405
406                 // Prepare referal things
407                 if (!isset($_GET['refid'])) {
408                         // No referal link
409                         define('__SPONSOR_REFID', "0");
410                 } else  {
411                         // Referal ID transmitted, we don't care here if it is right or not
412                         define('__SPONSOR_REFID', bigintval($_GET['refid']));
413                 }
414
415                 // Display registration form
416                 LOAD_TEMPLATE("guest_sponsor_reg");
417         } else {
418                 // Nothing added so far
419                 LOAD_TEMPLATE("admin_settings_saved", false, SPONSOR_PAYMENT_LIST_IS_EMPTY);
420         }
421
422         // Free memory
423         SQL_FREERESULT($result);
424 }
425
426 //
427 ?>