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