9065ccf701edaa5bc5d6800c9c54b79bdd00d78f
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Add description as navigation point
45 addMenuDescription('guest', __FILE__);
46
47 if (!isExtensionActive('register')) {
48         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('register'));
49         return;
50 } elseif (!isExtensionActive('country')) {
51         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('country'));
52         return;
53 }
54
55 // Initialize variables
56 $isOkay     = true;
57 $shortPass  = false;
58 $cats       = '0';
59 $ipTimedOut = false;
60
61 // Default refid is zero
62 setRequestPostElement('refid', 0);
63 if (determineReferalId() > 0) {
64         // Test if the refid is valid
65         if (!fetchUserData(determineReferalId())) {
66                 // Not found so we set your refid!
67                 setRequestPostElement('refid', getConfig('def_refid'));
68                 setSession('refid', getConfig('def_refid'));
69         } else {
70                 // Use the refid here
71                 setRequestPostElement('refid', determineReferalId());
72         }
73 } // END - if
74
75 if (isFormSent()) {
76         // First we only check the submitted data then we continue... :)
77         //
78         // Did he agree to our Terms Of Usage?
79         if (postRequestElement('agree') != 'Y') {
80                 setRequestPostElement('agree', '!');
81                 $isOkay = false;
82         } // END - if
83
84         // Did he enter a valid email address? (we really don't care about
85         // that, he has to click on a confirmation link :P )
86         if ((!isPostRequestElementSet('email')) || (!isEmailValid(postRequestElement('email')))) {
87                 setRequestPostElement('email', '!');
88                 $isOkay = false;
89         } // END - if
90
91         // And what about surname and family's name?
92         if (!isPostRequestElementSet('surname')) {
93                 setRequestPostElement('surname', '!');
94                 $isOkay = false;
95         } // END - if
96         if (!isPostRequestElementSet('family')) {
97                 setRequestPostElement('family', '!');
98                 $isOkay = false;
99         } // END - if
100
101         // Get temporary array for modification
102         $postArray = postRequestArray();
103
104         // Check for required fields
105         if ($isOkay === true) $isOkay = ifRequiredRegisterFieldsAreSet($postArray);
106
107         // Set it back in request
108         setPostRequestArray($postArray);
109
110         // Did he enter his password twice?
111         if (((!isPostRequestElementSet('pass1')) || (!isPostRequestElementSet('pass2'))) || ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2')))) {
112                 if ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2'))) {
113                         setRequestPostElement('pass1', '!');
114                         setRequestPostElement('pass2', '!');
115                 } else {
116                         if (!isPostRequestElementSet('pass1')) { setRequestPostElement('pass1', '!'); } else { setRequestPostElement('pass1', ''); }
117                         if (!isPostRequestElementSet('pass2')) { setRequestPostElement('pass2', '!'); } else { setRequestPostElement('pass2', ''); }
118                 }
119                 $isOkay = false;
120         } // END - if
121
122         // Is the password long enouth?
123         if ((strlen(postRequestElement('pass1')) < getConfig('pass_len')) && ($isOkay === true)) {
124                 $shortPass = true;
125                 $isOkay = false;
126         } // END - if
127
128         // No admin? Admins can always register!
129         if (!isAdmin()) {
130                 // Do this check only when no admin is logged in
131                 foreach (postRequestElement('cat') as $id => $answer) {
132                         if ($answer == 'Y') $cats++;
133                 } // END - foreach
134
135                 if ($cats < getConfig('least_cats')) {
136                         // ... nope!
137                         $isOkay = false;
138                 } // END - if
139         } // END - if
140
141         if ((postRequestElement('email') != '!') && (getConfig('check_double_email') == 'Y')) {
142                 // Does the email address already exists in our database?
143                 $CHK = isEmailTaken(postRequestElement('email'));
144                 if ($CHK === true) {
145                         setRequestPostElement('email', '?');
146                         $isOkay = false;
147                 } // END - if
148         } // END - if
149
150         // Check for IP timeout?
151         if (getConfig('ip_timeout') > 0) {
152                 // Check his IP number
153                 $result = SQL_QUERY_ESC("SELECT
154         `userid`
155 FROM
156         `{?_MYSQL_PREFIX?}_user_data`
157 WHERE
158         `REMOTE_ADDR`='%s' AND
159         (`joined` > (UNIX_TIMESTAMP() - {?ip_timeout?}) OR `last_update` > (UNIX_TIMESTAMP() - {?ip_timeout?}))
160 LIMIT 1",
161                         array(detectRemoteAddr()), __FILE__, __LINE__);
162                 if (SQL_NUMROWS($result) == 1) {
163                         // Same IP in timeout range and different email address entered... Eat this, faker! ;-)
164                         // But admins are allowed to fake their own exchange service.
165                         $ipTimedOut = true;
166                         $isOkay = false;
167                 } // END - if
168
169                 // Free memory
170                 SQL_FREERESULT($result);
171         } // END - if
172 } // END - if
173
174 // Is the form sent and all went fine or admin logged in?
175 //* DEBUG: */ print intval(isFormSent()).'/'.intval($isOkay).'/'.intval(isAdmin()).'<br />';
176 if ((isFormSent()) && (($isOkay === true) || (isAdmin()))) {
177         // Prepapre month and day of birth
178         if (strlen(postRequestElement('day'))   == 1) setRequestPostElement('day'  , 0 . postRequestElement('day'));
179         if (strlen(postRequestElement('month')) == 1) setRequestPostElement('month', 0 . postRequestElement('month'));
180
181         // Get total ...
182         // ... confirmed, ...
183         $confirmedUsers   = countSumTotalData('CONFIRMED'  , 'user_data', 'userid', 'status', true);
184         // ... unconfirmed ...
185         $unconfirmedUsers = countSumTotalData('UNCONFIRMED', 'user_data', 'userid', 'status', true);
186         // ... and locked users!
187         $lockedUsers      = countSumTotalData('LOCKED'     , 'user_data', 'userid', 'status', true);
188
189         // Generate hash which will be inserted into confirmation mail
190         $hash = generateHash(sha1($confirmedUsers.getConfig('ENCRYPT_SEPERATOR').$unconfirmedUsers.getConfig('ENCRYPT_SEPERATOR').$lockedUsers.getConfig('ENCRYPT_SEPERATOR').postRequestElement('month') . '-'.postRequestElement('day') . '-'.postRequestElement('year').getConfig('ENCRYPT_SEPERATOR').getenv('SERVER_NAME').getConfig('ENCRYPT_SEPERATOR').detectRemoteAddr().getConfig('ENCRYPT_SEPERATOR').detectUserAgent() . '/' . getConfig('SITE_KEY') . '/' . getConfig('DATE_KEY') . '/'. getConfig('CACHE_BUSTER')));
191
192         // Add design when extension sql_patches is v0.2.7 or greater
193         // @TODO Rewrite these all to a single filter
194         $ADD1 = '';
195         $ADD2 = '';
196         if (isExtensionInstalledAndNewer('theme', '0.0.8')) {
197                 // Okay, add design here
198                 $ADD1 = ', `curr_theme`';
199                 $ADD2 = ", '".getCurrentTheme()."'";
200         } // END - if
201
202         // Check if I shall disable sending mail to newly registered members out about active/begging rallye
203         //
204         // First comes first: begging rallye
205         if (isExtensionInstalledAndNewer('beg', '0.1.7')) {
206                 // Okay, shall I disable now?
207                 if (getConfig('beg_new_mem_notify') != 'Y') {
208                         $ADD1 .= ', `beg_ral_notify`, `beg_ral_en_notify`';
209                         $ADD2 .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
210                 } // END - if
211         } // END - if
212
213         // Second: active rallye
214         if (isExtensionInstalledAndNewer('bonus', '0.7.7')) {
215                 // Okay, shall I disable now?
216                 if (getConfig('bonus_new_mem_notify') != 'Y') {
217                         $ADD1 .= ', `bonus_ral_notify`, `bonus_ral_en_notify`';
218                         $ADD2 .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
219                 } // END - if
220         } // END - if
221
222         // Write user data to table
223         if (isExtensionActive('country')) {
224                 // Save with new selectable country code
225                 $countryRow = '`country_code`';
226                 $countryData = bigintval(postRequestElement('country_code'));
227         } else {
228                 // Old way with enterable two-char-code
229                 $countryRow = '`country`';
230                 $countryData = substr(postRequestElement('cntry'), 0, 2);
231         }
232
233         //////////////////////////////
234         // Create user's account... //
235         //////////////////////////////
236         //
237         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.")
238 VALUES ('%s','%s','%s','%s','%s',%s,'%s','%s',%s, %s,%s,'%s',%s, %s,'%s','UNCONFIRMED','%s','%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()".$ADD2.")",
239         array(
240                 $countryRow,
241                 substr(postRequestElement('gender'), 0, 1),
242                 postRequestElement('surname'),
243                 postRequestElement('family'),
244                 postRequestElement('street_nr'),
245                 $countryData,
246                 bigintval(postRequestElement('zip')),
247                 postRequestElement('city'),
248                 postRequestElement('email'),
249                 bigintval(postRequestElement('day')),
250                 bigintval(postRequestElement('month')),
251                 bigintval(postRequestElement('year')),
252                 generateHash(postRequestElement('pass1')),
253                 bigintval(postRequestElement('max_mails')),
254                 bigintval(postRequestElement('max_mails')),
255                 bigintval(postRequestElement('refid')),
256                 $hash,
257                 detectRemoteAddr(),
258         ), __FILE__, __LINE__);
259
260         // Get his userid
261         $userid = SQL_INSERTID();
262
263         // Did this work?
264         if ($userid == '0') {
265                 // Something bad happened!
266                 loadTemplate('admin_settings_saved', false, getMessage('USER_NOT_REGISTERED'));
267                 return;
268         } // END - if
269
270         // Is the refback extension there?
271         // @TODO Rewrite this to a filter
272         if (isExtensionActive('refback')) {
273                 // Update refback table
274                 updateRefbackTable($userid);
275         } // END - if
276
277         // Write his welcome-points
278         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_points` WHERE `userid`=%s AND `ref_depth`=0 LIMIT 1",
279                 array(bigintval($userid)), __FILE__, __LINE__);
280         if (SQL_NUMROWS($result) == '0') {
281                 // Add only when the line was not found (maybe some more secure?)
282                 $locked = 'points';
283
284                 // Pay him later. First he has to confirm some mails!
285                 if (getConfig('ref_payout') > 0) $locked = 'locked_points';
286
287                 // @TODO Rewrite this to addPointsDirectly()
288                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `".$locked."`) VALUES (%s,0,'{?points_register?}')",
289                         array(bigintval($userid)), __FILE__, __LINE__);
290
291                 // Update mediadata as well
292                 if ((isExtensionInstalledAndNewer('mediadata', '0.0.4')) && ($locked == 'points')) {
293                         // Update database
294                         updateMediadataEntry(array('total_points'), 'add', getConfig('points_register'));
295                 } // END - if
296         } // END - if
297
298         // Write catgories
299         if ((is_array(postRequestElement('cat'))) && (count(postRequestElement('cat')))) {
300                 foreach (postRequestElement('cat') as $cat => $joined) {
301                         if ($joined == 'Y') {
302                                 // Insert category entry
303                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (userid, cat_id) VALUES (%s, %s)",
304                                 array(bigintval($userid), bigintval($cat)), __FILE__, __LINE__);
305                         } // END - if
306                 } // END - foreach
307         } // END - if
308
309         // Rewrite gender
310         $gender = translateGender(postRequestElement('gender'));
311
312         // ... rewrite a zero referal id to the main title
313         if (postRequestElement('refid') == '0') setRequestPostElement('refid', getConfig('MAIN_TITLE'));
314
315         // Is ZIP code set?
316         if (isPostRequestElementSet('zip')) {
317                 // Prepare data array for the email template
318                 // Start with the gender...
319                 $content = array(
320                         'hash'    => $hash,
321                         'userid'  => $userid,
322                         'gender'  => $gender,
323                         'surname' => SQL_ESCAPE(postRequestElement('surname')),
324                         'family'  => SQL_ESCAPE(postRequestElement('family')),
325                         'email'   => SQL_ESCAPE(postRequestElement('email')),
326                         'street'  => SQL_ESCAPE(postRequestElement('street_nr')),
327                         'city'    => SQL_ESCAPE(postRequestElement('city')),
328                         'zip'     => bigintval(postRequestElement('zip')),
329                         'country' => $countryData,
330                         'refid'   => SQL_ESCAPE(postRequestElement('refid')),
331                         'pass'    => SQL_ESCAPE(postRequestElement('pass1')),
332                 );
333         } else {
334                 // No ZIP code entered
335                 $content = array(
336                         'hash'    => $hash,
337                         'userid'  => $userid,
338                         'gender'  => $gender,
339                         'surname' => SQL_ESCAPE(postRequestElement('surname')),
340                         'family'  => SQL_ESCAPE(postRequestElement('family')),
341                         'email'   => SQL_ESCAPE(postRequestElement('email')),
342                         'street'  => SQL_ESCAPE(postRequestElement('street_nr')),
343                         'city'    => SQL_ESCAPE(postRequestElement('city')),
344                         'zip'     => '',
345                         'country' => $countryData,
346                         'refid'   => SQL_ESCAPE(postRequestElement('refid')),
347                         'pass'    => SQL_ESCAPE(postRequestElement('pass1')),
348                 );
349         }
350
351         // Continue with birthday...
352         switch (getLanguage()) {
353                 case 'de':
354                         $content['birthday'] = bigintval(postRequestElement('day')) . '.' . bigintval(postRequestElement('month')) . '.' . bigintval(postRequestElement('year'));
355                         break;
356
357                 default:
358                         $content['birthday'] = bigintval(postRequestElement('month')) . '/' . bigintval(postRequestElement('day')) . '/' . bigintval(postRequestElement('year'));
359                         break;
360         } // END - switch
361
362         // Display information to the user that he got mail and send it away
363         $messageGuest = loadEmailTemplate('register-member', $content, $userid);
364
365         // Send mail to user (confirmation link!)
366         $email = $content['email'];
367         sendEmail($content['email'], getMessage('GUEST_SUBJECT_CONFIRM_LINK'), $messageGuest);
368         $content['email'] = $email;
369
370         // Send mail to admin
371         sendAdminNotification(getMessage('ADMIN_SUBJECT_NEW_ACCOUNT'), 'register-admin', $content, $userid);
372
373         // Output success registration
374         loadTemplate('admin_settings_saved', false, getMessage('REGISTRATION_DONE'));
375 } else {
376         if (postRequestElement('agree') == '!') {
377                 registerOutputFailedMessage('HAVE_TO_AGREE');
378         } // END - if
379
380         if (postRequestElement('email') == '!') {
381                 registerOutputFailedMessage('ENTER_EMAIL');
382                 setRequestPostElement('email', '');
383         } elseif (postRequestElement('email') == '?') {
384                 registerOutputFailedMessage('EMAIL_ALREADY_DB');
385                 setRequestPostElement('email', '');
386         }
387
388         if (postRequestElement('surname') == '!') {
389                 registerOutputFailedMessage('ENTER_SURNAME');
390                 setRequestPostElement('surname', '');
391         } // END - if
392
393         if (postRequestElement('family') == '!') {
394                 registerOutputFailedMessage('ENTER_FAMILY');
395                 setRequestPostElement('family', '');
396         } // END - if
397
398         if ((postRequestElement('pass1') == '!') && (postRequestElement('pass2') == '!')) {
399                 registerOutputFailedMessage('ENTER_BOTH_PASSWORDS');
400         } elseif (postRequestElement('pass1') == '!') {
401                 registerOutputFailedMessage('ENTER_PASS1');
402         } elseif (postRequestElement('pass2') == '!') {
403                 registerOutputFailedMessage('ENTER_PASS2');
404         }
405
406         if ($shortPass === true) {
407                 registerOutputFailedMessage('SHORT_PASS', ": ".getConfig('pass_len'));
408         } // END - if
409
410         if ($ipTimedOut === true) {
411                 registerOutputFailedMessage('REMOTE_ADDR_TIMEOUT');
412         } // END - if
413
414         if ((!empty($cats)) && ($cats < getConfig('least_cats'))) {
415                 registerOutputFailedMessage(sprintf(getMessage('CATS_LEAST'), getConfig('least_cats')));
416         } // END - if
417
418         // Generate birthday selection
419         switch (getLanguage()) {
420                 case 'de': // German date format
421                         $content['birthday_selection'] = addSelectionBox('day', postRequestElement('day')).addSelectionBox('month', postRequestElement('month')).addSelectionBox('year', postRequestElement('year'));
422                         break;
423
424                 default: // Default is the US date format... :)
425                         $content['birthday_selection'] = addSelectionBox('month', postRequestElement('month')).addSelectionBox('day', postRequestElement('day')).addSelectionBox('year', postRequestElement('year'));
426                         break;
427         }
428
429         // Adds a table for the guests with all visible categories
430         $content['category_selection'] = registerGenerateCategoryTable('guest', true);
431
432         // Adds maximum receiveable mails list... :)
433         $content['max_receive_list'] = addMaxReceiveList('guest', '', true);
434
435         // Shall I display the refid or shall I make it editable?
436         if (getConfig('display_refid') == 'Y') {
437                 // Load "hide" form template
438                 $content['refid_content'] = loadTemplate('guest_register_refid_hide', true, determineReferalId());
439         } else {
440                 // Load template to enter it
441                 $content['refid_content'] = loadTemplate('guest_register_refid', true, determineReferalId());
442         }
443
444         // You may want to modify the register_header.tpl.xx file and not this script when you add your scripts etc. :-)
445         $content['header_content'] = loadTemplate('register_header', true);
446
447         // Other values
448         $content['surname']   = SQL_ESCAPE(postRequestElement('surname'));
449         $content['family']    = SQL_ESCAPE(postRequestElement('family'));
450         $content['street_nr'] = SQL_ESCAPE(postRequestElement('street_nr'));
451         $content['zip']       = bigintval(postRequestElement('zip'));
452         $content['city']      = SQL_ESCAPE(postRequestElement('city'));
453         $content['email']      = SQL_ESCAPE(postRequestElement('email'));
454
455         // Prepare country selection box
456         $OUT  = "<select name=\"country_code\" class=\"guest_select\" size=\"1\">\n";
457         $whereStatement = "WHERE `is_active`='Y'";
458         if (isAdmin()) $whereStatement = '';
459         $OUT .= generateOptionList('countries', 'id', 'descr', postRequestElement('country_code'), 'code', $whereStatement);
460         $OUT .= "</select>";
461         $content['country'] = $OUT;
462
463         // Set must-fillout fields
464         $content = runFilterChain('register_must_fillout', $content);
465
466         // Display registration form
467         loadTemplate('guest_register', false, $content);
468 }
469
470 // [EOF]
471 ?>