]> git.mxchange.org Git - mailer.git/blobdiff - inc/libs/wernis_functions.php
Rewrote 2 str_replace() calls to one with array()
[mailer.git] / inc / libs / wernis_functions.php
index 1e8dc198005711d9ccbc78bacba7f9bc86c64879..ff782fac567f9dbf6643cae438b06893ad6e0e46 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!
@@ -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());
 
@@ -224,7 +224,7 @@ function WERNIS_TEST_API () {
        );
 
        // 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') {
@@ -232,7 +232,7 @@ function WERNIS_TEST_API () {
                $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 executeWernisWithdraw ($wdsId, $userMd5, $amount) {
        // Is the sponsor extension installed?
        if (!isWernisWithdrawActive()) {
                if (!isExtensionActive('sponsor')) {
@@ -266,29 +266,28 @@ 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') {
                // 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 executeWernisPayout ($wdsId, $amount) {
        // Default is failed attempt
        $result = FALSE;
 
@@ -303,26 +302,41 @@ 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') {
                // 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 executeWernisAuth ($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;
+}
+
 // Translate the status IN/OUT
 function translateWernisTransferStatus ($status) {
        // Default status is unknown
@@ -342,7 +356,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 +369,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 +403,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 +418,139 @@ 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 = executeWernisAuth(postRequestElement('wernis_id'), postRequestElement('wernis_password'));
+
+                       // Was the status okay?
+                       if ((isset($response['status'])) && ($response['status'] == 'OK') && (!empty($response['response']))) {
+                               // All fine, then analyze 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);
+                               die(__FUNCTION__ . ': status[' . gettype($status) . ']=' . $status);
+                       } else {
+                               // Something bad happened
+                               displayMessage($response['message']);
+                       }
+               }
+       } // END - if
+
+       // Is the form not sent? (E.g. missing form fields)
+       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 again
+               $response = $salt . hashSha256($salt . getWernisApiMd5() . getRequestElement('challenge'));
+
+               // Is the response valid?
+               if ($response != 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. So get.php can be called.
+                */
+               die('!ojk');
+       } elseif (!isFormSent('register')) {
+               // Form not send, so load form template
+               loadTemplate('guest_wernis_registration_form');
+       } // END - if
+}
+
+//-----------------------------------------------------------------------------
+//                      Auth status callback functions
+//-----------------------------------------------------------------------------
+
+// Handler for auth_status=PENDING
+function doWernisAuthPending ($args) {
+       // auth_key must be set
+       assert(isset($args['auth_key']));
+
+       // 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__
+               );
+
+               // Should be inserted
+               assert(sqlAffectedRows() == 1);
+       } 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 be updated
+               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);
+}
+
 //-----------------------------------------------------------------------------
 //                             Wrapper functions
 //-----------------------------------------------------------------------------
@@ -611,5 +759,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]
 ?>