Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registration cleanup (part 3) #174

Merged
merged 13 commits into from
Mar 19, 2022
3 changes: 3 additions & 0 deletions modules/registration/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

include($includePath . './components/RegistrationConfirmationMail/RegistrationConfirmationMail.component.php');

include($includePath . './input/normalization.input.php');

include($includePath . './utils/cookies.utils.php');
include($includePath . './utils/galaxy.utils.php');
include($includePath . './utils/general.utils.php');
include($includePath . './utils/queries.utils.php');

include($includePath . './validators/validateInputs.validators.php');
include($includePath . './validators/validateTakenParams.validators.php');
include($includePath . './validators/validateReCaptcha.validators.php');

Expand Down
5 changes: 5 additions & 0 deletions modules/registration/input/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
69 changes: 69 additions & 0 deletions modules/registration/input/normalization.input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace UniEngine\Engine\Modules\Registration\Input;

/**
* @param $_GET|$_POST $input
* @param String $input['username']
* @param String $input['password']
* @param String $input['email']
* @param String $input['rules']
* @param String $input['galaxy']
* @param String $input['lang']
*/
function normalizeUserInput(&$input) {
$normalizedUsername = (
isset($input['username']) ?
trim($input['username']) :
null
);
$normalizedPassword = (
isset($input['password']) ?
trim($input['password']) :
null
);
$normalizedEmail = (
isset($input['email']) ?
trim($input['email']) :
null
);
$escapedEmail = getDBLink()->escape_string($normalizedEmail);
$normalizedHasAcceptedRules = (
isset($input['rules']) ?
($input['rules'] == 'on') :
false
);
$normalizedGalaxyNo = (
isset($input['galaxy']) ?
intval($input['galaxy']) :
null
);
$normalizedLangCode = (
(
isset($input['lang']) &&
in_array($input['lang'], UNIENGINE_LANGS_AVAILABLE)
) ?
$input['lang'] :
null
);
$normalizedCaptchaResponse = (
isset($input['captcha_response']) ?
$input['captcha_response'] :
null
);

return [
'username' => $normalizedUsername,
'password' => $normalizedPassword,
'email' => [
'original' => $normalizedEmail,
'escaped' => $escapedEmail
],
'hasAcceptedRules' => $normalizedHasAcceptedRules,
'galaxyNo' => $normalizedGalaxyNo,
'langCode' => $normalizedLangCode,
'captchaResponse' => $normalizedCaptchaResponse,
];
}

?>
21 changes: 21 additions & 0 deletions modules/registration/utils/queries.utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,25 @@ function incrementUsersCounterInGameConfig () {
$_MemCache->GameConfig = $_GameConfig;
}

// Arguments
// - $params (Object)
// - email (String)
//
function updateAllMailChanges ($params) {
// This confirmation type means that the process has been interrupted externally
$confirmType = "4";

$updateMailChangesQuery = (
"UPDATE {{table}} " .
"SET " .
"`ConfirmType` = {$confirmType} " .
"WHERE " .
"`NewMail` = '{$params['email']}' AND " .
"`ConfirmType` = 0 " .
";"
);

doquery($updateMailChangesQuery, 'mailchange');
}

?>
210 changes: 210 additions & 0 deletions modules/registration/validators/validateInputs.validators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

namespace UniEngine\Engine\Modules\Registration\Validators;

function _createFuncWithResultHelpers($func) {
return function ($arguments) use ($func) {
$createSuccess = function ($payload) {
return [
'isSuccess' => true,
'payload' => $payload,
];
};
$createFailure = function ($payload) {
return [
'isSuccess' => false,
'error' => $payload,
];
};

return $func($arguments, [
'createSuccess' => $createSuccess,
'createFailure' => $createFailure,
]);
};
}

function _validateUsername($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['username'];

$minLength = 4;
$maxLenght = 64;

if (strlen($value) < $minLength) {
return $resultHelpers['createFailure']([
'code' => 'USERNAME_TOO_SHORT',
'minLength' => $minLength,
]);
}
if (strlen($value) > $maxLenght) {
return $resultHelpers['createFailure']([
'code' => 'USERNAME_TOO_LONG',
'maxLength' => $maxLenght,
]);
}
if (!preg_match(REGEXP_USERNAME_ABSOLUTE, $value)) {
return $resultHelpers['createFailure']([
'code' => 'USERNAME_INVALID',
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

function _validatePassword($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['password'];

$minLength = 4;

if (strlen($value) < $minLength) {
return $resultHelpers['createFailure']([
'code' => 'PASSWORD_TOO_SHORT',
'minLength' => $minLength,
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

function _validateEmail($normalizedInput) {
$validator = function ($input, $resultHelpers) {
global $_GameConfig;

$value = $input['email'];

$bannedDomains = str_replace('.', '\.', $_GameConfig['BannedMailDomains']);

if (empty($value['escaped'])) {
return $resultHelpers['createFailure']([
'code' => 'EMAIL_EMPTY',
]);
}
if ($value['escaped'] != $value['original']) {
return $resultHelpers['createFailure']([
'code' => 'EMAIL_HAS_ILLEGAL_CHARACTERS',
]);
}
if (!is_email($value['escaped'])) {
return $resultHelpers['createFailure']([
'code' => 'EMAIL_INVALID',
]);
}
if (!empty($bannedDomains) && preg_match('#('.$bannedDomains.')+#si', $value['escaped'])) {
return $resultHelpers['createFailure']([
'code' => 'EMAIL_ON_BANNED_DOMAIN',
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

function _validateGalaxyNo($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['galaxyNo'];

$minGalaxyNo = 1;
$maxGalaxyNo = MAX_GALAXY_IN_WORLD;

if ($value < $minGalaxyNo) {
return $resultHelpers['createFailure']([
'code' => 'GALAXY_NO_TOO_LOW',
'minLength' => $minGalaxyNo,
]);
}
if ($value > $maxGalaxyNo) {
return $resultHelpers['createFailure']([
'code' => 'GALAXY_NO_TOO_HIGH',
'maxLength' => $maxGalaxyNo,
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

function _validateLangCode($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['langCode'];

if (empty($value)) {
return $resultHelpers['createFailure']([
'code' => 'LANG_CODE_EMPTY',
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

function _validateHasAcceptedRules($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['hasAcceptedRules'];

if ($value !== true) {
return $resultHelpers['createFailure']([
'code' => 'RULES_NOT_ACCEPTED',
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

function _validateAntiBot($normalizedInput, $params) {
$validator = function ($input, $resultHelpers) use ($params) {
if (!REGISTER_RECAPTCHA_ENABLE) {
return $resultHelpers['createSuccess']([]);
}

$value = $input['captchaResponse'];

$reCaptchaValidationResult = validateReCaptcha([
'responseValue' => $value,
'currentSessionIp' => $params['userSessionIp']
]);

if (!($reCaptchaValidationResult['isValid'])) {
return $resultHelpers['createFailure']([
'code' => 'RECAPTCHA_VALIDATION_FAILED',
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

// Arguments
// - $normalizedInput (Object)
//
function validateInputs($normalizedInput, $params) {
return [
'username' => _validateUsername($normalizedInput),
'password' => _validatePassword($normalizedInput),
'email' => _validateEmail($normalizedInput),
'galaxyNo' => _validateGalaxyNo($normalizedInput),
'langCode' => _validateLangCode($normalizedInput),
'hasAcceptedRules' => _validateHasAcceptedRules($normalizedInput),
'antiBot' => _validateAntiBot($normalizedInput, $params),
];
}

?>
Loading