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