diff --git a/system/libs/CreateCharacter.php b/system/libs/CreateCharacter.php index 5aa87ad4..6eb4771b 100644 --- a/system/libs/CreateCharacter.php +++ b/system/libs/CreateCharacter.php @@ -12,27 +12,44 @@ class CreateCharacter { /** - * @param string $name - * @param int $sex - * @param int $vocation - * @param int $town - * @param array $errors + * @param $name + * @param $errors * @return bool */ - public function check($name, $sex, &$vocation, &$town, &$errors) { + public function checkName($name, &$errors) + { $minLength = config('character_name_min_length'); $maxLength = config('character_name_max_length'); - if(empty($name)) + if(empty($name)) { $errors['name'] = 'Please enter a name for your character!'; - else if(strlen($name) > $maxLength) - $errors['name'] = 'Name is too long. Max. length '.$maxLength.' letters.'; - else if(strlen($name) < $minLength) - $errors['name'] = 'Name is too short. Min. length '.$minLength.' letters.'; - else { - if(!admin() && !Validator::newCharacterName($name)) { - $errors['name'] = Validator::getLastError(); - } + return false; + } + + if(strlen($name) > $maxLength) { + $errors['name'] = 'Name is too long. Max. length ' . $maxLength . ' letters.'; + return false; + } + + if(strlen($name) < $minLength) { + $errors['name'] = 'Name is too short. Min. length ' . $minLength . ' letters.'; + return false; + } + + $name_length = strlen($name); + if(strspn($name, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM- '") != $name_length) { + $errors['name'] = 'This name contains invalid letters, words or format. Please use only a-Z, - , \' and space.'; + return false; + } + + if(!preg_match("/[A-z ']/", $name)) { + $errors['name'] = 'Your name contains illegal characters.'; + return false; + } + + if(!admin() && !Validator::newCharacterName($name)) { + $errors['name'] = Validator::getLastError(); + return false; } $player = new OTS_Player(); @@ -42,20 +59,38 @@ class CreateCharacter return false; } - if(empty($sex) && $sex != "0") + return empty($errors); + } + + /** + * @param string $name + * @param int $sex + * @param int $vocation + * @param int $town + * @param array $errors + * @return bool + */ + public function check($name, $sex, &$vocation, &$town, &$errors) + { + $this->checkName($name, $errors); + + if(empty($sex) && $sex != "0") { $errors['sex'] = 'Please select the sex for your character!'; + } if(count(config('character_samples')) > 1) { if(!isset($vocation)) $errors['vocation'] = 'Please select a vocation for your character.'; } - else + else { $vocation = config('character_samples')[0]; + } if(count(config('character_towns')) > 1) { - if(!isset($town)) + if(!isset($town)) { $errors['town'] = 'Please select a town for your character.'; + } } else { $town = config('character_towns')[0]; diff --git a/system/libs/validator.php b/system/libs/validator.php index c69f0039..38db7068 100644 --- a/system/libs/validator.php +++ b/system/libs/validator.php @@ -354,16 +354,6 @@ class Validator } } - if(strspn($name, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM- '") != $name_length) { - self::$lastError = 'This name contains invalid letters, words or format. Please use only a-Z, - , \' and space.'; - return false; - } - - if(!preg_match("/[A-z ']/", $name)) { - self::$lastError = 'Your name containst illegal characters.'; - return false; - } - return true; } diff --git a/tools/validate.php b/tools/validate.php index b805778e..b0718062 100644 --- a/tools/validate.php +++ b/tools/validate.php @@ -66,10 +66,10 @@ else if(isset($_GET['name'])) if(!admin() && !Validator::newCharacterName($name)) error_(Validator::getLastError()); - $player = new OTS_Player(); - $player->find($name); - if($player->isLoaded()) { - error_('Character with this name already exist.'); + require_once LIBS . 'CreateCharacter.php'; + $createCharacter = new CreateCharacter(); + if (!$createCharacter->checkName($name, $errors)) { + error_($errors['name']); } success_('Good. Your name will be:
' . (admin() ? $name : ucwords($name)) . '');