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