541575379cc3caea5ed9b2973ca7974e2b7846eb
[mailer.git] / inc / modules / guest / what-register.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/10/2003 *
4  * ===============                              Last change: 11/26/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-register.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Registration form                                *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Anmeldeformular                                  *
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, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40  elseif ((!EXT_IS_ACTIVE("register")))
41 {
42         if (IS_ADMIN()) {
43                 ADD_FATAL(sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "register"));
44         } else {
45                 ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "register");
46         }
47         return;
48 }
49
50 // Add description as navigation point
51 ADD_DESCR("guest", basename(__FILE__));
52
53 OPEN_TABLE("100%", "guest_content_align", "");
54 global $CONFIG, $DATA;
55
56 // Initialize variables
57 $FAILED = false; $SHORT_PASS = false; $cats = 0; $IP_TIMEOUT = false;
58 if (!isset($_POST['ok']))          unset($_POST['ok']);
59 if (empty($_POST['agree']))        $_POST['agree']        = "";
60 if (empty($_POST['addy']))         $_POST['addy']         = "";
61 if (empty($_POST['surname']))      $_POST['surname']      = "";
62 if (empty($_POST['family_name']))  $_POST['family_name']  = "";
63 if (empty($_POST['pass1']))        $_POST['pass1']        = "";
64 if (empty($_POST['pass2']))        $_POST['pass2']        = "";
65 if (empty($_POST['day']))          $_POST['day']          = "";
66 if (empty($_POST['month']))        $_POST['month']        = "";
67 if (empty($_POST['year']))         $_POST['year']         = "";
68 if (empty($_POST['max_mails']))    $_POST['max_mails']    = "";
69 if (empty($_POST['street_nr']))    $_POST['street_nr']    = "";
70 if (empty($_POST['zip']))          $_POST['zip']          = "";
71 if (empty($_POST['city']))         $_POST['city']         = "";
72 if (empty($_POST['cntry']))        $_POST['cntry']        = "";
73 if (empty($_POST['country_code'])) $_POST['country_code'] = "1";
74
75 if (isset($_POST['ok']))
76 {
77         // First we only check the submitted data then we continue... :)
78         //
79         // Did he agree to our Terms Of Usage?
80         if ($_POST['agree'] != "Y")
81         {
82                 $_POST['agree'] = "!";
83                 $FAILED = true;
84         }
85
86         // Did he enter a valid email address? (we really don't care about
87         // that, he has to click on a confirmation link :P )
88         if ((empty($_POST['addy'])) || (!VALIDATE_EMAIL($_POST['addy'])))
89         {
90                 $_POST['addy'] = "!";
91                 $FAILED = true;
92         }
93
94         // And what about surname and family's name?
95         if (empty($_POST['surname']))
96         {
97                 $_POST['surname'] = "!";
98                 $FAILED = true;
99         }
100         if (empty($_POST['family_name']))
101         {
102                 $_POST['family_name'] = "!";
103                 $FAILED = true;
104         }
105
106         // Check for required fields
107         if (!$FAILED) $FAILED = REGISTER_CHECK_REQUIRED_FIELDS($_POST);
108
109         // Did he enter his password twice?
110         if (((empty($_POST['pass1'])) || (empty($_POST['pass2']))) || (($_POST['pass1'] != $_POST['pass2']) && (!empty($_POST['pass1'])) && (!empty($_POST['pass2']))))
111         {
112                 if (($_POST['pass1'] != $_POST['pass2']) && (!empty($_POST['pass1'])) && (!empty($_POST['pass2'])))
113                 {
114                         $_POST['pass1'] = "!";
115                         $_POST['pass2'] = "!";
116                 }
117                  else
118                 {
119                         if (empty($_POST['pass1'])) { $_POST['pass1'] = "!"; } else { $_POST['pass1'] = ""; }
120                         if (empty($_POST['pass2'])) { $_POST['pass2'] = "!"; } else { $_POST['pass2'] = ""; }
121                 }
122                 $FAILED = true;
123         }
124         // Is the password long enouth?
125         if ((strlen($_POST['pass1']) < $CONFIG['pass_len']) && (!$FAILED) && (!IS_ADMIN()))
126         {
127                 $SHORT_PASS = true;
128                 $FAILED = true;
129         }
130         // Did he select enougth categories?
131         if (!IS_ADMIN())
132         {
133                 // Do this check only when no admin is logged in
134                 foreach ($_POST['cat'] as $id=>$answer)
135                 {
136                         if ($answer == "Y") $cats++;
137                 }
138                 if ($cats < $CONFIG['least_cats'])
139                 {
140                         // ... nope!
141                         $FAILED = true;
142                 }
143         }
144         if (($_POST['addy'] != "!") && ($CONFIG['check_double_email'] == "Y") && (!IS_ADMIN()))
145         {
146                 // Does the email address already exists in our database?
147                 $CHK = SEARCH_EMAIL_USERTAB($_POST['addy']);
148                 if ($CHK) { $_POST['addy'] = "?"; $FAILED = true; }
149         }
150
151         // Check his IP number
152         $to = bigintval(time() - $CONFIG['ip_timeout']);
153         $result = SQL_QUERY_ESC("SELECT joined, last_update FROM "._MYSQL_PREFIX."_user_data WHERE REMOTE_ADDR='%s' AND (joined > %s OR last_update > %s) LIMIT 1",
154          array(getenv('REMOTE_ADDR'), $to, $to), __FILE__, __LINE__);
155         if ((SQL_NUMROWS($result) == 1) && (!IS_ADMIN()))
156         {
157                 // Same IP in timeout range and different email address entered... Eat this, faker! ;-)
158                 // But admins are allowed to fake their own exchange service.
159                 $IP_TIMEOUT = true;
160                 $FAILED = true;
161         }
162
163         // Test the refid (because some strange hackers... :-P)
164         $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1",
165          array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
166         if (SQL_NUMROWS($result) == 0)
167         {
168                 // Not found so we set your refid!
169                 $_POST['refid'] = $CONFIG['def_refid'];
170                 @setcookie("refid", $CONFIG['def_refid'], (time() + $CONFIG['online_timeout']), COOKIE_PATH);
171         }
172
173         // Free memory
174         SQL_FREERESULT($result);
175 }
176
177 if ((isset($_POST['ok'])) && (!$FAILED))
178 {
179         // Save the registration
180         if (strlen($_POST['day'])   == 1) $_POST['day']   = "0".$_POST['day'];
181         if (strlen($_POST['month']) == 1) $_POST['month'] = "0".$_POST['month'];
182
183         // Hash = MM-DD-YYYY:IP:USER_AGENT:TIMEMARK
184         $hash = generateHash($_POST['month']."-".$_POST['day']."-".$_POST['year'].":".getenv('SERVER_NAME').":".getenv('REMOTE_ADDR').":".getenv('HTTP_USER_AGENT').":".time());
185
186         // Add design when extension sql_patches is v0.2.7 or greater
187         $ADD1 = ""; $ADD2 = "";
188         if (GET_EXT_VERSION("sql_patches") >= "0.2.7")
189         {
190                 // Okay, add design here
191                 $ADD1 = ", curr_theme";
192                 $ADD2 = ", '".GET_CURR_THEME()."'";
193         }
194
195         // Check if I shall disable sending mail to newly registered members out about active/begging rallye
196         //
197         // First comes first: begging rallye
198         if (GET_EXT_VERSION("beg") >= "0.1.7")
199         {
200                 // Okay, shall I disable now?
201                 if ($CONFIG['beg_new_mem_notify'] == "N")
202                 {
203                         $ADD1 .= ", beg_ral_notify, beg_ral_en_notify";
204                         $ADD2 .= ", UNIX_TIMESTAMP(), UNIX_TIMESTAMP()";
205                 }
206         }
207
208         // Second: active rallye
209         if (GET_EXT_VERSION("bonus") >= "0.7.7")
210         {
211                 // Okay, shall I disable now?
212                 if ($CONFIG['bonus_new_mem_notify'] == "N")
213                 {
214                         $ADD1 .= ", bonus_ral_notify, bonus_ral_en_notify";
215                         $ADD2 .= ", UNIX_TIMESTAMP(), UNIX_TIMESTAMP()";
216                 }
217         }
218
219         // Write user data to table
220         if (EXT_IS_ACTIVE("country"))
221         {
222                 // Save with new selectable country code
223                 $countryRow = "country_code";
224                 $countryData = bigintval($_POST['country_code']);
225         }
226          else
227         {
228                 // Old way with enterable two-char-code
229                 $countryRow = "country";
230                 $countryData = addslashes(substr($_POST['cntry'], 0, 2));
231         }
232
233         //////////////////////////////
234         // Create user's account... //
235         //////////////////////////////
236         //
237         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_data (sex, surname, family, street_nr, %s, zip, city, email, birth_day, birth_month, birth_year, password, max_mails, receive_mails, refid, status, user_hash, REMOTE_ADDR, joined, last_update".$ADD1.")
238 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', %d, %d, %d, 'UNCONFIRMED', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()".$ADD2.")",
239 array(
240         $countryRow,
241         SQL_ESCAPE(substr($_POST['sex'], 0, 1)),
242         SQL_ESCAPE($_POST['surname']),
243         SQL_ESCAPE($_POST['family_name']),
244         SQL_ESCAPE($_POST['street_nr']),
245         $countryData,
246         bigintval($_POST['zip']),
247         SQL_ESCAPE($_POST['city']),
248         SQL_ESCAPE($_POST['addy']),
249         bigintval($_POST['day']),
250         bigintval($_POST['month']),
251         bigintval($_POST['year']),
252         generateHash($_POST['pass1']),
253         bigintval($_POST['max_mails']),
254         bigintval($_POST['max_mails']),
255         bigintval($_POST['refid']),
256         $hash,
257         getenv('REMOTE_ADDR'),
258 ), __FILE__, __LINE__);
259
260         // Get his userid
261         $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE user_hash='%s' LIMIT 1",
262          array($hash), __FILE__, __LINE__);
263         list($userid) = SQL_FETCHROW($result);
264
265         // Secure userid (we have a little paranoia ;-) )
266         $userid = bigintval($userid);
267
268         // Write his welcome-points
269         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_points WHERE userid=%d AND ref_depth=0 LIMIT 1",
270          array(bigintval($userid)), __FILE__, __LINE__);
271         if (SQL_NUMROWS($result) == 0)
272         {
273                 // Add only when the line was not found (maybe some more secure?)
274                 $locked = "points";
275                 if ($CONFIG['ref_payout'] > 0) $locked = "locked_points"; // Pay him later. First he has to confirm some mails!
276                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_points (userid, ref_depth, ".$locked.") VALUES(%d, 0, '%s')",
277                  array(bigintval($userid), $CONFIG['points_register']), __FILE__, __LINE__);
278
279                 // Update mediadata as well
280                 if ((GET_EXT_VERSION("mediadata") >= "0.0.4") && ($locked == "points")) {
281                         // Update database
282                         MEDIA_UPDATE_ENTRY(array("total_points"), "add", $CONFIG['points_register']);
283                 }
284         }
285
286         // Write catgories
287         if ((is_array($_POST['cat'])) && (count($_POST['cat']))) {
288                 foreach ($_POST['cat'] as $cat=>$joined) {
289                         if ($joined == "Y") {
290                                 // Insert category entry
291                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_cats (userid, cat_id) VALUES (%d, %d)",
292                                  array(bigintval($userid), bigintval($cat)), __FILE__, __LINE__);
293                         }
294                 }
295         }
296
297         // Rewrite sex
298         $sex = TRANSLATE_SEX($_POST['sex']);
299
300         // ... rewrite a zero referral ID to the main title
301         if ($_POST['refid'] == "0") $_POST['refid'] = MAIN_TITLE;
302
303         // Prepare data array for the email template
304         // Start with the salutation...
305         $DATA = array(
306                 'hash'    => $hash,
307                 'uid'     => $userid,
308                 'salut'   => $sex,
309                 'surname' => $_POST['surname'],
310                 'family'  => $_POST['family_name'],
311                 'email'   => $_POST['addy'],
312                 'street'  => $_POST['street_nr'],
313                 'city'    => $_POST['city'],
314                 'zip'     => bigintval($_POST['zip']),
315                 'country' => $countryData,
316                 'refid'   => $_POST['refid'],
317                 'pass'    => $_POST['pass1'],
318         );
319
320         // Continue with birthday...
321         switch (GET_LANGUAGE())
322         {
323         case "de":
324                 $DATA['birthday'] = $_POST['day'].".".$_POST['month'].".".$_POST['year'];
325                 break;
326
327         default:
328                 $DATA['birthday'] = $_POST['month']."/".$_POST['day']."/".$_POST['year'];
329                 break;
330         }
331
332         // Display information to the user that he got mail and send it away
333         $msg_guest = LOAD_EMAIL_TEMPLATE("register-member", $DATA, $userid);
334
335         // Send mail to user (confirmation link!)
336         $EMAIL = $DATA['email'];
337         SEND_EMAIL ($DATA['email'], GUEST_CONFIRM_LINK, $msg_guest);
338         $DATA['email'] = $EMAIL;
339
340         // Send mail to admin
341         if (GET_EXT_VERSION("admins") >= "0.4.1")
342         {
343                 // Use new system
344                 SEND_ADMIN_EMAILS_PRO(ADMIN_NEW_ACCOUNT, "register-admin", $DATA, $userid);
345         }
346          else
347         {
348                 // Use old system
349                 $msg_admin = LOAD_EMAIL_TEMPLATE("register-admin", $DATA, $userid);
350                 SEND_ADMIN_EMAILS (ADMIN_NEW_ACCOUNT, $msg_admin);
351         }
352
353         // Output success registration
354         LOAD_TEMPLATE("admin_settings_saved", false, REGISTRATION_DONE);
355 }
356  else
357 {
358         if ($_POST['agree'] == "!")
359         {
360                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".HAVE_TO_AGREE."</SPAN></STRONG><BR><BR>");
361         }
362         if ($_POST['addy'] == "!")
363         {
364                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".ENTER_EMAIL."</SPAN></STRONG><BR><BR>");
365                 $_POST['addy'] = "";
366         }
367          elseif ($_POST['addy'] == "?")
368         {
369                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".EMAIL_ALREADY_DB."</SPAN></STRONG><BR><BR>");
370                 $_POST['addy'] = "";
371         }
372         if ($_POST['surname'] == "!")
373         {
374                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".ENTER_SURNAME."</SPAN></STRONG><BR><BR>");
375                 $_POST['surname'] = "";
376         }
377         if ($_POST['family_name'] == "!")
378         {
379                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".ENTER_FAMILY."</SPAN></STRONG><BR><BR>");
380                 $_POST['family_name'] = "";
381         }
382         if (($_POST['pass1'] == "!") && ($_POST['pass2'] == "!"))
383         {
384                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".ENTER_BOTH_PASSWORDS."</SPAN></STRONG><BR><BR>");
385         }
386          elseif ($_POST['pass1'] == "!")
387         {
388                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".ENTER_PASS1."</SPAN></STRONG><BR><BR>");
389         }
390          elseif ($_POST['pass2'] == "!")
391         {
392                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".ENTER_PASS2."</SPAN></STRONG><BR><BR>");
393         }
394         if ($SHORT_PASS)
395         {
396                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".SHORT_PASS.": ".$CONFIG['pass_len']."</SPAN></STRONG><BR><BR>");
397         }
398         if ($IP_TIMEOUT)
399         {
400                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".REMOTE_ADDR_TIMEOUT."</SPAN></STRONG><BR><BR>");
401         }
402         if ((!empty($cats)) && ($cats < $CONFIG['least_cats']))
403         {
404                 OUTPUT_HTML ("<STRONG><SPAN class=\"register_failed\">".CATS_LEAST.": ".$CONFIG['least_cats']."</SPAN></STRONG><BR><BR>");
405         }
406
407         // Generate birthday selection
408         switch (GET_LANGUAGE())
409         {
410         case "de": // German date format
411                 define('BIRTHDAY_SELECTION', ADD_SELECTION("day", $_POST['day']).ADD_SELECTION("month", $_POST['month']).ADD_SELECTION("year", $_POST['year']));
412                 break;
413
414         default: // Default is the US date format... :)
415                 break;
416         }
417
418         // Adds a table for the guests with all visible categories
419         define ('CATEGORY_SELECTION', REGISTER_ADD_CATEGORY_TABLE("guest", true));
420
421         // Adds maximum receiveable mails list... :)
422         define ('MAX_RECEIVE_LIST', ADD_MAX_RECEIVE_LIST("guest", "", true));
423
424         // Check if nickname extension is active and get state if nickname is selected or userid
425         $nick = false;
426         if (EXT_IS_ACTIVE("nickname")) $nick = NICKNAME_IS_ACTIVE($GLOBALS['refid']);
427
428         // Is the nickname valid?
429         if (!$nick) {
430                 // Nope, disable it
431                 if (GET_EXT_VERSION("sql_patches") != "") {
432                         // Use default refid
433                         $GLOBALS['refid'] = $CONFIG['def_refid'];
434                 } else {
435                         // Set zero
436                         $GLOBALS['refid'] = 0;
437                 }
438         }
439
440         // Shall I display the refid or shall I make it editable?
441         if ($CONFIG['display_refid'] == "Y") {
442                 // Load template to enter it
443                 define('REFID_CONTENT', LOAD_TEMPLATE("guest_register_refid", true, $GLOBALS['refid']));
444         } else {
445                 // Load "hide" form template
446                 define ('REFID_CONTENT', LOAD_TEMPLATE("guest_register_refid_hide", true, $GLOBALS['refid']));
447         }
448
449         // You may want to modify the register_header.tpl.xx file and not this script when you add your scripts etc. :-)
450         define('REGISTER_HEADER_CONTENT', LOAD_TEMPLATE("register_header", true));
451
452         // Please select at least x categories
453         define('LEAST_CATS_VALUE', $CONFIG['least_cats']);
454
455         // Other values
456         define('__SURNAME', $_POST['surname']);   define('__FAMILY',  $_POST['family_name']);
457         define('__STREET',  $_POST['street_nr']); define('__COUNTRY', $_POST['cntry']);
458         define('__ZIP',     $_POST['zip']);       define('__CITY',    $_POST['city']);
459         define('__ADDY',    $_POST['addy']);
460
461         // Shall I add a counrty selection box or the old input box?
462         if (EXT_IS_ACTIVE("country"))
463         {
464                 // New variant, good!
465                 $OUT  = "<SELECT name=\"country_code\" class=\"guest_select\" size=\"1\">\n";
466                 $WHERE = "WHERE is_active='Y'";
467                 if (IS_ADMIN()) $WHERE = "";
468                 $OUT .= ADD_OPTION_LINES("countries", "id", "descr", $_POST['country_code'], "code", $WHERE);
469                 $OUT .= "</SELECT>";
470                 define('__COUNTRY_CONTENT', $OUT);
471         }
472          else
473         {
474                 // Old out-dated variant
475                 define('__COUNTRY_CONTENT', "<INPUT type=\"text\" name=\"cntry\" class=\"guest_normal\" size=\"2\" maxlength=\"3\" value=\"".__COUNTRY."\">");
476         }
477
478         // Set MUST_??? constants
479         if ((EXT_IS_ACTIVE("register")) && (GET_EXT_VERSION("register") > "0.0")) REGISTER_FILL_MUST_CONSTANTS();
480
481         // Display registration form
482         LOAD_TEMPLATE("guest_register");
483 }
484 CLOSE_TABLE();
485 //
486 ?>