Skip to content

Commit

Permalink
GH-150 Use simple returns & create separate sendResponse function
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Mar 22, 2022
1 parent 35f0bfb commit 02054dd
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions reg_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
function handleRegistration(&$input) {
global $_EnginePath, $_Lang, $_GameConfig;

header('access-control-allow-origin: *');

if (!isset($input['register'])) {
header('Location: index.php');
die('regCallback({});');

return;
return [
'params' => [
'headers' => [
'Location: index.php'
]
],
'payload' => [],
];
}

includeLang('reg_ajax');
Expand Down Expand Up @@ -120,7 +122,10 @@ function handleRegistration(&$input) {
}

if (!empty($JSONResponse['Errors'])) {
die('regCallback('.json_encode($JSONResponse).');');
return [
'params' => null,
'payload' => $JSONResponse,
];
}

unset($JSONResponse['Errors']);
Expand All @@ -134,7 +139,10 @@ function handleRegistration(&$input) {
$JSONResponse['Errors'][] = 15;
$JSONResponse['BadFields'][] = 'email';

die('regCallback('.json_encode($JSONResponse).');');
return [
'params' => null,
'payload' => $JSONResponse,
];
}

$passwordHash = Session\Utils\LocalIdentityV1\hashPassword([
Expand Down Expand Up @@ -250,7 +258,10 @@ function handleRegistration(&$input) {
if (!isGameStartTimeReached($Now)) {
$JSONResponse['Code'] = 2;

die('regCallback('.json_encode($JSONResponse).');');
return [
'params' => null,
'payload' => $JSONResponse,
];
}

$sessionTokenValue = Session\Utils\Cookie\packSessionCookie([
Expand All @@ -269,9 +280,33 @@ function handleRegistration(&$input) {
];
$JSONResponse['Redirect'] = GAMEURL_UNISTRICT.'/overview.php';

die('regCallback('.json_encode($JSONResponse).');');
return [
'params' => null,
'payload' => $JSONResponse,
];
}

handleRegistration($_GET);
function sendResponse($payload, $params) {
header('access-control-allow-origin: *');

if (!empty($params)) {
if (!empty($params['headers'])) {
foreach ($params['headers'] as $header) {
header($header);
}
}
}

$jsonContent = json_encode((object) $payload);

die("regCallback({$jsonContent});");
}

$registrationResult = handleRegistration($_GET);

sendResponse(
$registrationResult['payload'],
$registrationResult['params']
);

?>

0 comments on commit 02054dd

Please sign in to comment.