Mahor rewrite:
[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']) < getConfig('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                 // Homepage URL is empty
198                 $FORM_ERRORS[] = SPONSOR_TERMS_NOT_ACCEPTED;
199         }
200         
201         // If there is something wrong/missing stop registration
202         if (count($FORM_ERRORS) > 0) unset($_POST['ok']);
203 }
204
205 if ((isset($_POST['ok'])) && (count($FORM_ERRORS) == 0)) {
206         // Generate message array
207         $MSGs = array(
208                 'failed' => SPONSOR_REGISTRATION_FAILED,
209                 'added'  => SPONSOR_REGISTRATION_COMPLETED,
210         );
211
212         // Calulate points
213         $result = SQL_QUERY_ESC("SELECT (pay_rate * pay_min_count) AS points, pay_min_count AS pay, pay_currency AS curr
214 FROM "._MYSQL_PREFIX."_sponsor_paytypes
215 WHERE id='%s' LIMIT 1", array($_POST['pay_type']), __FILE__, __LINE__);
216         list($points, $pay, $curr) = SQL_FETCHROW($result);
217
218         // Free memory
219         SQL_FREERESULT($result);
220
221         // Add points to array
222         $_POST['points_amount'] = $points;
223         $_POST['points_used']   = "0.00000";
224         $_POST['last_pay']      = $pay;
225         $_POST['last_curr']     = $curr;
226
227         // Register sponsor but never ever update here!
228         $STATUS = SPONSOR_HANDLE_SPONSOR($_POST, true, $MSGs, true);
229
230         // Check the status of the registration process
231         switch ($STATUS)
232         {
233         case "added": // Sponsor successfully added with account status = UNCONFIRMED!
234                 // Check for his ID number
235                 $result = SQL_QUERY_ESC("SELECT id, hash FROM "._MYSQL_PREFIX."_sponsor_data WHERE email='%s' LIMIT 1",
236                  array($_POST['email']), __FILE__, __LINE__);
237                 if (SQL_NUMROWS($result) == 1)
238                 {
239                         // ID found so let's load it for the confirmation email
240                         list($id, $hash) = SQL_FETCHROW($result);
241
242                         // Prepare data for the email template
243                         define('__ID'       , $id);
244                         define('__HASH'     , $hash);
245                         define('__EMAIL'    , $_POST['email']);
246                         define('__SURNAME'  , $_POST['surname']);
247                         define('__FAMILY'   , $_POST['family']);
248                         define('__GENDER'    , TRANSLATE_GENDER($_POST['gender']));
249                         define('__TIMESTAMP', MAKE_DATETIME(time(), 0));
250                         define('__PASSWORD' , $_POST['pass1']);
251
252                         // Generate email and send it to the new sponsor
253                         $EMAIL_MSG = LOAD_EMAIL_TEMPLATE("sponsor_confirm", $hash);
254                         SEND_EMAIL($_POST['email'], SPONSOR_PLEASE_CONFIRM_SUBJ, $EMAIL_MSG);
255
256                         // Send mail to admin
257                         SEND_ADMIN_NOTIFICATION(ADMIN_NEW_SPONSOR, "admin_sponsor_reg", $hash);
258
259                         // Output message: DONE
260                         $MSG = $MSGs['added'];
261                 }
262                  else
263                 {
264                         // Sponsor account not found???
265                         $MSG = SPONSOR_EMAIL_404_1.$_POST['email'].SPONSOR_EMAIL_404_2;
266                 }
267
268                 // Free memory
269                 SQL_FREERESULT($result);
270                 break;
271
272         default:
273                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown status %s detected.", $STATUS));
274                 if (!IS_ADMIN()) {
275                         // Message for testing admin
276                         $MSG = ADMIN_SPONSOR_UNKOWN_STATUS_1.$STATUS.ADMIN_SPONSOR_UNKOWN_STATUS_2;
277                 } else {
278                         // Message for the guest
279                         $MSG = SPONSOR_UNKOWN_STATUS_1.$STATUS.SPONSOR_UNKOWN_STATUS_2;
280                 }
281                 break;
282         }
283
284         // Display message
285         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
286 } else {
287         // Check for payment types
288         $result = SQL_QUERY("SELECT id, pay_name, pay_rate, pay_currency, pay_min_count
289 FROM "._MYSQL_PREFIX."_sponsor_paytypes
290 ORDER BY pay_name", __FILE__, __LINE__);
291         if (SQL_NUMROWS($result) > 0) {
292                 // Load all types...
293                 $OUT = "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left2 right2 top2\">\n";
294                 $SW = 2;
295                 while(list($id, $name, $rate, $curr, $min) = SQL_FETCHROW($result))
296                 {
297                         $OUT .= "<TR>
298   <TD class=\"bottom2 switch_sw".$SW."\" height=\"50\" style=\"padding-left: 2px; padding-right: 2px\">
299     <INPUT type=\"radio\" name=\"pay_type\" class=\"guest_normal\" value=\"".bigintval($id)."\">&nbsp;".$name."<br />
300     <DIV align=\"right\">(".$min." ".$curr." = ".TRANSLATE_COMMA($rate * $min)." ".POINTS.")</DIV>
301   </TD>
302 </TR>\n";
303                         $SW = 3 - $SW;
304                 }
305                 $OUT .= "</TABLE>\n";
306                 define('__PAYMENT_LIST', $OUT);
307                 SQL_FREERESULT($result);
308
309                 // Check for invalid entries
310                 if (count($FORM_ERRORS) > 0)
311                 {
312                         // Some found... :-(
313                         define('__COMPANY'  , COMPILE_CODE($_POST['company']));
314                         define('__POSITION' , COMPILE_CODE($_POST['position']));
315                         define('__TAX_IDENT', COMPILE_CODE($_POST['tax_ident']));
316                         define('__SURNAME'  , COMPILE_CODE($_POST['surname']));
317                         define('__FAMILY'   , COMPILE_CODE($_POST['family']));
318                         define('__STREET1'  , COMPILE_CODE($_POST['street_nr1']));
319                         define('__STREET2'  , COMPILE_CODE($_POST['street_nr2']));
320                         define('__COUNTRY'  , COMPILE_CODE($_POST['country']));
321                         define('__ZIP'      , COMPILE_CODE($_POST['zip']));
322                         define('__CITY'     , COMPILE_CODE($_POST['city']));
323                         define('__PHONE'    , COMPILE_CODE($_POST['phone']));
324                         define('__FAX'      , COMPILE_CODE($_POST['fax']));
325                         define('__CELL'     , COMPILE_CODE($_POST['cell']));
326                         define('__EMAIL'    , COMPILE_CODE($_POST['email']));
327                         define('__URL'      , COMPILE_CODE($_POST['url']));
328
329                         // Check for gender selection
330                         switch ($_POST['gender'])
331                         {
332                         case "M": // Male
333                                 define('__GENDER_M'  , " selected=\"selected\"");
334                                 define('__GENDER_F'  , "");
335                                 define('__GENDER_C'  , "");
336                                 break;
337
338                         case "F": // Female
339                                 define('__GENDER_M'  , "");
340                                 define('__GENDER_F'  , " selected=\"selected\"");
341                                 define('__GENDER_C'  , "");
342                                 break;
343
344                         case "C": // Company
345                                 define('__GENDER_M'  , "");
346                                 define('__GENDER_F'  , "");
347                                 define('__GENDER_C'  , " selected=\"selected\"");
348                                 break;
349                         }
350
351                         // Check for receive_warnings
352                         switch ($_POST['receive_warnings'])
353                         {
354                         case "Y":
355                                 define('__REC_Y'    , " selected=\"selected\"");
356                                 define('__REC_N'    , "");
357                                 break;
358
359                         case "N":
360                                 define('__REC_Y'    , "");
361                                 define('__REC_N'    , " selected=\"selected\"");
362                                 break;
363                         }
364
365                         // Errors found
366                         $OUT = "<P align=\"center\">
367   <STRONG><FONT class=\"guest_failed\">".SPONSOR_REGISTER_SOMETHING_MISSING.":</FONT></STRONG><br />
368 </P>
369 <OL>\n";
370                         foreach ($FORM_ERRORS as $error) {
371                                 $OUT .= "<LI>".$error."</LI>\n";
372                         } // END - if
373
374                         $OUT .= "</OL><br />\n";
375                         define('__SPONSOR_FORM_ERRORS', $OUT);
376                         define('__SPONSOR_REFID', $_POST['refid']);
377                 } else {
378                         // None found, first call
379                         define('__COMPANY'  , "");
380                         define('__POSITION' , "");
381                         define('__TAX_IDENT', "");
382                         define('__GENDER_M'  , "");
383                         define('__GENDER_F'  , "");
384                         define('__GENDER_C'  , "");
385                         define('__SURNAME'  , "");
386                         define('__FAMILY'   , "");
387                         define('__STREET1'  , "");
388                         define('__STREET2'  , "");
389                         define('__COUNTRY'  , "");
390                         define('__ZIP'      , "");
391                         define('__CITY'     , "");
392                         define('__PHONE'    , "");
393                         define('__FAX'      , "");
394                         define('__CELL'     , "");
395                         define('__EMAIL'    , "");
396                         define('__URL'      , "");
397                         define('__REC_Y'    , "");
398                         define('__REC_N'    , "");
399
400                         // No errors found so far
401                         define('__SPONSOR_FORM_ERRORS', "");
402                 }
403
404                 // Prepare referal things
405                 if (!isset($_GET['refid'])) {
406                         // No referal link
407                         define('__SPONSOR_REFID', "0");
408                 } else  {
409                         // Referal ID transmitted, we don't care here if it is right or not
410                         define('__SPONSOR_REFID', bigintval($_GET['refid']));
411                 }
412
413                 // Display registration form
414                 LOAD_TEMPLATE("guest_sponsor_reg");
415         } else {
416                 // Nothing added so far
417                 LOAD_TEMPLATE("admin_settings_saved", false, SPONSOR_PAYMENT_LIST_IS_EMPTY);
418         }
419
420         // Free memory
421         SQL_FREERESULT($result);
422 }
423
424 //
425 ?>