Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / libs / register_functions.php
index 2d6c8e719eb6647449807a168632aa4dad4910ca..1bb911df8f768f3c49a57e1e3748c19517f88dc8 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -46,11 +46,11 @@ function ifRequiredRegisterFieldsAreSet (&$array) {
        $ret = TRUE;
        foreach ($array as $key => $value) {
                // Check all fields that must register
-               $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_must_register` WHERE `field_name`='%s' AND `field_required`='Y' LIMIT 1",
+               $result = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_must_register` WHERE `field_name`='%s' AND `field_required`='Y' LIMIT 1",
                        array($key), __FUNCTION__, __LINE__);
 
                // Entry found?
-               if (SQL_NUMROWS($result) == 1) {
+               if (sqlNumRows($result) == 1) {
                        // Check if extension country is not found (you have to enter the 2-chars long country code) or
                        // if extensions is present check if country code was selected
                        //         01              2         21    12             3         32    234     5      54    4               43    34                      4    4      5      5432    2      3                      3210
@@ -63,7 +63,7 @@ function ifRequiredRegisterFieldsAreSet (&$array) {
                } // END - if
 
                // Free result
-               SQL_FREERESULT($result);
+               sqlFreeResult($result);
        } // END - foreach
 
        // Return result
@@ -87,7 +87,7 @@ function registerGenerateCategoryTable ($mode, $configEntry = 'register_default'
        } // END - if
 
        // Look for categories
-       $result = SQL_QUERY('SELECT
+       $result = sqlQuery('SELECT
        `id`,
        `cat`,
        `visible`
@@ -98,10 +98,10 @@ ORDER BY
        `sort` ASC',
                __FUNCTION__, __LINE__);
 
-       if (!SQL_HASZERONUMS($result)) {
+       if (!ifSqlHasZeroNumRows($result)) {
                // List alle visible modules (or all to the admin)
                $OUT .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
-               while ($content = SQL_FETCHARRAY($result)) {
+               while ($content = sqlFetchArray($result)) {
                        // Prepare array for the template
                        $content['default_yes'] = '';
                        $content['default_no']  = '';
@@ -119,7 +119,7 @@ ORDER BY
                $OUT .= '</table>';
 
                // Free memory
-               SQL_FREERESULT($result);
+               sqlFreeResult($result);
        } else {
                // No categories setted up so far...
                $OUT .= displayMessage('{--NO_CATEGORIES_VISIBLE--}', TRUE);
@@ -130,7 +130,7 @@ ORDER BY
 }
 
 // Outputs a 'failed message'
-function registerOutputFailedMessage ($messageId, $extra='') {
+function registerOutputFailedMessage ($messageId, $extra = '') {
        if (empty($messageId)) {
                outputHtml('<div class="bad">' . $extra . '</div>');
        } else {
@@ -141,12 +141,13 @@ function registerOutputFailedMessage ($messageId, $extra='') {
 // Checks whether the registration data is complete
 function isRegistrationDataComplete () {
        // Init elements
-       $GLOBALS['registration_ip_timeout']     = FALSE;
-       $GLOBALS['registration_short_password'] = FALSE;
-       $GLOBALS['registration_selected_cats']  = '0';
+       $GLOBALS['registration_ip_timeout']    = FALSE;
+       $GLOBALS['registration_weak_password'] = FALSE;
+       $GLOBALS['registration_selected_cats'] = '0';
 
        // Default is okay
        $isOkay = TRUE;
+       $isRandom = FALSE;
 
        // First we only check the submitted data then we continue... :)
        //
@@ -192,6 +193,7 @@ function isRegistrationDataComplete () {
                if ((isExtensionInstalledAndNewer('register', '0.5.5')) && (isRegisterGeneratePasswordEmptyEnabled())) {
                        // Generate a random password
                        $randomPassword = generatePassword();
+                       $isRandom = TRUE;
 
                        // Set it in both entries
                        setPostRequestElement('password1', $randomPassword);
@@ -235,9 +237,9 @@ function isRegistrationDataComplete () {
                $isOkay = FALSE;
        } // END - if
 
-       // Is the password long enouth?
-       if ((strlen(postRequestElement('password1')) < getPassLen()) && ($isOkay === TRUE)) {
-               $GLOBALS['registration_short_password'] = TRUE;
+       // Is the password strong enough?
+       if (($isRandom === FALSE) && (!isStrongPassword(postRequestElement('password1')))) {
+               $GLOBALS['registration_weak_password'] = TRUE;
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'User did enter a short password.');
                $isOkay = FALSE;
        } // END - if
@@ -287,30 +289,55 @@ function doUserRegistration () {
                reportBug(__FUNCTION__, __LINE__, 'Tried to register a user account without ext-user installed.');
        } // END - if
 
-       // Init filter data
+       // Init filter data array
        $filterData = array(
-               // Initialization not done by default
-               'init_done' => FALSE,
-               'post_data' => postRequestArray(),
+               // Registration status is always FALSE by default
+               'status' => FALSE,
        );
 
+       // Run filter chain for user registration
+       $filterData = runFilterChain('user_registration', $filterData);
+
+       // Return status
+       return $filterData['status'];
+}
+
+// Generic user registration
+function doGenericUserRegistration () {
        // Init extra SQL data
        initExtraRegistrationSql();
 
+       // Init filter data
+       $filterData = array(
+               // Initialization not done by default
+               'init_done'   => FALSE,
+               'post_data'   => postRequestArray(),
+               'blacklisted' => '',
+               'message'     => '{--PRE_USER_REGISTRATION_FAILED--}',
+       );
+
        // Run the pre-registration chain
        $filterData = runFilterChain('pre_user_registration', $filterData);
 
        // Did the initialization work?
        if ($filterData['init_done'] === FALSE) {
                // Something bad happened!
-               displayMessage('{--PRE_USER_REGISTRATION_FAILED--}');
+               displayMessage($filterData['message']);
 
                // Stop here
                return FALSE;
        } // END - if
 
+       // These elements must be set
+       assert(isset($GLOBALS['register_country_row']));
+       assert(isset($GLOBALS['register_country_data']));
+       assert(isset($GLOBALS['register_confirm_hash']));
+
+       // Only comment this in if you develop
+       //* DEVELOPER-CODE: */ $GLOBALS['register_userid'] = 1; return TRUE;
+
        // Create user's account...
-       SQL_QUERY_ESC("INSERT INTO
+       sqlQueryEscaped("INSERT INTO
        `{?_MYSQL_PREFIX?}_user_data`
 (
        `gender`,
@@ -381,7 +408,7 @@ function doUserRegistration () {
        ), __FUNCTION__, __LINE__);
 
        // Get his userid
-       $filterData['register_insert_id'] = SQL_INSERT_ID();
+       $filterData['register_insert_id'] = getSqlInsertId();
 
        // Did this work?
        if (!isValidId($filterData['register_insert_id'])) {
@@ -392,10 +419,13 @@ function doUserRegistration () {
                return FALSE;
        } // END - if
 
+       // Set new user id globally
+       $GLOBALS['register_userid'] = $filterData['register_insert_id'];
+
        // Shall we reset random refid? Only possible with latest ext-user
        if (isExtensionInstalledAndNewer('user', '0.3.4')) {
                // Reset all accounts, registration is done
-               SQL_QUERY('UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=0', __FUNCTION__, __LINE__);
+               sqlQuery('UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=0', __FUNCTION__, __LINE__);
        } // END - if
 
        // Update referral table
@@ -429,7 +459,7 @@ function doUserRegistration () {
                } // END - foreach
 
                // Run SQL without last commata
-               SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
+               sqlQuery(substr($sql, 0, -1), __FUNCTION__, __LINE__);
        } // END - if
 
        // Registration phase is done here, so for tester accounts we end here
@@ -449,32 +479,32 @@ function doUserRegistration () {
                $content = array(
                        'hash'     => $GLOBALS['register_confirm_hash'],
                        'userid'   => $filterData['register_insert_id'],
-                       'gender'   => SQL_ESCAPE(postRequestElement('gender')),
-                       'surname'  => SQL_ESCAPE(postRequestElement('surname')),
-                       'family'   => SQL_ESCAPE(postRequestElement('family')),
-                       'email'    => SQL_ESCAPE(postRequestElement('email')),
-                       'street'   => SQL_ESCAPE(postRequestElement('street_nr')),
-                       'city'     => SQL_ESCAPE(postRequestElement('city')),
+                       'gender'   => sqlEscapeString(postRequestElement('gender')),
+                       'surname'  => sqlEscapeString(postRequestElement('surname')),
+                       'family'   => sqlEscapeString(postRequestElement('family')),
+                       'email'    => sqlEscapeString(postRequestElement('email')),
+                       'street'   => sqlEscapeString(postRequestElement('street_nr')),
+                       'city'     => sqlEscapeString(postRequestElement('city')),
                        'zip'      => bigintval(postRequestElement('zip')),
                        'country'  => $GLOBALS['register_country_data'],
-                       'refid'    => SQL_ESCAPE(postRequestElement('refid')),
-                       'password' => SQL_ESCAPE(postRequestElement('password1')),
+                       'refid'    => sqlEscapeString(postRequestElement('refid')),
+                       'password' => sqlEscapeString(postRequestElement('password1')),
                );
        } else {
                // No ZIP code entered
                $content = array(
                        'hash'     => $GLOBALS['register_confirm_hash'],
                        'userid'   => $filterData['register_insert_id'],
-                       'gender'   => SQL_ESCAPE(postRequestElement('gender')),
-                       'surname'  => SQL_ESCAPE(postRequestElement('surname')),
-                       'family'   => SQL_ESCAPE(postRequestElement('family')),
-                       'email'    => SQL_ESCAPE(postRequestElement('email')),
-                       'street'   => SQL_ESCAPE(postRequestElement('street_nr')),
-                       'city'     => SQL_ESCAPE(postRequestElement('city')),
+                       'gender'   => sqlEscapeString(postRequestElement('gender')),
+                       'surname'  => sqlEscapeString(postRequestElement('surname')),
+                       'family'   => sqlEscapeString(postRequestElement('family')),
+                       'email'    => sqlEscapeString(postRequestElement('email')),
+                       'street'   => sqlEscapeString(postRequestElement('street_nr')),
+                       'city'     => sqlEscapeString(postRequestElement('city')),
                        'zip'      => '',
                        'country'  => $GLOBALS['register_country_data'],
-                       'refid'    => SQL_ESCAPE(postRequestElement('refid')),
-                       'password' => SQL_ESCAPE(postRequestElement('password1')),
+                       'refid'    => sqlEscapeString(postRequestElement('refid')),
+                       'password' => sqlEscapeString(postRequestElement('password1')),
                );
        }
 
@@ -608,6 +638,18 @@ function isRegisterGeneratePasswordEmptyEnabled () {
        return $GLOBALS[__FUNCTION__];
 }
 
+// Getter for 'default_registration_provider'
+function getDefaultRegistrationProvider () {
+       // Is the cache entry set?
+       if (!isset($GLOBALS[__FUNCTION__])) {
+               // No, so determine it
+               $GLOBALS[__FUNCTION__] = getConfig('default_registration_provider');
+       } // END - if
+
+       // Return cached entry
+       return $GLOBALS[__FUNCTION__];
+}
+
 // "Getter" for least_cats
 function getLeastCats () {
        // Is there cache?
@@ -630,5 +672,63 @@ function doTemplateAdminRegisterCategoryTable ($templateName, $clear = FALSE, $c
        return registerGenerateCategoryTable('admin', $configEntry);
 }
 
+// Template helper for generating a list of all activated user registration provider
+function doTemplateGuestRegistrationList ($templateName, $clear = FALSE) {
+       // Init output
+       $content = '';
+
+       // Default is only activated provider
+       $addSql = " AND `provider_is_active`='Y'";
+
+       // Is admin logged-in?
+       if (isAdmin()) {
+               // Then show all
+               $addSql = '';
+       } // END - if
+
+       // Search for all
+       $result = sqlQuery("SELECT
+       `provider_name`,
+       `provider_extension`
+FROM
+       `{?_MYSQL_PREFIX?}_user_register_provider`
+WHERE
+       `provider_extension` != 'register'
+       " . $addSql . "
+ORDER BY
+       `provider_name` ASC", __FUNCTION__, __LINE__);
+
+       // Are there entries?
+       if (sqlNumRows($result) > 0) {
+               // Loop through all
+               $row = '';
+               while ($content = sqlFetchArray($result)) {
+                       // Load row template
+                       $row .= loadTemplate('guest_registration_provider_row', TRUE, $content);
+               } // END - while
+
+               // Load main template
+               $content = loadTemplate('guest_registration_provider', TRUE, $row);
+       } else {
+               // Nothing found
+               $content = returnMessage('{--GUEST_EXTRA_REGISTRATION_PROVIDER_404--}');
+       }
+
+       // Free result
+       sqlFreeResult($result);
+
+       // Return the generated content
+       return $content;
+}
+
+// ----------------------------------------------------------------------------
+//                            "Translator" functions
+// ----------------------------------------------------------------------------
+
+function translateRegistrationProviderName ($providerName) {
+       // "Translate it"
+       return '{--REGISTRATION_PROVIDER_' . strtoupper($providerName) . '--}';
+}
+
 // [EOF]
 ?>