Opps, not all elements for sprintf() has been set.
[mailer.git] / inc / libs / wernis_functions.php
index 48f3677c9c2a4c0e3efd7061f5c07f39b0dc0bd1..263e40935d67291ca4f4e33c84b604d0e37058ce 100644 (file)
@@ -41,13 +41,13 @@ if (!defined('__SECURITY')) {
 } // END - if
 
 // Sets a status message and code
-function WERNIS_STATUS_MESSAGE ($message, $status) {
+function setWernisStatusMessage ($message, $status) {
        $GLOBALS['wernis_data']['message'] = $message;
        $GLOBALS['wernis_data']['status']  = $status;
 }
 
 // Get the status message
-function GET_WERNIS_ERROR_MESSAGE () {
+function getWernisErrorMessage () {
        if (isset($GLOBALS['wernis_data']['message'])) {
                // Use raw message
                return $GLOBALS['wernis_data']['message'];
@@ -61,7 +61,7 @@ function GET_WERNIS_ERROR_MESSAGE () {
 }
 
 // Get the status code
-function GET_WERNIS_ERROR_CODE () {
+function getWernisErrorCode () {
        if (isset($GLOBALS['wernis_data']['status'])) {
                // Use raw message
                return $GLOBALS['wernis_data']['status'];
@@ -72,7 +72,7 @@ function GET_WERNIS_ERROR_CODE () {
 }
 
 // Sends out a request to the API and returns it's result
-function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
+function sendWernisApiRequest ($scriptName, $requestData = array()) {
        // Is the requestData an array?
        if (!is_array($requestData)) {
                // Then abort here!
@@ -96,7 +96,7 @@ function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
        $requestData['api_key'] = getWernisApiMd5();
 
        // Is a purpose there?
-       if (isset($requestData['purpose'])) {
+       if (!empty($requestData['purpose'])) {
                // Eval the purpose
                eval('$purpose = "' . doFinalCompilation($requestData['purpose'], FALSE) . '";');
 
@@ -210,7 +210,7 @@ function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
 }
 
 // Tests the function by calling balance.php on the API
-function WERNIS_TEST_API () {
+function doAdminTestWernisApi () {
        // Only as admin
        assert(isAdmin());
 
@@ -219,20 +219,20 @@ function WERNIS_TEST_API () {
 
        // Prepare the request data
        $requestData = array(
-               't_uid'       => getWernisRefid(),
-               't_md5'       => getWernisPassMd5()
+               't_uid' => getWernisRefid(),
+               't_md5' => getWernisPassMd5()
        );
 
        // Return the result from the lower functions
-       $return = WERNIS_SEND_REQUEST('balance.php', $requestData);
+       $return = sendWernisApiRequest('balance.php', $requestData);
 
        // Did it went smoothly?
-       if ($return['status'] == 'OK') {
+       if (isHttpResponseStatusOkay($return)) {
                // All fine!
                $result = TRUE;
        } else {
                // Status failure text
-               WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
+               setWernisStatusMessage($return['message'], $return['status']);
        }
 
        // Return result
@@ -240,7 +240,7 @@ function WERNIS_TEST_API () {
 }
 
 // Widthdraw this amount
-function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
+function executeWernisApiWithdraw ($wdsId, $userMd5, $amount) {
        // Is the sponsor extension installed?
        if (!isWernisWithdrawActive()) {
                if (!isExtensionActive('sponsor')) {
@@ -266,29 +266,29 @@ function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
        );
 
        // Return the result from the lower functions
-       $return = WERNIS_SEND_REQUEST('book.php', $requestData);
+       $return = sendWernisApiRequest('book.php', $requestData);
 
-       if ($return['status'] == 'OK') {
+       // Did it went smoothly?
+       if (isHttpResponseStatusOkay($return)) {
                // All fine!
                $result = TRUE;
 
                // Log the transfer
-               WERNIS_LOG_TRANSFER($wdsId, $amount, 'WITHDRAW');
+               logWernisTransfer($wdsId, $amount, 'WITHDRAW');
        } else {
                // Status failure text
-               WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
+               setWernisStatusMessage($return['message'], $return['status']);
 
                // Log the transfer
-               WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
+               logWernisTransfer($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
        }
 
        // Return result
        return $result;
 }
 
-
 // Payout this amount
-function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
+function executeWernisApiPayout ($wdsId, $amount) {
        // Default is failed attempt
        $result = FALSE;
 
@@ -303,26 +303,67 @@ function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
        );
 
        // Return the result from the lower functions
-       $return = WERNIS_SEND_REQUEST('book.php', $requestData);
+       $return = sendWernisApiRequest('book.php', $requestData);
 
-       if ($return['status'] == 'OK') {
+       if (isHttpResponseStatusOkay($return)) {
                // All fine!
                $result = TRUE;
 
                // Log the transfer
-               WERNIS_LOG_TRANSFER($wdsId, $amount, 'PAYOUT');
+               logWernisTransfer($wdsId, $amount, 'PAYOUT');
        } else {
                // Status failure text
-               WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
+               setWernisStatusMessage($return['message'], $return['status']);
 
                // Log the transfer
-               WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
+               logWernisTransfer($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
        }
 
        // Return result
        return $result;
 }
 
+// Execute auth.php request
+function executeWernisApiAuth ($wernisId, $wernisPassword) {
+       // Prepare request data
+       $requestData = array(
+               't_uid'       => bigintval($wernisId),
+               't_md5'       => hashSha256($wernisPassword),
+       );
+
+       // Call auth.php
+       $return = sendWernisApiRequest('auth.php', $requestData);
+
+       // Return full array
+       return $return;
+}
+
+// Execute get.php reguest with given auth data (not all are used)
+function executeWernisApiGet ($authData, $subRequest, $fields) {
+       // It must be an array
+       assert(is_array($authData));
+
+       // Check required array elements
+       assert(isset($authData['wernis_userid']));
+       assert(isset($authData['api_auth_key']));
+       assert(isset($authData['api_redirect_challenge']));
+
+       // Then create request array
+       $requestData = array(
+               'sub_request' => $subRequest,
+               'fields'      => $fields,
+               't_uid'       => bigintval($authData['wernis_userid']),
+               'auth_key'    => $authData['api_auth_key'],
+               'challenge'   => $authData['api_redirect_challenge']
+       );
+
+       // Call get.php
+       $return = sendWernisApiRequest('get.php', $requestData);
+
+       // Return full array
+       return $return;
+}
+
 // Translate the status IN/OUT
 function translateWernisTransferStatus ($status) {
        // Default status is unknown
@@ -342,7 +383,7 @@ function translateWernisTransferStatus ($status) {
 }
 
 // Log the transfer
-function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
+function logWernisTransfer ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
        // Register this wernis movement
        sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_wernis` (`userid`, `wernis_account`, `wernis_amount`, `wernis_timestamp`, `wernis_type`, `wernis_api_message`, `wernis_api_status`) VALUES (%s, %s, %s, UNIX_TIMESTAMP(), '%s', '%s', '%s')",
                array(
@@ -355,8 +396,8 @@ function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '',
                ), __FUNCTION__, __LINE__);
 }
 
-// Take fees and factor
-function WERNIS_TAKE_FEE ($points, $mode) {
+// Calulcate fees and factor
+function calculateWernisFee ($points, $mode) {
        // Payout or withdraw are allowed modes!
        //* DEBUG: */ debugOutput('mode=' . $mode . ',points=' . $points);
        if (!in_array($mode, array('payout', 'withdraw'))) {
@@ -389,7 +430,8 @@ function WERNIS_TAKE_FEE ($points, $mode) {
 }
 
 // Add withdraw fees and factor
-function WERNIS_ADD_WITHDRAW_FEE ($points) {
+// @TODO Unused?
+function calulcateWernisWithdrawFee ($points) {
        // Is there a percentage or fixed fee?
        if (getWernisWithdrawFeePercent() > 0) {
                // Percentage fee
@@ -403,6 +445,380 @@ function WERNIS_ADD_WITHDRAW_FEE ($points) {
        return $points;
 }
 
+// Displays registration form for WDS66 registration
+function doDisplayWernisUserRegistrationForm () {
+       // Is the form sent?
+       if (isFormSent('register')) {
+               // Is wernis_id set?
+               if (!isPostRequestElementSet('wernis_id')) {
+                       // Id not set
+                       displayMessage('{--GUEST_WERNIS_REGISTRATION_ID_NOT_SET--}');
+               } elseif (!isPostRequestElementSet('wernis_password')) {
+                       // Password not set
+                       displayMessage('{--GUEST_WERNIS_REGISTRATION_PASSWORD_NOT_SET--}');
+               } else {
+                       // So far, all fine, then let's do the call-back on auth.php ...
+                       $response = executeWernisApiAuth(postRequestElement('wernis_id'), postRequestElement('wernis_password'));
+
+                       // Was the status okay?
+                       if (isHttpResponseStatusOkay($response)) {
+                               // All fine, then analyze API response
+                               $args = convertApiResponseToArray($response['response'], '&', '=');
+
+                               // Is status set?
+                               assert(isset($args['auth_status']));
+
+                               // Add WDS66 userid
+                               $args['wernis_userid'] = postRequestElement('wernis_id');
+
+                               // "Detect" auth status
+                               $callbackFunction = 'doWernisAuth' . capitalizeUnderscoreString($args['auth_status']);
+
+                               // Is the call-back there?
+                               if (!is_callable($callbackFunction, FALSE, $callableName)) {
+                                       // Not there, could be bad. :(
+                                       reportBug(__FUNCTION__, __LINE__, 'Unsupported auth_status=' . $args['auth_status'] . ',args()=' . count($args) . ',callbackFunction=' . $callbackFunction . ' detected.');
+                               } // END - if
+
+                               // Then call it
+                               $status = call_user_func($callbackFunction, $args);
+
+                               // @TODO Something more to do here?
+                               die(__FUNCTION__ . ':' . __LINE__ . ': status[' . gettype($status) . ']=' . $status . ' - Unfinished.');
+                       } else {
+                               // Something bad happened
+                               displayMessage($response['message']);
+                       }
+               }
+       } // END - if
+
+       // Is there a challenge + response?
+       if ((isGetRequestElementSet('status')) && (isGetRequestElementSet('challenge')) && (isGetRequestElementSet('__challenge_response'))) {
+               // Redirect from modules.php?module=auth, so validate challenge response ...
+               // 1) Get first 24 characters = salt
+               $salt = substr(getRequestElement('__challenge_response'), 0, 24);
+
+               // 2) Generate hash for challenge response
+               $challengeResponse = $salt . hashSha256($salt . getWernisApiMd5() . getRequestElement('challenge'));
+
+               // Is the response valid?
+               if ($challengeResponse != getRequestElement('__challenge_response')) {
+                       // Not valid
+                       displayMessage('{--GUEST_WERNIS_REGISTRATION_INVALID_CHALLENGE_RESPONSE--}');
+                       return;
+               } // END - if
+
+               /*
+                * Now, that the challenge-response is the same, the challenge itself
+                * is also the same. Next get the data from wernis_regs table by
+                * challenge. There is currently no other way to get the data as there
+                * is no Wernis user id provided. Later on the stored challenge response
+                * can be compared with provided.
+                */
+               $return = doWernisFinishUserRegistration(getRequestElement('challenge'), getRequestElement('__challenge_response'), getRequestElement('status'));
+
+               // Is the registration finished?
+               if ($return === FALSE) {
+                       // No, then abort here silently as the function should have already displayed a message
+                       return;
+               } // END - if
+       } elseif (!isFormSent('register')) {
+               // Form not send, so load form template
+               loadTemplate('guest_wernis_registration_rpc_form');
+       }
+}
+
+// Finish user registration with WDS66 API
+function doWernisFinishUserRegistration ($challenge, $challengeResponse, $status) {
+       // Is the status 1? (= all fine with API call)
+       if ($status == '1') {
+               // Get mapped data based on challenge
+               $return = getWernisMappedDataFromApiByChallenge($challenge, $status);
+
+               // Is the array filled?
+               if ((count($return['mapped_data']) > 0) && (empty($return['message']))) {
+                       // Set must-fillout fields
+                       $return['mapped_data'] = runFilterChain('register_must_fillout', $return['mapped_data']);
+
+                       // Add missing elements
+                       $return['mapped_data']['gender']               = NULL;
+                       $return['mapped_data']['birthday_selection']   = generateDayMonthYearSelectionBox($return['mapped_data']['birth_day'], $return['mapped_data']['birth_month'], $return['mapped_data']['birth_year']);
+                       $return['mapped_data']['challenge']            = getRequestElement('challenge');
+                       $return['mapped_data']['__challenge_response'] = getRequestElement('__challenge_response');
+
+                       // Display form
+                       loadTemplate('guest_wernis_registration_form', FALSE, $return['mapped_data']);
+
+                       // All fine
+                       return TRUE;
+               } else {
+                       // Something unexpected happened (e.g. no API requests left)
+                       displayMessage($return['message']);
+                       return FALSE;
+               }
+       } else {
+               // Status does not need to be changed
+               die(__FUNCTION__ . ':' . __LINE__ . ': Reached!');
+       }
+}
+
+// "Getter" for mapped data by calling the API and given challenge and status
+function getWernisMappedDataFromApiByChallenge ($challenge, $status) {
+       // Get stored registration data
+       $rows = getWernisRegistrationDataByKey('api_redirect_challenge', $challenge);
+
+       // Zero result found?
+       if (count($rows) == 0) {
+               // Nothing found
+               displayMessage('{--GUEST_WERNIS_REGISTRATION_ZERO_ROWS_FOUND--}');
+
+               // Display form
+               loadTemplate('guest_wernis_registration_rpc_form');
+               return array();
+       } // END - if
+
+       // Init array
+       $return = array(
+               // Mapped data
+               'mapped_data' => array(),
+               // Any error message from API
+               'message'     => ''
+       );
+
+       // Has the auth status changed?
+       if ($rows[0]['api_auth_status'] != 'ACCEPTED') {
+               /*
+                * The authorization of this application has been accepted, so
+                * update it and ignore result from function because the update
+                * will always run.
+                */
+               updateWernisRegistrationDataByKey('api_auth_status', 'api_redirect_challenge', $challenge, 'ACCEPTED');
+       } // END - if
+
+       // Now call "get.php"
+       $response = executeWernisApiGet($rows[0], 'data', 'vorname|name|strasse|plz|ort|birth_day|birth_month|birth_year|email|werber');
+
+       // Was the status okay?
+       if (isHttpResponseStatusOkay($response)) {
+               // API returned non-errous response, 'data=' must be found
+               assert(substr($response['response'], 0, 5) == 'data=');
+
+               // And remove it, this is now BASE64-encoded
+               $encodedData = urldecode(substr($response['response'], 5));
+
+               // And decode it (all steps separated to later "easily" debug them)
+               $decodedData = base64_decode($encodedData);
+
+               /*
+                * Do some checks on the decoded string, it should be a
+                * serialized array with 10 entries (see above
+                * executeWernisApiGet() call).
+                */
+               assert(substr($decodedData, 0, 6) == 'a:10:{');
+               assert(substr($decodedData, -1, 1) == '}');
+
+               // The array seems to be fine, unserialize it
+               $userData = unserialize($decodedData);
+
+               // All mappings WDS66->mailer
+               $mappings = array(
+                       'vorname'     => 'surname',
+                       'name'        => 'family',
+                       'strasse'     => 'street_nr',
+                       'plz'         => 'zip',
+                       'ort'         => 'city',
+                       'email'       => 'email',
+                       'birth_day'   => 'birth_day',
+                       'birth_month' => 'birth_month',
+                       'birth_year'  => 'birth_year',
+                       'werber'      => 'wernis_refid'
+               );
+
+               // Map all WDS66 entries into mailer entries
+               foreach ($mappings as $from => $to) {
+                       // All must exist
+                       if (!isset($userData[$from])) {
+                               // Element $from does not exist
+                               reportBug(__FUNCTION__, __LINE__, 'Cannot map from=' . $from . ' -> to=' . $to . ': element does not exist.');
+                       } // END - if
+
+                       // "Map" all
+                       $return['mapped_data'][$to] = convertEmptyToNull($userData[$from]);
+               } // END - foreach
+
+               // Both arrays must have same size
+               assert(count($userData) == count($return['mapped_data']));
+
+               // Now add userid from WDS66
+               $return['mapped_data']['wernis_userid'] = bigintval($rows[0]['wernis_userid']);
+       } else {
+               // Something bad happened so copy the message
+               $return['message'] = $response['message'];
+       }
+
+       // Return mapped data array
+       return $return;
+}
+
+// Updates auth status by given key/value pair
+function updateWernisRegistrationDataByKey ($updatedColumn, $key, $oldValue, $newValue) {
+       // Run the update
+       sqlQueryEscaped("UPDATE
+       `{?_MYSQL_PREFIX?}_wernis_regs`
+SET
+       `%s` = '%s'
+WHERE
+       `%s` = '%s' AND
+       `%s` != '%s'
+LIMIT 1",
+               array(
+                       $updatedColumn,
+                       $newValue,
+                       $key,
+                       $oldValue,
+                       $updatedColumn,
+                       $oldValue
+               ), __FUNCTION__, __LINE__
+       );
+
+       // Check if rows as been affected
+       return ifSqlHasZeroAffectedRows();
+}
+
+// "Getter" for Wernis registration data by given key and value
+function getWernisRegistrationDataByKey ($key, $value, $limit = 1) {
+       // Init array
+       $rows = array();
+
+       // Now search for it
+       $result = sqlQueryEscaped("SELECT
+       `local_userid`,
+       `wernis_userid`,
+       `api_auth_status`,
+       `api_auth_key`,
+       `api_redirect_challenge`,
+       UNIX_TIMESTAMP(`record_inserted`) AS `record_inserted`
+FROM
+       `{?_MYSQL_PREFIX?}_wernis_regs`
+WHERE
+       `%s`='%s'
+ORDER BY
+       `id`
+LIMIT %d",
+               array(
+                       $key,
+                       $value,
+                       $limit
+               ), __FUNCTION__, __LINE__
+       );
+
+       // Is there an entry?
+       if (sqlNumRows($result) > 0) {
+               // At least one entry has been found, so loop through all
+               while ($row = sqlFetchArray($result)) {
+                       // Add it
+                       array_push($rows, $row);
+               } // END - while
+       } // END - if
+
+       // Free result
+       sqlFreeResult($result);
+
+       // Return found entries
+       return $rows;
+}
+
+// Do local user registration with data from WDS66 API
+function doWernisUserRegistration () {
+       // Call generic registration function
+       $status = doGenericUserRegistration();
+
+       // Does this went fine?
+       if ($status === FALSE) {
+               // No, then abort here silently
+               return FALSE;
+       } // END - if
+
+       // Make sure the user id is valid
+       assert(isset($GLOBALS['register_userid']));
+       assert(isValidId($GLOBALS['register_userid']));
+
+       // Generic registration is finished, so add more data:
+}
+
+//-----------------------------------------------------------------------------
+//                      Auth status callback functions
+//-----------------------------------------------------------------------------
+
+// Handler for auth_status=PENDING
+function doWernisAuthPending ($args) {
+       // $args must always be an array
+       assert(is_array($args));
+
+       // auth_key and wernis_userid must be set
+       assert(isset($args['auth_key']));
+       assert(isset($args['wernis_userid']));
+
+       // Generate a challenge that will be added to the URL
+       $challenge = hashSha256(generatePassword(128));
+
+       // Search entry in database by auth_key
+       if (countSumTotalData($args['auth_key'], 'wernis_regs', 'id', 'api_auth_key', TRUE) == 0) {
+               // "Register" this call
+               sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_wernis_regs` (
+       `wernis_userid`,
+       `api_auth_status`,
+       `api_auth_key`,
+       `api_redirect_challenge`
+) VALUES (
+       %s,
+       'PENDING',
+       '%s',
+       '%s'
+)",
+                       array(
+                               bigintval($args['wernis_userid']),
+                               $args['auth_key'],
+                               $challenge
+                       ), __FUNCTION__, __LINE__
+               );
+       } else {
+               // Update challenge
+               sqlQueryEscaped("UPDATE
+       `{?_MYSQL_PREFIX?}_wernis_regs`
+SET
+       `api_redirect_challenge`='%s'
+WHERE
+       `api_auth_key`='%s' AND
+       `wernis_userid`=%s
+       `api_auth_status`='PENDING'
+LIMIT 1",
+                       array(
+                               $challenge,
+                               $args['auth_key'],
+                               bigintval($args['wernis_userid'])
+                       ), __FUNCTION__, __LINE__
+               );
+       }
+
+       // Should always update/insert
+       assert(sqlAffectedRows() == 1);
+
+       // Redirect to WDS66 module=auth ...
+       redirectToUrl(getWernisBaseUrl() . '/modules.php?module=auth&auth_key=' . $args['auth_key'] . '&params=' . urlencode(base64_encode('&module=' . getModule() . '&what=' . getWhat())) . '&challenge=' . $challenge);
+}
+
+// Handler for auth_status=ACCEPTED
+function doWernisAuthAccepted ($args) {
+       // $args must always be an array
+       assert(is_array($args));
+
+       // auth_key and wernis_userid must be set
+       assert(isset($args['auth_key']));
+       assert(isset($args['wernis_userid']));
+       die(__FUNCTION__ . ':' . __LINE__ . '<pre>' . print_r($args, TRUE) . '</pre>');
+}
+
 //-----------------------------------------------------------------------------
 //                             Wrapper functions
 //-----------------------------------------------------------------------------
@@ -611,5 +1027,17 @@ function getWernisMinWithdraw () {
        return $GLOBALS[__FUNCTION__];
 }
 
+// Wrapper function for 'wernis_base_url'
+function getWernisBaseUrl () {
+       // Is there cache?
+       if (!isset($GLOBALS[__FUNCTION__])) {
+               // Get config entry
+               $GLOBALS[__FUNCTION__] = getConfig('wernis_base_url');
+       } // END - if
+
+       // Return cache
+       return $GLOBALS[__FUNCTION__];
+}
+
 // [EOF]
 ?>