7b8b0f389e1e27f4a146afa4f939aa85e7797f6e
[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 }
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['gender']))
51         {
52                 // Surname is empty
53                 $FORM_ERRORS[] = SPONSOR_NO_GENDER_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('__GENDER'    , TRANSLATE_GENDER($_POST['gender']));
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                         SEND_ADMIN_NOTIFICATION(ADMIN_NEW_SPONSOR, "admin_sponsor_reg", $hash);
267
268                         // Output message: DONE
269                         $MSG = $MSGs['added'];
270                 }
271                  else
272                 {
273                         // Sponsor account not found???
274                         $MSG = SPONSOR_EMAIL_404_1.$_POST['email'].SPONSOR_EMAIL_404_2;
275                 }
276
277                 // Free memory
278                 SQL_FREERESULT($result);
279                 break;
280
281         default:
282                 if (!IS_ADMIN())
283                 {
284                         // Message for testing admin
285                         $MSG = ADMIN_SPONSOR_UNKOWN_STATUS_1.$STATUS.ADMIN_SPONSOR_UNKOWN_STATUS_2;
286                 }
287                  else
288                 {
289                         // Message for the guest
290                         $MSG = SPONSOR_UNKOWN_STATUS_1.$STATUS.SPONSOR_UNKOWN_STATUS_2;
291                 }
292                 break;
293         }
294
295         // Display message
296         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
297 }
298  else
299 {
300         // Check for payment types
301         $result = SQL_QUERY("SELECT id, pay_name, pay_rate, pay_currency, pay_min_count
302 FROM "._MYSQL_PREFIX."_sponsor_paytypes
303 ORDER BY pay_name", __FILE__, __LINE__);
304         if (SQL_NUMROWS($result) > 0)
305         {
306                 // Load all types...
307                 $OUT = "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left2 right2 top2\">\n";
308                 $SW = 2;
309                 while(list($id, $name, $rate, $curr, $min) = SQL_FETCHROW($result))
310                 {
311                         $OUT .= "<TR>
312   <TD class=\"bottom2 switch_sw".$SW."\" height=\"50\" style=\"padding-left: 2px; padding-right: 2px\">
313     <INPUT type=\"radio\" name=\"pay_type\" class=\"guest_normal\" value=\"".bigintval($id)."\">&nbsp;".$name."<br />
314     <DIV align=\"right\">(".$min." ".$curr." = ".TRANSLATE_COMMA($rate * $min)." ".POINTS.")</DIV>
315   </TD>
316 </TR>\n";
317                         $SW = 3 - $SW;
318                 }
319                 $OUT .= "</TABLE>\n";
320                 define('__PAYMENT_LIST', $OUT);
321                 SQL_FREERESULT($result);
322
323                 // Check for invalid entries
324                 if (count($FORM_ERRORS) > 0)
325                 {
326                         // Some found... :-(
327                         define('__COMPANY'  , COMPILE_CODE($_POST['company']));
328                         define('__POSITION' , COMPILE_CODE($_POST['position']));
329                         define('__TAX_IDENT', COMPILE_CODE($_POST['tax_ident']));
330                         define('__SURNAME'  , COMPILE_CODE($_POST['surname']));
331                         define('__FAMILY'   , COMPILE_CODE($_POST['family']));
332                         define('__STREET1'  , COMPILE_CODE($_POST['street_nr1']));
333                         define('__STREET2'  , COMPILE_CODE($_POST['street_nr2']));
334                         define('__COUNTRY'  , COMPILE_CODE($_POST['country']));
335                         define('__ZIP'      , COMPILE_CODE($_POST['zip']));
336                         define('__CITY'     , COMPILE_CODE($_POST['city']));
337                         define('__PHONE'    , COMPILE_CODE($_POST['phone']));
338                         define('__FAX'      , COMPILE_CODE($_POST['fax']));
339                         define('__CELL'     , COMPILE_CODE($_POST['cell']));
340                         define('__EMAIL'    , COMPILE_CODE($_POST['email']));
341                         define('__URL'      , COMPILE_CODE($_POST['url']));
342
343                         // Check for gender selection
344                         switch ($_POST['gender'])
345                         {
346                         case "M": // Male
347                                 define('__GENDER_M'  , " selected=\"selected\"");
348                                 define('__GENDER_F'  , "");
349                                 define('__GENDER_C'  , "");
350                                 break;
351
352                         case "F": // Female
353                                 define('__GENDER_M'  , "");
354                                 define('__GENDER_F'  , " selected=\"selected\"");
355                                 define('__GENDER_C'  , "");
356                                 break;
357
358                         case "C": // Company
359                                 define('__GENDER_M'  , "");
360                                 define('__GENDER_F'  , "");
361                                 define('__GENDER_C'  , " selected=\"selected\"");
362                                 break;
363                         }
364
365                         // Check for receive_warnings
366                         switch ($_POST['receive_warnings'])
367                         {
368                         case "Y":
369                                 define('__REC_Y'    , " selected=\"selected\"");
370                                 define('__REC_N'    , "");
371                                 break;
372
373                         case "N":
374                                 define('__REC_Y'    , "");
375                                 define('__REC_N'    , " selected=\"selected\"");
376                                 break;
377                         }
378
379                         // Errors found
380                         $OUT = "<P align=\"center\">
381   <STRONG><FONT class=\"guest_failed\">".SPONSOR_REGISTER_SOMETHING_MISSING.":</FONT></STRONG><br />
382 </P>
383 <OL>\n";
384                         foreach ($FORM_ERRORS as $error)
385                         {
386                                 $OUT .= "<LI>".$error."</LI>\n";
387                         }
388                         $OUT .= "</OL><br />\n";
389                         define('__SPONSOR_FORM_ERRORS', $OUT);
390                         define('__SPONSOR_REFID', $_POST['refid']);
391                 }
392                  else
393                 {
394                         // None found, first call
395                         define('__COMPANY'  , "");
396                         define('__POSITION' , "");
397                         define('__TAX_IDENT', "");
398                         define('__GENDER_M'  , "");
399                         define('__GENDER_F'  , "");
400                         define('__GENDER_C'  , "");
401                         define('__SURNAME'  , "");
402                         define('__FAMILY'   , "");
403                         define('__STREET1'  , "");
404                         define('__STREET2'  , "");
405                         define('__COUNTRY'  , "");
406                         define('__ZIP'      , "");
407                         define('__CITY'     , "");
408                         define('__PHONE'    , "");
409                         define('__FAX'      , "");
410                         define('__CELL'     , "");
411                         define('__EMAIL'    , "");
412                         define('__URL'      , "");
413                         define('__REC_Y'    , "");
414                         define('__REC_N'    , "");
415
416                         // No errors found so far
417                         define('__SPONSOR_FORM_ERRORS', "");
418                 }
419
420                 // Prepare referral things
421                 if (!isset($_GET['refid']))
422                 {
423                         // No referral link
424                         define('__SPONSOR_REFID', "0");
425                 }
426                  else
427                 {
428                         // Referral ID transmitted, we don't care here if it is right or not
429                         define('__SPONSOR_REFID', bigintval($_GET['refid']));
430                 }
431
432                 // Display registration form
433                 LOAD_TEMPLATE("guest_sponsor_reg");
434         }
435          else
436         {
437                 // Nothing added so far
438                 LOAD_TEMPLATE("admin_settings_saved", false, SPONSOR_PAYMENT_LIST_IS_EMPTY);
439         }
440
441         // Free memory
442         SQL_FREERESULT($result);
443 }
444
445 //
446 ?>