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