Rewrite of all mail templates with user data to new 'tag-like' functionality
[mailer.git] / inc / modules / guest / what-register.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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                 if ((!isAdmin()) && (isEmailTaken(postRequestElement('email')))) {
144                         setRequestPostElement('email', '?');
145                         $isOkay = false;
146                 } // END - if
147         } // END - if
148
149         // Check for IP timeout?
150         if (getConfig('ip_timeout') > 0) {
151                 // Check his IP number
152                 $result = SQL_QUERY_ESC("SELECT
153         `userid`
154 FROM
155         `{?_MYSQL_PREFIX?}_user_data`
156 WHERE
157         `REMOTE_ADDR`='%s' AND
158         (`joined` > (UNIX_TIMESTAMP() - {?ip_timeout?}) OR `last_update` > (UNIX_TIMESTAMP() - {?ip_timeout?}))
159 LIMIT 1",
160                         array(detectRemoteAddr()), __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                         $ipTimedOut = true;
165                         $isOkay = false;
166                 } // END - if
167
168                 // Free memory
169                 SQL_FREERESULT($result);
170         } // END - if
171 } // END - if
172
173 // Is the form sent and all went fine or admin logged in?
174 //* DEBUG: */ print intval(isFormSent()).'/'.intval($isOkay).'/'.intval(isAdmin()).'<br />';
175 if ((isFormSent()) && (($isOkay === true) || (isAdmin()))) {
176         // Prepapre month and day of birth
177         if (strlen(postRequestElement('day'))   == 1) setRequestPostElement('day'  , 0 . postRequestElement('day'));
178         if (strlen(postRequestElement('month')) == 1) setRequestPostElement('month', 0 . postRequestElement('month'));
179
180         // Get total ...
181         // ... confirmed, ...
182         $confirmedUsers   = countSumTotalData('CONFIRMED'  , 'user_data', 'userid', 'status', true);
183         // ... unconfirmed ...
184         $unconfirmedUsers = countSumTotalData('UNCONFIRMED', 'user_data', 'userid', 'status', true);
185         // ... and locked users!
186         $lockedUsers      = countSumTotalData('LOCKED'     , 'user_data', 'userid', 'status', true);
187
188         // Generate hash which will be inserted into confirmation mail
189         $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')));
190
191         // Add design when extension sql_patches is v0.2.7 or greater
192         // @TODO Rewrite these all to a single filter
193         $ADD1 = '';
194         $ADD2 = '';
195         if (isExtensionInstalledAndNewer('theme', '0.0.8')) {
196                 // Okay, add design here
197                 $ADD1 = ', `curr_theme`';
198                 $ADD2 = ", '".getCurrentTheme()."'";
199         } // END - if
200
201         // Check if I shall disable sending mail to newly registered members out about active/begging rallye
202         //
203         // First comes first: begging rallye
204         if (isExtensionInstalledAndNewer('beg', '0.1.7')) {
205                 // Okay, shall I disable now?
206                 if (getConfig('beg_new_mem_notify') != 'Y') {
207                         $ADD1 .= ', `beg_ral_notify`, `beg_ral_en_notify`';
208                         $ADD2 .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
209                 } // END - if
210         } // END - if
211
212         // Second: active rallye
213         if (isExtensionInstalledAndNewer('bonus', '0.7.7')) {
214                 // Okay, shall I disable now?
215                 if (getConfig('bonus_new_mem_notify') != 'Y') {
216                         $ADD1 .= ', `bonus_ral_notify`, `bonus_ral_en_notify`';
217                         $ADD2 .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
218                 } // END - if
219         } // END - if
220
221         // Write user data to table
222         if (isExtensionActive('country')) {
223                 // Save with new selectable country code
224                 $countryRow = '`country_code`';
225                 $countryData = bigintval(postRequestElement('country_code'));
226         } else {
227                 // Old way with enterable two-char-code
228                 $countryRow = '`country`';
229                 $countryData = substr(postRequestElement('cntry'), 0, 2);
230         }
231
232         //////////////////////////////
233         // Create user's account... //
234         //////////////////////////////
235         //
236         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.")
237 VALUES ('%s','%s','%s','%s','%s',%s,'%s','%s',%s, %s,%s,'%s',%s, %s,'%s','UNCONFIRMED','%s','%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()".$ADD2.")",
238         array(
239                 $countryRow,
240                 substr(postRequestElement('gender'), 0, 1),
241                 postRequestElement('surname'),
242                 postRequestElement('family'),
243                 postRequestElement('street_nr'),
244                 $countryData,
245                 bigintval(postRequestElement('zip')),
246                 postRequestElement('city'),
247                 postRequestElement('email'),
248                 bigintval(postRequestElement('day')),
249                 bigintval(postRequestElement('month')),
250                 bigintval(postRequestElement('year')),
251                 generateHash(postRequestElement('pass1')),
252                 bigintval(postRequestElement('max_mails')),
253                 bigintval(postRequestElement('max_mails')),
254                 bigintval(postRequestElement('refid')),
255                 $hash,
256                 detectRemoteAddr(),
257         ), __FILE__, __LINE__);
258
259         // Get his userid
260         $userid = SQL_INSERTID();
261
262         // Did this work?
263         if ($userid == '0') {
264                 // Something bad happened!
265                 loadTemplate('admin_settings_saved', false, getMessage('USER_NOT_REGISTERED'));
266                 return;
267         } // END - if
268
269         // Is the refback extension there?
270         // @TODO Rewrite this to a filter
271         if (isExtensionActive('refback')) {
272                 // Update refback table
273                 updateRefbackTable($userid);
274         } // END - if
275
276         // Write his welcome-points
277         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_points` WHERE `userid`=%s AND `ref_depth`=0 LIMIT 1",
278                 array(bigintval($userid)), __FILE__, __LINE__);
279         if (SQL_NUMROWS($result) == '0') {
280                 // Add only when the line was not found (maybe some more secure?)
281                 $locked = 'points';
282
283                 // Pay him later. First he has to confirm some mails!
284                 if (getConfig('ref_payout') > 0) $locked = 'locked_points';
285
286                 // @TODO Rewrite this to addPointsDirectly()
287                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `".$locked."`) VALUES (%s,0,'{?points_register?}')",
288                         array(bigintval($userid)), __FILE__, __LINE__);
289
290                 // Update mediadata as well
291                 if ((isExtensionInstalledAndNewer('mediadata', '0.0.4')) && ($locked == 'points')) {
292                         // Update database
293                         updateMediadataEntry(array('total_points'), 'add', getConfig('points_register'));
294                 } // END - if
295         } // END - if
296
297         // Write catgories
298         if ((is_array(postRequestElement('cat'))) && (count(postRequestElement('cat')))) {
299                 foreach (postRequestElement('cat') as $cat => $joined) {
300                         if ($joined == 'Y') {
301                                 // Insert category entry
302                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (userid, cat_id) VALUES (%s, %s)",
303                                 array(bigintval($userid), bigintval($cat)), __FILE__, __LINE__);
304                         } // END - if
305                 } // END - foreach
306         } // END - if
307
308         // Rewrite gender
309         $gender = translateGender(postRequestElement('gender'));
310
311         // ... rewrite a zero referal id to the main title
312         if (postRequestElement('refid') == '0') setRequestPostElement('refid', getConfig('MAIN_TITLE'));
313
314         // Is ZIP code set?
315         if (isPostRequestElementSet('zip')) {
316                 // Prepare data array for the email template
317                 // Start with the gender...
318                 $content = array(
319                         'hash'    => $hash,
320                         'userid'  => $userid,
321                         'gender'  => $gender,
322                         'surname' => SQL_ESCAPE(postRequestElement('surname')),
323                         'family'  => SQL_ESCAPE(postRequestElement('family')),
324                         'email'   => SQL_ESCAPE(postRequestElement('email')),
325                         'street'  => SQL_ESCAPE(postRequestElement('street_nr')),
326                         'city'    => SQL_ESCAPE(postRequestElement('city')),
327                         'zip'     => bigintval(postRequestElement('zip')),
328                         'country' => $countryData,
329                         'refid'   => SQL_ESCAPE(postRequestElement('refid')),
330                         'pass'    => SQL_ESCAPE(postRequestElement('pass1')),
331                 );
332         } else {
333                 // No ZIP code entered
334                 $content = array(
335                         'hash'    => $hash,
336                         'userid'  => $userid,
337                         'gender'  => $gender,
338                         'surname' => SQL_ESCAPE(postRequestElement('surname')),
339                         'family'  => SQL_ESCAPE(postRequestElement('family')),
340                         'email'   => SQL_ESCAPE(postRequestElement('email')),
341                         'street'  => SQL_ESCAPE(postRequestElement('street_nr')),
342                         'city'    => SQL_ESCAPE(postRequestElement('city')),
343                         'zip'     => '',
344                         'country' => $countryData,
345                         'refid'   => SQL_ESCAPE(postRequestElement('refid')),
346                         'pass'    => SQL_ESCAPE(postRequestElement('pass1')),
347                 );
348         }
349
350         // Continue with birthday...
351         switch (getLanguage()) {
352                 case 'de':
353                         $content['birthday'] = bigintval(postRequestElement('day')) . '.' . bigintval(postRequestElement('month')) . '.' . bigintval(postRequestElement('year'));
354                         break;
355
356                 default:
357                         $content['birthday'] = bigintval(postRequestElement('month')) . '/' . bigintval(postRequestElement('day')) . '/' . bigintval(postRequestElement('year'));
358                         break;
359         } // END - switch
360
361         // Display information to the user that he got mail and send it away
362         $messageGuest = loadEmailTemplate('register-member', $content, $userid);
363
364         // Send mail to user (confirmation link!)
365         $email = $content['email'];
366         sendEmail($content['email'], getMessage('GUEST_SUBJECT_CONFIRM_LINK'), $messageGuest);
367         $content['email'] = $email;
368
369         // Send mail to admin
370         sendAdminNotification(getMessage('ADMIN_SUBJECT_NEW_ACCOUNT'), 'register-admin', $content, $userid);
371
372         // Output success registration
373         loadTemplate('admin_settings_saved', false, getMessage('REGISTRATION_DONE'));
374 } else {
375         if (postRequestElement('agree') == '!') {
376                 registerOutputFailedMessage('HAVE_TO_AGREE');
377         } // END - if
378
379         if (postRequestElement('email') == '!') {
380                 registerOutputFailedMessage('ENTER_EMAIL');
381                 setRequestPostElement('email', '');
382         } elseif (postRequestElement('email') == '?') {
383                 registerOutputFailedMessage('EMAIL_ALREADY_DB');
384                 setRequestPostElement('email', '');
385         }
386
387         if (postRequestElement('surname') == '!') {
388                 registerOutputFailedMessage('ENTER_SURNAME');
389                 setRequestPostElement('surname', '');
390         } // END - if
391
392         if (postRequestElement('family') == '!') {
393                 registerOutputFailedMessage('ENTER_FAMILY');
394                 setRequestPostElement('family', '');
395         } // END - if
396
397         if ((postRequestElement('pass1') == '!') && (postRequestElement('pass2') == '!')) {
398                 registerOutputFailedMessage('ENTER_BOTH_PASSWORDS');
399         } elseif (postRequestElement('pass1') == '!') {
400                 registerOutputFailedMessage('ENTER_PASS1');
401         } elseif (postRequestElement('pass2') == '!') {
402                 registerOutputFailedMessage('ENTER_PASS2');
403         }
404
405         if ($shortPass === true) {
406                 registerOutputFailedMessage('SHORT_PASS', ': ' . getConfig('pass_len'));
407         } // END - if
408
409         if ($ipTimedOut === true) {
410                 registerOutputFailedMessage('REMOTE_ADDR_TIMEOUT');
411         } // END - if
412
413         if ((!empty($cats)) && ($cats < getConfig('least_cats'))) {
414                 registerOutputFailedMessage('', sprintf(getMessage('CATS_LEAST'), getConfig('least_cats')));
415         } // END - if
416
417         // Generate birthday selection
418         switch (getLanguage()) {
419                 case 'de': // German date format
420                         $content['birthday_selection'] = addSelectionBox('day', postRequestElement('day')).addSelectionBox('month', postRequestElement('month')).addSelectionBox('year', postRequestElement('year'));
421                         break;
422
423                 default: // Default is the US date format... :)
424                         $content['birthday_selection'] = addSelectionBox('month', postRequestElement('month')).addSelectionBox('day', postRequestElement('day')).addSelectionBox('year', postRequestElement('year'));
425                         break;
426         }
427
428         // Adds a table for the guests with all visible categories
429         $content['category_selection'] = registerGenerateCategoryTable('guest', true);
430
431         // Adds maximum receiveable mails list... :)
432         $content['max_receive_list'] = addMaxReceiveList('guest', '', true);
433
434         // Shall I display the refid or shall I make it editable?
435         if (getConfig('display_refid') == 'Y') {
436                 // Load "hide" form template
437                 $content['refid_content'] = loadTemplate('guest_register_refid_hide', true, determineReferalId());
438         } else {
439                 // Load template to enter it
440                 $content['refid_content'] = loadTemplate('guest_register_refid', true, determineReferalId());
441         }
442
443         // You may want to modify the register_header.tpl.xx file and not this script when you add your scripts etc. :-)
444         $content['header_content'] = loadTemplate('register_header', true);
445
446         // Other values
447         $content['surname']   = SQL_ESCAPE(postRequestElement('surname'));
448         $content['family']    = SQL_ESCAPE(postRequestElement('family'));
449         $content['street_nr'] = SQL_ESCAPE(postRequestElement('street_nr'));
450         $content['zip']       = '';
451         if (isPostRequestElementSet('zip')) {
452                 $content['zip']       = bigintval(postRequestElement('zip'));
453         } // END - if
454         $content['city']      = SQL_ESCAPE(postRequestElement('city'));
455         $content['email']     = SQL_ESCAPE(postRequestElement('email'));
456
457         // Prepare country selection box
458         $OUT  = "<select name=\"country_code\" class=\"guest_select\" size=\"1\">\n";
459         $whereStatement = "WHERE `is_active`='Y'";
460         if (isAdmin()) $whereStatement = '';
461         $OUT .= generateOptionList('countries', 'id', 'descr', postRequestElement('country_code'), 'code', $whereStatement);
462         $OUT .= "</select>";
463         $content['country'] = $OUT;
464
465         // Set must-fillout fields
466         $content = runFilterChain('register_must_fillout', $content);
467
468         // Display registration form
469         loadTemplate('guest_register', false, $content);
470 }
471
472 // [EOF]
473 ?>