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