diff --git a/system/libs/CreateCharacter.php b/system/libs/CreateCharacter.php index 8a57d604..01fb70ac 100644 --- a/system/libs/CreateCharacter.php +++ b/system/libs/CreateCharacter.php @@ -11,6 +11,57 @@ class CreateCharacter { + /** + * @param $name + * @param $errors + * @return bool + */ + public function checkName($name, &$errors) + { + $minLength = config('character_name_min_length'); + $maxLength = config('character_name_max_length'); + + if(empty($name)) { + $errors['name'] = 'Please enter a name for your character!'; + 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(); + $player->find($name); + if($player->isLoaded()) { + $errors['name'] = 'Character with this name already exist.'; + return false; + } + + return empty($errors); + } + /** * @param string $name * @param int $sex @@ -19,36 +70,27 @@ class CreateCharacter * @param array $errors * @return bool */ - public function check($name, $sex, &$vocation, &$town, &$errors) { - $minLength = config('character_name_min_length'); - $maxLength = config('character_name_max_length'); + public function check($name, $sex, &$vocation, &$town, &$errors) + { + $this->checkName($name, $errors); - 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(); - } - } - - if(empty($sex) && $sex != "0") + 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]; @@ -214,4 +256,4 @@ class CreateCharacter $account->logAction('Created character ' . $name . '.'); return true; } -} \ No newline at end of file +} diff --git a/system/libs/validator.php b/system/libs/validator.php index 81ffbba5..9f109779 100644 --- a/system/libs/validator.php +++ b/system/libs/validator.php @@ -323,16 +323,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 48536c72..a1eec55e 100644 --- a/tools/validate.php +++ b/tools/validate.php @@ -13,6 +13,7 @@ require '../common.php'; require SYSTEM . 'functions.php'; require SYSTEM . 'init.php'; +require SYSTEM . 'login.php'; $error = ''; if(isset($_GET['account'])) @@ -58,8 +59,15 @@ else if(isset($_GET['name'])) if(!Validator::characterName($name)) error_(Validator::getLastError()); - if(!Validator::newCharacterName($name)) + if(!admin() && !Validator::newCharacterName($name)){ error_(Validator::getLastError()); + } + + require_once LIBS . 'CreateCharacter.php'; + $createCharacter = new CreateCharacter(); + if (!$createCharacter->checkName($name, $errors)) { + error_($errors['name']); + } success_('Good. Your name will be:
' . ucwords($name) . ''); }