// 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... :(
WHERE
`%s`='%s'
ORDER BY
- `id`
+ `id` ASC
LIMIT %d",
array(
$key,
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
`api_redirect_challenge`
) VALUES (
%s,
- 'PENDING',
+ '%s',
'%s',
'%s'
)",
array(
bigintval($args['wernis_userid']),
+ $status,
$args['auth_key'],
$challenge
), __FUNCTION__, __LINE__
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>');
// 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&auth_key=' . trim($args['auth_key']) . '&params=' . urlencode(base64_encode('&module=' . getModule() . '&what=' . getWhat())) . '&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']);