Skip to content

Commit

Permalink
GH-150 Limit shared variable usage when returning payload
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Mar 22, 2022
1 parent 02054dd commit 2b78709
Showing 1 changed file with 50 additions and 51 deletions.
101 changes: 50 additions & 51 deletions reg_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ function handleRegistration(&$input) {
includeLang('reg_ajax');
$Now = time();

$JSONResponse = [
'Errors' => [],
'BadFields' => [],
];

$normalizedInput = Registration\Input\normalizeUserInput($input);
$userSessionIP = Users\Session\getCurrentIP();

Expand All @@ -44,60 +39,65 @@ function handleRegistration(&$input) {
]
);

$errorsJSONPayload = [
'Errors' => [],
'BadFields' => [],
];

foreach ($validationResults as $fieldName => $fieldValidationResult) {
if ($fieldValidationResult['isSuccess']) {
continue;
}

switch ($fieldValidationResult['error']['code']) {
case 'USERNAME_TOO_SHORT':
$JSONResponse['Errors'][] = 1;
$JSONResponse['BadFields'][] = 'username';
$errorsJSONPayload['Errors'][] = 1;
$errorsJSONPayload['BadFields'][] = 'username';
break;
case 'USERNAME_TOO_LONG':
$JSONResponse['Errors'][] = 2;
$JSONResponse['BadFields'][] = 'username';
$errorsJSONPayload['Errors'][] = 2;
$errorsJSONPayload['BadFields'][] = 'username';
break;
case 'USERNAME_INVALID':
$JSONResponse['Errors'][] = 3;
$JSONResponse['BadFields'][] = 'username';
$errorsJSONPayload['Errors'][] = 3;
$errorsJSONPayload['BadFields'][] = 'username';
break;
case 'PASSWORD_TOO_SHORT':
$JSONResponse['Errors'][] = 4;
$JSONResponse['BadFields'][] = 'password';
$errorsJSONPayload['Errors'][] = 4;
$errorsJSONPayload['BadFields'][] = 'password';
break;
case 'EMAIL_EMPTY':
$JSONResponse['Errors'][] = 5;
$JSONResponse['BadFields'][] = 'email';
$errorsJSONPayload['Errors'][] = 5;
$errorsJSONPayload['BadFields'][] = 'email';
break;
case 'EMAIL_HAS_ILLEGAL_CHARACTERS':
$JSONResponse['Errors'][] = 6;
$JSONResponse['BadFields'][] = 'email';
$errorsJSONPayload['Errors'][] = 6;
$errorsJSONPayload['BadFields'][] = 'email';
break;
case 'EMAIL_INVALID':
$JSONResponse['Errors'][] = 7;
$JSONResponse['BadFields'][] = 'email';
$errorsJSONPayload['Errors'][] = 7;
$errorsJSONPayload['BadFields'][] = 'email';
break;
case 'EMAIL_ON_BANNED_DOMAIN':
$JSONResponse['Errors'][] = 8;
$JSONResponse['BadFields'][] = 'email';
$errorsJSONPayload['Errors'][] = 8;
$errorsJSONPayload['BadFields'][] = 'email';
break;
case 'GALAXY_NO_TOO_LOW':
$JSONResponse['Errors'][] = 13;
$JSONResponse['BadFields'][] = 'galaxy';
$errorsJSONPayload['Errors'][] = 13;
$errorsJSONPayload['BadFields'][] = 'galaxy';
break;
case 'GALAXY_NO_TOO_HIGH':
$JSONResponse['Errors'][] = 14;
$JSONResponse['BadFields'][] = 'galaxy';
$errorsJSONPayload['Errors'][] = 14;
$errorsJSONPayload['BadFields'][] = 'galaxy';
break;
case 'LANG_CODE_EMPTY':
$JSONResponse['Errors'][] = 16;
$errorsJSONPayload['Errors'][] = 16;
break;
case 'RULES_NOT_ACCEPTED':
$JSONResponse['Errors'][] = 9;
$errorsJSONPayload['Errors'][] = 9;
break;
case 'RECAPTCHA_VALIDATION_FAILED':
$JSONResponse['Errors'][] = 10;
$errorsJSONPayload['Errors'][] = 10;
break;
}
}
Expand All @@ -112,36 +112,33 @@ function handleRegistration(&$input) {
]);

if ($takenParamsValidationResult['isUsernameTaken']) {
$JSONResponse['Errors'][] = 11;
$JSONResponse['BadFields'][] = 'username';
$errorsJSONPayload['Errors'][] = 11;
$errorsJSONPayload['BadFields'][] = 'username';
}
if ($takenParamsValidationResult['isEmailTaken']) {
$JSONResponse['Errors'][] = 12;
$JSONResponse['BadFields'][] = 'email';
$errorsJSONPayload['Errors'][] = 12;
$errorsJSONPayload['BadFields'][] = 'email';
}
}

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

unset($JSONResponse['Errors']);

$newPlanetCoordinates = Registration\Utils\Galaxy\findNewPlanetPosition([
'preferredGalaxy' => $normalizedInput['galaxyNo']
]);

if ($newPlanetCoordinates === null) {
$JSONResponse['Errors'] = [];
$JSONResponse['Errors'][] = 15;
$JSONResponse['BadFields'][] = 'email';
$errorsJSONPayload['Errors'][] = 15;
$errorsJSONPayload['BadFields'][] = 'email';

return [
'params' => null,
'payload' => $JSONResponse,
'payload' => $errorsJSONPayload,
];
}

Expand Down Expand Up @@ -256,11 +253,11 @@ function handleRegistration(&$input) {
}

if (!isGameStartTimeReached($Now)) {
$JSONResponse['Code'] = 2;

return [
'params' => null,
'payload' => $JSONResponse,
'payload' => [
'Code' => 2
],
];
}

Expand All @@ -273,16 +270,18 @@ function handleRegistration(&$input) {
'isRememberMeActive' => 0,
]);

$JSONResponse['Code'] = 1;
$JSONResponse['Cookie'][] = [
'Name' => getSessionCookieKey(),
'Value' => $sessionTokenValue
];
$JSONResponse['Redirect'] = GAMEURL_UNISTRICT.'/overview.php';

return [
'params' => null,
'payload' => $JSONResponse,
'payload' => [
'Code' => 1,
'Cookie' => [
[
'Name' => getSessionCookieKey(),
'Value' => $sessionTokenValue
]
],
'Redirect' => GAMEURL_UNISTRICT.'/overview.php'
],
];
}

Expand Down

0 comments on commit 2b78709

Please sign in to comment.