-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…eanup-3 Registration cleanup (part 3)
- Loading branch information
Showing
6 changed files
with
402 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
header("Location: ../index.php"); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
modules/registration/validators/validateInputs.validators.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
|
||
?> |
Oops, something went wrong.