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