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 - 2013 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 = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_must_register` WHERE `field_name`='%s' AND `field_required`='Y' LIMIT 1",
50 array($key), __FUNCTION__, __LINE__);
53 if (sqlNumRows($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 sqlFreeResult($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 = sqlQuery('SELECT
95 `{?_MYSQL_PREFIX?}_cats`
96 ' . $whereStatement . '
99 __FUNCTION__, __LINE__);
101 if (!ifSqlHasZeroNums($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 = sqlFetchArray($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 sqlFreeResult($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_weak_password'] = FALSE;
146 $GLOBALS['registration_selected_cats'] = '0';
152 // First we only check the submitted data then we continue... :)
154 // Did he agree to the terms of usage?
155 if (postRequestElement('agree') != 'Y') {
156 setPostRequestElement('agree', '!');
157 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'agree=N - User did not agree with terms of usage.');
161 // Did he enter a valid email address? (we really don't care about
162 // that, he has to click on a confirmation link :P )
163 if ((!isAdmin()) && ((!isPostRequestElementSet('email')) || (!isEmailValid(postRequestElement('email'))))) {
164 setPostRequestElement('email', '!');
165 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter proper email address.');
169 // And what about surname and family's name?
170 if (!isPostRequestElementSet('surname')) {
171 setPostRequestElement('surname', '!');
172 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter surname.');
175 if (!isPostRequestElementSet('family')) {
176 setPostRequestElement('family', '!');
177 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter family name.');
181 // Get temporary array for modification
182 $postArray = postRequestArray();
184 // Check for required fields
185 $isOkay = ($isOkay && ifRequiredRegisterFieldsAreSet($postArray));
187 // Set it back in request
188 setPostRequestArray($postArray);
190 // Are both passwords zero length?
191 if ((strlen(postRequestElement('password1')) == 0) && (strlen(postRequestElement('password2')) == 0) && ($isOkay === TRUE)) {
192 // Is the extension 'register' newer or equal 0.5.5?
193 if ((isExtensionInstalledAndNewer('register', '0.5.5')) && (isRegisterGeneratePasswordEmptyEnabled())) {
194 // Generate a random password
195 $randomPassword = generatePassword();
198 // Set it in both entries
199 setPostRequestElement('password1', $randomPassword);
200 setPostRequestElement('password2', $randomPassword);
202 // Not allowed or no recent extension version
203 setPostRequestElement('password1', '!');
204 setPostRequestElement('password2', '!');
206 // ... which is both not okay
207 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Random password generation not possible, isExtensionInstalledAndNewer(register, 0.5.5)=' . intval(isExtensionInstalledAndNewer('register', '0.5.5')) . ',isRegisterGeneratePasswordEmptyEnabled()=' . intval(isRegisterGeneratePasswordEmptyEnabled()));
212 // Did he enter his password twice?
213 if (((!isPostRequestElementSet('password1')) || (!isPostRequestElementSet('password2'))) || ((postRequestElement('password1') != postRequestElement('password2')) && (isPostRequestElementSet('password1')) && (isPostRequestElementSet('password2')))) {
214 if ((postRequestElement('password1') != postRequestElement('password2')) && (isPostRequestElementSet('password1')) && (isPostRequestElementSet('password2'))) {
215 // Both passwords did not match
216 setPostRequestElement('password1', '!');
217 setPostRequestElement('password2', '!');
218 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter same passwords.');
220 if (!isPostRequestElementSet('password1')) {
221 // Password 1 is empty
222 setPostRequestElement('password1', '!');
223 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter password1.');
225 // Password 2 is empty
226 setPostRequestElement('password1', '');
228 if (!isPostRequestElementSet('password2')) {
229 // Password 2 is empty
230 setPostRequestElement('password2', '!');
231 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did not enter password2.');
233 // Password 1 is empty
234 setPostRequestElement('password2', '');
240 // Is the password strong enough?
241 if (($isRandom === FALSE) && (!isStrongPassword(postRequestElement('password1')))) {
242 $GLOBALS['registration_weak_password'] = TRUE;
243 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did enter a short password.');
247 // Do this check only when no admin is logged in
248 if (ifPostContainsSelections('cat')) {
249 // Only continue with array
250 foreach (postRequestElement('cat') as $id => $answer) {
251 // Is this category choosen?
252 if ($answer == 'Y') {
253 $GLOBALS['registration_selected_cats']++;
258 // Enougth categories selected?
259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay) . ',selected=' . $GLOBALS['registration_selected_cats'] . '/' . getLeastCats());
260 $isOkay = (($isOkay) && ($GLOBALS['registration_selected_cats'] >= getLeastCats()));
262 // Check if email is taken, if configured
263 if ((isExtensionInstalledAndNewer('other', '0.3.0')) && (isCheckDoubleEmailEnabled()) && (postRequestElement('email') != '!') && (isEmailTaken(postRequestElement('email'))) && (!isAdmin())) {
265 setPostRequestElement('email', '?');
266 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did enter a already used email address.');
270 // Check for IP timeout?
271 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay));
272 if ((!isAdmin()) && (getIpTimeout() > 0)) {
273 // Check his IP number
274 $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);
275 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay).',timeout='.intval($GLOBALS['registration_ip_timeout']));
276 $isOkay = (($isOkay) && (!$GLOBALS['registration_ip_timeout']));
280 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay=' . intval($isOkay) . ' - EXIT!');
284 // Do the registration
285 function doUserRegistration () {
286 // Do not register an account on absent ext-user
287 if (!isExtensionInstalled('user')) {
288 // Please report this
289 reportBug(__FUNCTION__, __LINE__, 'Tried to register a user account without ext-user installed.');
292 // Init extra SQL data
293 initExtraRegistrationSql();
297 // Initialization not done by default
298 'init_done' => FALSE,
299 'post_data' => postRequestArray(),
301 'message' => '{--PRE_USER_REGISTRATION_FAILED--}',
304 // Run the pre-registration chain
305 $filterData = runFilterChain('pre_user_registration', $filterData);
307 // Did the initialization work?
308 if ($filterData['init_done'] === FALSE) {
309 // Something bad happened!
310 displayMessage($filterData['message']);
316 // Create user's account...
317 sqlQueryEscaped("INSERT INTO
318 `{?_MYSQL_PREFIX?}_user_data`
341 " . $GLOBALS['register_sql_columns'] . "
360 '{%%pipe,detectRemoteAddr%%}',
364 " . $GLOBALS['register_sql_data'] . "
367 $GLOBALS['register_country_row'],
368 substr(postRequestElement('gender'), 0, 1),
369 postRequestElement('surname'),
370 postRequestElement('family'),
371 postRequestElement('street_nr'),
372 $GLOBALS['register_country_data'],
373 bigintval(postRequestElement('zip')),
374 postRequestElement('city'),
375 postRequestElement('email'),
376 bigintval(postRequestElement('day')),
377 bigintval(postRequestElement('month')),
378 bigintval(postRequestElement('year')),
379 generateHash(postRequestElement('password1')),
380 bigintval(postRequestElement('max_mails')),
381 bigintval(postRequestElement('max_mails')),
382 convertZeroToNull(postRequestElement('refid')),
383 postRequestElement('status'),
384 $GLOBALS['register_confirm_hash']
385 ), __FUNCTION__, __LINE__);
388 $filterData['register_insert_id'] = getSqlInsertId();
391 if (!isValidId($filterData['register_insert_id'])) {
392 // Something bad happened!
393 displayMessage('{--USER_NOT_REGISTERED--}');
399 // Shall we reset random refid? Only possible with latest ext-user
400 if (isExtensionInstalledAndNewer('user', '0.3.4')) {
401 // Reset all accounts, registration is done
402 sqlQuery('UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=0', __FUNCTION__, __LINE__);
405 // Update referral table
406 updateReferralCounter($filterData['register_insert_id']);
408 // Write his welcome-points
409 initReferralSystem();
410 addPointsThroughReferralSystem(
414 $filterData['register_insert_id'],
417 // Referral id (or NULL if none set)
418 convertZeroToNull(postRequestElement('refid'))
422 if (ifPostContainsSelections('cat')) {
424 $sql = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES';
427 foreach (postRequestElement('cat') as $categoryId => $joined) {
428 // "Join" this group?
429 if ($joined == 'Y') {
430 // Insert category entry
431 $sql .= ' (' . $filterData['register_insert_id'] . ', ' . bigintval($categoryId) . '),';
435 // Run SQL without last commata
436 sqlQuery(substr($sql, 0, -1), __FUNCTION__, __LINE__);
439 // Registration phase is done here, so for tester accounts we end here
440 if (((getExtensionVersion('user') >= '0.5.0')) && (isTesterUserName(postRequestElement('surname'))) && (ifTesterAccountsAllowed())) {
445 // ... rewrite a zero referral id to the main title
446 if (!isValidId(postRequestElement('refid'))) {
447 setPostRequestElement('refid', getMainTitle());
451 if (isPostRequestElementSet('zip')) {
452 // Prepare data array for the email template
454 'hash' => $GLOBALS['register_confirm_hash'],
455 'userid' => $filterData['register_insert_id'],
456 'gender' => sqlEscapeString(postRequestElement('gender')),
457 'surname' => sqlEscapeString(postRequestElement('surname')),
458 'family' => sqlEscapeString(postRequestElement('family')),
459 'email' => sqlEscapeString(postRequestElement('email')),
460 'street' => sqlEscapeString(postRequestElement('street_nr')),
461 'city' => sqlEscapeString(postRequestElement('city')),
462 'zip' => bigintval(postRequestElement('zip')),
463 'country' => $GLOBALS['register_country_data'],
464 'refid' => sqlEscapeString(postRequestElement('refid')),
465 'password' => sqlEscapeString(postRequestElement('password1')),
468 // No ZIP code entered
470 'hash' => $GLOBALS['register_confirm_hash'],
471 'userid' => $filterData['register_insert_id'],
472 'gender' => sqlEscapeString(postRequestElement('gender')),
473 'surname' => sqlEscapeString(postRequestElement('surname')),
474 'family' => sqlEscapeString(postRequestElement('family')),
475 'email' => sqlEscapeString(postRequestElement('email')),
476 'street' => sqlEscapeString(postRequestElement('street_nr')),
477 'city' => sqlEscapeString(postRequestElement('city')),
479 'country' => $GLOBALS['register_country_data'],
480 'refid' => sqlEscapeString(postRequestElement('refid')),
481 'password' => sqlEscapeString(postRequestElement('password1')),
485 // Continue with birthday...
486 switch (getLanguage()) {
488 $content['birthday'] = bigintval(postRequestElement('day')) . '.' . bigintval(postRequestElement('month')) . '.' . bigintval(postRequestElement('year'));
492 $content['birthday'] = bigintval(postRequestElement('month')) . '/' . bigintval(postRequestElement('day')) . '/' . bigintval(postRequestElement('year'));
496 // Display information to the user that he got mail and send it away
497 $messageGuest = loadEmailTemplate('guest_register_done', $content, $filterData['register_insert_id'], FALSE);
499 // Send mail to user (confirmation link!)
500 sendEmail($filterData['register_insert_id'], '{--GUEST_CONFIRM_LINK_SUBJECT--}', $messageGuest);
502 // Send mail to admin
503 sendAdminNotification('{--ADMIN_NEW_ACCOUNT_SUBJECT--}', 'admin_register_done', $content, $filterData['register_insert_id']);
509 // Initialize extra registration SQL
510 function initExtraRegistrationSql () {
511 $GLOBALS['register_sql_columns'] = '';
512 $GLOBALS['register_sql_data'] = '';
515 // Add extra column for registration SQL
516 function addExtraRegistrationColumns ($column) {
518 $GLOBALS['register_sql_columns'] .= $column;
521 // Add extra data for registration SQL
522 function addExtraRegistrationData ($data) {
524 $GLOBALS['register_sql_data'] .= $data;
527 //-----------------------------------------------------------------------------
528 // Wrapper functions for ext-register
529 //-----------------------------------------------------------------------------
531 // Getter for 'display_refid'
532 function getDisplayRefid () {
533 // Is the cache entry set?
534 if (!isset($GLOBALS[__FUNCTION__])) {
535 // No, so determine it
536 $GLOBALS[__FUNCTION__] = getConfig('display_refid');
539 // Return cached entry
540 return $GLOBALS[__FUNCTION__];
543 // Checks whether 'display_refid' is "Y"
544 function isDisplayRefidEnabled () {
545 // Is the cache entry set?
546 if (!isset($GLOBALS[__FUNCTION__])) {
547 // No, so determine it
548 $GLOBALS[__FUNCTION__] = (getDisplayRefid() == 'Y');
551 // Return cached entry
552 return $GLOBALS[__FUNCTION__];
555 // Getter for 'ip_timeout'
556 function getIpTimeout () {
557 // Is the cache entry set?
558 if (!isset($GLOBALS[__FUNCTION__])) {
559 // No, so determine it
560 $GLOBALS[__FUNCTION__] = getConfig('ip_timeout');
563 // Return cached entry
564 return $GLOBALS[__FUNCTION__];
567 // Getter for 'register_default'
568 function getRegisterDefault () {
569 // Is the cache entry set?
570 if (!isset($GLOBALS[__FUNCTION__])) {
571 // No, so determine it
572 $GLOBALS[__FUNCTION__] = getConfig('register_default');
575 // Return cached entry
576 return $GLOBALS[__FUNCTION__];
579 // Checks whether 'register_default' is "YES"
580 function isRegisterDefaultEnabled () {
581 // Is the cache entry set?
582 if (!isset($GLOBALS[__FUNCTION__])) {
583 // No, so determine it
584 $GLOBALS[__FUNCTION__] = (getRegisterDefault() == 'Y');
587 // Return cached entry
588 return $GLOBALS[__FUNCTION__];
591 // Getter for 'register_generate_password_empty'
592 function getRegisterGeneratePasswordEmpty () {
593 // Is the cache entry set?
594 if (!isset($GLOBALS[__FUNCTION__])) {
595 // No, so determine it
596 $GLOBALS[__FUNCTION__] = getConfig('register_generate_password_empty');
599 // Return cached entry
600 return $GLOBALS[__FUNCTION__];
603 // Checks whether 'register_generate_password_empty' is "YES"
604 function isRegisterGeneratePasswordEmptyEnabled () {
605 // Is the cache entry set?
606 if (!isset($GLOBALS[__FUNCTION__])) {
607 // No, so determine it
608 $GLOBALS[__FUNCTION__] = (getRegisterGeneratePasswordEmpty() == 'Y');
611 // Return cached entry
612 return $GLOBALS[__FUNCTION__];
615 // "Getter" for least_cats
616 function getLeastCats () {
618 if (!isset($GLOBALS[__FUNCTION__])) {
620 $GLOBALS[__FUNCTION__] = getConfig('least_cats');
624 return $GLOBALS[__FUNCTION__];
627 // ----------------------------------------------------------------------------
628 // Template helper functions
629 // ----------------------------------------------------------------------------
631 // Template helper for generating a category selection table for admin area with given configuration entry
632 function doTemplateAdminRegisterCategoryTable ($templateName, $clear = FALSE, $configEntry) {
633 // Call the inner function
634 return registerGenerateCategoryTable('admin', $configEntry);