]> git.mxchange.org Git - mailer.git/blobdiff - inc/libs/wernis_functions.php
Continued:
[mailer.git] / inc / libs / wernis_functions.php
index e01bcb9c31c0fc0813eb3fc47c8e21c0faec19bc..c9fe0da434ea4cb7b4f0fdbc3ddd449d02338e20 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -72,7 +72,7 @@ function getWernisErrorCode () {
 }
 
 // Sends out a request to the API and returns it's result
-function sendWernisApiRequest ($scriptName, $requestData = array()) {
+function sendWernisApiRequest ($scriptName, $requestData = []) {
        // Debug call
        //* DEBUG */ reportBug(__FUNCTION__, __LINE__, 'scriptName=' . $scriptName . ',requestData=<pre>' . print_r($requestData, TRUE) . '</pre>');
 
@@ -113,6 +113,15 @@ function sendWernisApiRequest ($scriptName, $requestData = array()) {
        // Get the raw response from the lower function
        $response = sendHttpPostRequest($requestString, $requestData);
 
+       // Debug message
+       //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf('isDeveloperSystem()=%d', intval(isDeveloperSystem())));
+
+       // Log response only for development
+       if (isDeveloperSystem()) {
+               // Log whole message
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf('response[%s]=%s', gettype($response), print_r($response, TRUE)));
+       } // END - if
+
        // Check the response header if all is fine
        if (!isHttpStatusOkay($response[0])) {
                // Something bad happend... :(
@@ -148,7 +157,7 @@ function sendWernisApiRequest ($scriptName, $requestData = array()) {
        $data = explode('=', $responseLine);
 
        // Default return array (should not stay empty)
-       $return = array();
+       $return = [];
 
        // We use only the first two entries (which shall be fine)
        if ($data[0] === 'error') {
@@ -563,7 +572,7 @@ function doDisplayWernisUserRegistrationForm () {
 
                        // Status was okay?
                        if (isHttpResponseStatusOkay($args)) {
-                               // Is status set?
+                               // Is auth_status set?
                                //* DEBUG-DIE */ die('response=<pre>' . print_r($response, TRUE) . '</pre>,args=' . '<pre>'.print_r($args, TRUE).'</pre>');
                                assert(isset($args['auth_status']));
 
@@ -641,7 +650,7 @@ function doWernisFinishUserRegistration ($challenge, $challengeResponse, $status
                        $return['mapped_data'] = runFilterChain('register_must_fillout', $return['mapped_data']);
 
                        // Add missing elements
-                       $return['mapped_data']['gender']               = NULL;
+                       $return['mapped_data']['sex']               = 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');
@@ -674,13 +683,13 @@ function getWernisMappedDataFromApiByChallenge ($challenge, $challengeResponse,
 
                // Display form
                loadTemplate('guest_wernis_registration_rpc_form');
-               return array();
+               return [];
        } // END - if
 
        // Init array
        $return = array(
                // Mapped data
-               'mapped_data' => array(),
+               'mapped_data' => [],
                // Any error message from API
                'message'     => ''
        );
@@ -743,7 +752,7 @@ function getWernisMappedDataFromApiByChallenge ($challenge, $challengeResponse,
                                reportBug(__FUNCTION__, __LINE__, 'Cannot map from=' . $from . ' -> to=' . $to . ': element does not exist.');
                        } // END - if
 
-                       // "Map" all
+                       // "Map" all and make empty strings to NULL
                        $return['mapped_data'][$to] = convertEmptyToNull($userData[$from]);
                } // END - foreach
 
@@ -790,7 +799,7 @@ LIMIT 1",
 // "Getter" for Wernis registration data by given key and value
 function getWernisRegistrationDataByKey ($key, $value, $limit = 1) {
        // Init array
-       $rows = array();
+       $rows = [];
 
        // Now search for it
        $result = sqlQueryEscaped("SELECT
@@ -806,7 +815,7 @@ FROM
 WHERE
        `%s`='%s'
 ORDER BY
-       `id`
+       `id` ASC
 LIMIT %d",
                array(
                        $key,
@@ -863,22 +872,13 @@ function generateWernisDecryptionKey ($publicKey) {
        return $key;
 }
 
-//-----------------------------------------------------------------------------
-//                      Auth status callback functions
-//-----------------------------------------------------------------------------
-
-// Handler for auth_status=PENDING
-function doWernisAuthPending ($args) {
-       // $args must always be an array
+// Registers or updates Wernis registration data
+function registerUpdateWernisRegistrationCall ($args, $challenge, $status) {
+       // Make sure the required array elements are there
        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
@@ -889,12 +889,13 @@ function doWernisAuthPending ($args) {
        `api_redirect_challenge`
 ) VALUES (
        %s,
-       'PENDING',
+       '%s',
        '%s',
        '%s'
 )",
                        array(
                                bigintval($args['wernis_userid']),
+                               $status,
                                $args['auth_key'],
                                $challenge
                        ), __FUNCTION__, __LINE__
@@ -904,22 +905,46 @@ function doWernisAuthPending ($args) {
                sqlQueryEscaped("UPDATE
        `{?_MYSQL_PREFIX?}_wernis_regs`
 SET
-       `api_redirect_challenge`='%s'
+       `api_redirect_challenge`='%s',
+       `api_auth_status`='%s'
 WHERE
        `api_auth_key`='%s' AND
-       `wernis_userid`=%s AND
-       `api_auth_status`='PENDING'
+       `wernis_userid`=%s
 LIMIT 1",
                        array(
                                $challenge,
+                               $status,
                                $args['auth_key'],
                                bigintval($args['wernis_userid'])
                        ), __FUNCTION__, __LINE__
                );
        }
 
-       // Should always update/insert
-       assert(sqlAffectedRows() == 1);
+       // Return whether something has been inserted or updated
+       return (sqlAffectedRows() == 1);
+}
+
+//-----------------------------------------------------------------------------
+//                      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));
+
+       // Register or update the record
+       $registered = registerUpdateWernisRegistrationCall($args, $challenge, 'PENDING');
+
+       // Should always register/update
+       assert($registered === TRUE);
 
        // Redirect to WDS66 module=auth ...
        //* DEBUG-DIE */ die(__FUNCTION__ . ':' . __LINE__ . '<pre>' . print_r($args, TRUE) . '</pre>');
@@ -938,6 +963,27 @@ function doWernisAuthAccepted ($args) {
        // Get registration data by auth_key
        $rows = getWernisRegistrationDataByKey('api_auth_key', $args['auth_key']);
 
+       // Is developer system?
+       if (isDeveloperSystem()) {
+               // Log whole rows array
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf('args[%s]=%s,rows[%s]=%s', gettype($args), print_r($args, TRUE), gettype($rows), print_r($rows, TRUE)));
+       } // END - if
+
+       // Is rows empty?
+       if (count($rows) == 0) {
+               // Generate challenge
+               $challenge = hashSha256(generatePassword(128));
+
+               // Then register it ...
+               $registered = registerUpdateWernisRegistrationCall($args, $challenge, $args['auth_status']);
+
+               // Should always register/update
+               assert($registered === TRUE);
+
+               // ... and redirect to module=auth
+               redirectToUrl(getWernisBaseUrl() . '/modules.php?module=auth&amp;auth_key=' . trim($args['auth_key']) . '&amp;params=' . urlencode(base64_encode('&module=' . getModule() . '&what=' . getWhat())) . '&amp;challenge=' . $challenge, FALSE, FALSE);
+       } // END - if
+
        // The userid should be same
        assert(isset($rows[0]['wernis_userid']));
        assert($rows[0]['wernis_userid'] === $args['wernis_userid']);
@@ -973,7 +1019,7 @@ function doTemplateSelectWernisEncryptionAlgorithm ($templateName, $clear = FALS
        //* DEBUG-DIE */ die('algorithms=<pre>' . print_r($algorithms, TRUE) . '</pre>');
 
        // Init array
-       $options = array();
+       $options = [];
 
        // And fill it
        foreach ($algorithms as $key => $dummy) {
@@ -993,7 +1039,7 @@ function doTemplateSelectWernisEncryptionMode ($templateName, $clear = FALSE, $d
        $modes = getSupportedEncryptionModes();
 
        // Init array
-       $options = array();
+       $options = [];
 
        // And fill it
        foreach ($modes as $key => $dummy) {