Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / modules / guest / what-sponsor_reg.php
index 715dc6c2c95487515780c6262aa990ad4ac9c67b..cb93271c33f44e364cf55f95666a1f69fd86c1fd 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Anmeldeformular fuer Sponsoren                   *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * 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 *
  * it under the terms of the GNU General Public License as published by *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       die();
-}
+       exit();
+} // END - if
 
 // Add description as navigation point
-addMenuDescription('guest', __FILE__);
+addYouAreHereLink('guest', __FILE__);
 
 if ((!isExtensionActive('sponsor'))) {
-       loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('sponsor'));
+       displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=sponsor%}');
        return;
 } // END - if
 
 // Create array for form errors (= missing data)
-$FORM_ERRORS = array();
+$formErrors = array();
 
 if (isFormSent()) {
        //
@@ -59,304 +53,258 @@ if (isFormSent()) {
        // 1. Salutation / Surname / family name
        if (!isPostRequestElementSet('gender')) {
                // Surname is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_NO_GENDER_SELECTED');
+               array_push($formErrors, '{--SPONSOR_NO_GENDER_SELECTED--}');
        } // END - if
 
        if (!isPostRequestElementSet('surname')) {
                // Surname is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_SURNAME_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_SURNAME_EMPTY--}');
        } // END - if
 
        if (!isPostRequestElementSet('family')) {
                // Surname is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_FAMILY_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_FAMILY_EMPTY--}');
        } // END - if
 
        // 2. Company name
-       // 012                   3         32    23                  4           43    3                  4          4321    12                  3         32    2                   3           3210
+       // 012                          3         32    23                         4           43    3                         4          4321    12                         3         32    2                          3           3210
        if (((!isPostRequestElementSet('company')) && ((isPostRequestElementSet('tax_ident')) || (isPostRequestElementSet('position')))) || ((isPostRequestElementSet('company')) && (!isPostRequestElementSet('tax_ident')))) {
                if (!isPostRequestElementSet('company')) {
                        // Company name is empty
-                       $FORM_ERRORS[] = getMessage('SPONSOR_COMPANY_IS_EMPTY');
+                       array_push($formErrors, '{--SPONSOR_COMPANY_EMPTY--}');
                } elseif (!isPostRequestElementSet('tax_ident')) {
                        // Tax ident number name is empty
-                       $FORM_ERRORS[] = getMessage('SPONSOR_TAX_IDENT_IS_EMPTY');
+                       array_push($formErrors, '{--SPONSOR_TAX_IDENT_EMPTY--}');
                }
 
                if (!isPostRequestElementSet('position')) {
                        // Not fatal but not nice: position in company is empty
-                       $FORM_ERRORS[] = getMessage('SPONSOR_POSITION_IS_EMPTY');
+                       array_push($formErrors, '{--SPONSOR_POSITION_EMPTY--}');
                } // END - if
        } // END - if
 
        // 3. Street and number
        if (!isPostRequestElementSet('street_nr1')) {
                // Street name and house number are empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_STREET_NR1_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_STREET_NR1_EMPTY--}');
        } elseif ((isPostRequestElementSet('street_nr2')) && (!isPostRequestElementSet('street_nr1'))) {
                // 1st line for street is empty, but 2nd line not
-               $FORM_ERRORS[] = getMessage('SPONSOR_STREET_NR1_IS_EMPTY_2_NOT');
+               array_push($formErrors, '{--SPONSOR_STREET_NR1_EMPTY_2_NOT--}');
        }
 
        // 4. Country code
        if (!isPostRequestElementSet('country')) {
                // Country code is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_COUNTRY_IS_EMPTY');
+               array_push($formErrors, '{--kSPONSOR_COUNTRY_EMPTY--}');
        } elseif (strlen(postRequestElement('country')) != 2) {
                // Country code is invalid
-               $FORM_ERRORS[] = getMessage('SPONSOR_COUNTRY_IS_INVALID');
+               array_push($formErrors, '{--SPONSOR_COUNTRY_IS_INVALID--}');
        }
 
        // 3. ZIP code
        if (!isPostRequestElementSet('zip')) {
                // ZIP code is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_ZIP_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_ZIP_EMPTY--}');
        } elseif (bigintval(postRequestElement('zip')) != postRequestElement('zip')) {
                // ZIP is invalid
-               $FORM_ERRORS[] = getMessage('SPONSOR_ZIP_IS_INVALID');
-               setRequestPostElement('zip', '');
+               array_push($formErrors, '{--SPONSOR_ZIP_IS_INVALID--}');
+               setPostRequestElement('zip', '');
        }
 
        // 4. City
        if (!isPostRequestElementSet('city')) {
                // City is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_CITY_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_CITY_EMPTY--}');
        } // END - if
 
        // 5. Phone number
        if (!isPostRequestElementSet('phone')) {
                // City is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_PHONE_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_PHONE_EMPTY--}');
        } // END - if
 
        // 6. Homepage URL
        if (!isPostRequestElementSet('url')) {
                // Homepage URL is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_URL_IS_EMPTY');
+               array_push($formErrors, '{--SPONSOR_URL_EMPTY--}');
        } elseif (!isUrlValid(postRequestElement('url'))) {
                // Homepage URL is invalid
-               $FORM_ERRORS[] = getMessage('SPONSOR_URL_IS_INVALID');
-               setRequestPostElement('url', '');
+               array_push($formErrors, '{--SPONSOR_URL_IS_INVALID--}');
+               setPostRequestElement('url', '');
        }
 
        // 7. Light validation of email address
        if ((!isPostRequestElementSet('email')) || (postRequestElement('email') == '@')) {
                // Email is invalid/empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_EMAIL_IS_INVALID');
+               array_push($formErrors, '{--SPONSOR_EMAIL_IS_INVALID--}');
        } elseif (isSponsorRegisteredWithEmail(postRequestElement('email'))) {
                // Email already found in database!
-               $FORM_ERRORS[] = getMessage('SPONSOR_EMAIL_IS_ALREADY_REGISTERED');
-               setRequestPostElement('email', '');
+               array_push($formErrors, '{--SPONSOR_EMAIL_IS_ALREADY_REGISTERED--}');
+               setPostRequestElement('email', '');
        }
 
        // 8. Pay type selected?
        if (!isPostRequestElementSet('pay_type')) {
                // Not pay type selected
-               $FORM_ERRORS[] = getMessage('SPONSOR_NO_PAYTYPE_SELECTED');
+               array_push($formErrors, '{--SPONSOR_NO_PAYTYPE_SELECTED--}');
        } // END - if
 
        // 9. Interval of mails
        if (!isPostRequestElementSet('warning_interval')) {
                // No warning interval selected
-               $FORM_ERRORS[] = getMessage('SPONSOR_NO_WARNING_INTERVAL_SELECTED');
+               array_push($formErrors, '{--SPONSOR_NO_WARNING_INTERVAL_SELECTED--}');
        } // END - if
 
        // 10. Mail notifications disabled/enabled
        if (!isPostRequestElementSet('receive_warnings')) {
                // Option not selected!
-               $FORM_ERRORS[] = getMessage('SPONSOR_NO_RECEIVE_WARNINGS_SELECTED');
+               array_push($formErrors, '{--SPONSOR_NO_RECEIVE_WARNINGS_SELECTED--}');
        } // END - if
 
        // Did he enter his password twice?
-       if (((!isPostRequestElementSet('pass1')) || (!isPostRequestElementSet('pass2'))) || ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2')))) {
-               if ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2'))) {
+       if (((!isPostRequestElementSet('password1')) || (!isPostRequestElementSet('password2'))) || ((postRequestElement('password1') != postRequestElement('password2')) && (isPostRequestElementSet('password1')) && (isPostRequestElementSet('password2')))) {
+               if ((postRequestElement('password1') != postRequestElement('password2')) && (isPostRequestElementSet('password1')) && (isPostRequestElementSet('password2'))) {
                        // Passwords missmatch
-                       $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORDS_MISMATCH');
+                       array_push($formErrors, '{--SPONSOR_PASSWORDS_MISMATCH--}');
                } else {
-                       if (!isPostRequestElementSet('pass1')) {
+                       if (!isPostRequestElementSet('password1')) {
                                // Password 1 is empty
-                               $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD1_EMPTY');
+                               array_push($formErrors, '{--SPONSOR_PASSWORD1_EMPTY--}');
                        }
 
-                       if (!isPostRequestElementSet('pass2')) {
+                       if (!isPostRequestElementSet('password2')) {
                                // Password 2 is empty
-                               $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD2_EMPTY');
+                               array_push($formErrors, '{--SPONSOR_PASSWORD2_EMPTY--}');
                        }
                }
-       } elseif (strlen(postRequestElement('pass1')) < getConfig('pass_len')) {
+       } elseif (strlen(postRequestElement('password1')) < getMinPasswordLength()) {
                // Password is to short!
-               $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD_TOO_SHORT');
+               array_push($formErrors, '{--SPONSOR_PASSWORD_TOO_SHORT--}');
        }
 
        // Check if he has accepted the terms&conditions
        if (!isPostRequestElementSet('terms')) {
                // Homepage URL is empty
-               $FORM_ERRORS[] = getMessage('SPONSOR_TERMS_NOT_ACCEPTED');
+               array_push($formErrors, '{--SPONSOR_TERMS_NOT_ACCEPTED--}');
        } // END - if
 
        // If there is something wrong/missing stop registration
-       if (count($FORM_ERRORS) > 0) unsetPostRequestElement('ok');
-}
+       if (isFilledArray($formErrors)) unsetPostRequestElement('ok');
+} // END - if
 
-if ((isFormSent()) && (count($FORM_ERRORS) == '0')) {
+if ((isFormSent()) && (!isFilledArray($formErrors))) {
        // Generate message array
        $messageArray = array(
-               'failed' => getMessage('SPONSOR_REGISTRATION_FAILED'),
-               'added'  => getMessage('SPONSOR_REGISTRATION_COMPLETED'),
+               'failed' => '{--SPONSOR_REGISTRATION_FAILED--}',
+               'added'  => '{--SPONSOR_REGISTRATION_COMPLETED--}',
        );
 
        // Calulate points
-       $result = SQL_QUERY_ESC("SELECT (`pay_rate` * `pay_min_count`) AS points, `pay_min_count` AS pay, `pay_currency` AS curr
-FROM `{?_MYSQL_PREFIX?}_sponsor_paytypes`
-WHERE `id`='%s' LIMIT 1",
+       $result = sqlQueryEscaped('SELECT
+       (`pay_rate` * `pay_min_count`) AS `points`,
+       `pay_min_count`,
+       `pay_currency`
+FROM
+       `{?_MYSQL_PREFIX?}_sponsor_paytypes`
+WHERE
+       `id`=%s
+LIMIT 1',
                array(postRequestElement('pay_type')), __FILE__, __LINE__);
-       list($points, $pay, $curr) = SQL_FETCHROW($result);
+       list($points, $pay, $curr) = sqlFetchRow($result);
 
        // Free memory
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        // Add points to array
        postRequestElement('points_amount', $points);
        postRequestElement('points_used'  , '0.00000');
-       postRequestElement('last_pay'     , $pay);
-       postRequestElement('last_curr'    , $curr);
+       postRequestElement('last_payment' , $pay);
+       postRequestElement('last_currency', $curr);
 
        // Register sponsor but never ever update here!
-       $STATUS = handlSponsorRequest(postRequestArray(), true, $messageArray, true);
-
-       // Check the status of the registration process
-       switch ($STATUS) {
-               case 'added': // Sponsor successfully added with account status = UNCONFIRMED!
-                       // Check for his id number
-                       $result = SQL_QUERY_ESC("SELECT `id`, `hash` FROM `{?_MYSQL_PREFIX?}_sponsor_data` WHERE `email`='%s' LIMIT 1",
-                               array(postRequestElement('email')), __FILE__, __LINE__);
-                       if (SQL_NUMROWS($result) == 1) {
-                               // id found so let's load it for the confirmation email
-                               list($id, $hash) = SQL_FETCHROW($result);
-
-                               // Prepare data for the email template
-                               $content['id']        = $id;
-                               $content['hash']      = $hash;
-                               $content['email']     = secureString(postRequestElement('email'));
-                               $content['surname']   = secureString(postRequestElement('surname'));
-                               $content['family']    = secureString(postRequestElement('family'));
-                               $content['gender']    = translateGender(postRequestElement('gender'));
-                               $content['timestamp'] = generateDateTime(time(), 0);
-                               $content['password']  = secureString(postRequestElement('pass1'));
-
-                               // Generate email and send it to the new sponsor
-                               $message = loadEmailTemplate('sponsor_confirm', $hash);
-                               sendEmail(postRequestElement('email'), getMessage('SPONSOR_PLEASE_CONFIRM_SUBJ'), $message);
-
-                               // Send mail to admin
-                               sendAdminNotification(getMessage('ADMIN_NEW_SPONSOR'), 'admin_sponsor_reg', $hash);
-
-                               // Output message: DONE
-                               $message = $messageArray['added'];
-                       } else {
-                               // Sponsor account not found???
-                               $message = getMaskedMessage('SPONSOR_EMAIL_404', postRequestElement('email'));
-                       }
-
-                       // Free memory
-                       SQL_FREERESULT($result);
-                       break;
-
-               default:
-                       logDebugMessage(__FILE__, __LINE__, sprintf("Unknown status %s detected.", $STATUS));
-                       if (!isAdmin()) {
-                               // Message for testing admin
-                               $message = getMaskedMessage('ADMIN_SPONSOR_UNKNOWN_STATUS', $STATUS);
-                       } else {
-                               // Message for the guest
-                               $message = getMaskedMessage('SPONSOR_UNKNOWN_STATUS', $STATUS);
-                       }
-                       break;
-       }
+       $message = doProcessSponsorFormRequest($messageArray);
 
        // Display message
-       loadTemplate('admin_settings_saved', false, $message);
+       displayMessage($message);
 } else {
        // Check for payment types
-       $result = SQL_QUERY("SELECT `id`, `pay_name`, `pay_rate`, `pay_currency`, `pay_min_count`
-FROM `{?_MYSQL_PREFIX?}_sponsor_paytypes`
-ORDER BY `pay_name`", __FILE__, __LINE__);
-       if (SQL_NUMROWS($result) > 0) {
+       $result = sqlQuery('SELECT
+       `id`,
+       `pay_name`,
+       `pay_rate`,
+       `pay_currency`,
+       `pay_min_count`
+FROM
+       `{?_MYSQL_PREFIX?}_sponsor_paytypes`
+ORDER BY
+       `pay_name` ASC', __FILE__, __LINE__);
+
+       if (!ifSqlHasZeroNumRows($result)) {
                // Load all types...
-               // @TODO Swap this HTML code to templates
-               $OUT = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left right top\">\n";
-               $SW = 2;
-               while ($content = SQL_FETCHARRAY($result)) {
+               $OUT = '';
+               while ($content = sqlFetchArray($result)) {
                        // Prepare content for output
-                       $content['sw']  = $SW;
                        $content['res'] = translateComma($content['pay_rate'] * $content['pay_min_count']);
 
                        // Load row template and switch color
-                       $OUT .= loadTemplate('guest_sponsor_payment_row', false, $content);
-                       $SW = 3 - $SW;
-               }
-               $OUT .= "</table>\n";
+                       $OUT .= loadTemplate('guest_sponsor_payment_row', TRUE, $content);
+               } // END - while
 
                // Remember the list in table
-               $content['payment_list'] = $OUT;
+               $content['payment_list'] = loadTemplate('guest_sponsor_payment', TRUE, $OUT);
 
                // Init a lot array elements
-               foreach (array('company','position','tax_ident','gender_m','gender_f','gender_c','surname','family','street_nr1','street_nr2','country','zip','city','phone','fix','cell','email','url','rec_y','rec_n','form_errors','refid') as $entry) {
+               foreach (array('company','position','tax_ident','gender_m','gender_f','gender_c','surname','family','street_nr1','street_nr2','country','zip','city','phone','fax','cell','email','url','receive_warnings_y','receive_warnings_n','form_errors','refid') as $entry) {
                        $content[$entry]   = '';
                } // END - foreach
 
                // Check for invalid entries
-               if (count($FORM_ERRORS) > 0) {
+               if (isFilledArray($formErrors)) {
                        // Some found... :-(
-                       foreach (array('company','position','tax_ident','surname','family','street_nr1','street_nr2','country','zip','city','phone','fix','cell','email','url') as $entry) {
-                               $content[$entry]    = secureString(postRequestElement($entry));
-                       } // END - foreach
-
-                       // Init gender
-                       foreach (array('m', 'f', 'c') as $gender) {
-                               $content['gender_' . $gender] = '';
+                       foreach (array('company','position','tax_ident','surname','family','street_nr1','street_nr2','country','zip','city','phone','fax','cell','email','url') as $entry) {
+                               $content[$entry]    = postRequestElement($entry);
                        } // END - foreach
 
-                       // Check for gender selection
-                       $content['gender_' . strtolower(postRequestElement('gender'))] = ' selected="selected"';
-
                        // Init receive selection
-                       $content['rec_y'] = '';
-                       $content['rec_n'] = '';
+                       $content['receive_warnings_y'] = '';
+                       $content['receive_warnings_n'] = '';
 
                        // Check for receive_warnings
-                       $content['rec_' . strtolower(postRequestElement('receive_warnings'))] = ' selected="selected"';
+                       $content['receive_warnings_' . strtolower(postRequestElement('receive_warnings'))] = ' selected="selected"';
                        $OUT = '';
 
                        // Errors found?
-                       if (count($FORM_ERRORS) > 0) {
-                               $OUT = "<div align=\"center\">
-  <strong><div class=\"guest_failed\">".getMessage('SPONSOR_REGISTER_SOMETHING_MISSING').":</div></strong><br />
-</div>
-<ol>\n";
-                               foreach ($FORM_ERRORS as $error) {
-                                       $OUT .= "<li>".$error."</li>\n";
+                       if (isFilledArray($formErrors)) {
+                               $OUT = returnMessage('{--SPONSOR_REGISTER_SOMETHING_MISSING--}');
+                               $OUT .= '<ol>';
+                               foreach ($formErrors as $error) {
+                                       $OUT .= '<li>' . $error . '</li>';
                                } // END - foreach
 
-                               $OUT .= "</ol><br />\n";
+                               $OUT .= '</ol>';
                        } // END - if
 
                        $content['form_errors'] = $OUT;
-                       $content['refid']       = bigintval(postRequestElement('refid'));
-               } // END - if
 
-               // Prepare referal id
-               $content['refid'] = determineReferalId();
+                       // @TODO Maybe a default referral id?
+                       $content['refid'] = NULL;
+
+                       // Is the referral id valid?
+                       if ((isPostRequestElementSet('refid')) && (isValidId(postRequestElement('refid')))) {
+                               // Transfer referral id
+                               $content['refid'] = bigintval(postRequestElement('refid'));
+                       } // END - if
+               } // END - if
 
                // Display registration form
-               loadTemplate('guest_sponsor_reg', false, $content);
+               loadTemplate('guest_sponsor_reg', FALSE, $content);
        } else {
                // Nothing added so far
-               loadTemplate('admin_settings_saved', false, getMessage('SPONSOR_PAYMENT_LIST_IS_EMPTY'));
+               displayMessage('{--SPONSOR_PAYMENT_LIST_EMPTY--}');
        }
 
        // Free memory
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 }
 
 // [EOF]