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