2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/10/2004 *
4 * =================== Last change: 07/10/2004 *
6 * -------------------------------------------------------------------- *
7 * File : register_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Special functions for register extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Spezielle Funktion fuer register-Erweiterung *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Checks whether all required registration fields are set
44 function ifRequiredRegisterFieldsAreSet (&$array) {
45 // By default all is fine
47 foreach ($array as $key => $value) {
48 // Check all fields that must register
49 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_must_register` WHERE `field_name`='%s' AND `field_required`='Y' LIMIT 1",
50 array($key), __FUNCTION__, __LINE__);
53 if (SQL_NUMROWS($result) == 1) {
54 // Check if extension country is not found (you have to enter the 2-chars long country code) or
55 // if extensions is present check if country code was selected
56 // 01 2 21 12 3 32 234 5 54 4 43 34 4 4 5 5432 2 3 3210
57 $country = ((!isExtensionActive('country')) || ((isExtensionActive('country')) && (((empty($value)) && ($key == 'cntry')) || (($key == 'country_code') && (!empty($value)))) && (!empty($array['country_code']))));
58 if ((empty($value)) && ($country === FALSE)) {
59 // Required field not set
66 SQL_FREERESULT($result);
73 // Generates a 'category table' for the registration form
74 function registerGenerateCategoryTable ($mode, $configEntry = 'register_default') {
79 * Guests are mostly not interested in how many members has choosen an
80 * individual category.
82 $whereStatement = "WHERE `visible`='Y' ";
84 // Admins are allowed to see every category...
89 // Look for categories
90 $result = SQL_QUERY('SELECT
95 `{?_MYSQL_PREFIX?}_cats`
96 ' . $whereStatement . '
99 __FUNCTION__, __LINE__);
101 if (!SQL_HASZERONUMS($result)) {
102 // List alle visible modules (or all to the admin)
103 $OUT .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
104 while ($content = SQL_FETCHARRAY($result)) {
105 // Prepare array for the template
106 $content['default_yes'] = '';
107 $content['default_no'] = '';
110 if ((postRequestElement('cat', $content['id']) == 'Y') || ((getConfig($configEntry) == 'Y') && (!isPostRequestElementSet('cat', $content['id'])))) {
111 $content['default_yes'] = ' checked="checked"';
113 $content['default_no'] = ' checked="checked"';
116 // Load template and switch color
117 $OUT .= loadTemplate('guest_cat_row', TRUE, $content);
122 SQL_FREERESULT($result);
124 // No categories setted up so far...
125 $OUT .= displayMessage('{--NO_CATEGORIES_VISIBLE--}', TRUE);
128 // Return generated HTML code
132 // Outputs a 'failed message'
133 function registerOutputFailedMessage ($messageId, $extra='') {
134 if (empty($messageId)) {
135 outputHtml('<div class="bad">' . $extra . '</div>');
137 outputHtml('<div class="bad">{--' . $messageId . '--}' . $extra . '</div>');
141 // Checks whether the registration data is complete
142 function isRegistrationDataComplete () {
144 $GLOBALS['registration_ip_timeout'] = FALSE;
145 $GLOBALS['registration_short_password'] = FALSE;
146 $GLOBALS['registration_selected_cats'] = '0';
151 // First we only check the submitted data then we continue... :)
153 // Did he agree to the terms of usage?
154 if (postRequestElement('agree') != 'Y') {
155 setPostRequestElement('agree', '!');
156 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'agree=N - User did not agree with terms of usage.');
160 // Did he enter a valid email address? (we really don't care about
161 // that, he has to click on a confirmation link :P )
162 if ((!isAdmin()) && ((!isPostRequestElementSet('email')) || (!isEmailValid(postRequestElement('email'))))) {
163 setPostRequestElement('email', '!');
164 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter proper email address.');
168 // And what about surname and family's name?
169 if (!isPostRequestElementSet('surname')) {
170 setPostRequestElement('surname', '!');
171 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter surname.');
174 if (!isPostRequestElementSet('family')) {
175 setPostRequestElement('family', '!');
176 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter family name.');
180 // Get temporary array for modification
181 $postArray = postRequestArray();
183 // Check for required fields
184 $isOkay = ($isOkay && ifRequiredRegisterFieldsAreSet($postArray));
186 // Set it back in request
187 setPostRequestArray($postArray);
189 // Are both passwords zero length?
190 if ((strlen(postRequestElement('pass1')) == 0) && (strlen(postRequestElement('pass2')) == 0) && ($isOkay === TRUE)) {
191 // Is the extension 'register' newer or equal 0.5.5?
192 if ((isExtensionInstalledAndNewer('register', '0.5.5')) && (isRegisterGeneratePasswordEmptyEnabled())) {
193 // Generate a random password
194 $randomPassword = generatePassword();
196 // Set it in both entries
197 setPostRequestElement('pass1', $randomPassword);
198 setPostRequestElement('pass2', $randomPassword);
200 // Not allowed or no recent extension version
201 setPostRequestElement('pass1', '!');
202 setPostRequestElement('pass2', '!');
204 // ... which is both not okay
205 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Random password generation not possible, isExtensionInstalledAndNewer(register, 0.5.5)=' . intval(isExtensionInstalledAndNewer('register', '0.5.5')) . ',isRegisterGeneratePasswordEmptyEnabled()=' . intval(isRegisterGeneratePasswordEmptyEnabled()));
210 // Did he enter his password twice?
211 if (((!isPostRequestElementSet('pass1')) || (!isPostRequestElementSet('pass2'))) || ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2')))) {
212 if ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2'))) {
213 // Both passwords did not match
214 setPostRequestElement('pass1', '!');
215 setPostRequestElement('pass2', '!');
216 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter same passwords.');
218 if (!isPostRequestElementSet('pass1')) {
219 // Password 1 is empty
220 setPostRequestElement('pass1', '!');
221 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter pass1.');
223 // Password 2 is empty
224 setPostRequestElement('pass1', '');
226 if (!isPostRequestElementSet('pass2')) {
227 // Password 2 is empty
228 setPostRequestElement('pass2', '!');
229 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter pass2.');
231 // Password 1 is empty
232 setPostRequestElement('pass2', '');
238 // Is the password long enouth?
239 if ((strlen(postRequestElement('pass1')) < getPassLen()) && ($isOkay === TRUE)) {
240 $GLOBALS['registration_short_password'] = TRUE;
241 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did enter a short password.');
245 // Do this check only when no admin is logged in
246 if (ifPostContainsSelections('cat')) {
247 // Only continue with array
248 foreach (postRequestElement('cat') as $id => $answer) {
249 // Is this category choosen?
250 if ($answer == 'Y') {
251 $GLOBALS['registration_selected_cats']++;
256 // Enougth categories selected?
257 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay) . ',selected=' . $GLOBALS['registration_selected_cats'] . '/' . getLeastCats());
258 $isOkay = (($isOkay) && ($GLOBALS['registration_selected_cats'] >= getLeastCats()));
260 if ((postRequestElement('email') != '!') && (isCheckDoubleEmailEnabled())) {
261 // Does the email address already exists in our database?
262 if ((isEmailTaken(postRequestElement('email'))) && (!isAdmin())) {
263 setPostRequestElement('email', '?');
264 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did enter a already used email address.');
269 // Check for IP timeout?
270 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay));
271 if ((!isAdmin()) && (getIpTimeout() > 0)) {
272 // Check his IP number
273 $GLOBALS['registration_ip_timeout'] = (countSumTotalData(detectRemoteAddr() , 'user_data', 'userid', 'REMOTE_ADDR', TRUE, ' AND ((UNIX_TIMESTAMP() - `joined`) < {?ip_timeout?} OR (UNIX_TIMESTAMP() - `last_update`) < {?ip_timeout?})') == 1);
274 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay).',timeout='.intval($GLOBALS['registration_ip_timeout']));
275 $isOkay = (($isOkay) && (!$GLOBALS['registration_ip_timeout']));
279 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay) . ' - EXIT!');
283 // Do the registration
284 function doUserRegistration () {
285 // Do not register an account on absent ext-user
286 if (!isExtensionInstalled('user')) {
287 // Please report this
288 reportBug(__FUNCTION__, __LINE__, 'Tried to register a user account without ext-user installed.');
293 // Initialization not done by default
297 // Init extra SQL data
298 initExtraRegistrationSql();
300 // Run the pre-registration chain
301 $filterData = runFilterChain('pre_user_registration', $filterData);
303 // Did the initialization work?
304 if ($filterData['init_done'] === FALSE) {
305 // Something bad happened!
306 displayMessage('{--PRE_USER_REGISTRATION_FAILED--}');
312 // Create user's account...
313 SQL_QUERY_ESC("INSERT INTO
314 `{?_MYSQL_PREFIX?}_user_data`
337 " . $GLOBALS['register_sql_columns'] . "
356 '{%%pipe,detectRemoteAddr%%}',
360 " . $GLOBALS['register_sql_data'] . "
363 $GLOBALS['register_country_row'],
364 substr(postRequestElement('gender'), 0, 1),
365 postRequestElement('surname'),
366 postRequestElement('family'),
367 postRequestElement('street_nr'),
368 $GLOBALS['register_country_data'],
369 bigintval(postRequestElement('zip')),
370 postRequestElement('city'),
371 postRequestElement('email'),
372 bigintval(postRequestElement('day')),
373 bigintval(postRequestElement('month')),
374 bigintval(postRequestElement('year')),
375 generateHash(postRequestElement('pass1')),
376 bigintval(postRequestElement('max_mails')),
377 bigintval(postRequestElement('max_mails')),
378 convertZeroToNull(postRequestElement('refid')),
379 postRequestElement('status'),
380 $GLOBALS['register_confirm_hash']
381 ), __FUNCTION__, __LINE__);
384 $filterData['register_insert_id'] = bigintval(SQL_INSERTID());
387 if (!isValidUserId($filterData['register_insert_id'])) {
388 // Something bad happened!
389 displayMessage('{--USER_NOT_REGISTERED--}');
395 // Shall we reset random refid? Only possible with latest ext-user
396 if (isExtensionInstalledAndNewer('user', '0.3.4')) {
397 // Reset all accounts, registration is done
398 SQL_QUERY('UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=0', __FUNCTION__, __LINE__);
401 // Update referral table
402 updateReferralCounter($filterData['register_insert_id']);
404 // Write his welcome-points
405 initReferralSystem();
406 addPointsThroughReferralSystem(
410 $filterData['register_insert_id'],
413 // Referral id (or NULL if none set)
414 convertZeroToNull(postRequestElement('refid'))
418 if (ifPostContainsSelections('cat')) {
420 foreach (postRequestElement('cat') as $categoryId => $joined) {
421 // "Join" this group?
422 if ($joined == 'Y') {
423 // Insert category entry
424 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s, %s)",
426 $filterData['register_insert_id'],
427 bigintval($categoryId)
428 ), __FUNCTION__, __LINE__);
433 // Registration phase is done here, so for tester accounts we end here
434 if (((getExtensionVersion('user') >= '0.5.0')) && (isTesterUserName(postRequestElement('surname'))) && (ifTesterAccountsAllowed())) {
439 // ... rewrite a zero referral id to the main title
440 if (!isValidUserId(postRequestElement('refid'))) {
441 setPostRequestElement('refid', getMainTitle());
445 if (isPostRequestElementSet('zip')) {
446 // Prepare data array for the email template
447 // Start with the gender...
449 'hash' => $GLOBALS['register_confirm_hash'],
450 'userid' => $filterData['register_insert_id'],
451 'gender' => SQL_ESCAPE(postRequestElement('gender')),
452 'surname' => SQL_ESCAPE(postRequestElement('surname')),
453 'family' => SQL_ESCAPE(postRequestElement('family')),
454 'email' => SQL_ESCAPE(postRequestElement('email')),
455 'street' => SQL_ESCAPE(postRequestElement('street_nr')),
456 'city' => SQL_ESCAPE(postRequestElement('city')),
457 'zip' => bigintval(postRequestElement('zip')),
458 'country' => $GLOBALS['register_country_data'],
459 'refid' => SQL_ESCAPE(postRequestElement('refid')),
460 'password' => SQL_ESCAPE(postRequestElement('pass1')),
463 // No ZIP code entered
465 'hash' => $GLOBALS['register_confirm_hash'],
466 'userid' => $filterData['register_insert_id'],
467 'gender' => SQL_ESCAPE(postRequestElement('gender')),
468 'surname' => SQL_ESCAPE(postRequestElement('surname')),
469 'family' => SQL_ESCAPE(postRequestElement('family')),
470 'email' => SQL_ESCAPE(postRequestElement('email')),
471 'street' => SQL_ESCAPE(postRequestElement('street_nr')),
472 'city' => SQL_ESCAPE(postRequestElement('city')),
474 'country' => $GLOBALS['register_country_data'],
475 'refid' => SQL_ESCAPE(postRequestElement('refid')),
476 'password' => SQL_ESCAPE(postRequestElement('pass1')),
480 // Continue with birthday...
481 switch (getLanguage()) {
483 $content['birthday'] = bigintval(postRequestElement('day')) . '.' . bigintval(postRequestElement('month')) . '.' . bigintval(postRequestElement('year'));
487 $content['birthday'] = bigintval(postRequestElement('month')) . '/' . bigintval(postRequestElement('day')) . '/' . bigintval(postRequestElement('year'));
491 // Display information to the user that he got mail and send it away
492 $messageGuest = loadEmailTemplate('guest_register_done', $content, $filterData['register_insert_id'], FALSE);
494 // Send mail to user (confirmation link!)
495 sendEmail($filterData['register_insert_id'], '{--GUEST_CONFIRM_LINK_SUBJECT--}', $messageGuest);
497 // Send mail to admin
498 sendAdminNotification('{--ADMIN_NEW_ACCOUNT_SUBJECT--}', 'admin_register_done', $content, $filterData['register_insert_id']);
504 // Initialize extra registration SQL
505 function initExtraRegistrationSql () {
506 $GLOBALS['register_sql_columns'] = '';
507 $GLOBALS['register_sql_data'] = '';
510 // Add extra column for registration SQL
511 function addExtraRegistrationColumns ($column) {
513 $GLOBALS['register_sql_columns'] .= $column;
516 // Add extra data for registration SQL
517 function addExtraRegistrationData ($data) {
519 $GLOBALS['register_sql_data'] .= $data;
522 //-----------------------------------------------------------------------------
523 // Wrapper functions for ext-register
524 //-----------------------------------------------------------------------------
526 // Getter for 'display_refid'
527 function getDisplayRefid () {
528 // Is the cache entry set?
529 if (!isset($GLOBALS[__FUNCTION__])) {
530 // No, so determine it
531 $GLOBALS[__FUNCTION__] = getConfig('display_refid');
534 // Return cached entry
535 return $GLOBALS[__FUNCTION__];
538 // Checks whether 'display_refid' is "YES"
539 function isDisplayRefidEnabled () {
540 // Is the cache entry set?
541 if (!isset($GLOBALS[__FUNCTION__])) {
542 // No, so determine it
543 $GLOBALS[__FUNCTION__] = (getDisplayRefid() == 'Y');
546 // Return cached entry
547 return $GLOBALS[__FUNCTION__];
550 // Getter for 'ip_timeout'
551 function getIpTimeout () {
552 // Is the cache entry set?
553 if (!isset($GLOBALS[__FUNCTION__])) {
554 // No, so determine it
555 $GLOBALS[__FUNCTION__] = getConfig('ip_timeout');
558 // Return cached entry
559 return $GLOBALS[__FUNCTION__];
562 // Getter for 'register_default'
563 function getRegisterDefault () {
564 // Is the cache entry set?
565 if (!isset($GLOBALS[__FUNCTION__])) {
566 // No, so determine it
567 $GLOBALS[__FUNCTION__] = getConfig('register_default');
570 // Return cached entry
571 return $GLOBALS[__FUNCTION__];
574 // Checks whether 'register_default' is "YES"
575 function isRegisterDefaultEnabled () {
576 // Is the cache entry set?
577 if (!isset($GLOBALS[__FUNCTION__])) {
578 // No, so determine it
579 $GLOBALS[__FUNCTION__] = (getRegisterDefault() == 'Y');
582 // Return cached entry
583 return $GLOBALS[__FUNCTION__];
586 // Getter for 'register_generate_password_empty'
587 function getRegisterGeneratePasswordEmpty () {
588 // Is the cache entry set?
589 if (!isset($GLOBALS[__FUNCTION__])) {
590 // No, so determine it
591 $GLOBALS[__FUNCTION__] = getConfig('register_generate_password_empty');
594 // Return cached entry
595 return $GLOBALS[__FUNCTION__];
598 // Checks whether 'register_generate_password_empty' is "YES"
599 function isRegisterGeneratePasswordEmptyEnabled () {
600 // Is the cache entry set?
601 if (!isset($GLOBALS[__FUNCTION__])) {
602 // No, so determine it
603 $GLOBALS[__FUNCTION__] = (getRegisterGeneratePasswordEmpty() == 'Y');
606 // Return cached entry
607 return $GLOBALS[__FUNCTION__];
610 // ----------------------------------------------------------------------------
611 // Template helper functions
612 // ----------------------------------------------------------------------------
614 // Template helper for generating a category selection table for admin area with given configuration entry
615 function doTemplateAdminRegisterCategoryTable ($templateName, $clear = FALSE, $configEntry) {
616 // Call the inner function
617 return registerGenerateCategoryTable('admin', $configEntry);