Fixes to character name validation

Admin should be able to create any name
Also fixes to config.character_name_min_length being ignored
This commit is contained in:
slawkens 2021-01-20 18:02:07 +01:00
parent 8b41e144f8
commit 81b293a5a6
4 changed files with 32 additions and 14 deletions

View File

@ -35,6 +35,13 @@ class CreateCharacter
} }
} }
$player = new OTS_Player();
$player->find($name);
if($player->isLoaded()) {
$errors['name'] = 'Character with this name already exist.';
return false;
}
if(empty($sex) && $sex != "0") if(empty($sex) && $sex != "0")
$errors['sex'] = 'Please select the sex for your character!'; $errors['sex'] = 'Please select the sex for your character!';

View File

@ -180,14 +180,17 @@ class Validator
return false; return false;
} }
$minLength = config('character_name_min_length');
$maxLength = config('character_name_max_length');
$length = strlen($name); $length = strlen($name);
if($length < 3) if($length < $minLength)
{ {
self::$lastError = 'Character name is too short. Min. length <b>3</b> characters.'; self::$lastError = 'Character name is too short. Min. length <b>3</b> characters.';
return false; return false;
} }
if($length > 25) if($length > $maxLength)
{ {
self::$lastError = 'Character name is too long. Max. length <b>25</b> characters.'; self::$lastError = 'Character name is too long. Max. length <b>25</b> characters.';
return false; return false;
@ -201,7 +204,7 @@ class Validator
if(preg_match('/ {2,}/', $name)) if(preg_match('/ {2,}/', $name))
{ {
self::$lastError = 'Invalid character name format. Use only A-Z and numbers 0-9 and no double spaces.'; self::$lastError = 'Invalid character name format. Use only A-Z and no double spaces.';
return false; return false;
} }
@ -289,13 +292,6 @@ class Validator
} }
} }
$player = new OTS_Player();
$player->find($name);
if($player->isLoaded()) {
self::$lastError = 'Character with this name already exist.';
return false;
}
//check if was namelocked previously //check if was namelocked previously
if($db->hasTable('player_namelocks') && $db->hasColumn('player_namelocks', 'name')) { if($db->hasTable('player_namelocks') && $db->hasColumn('player_namelocks', 'name')) {
$namelock = $db->query('SELECT `player_id` FROM `player_namelocks` WHERE `name` = ' . $db->quote($name)); $namelock = $db->query('SELECT `player_id` FROM `player_namelocks` WHERE `name` = ' . $db->quote($name));

View File

@ -15,6 +15,10 @@ $character_sex = isset($_POST['sex']) ? (int)$_POST['sex'] : null;
$character_vocation = isset($_POST['vocation']) ? (int)$_POST['vocation'] : null; $character_vocation = isset($_POST['vocation']) ? (int)$_POST['vocation'] : null;
$character_town = isset($_POST['town']) ? (int)$_POST['town'] : null; $character_town = isset($_POST['town']) ? (int)$_POST['town'] : null;
if (!admin() && !empty($character_name)) {
$character_name = ucwords(strtolower($character_name));
}
$character_created = false; $character_created = false;
$save = isset($_POST['save']) && $_POST['save'] == 1; $save = isset($_POST['save']) && $_POST['save'] == 1;
$errors = array(); $errors = array();

View File

@ -13,6 +13,7 @@
require '../common.php'; require '../common.php';
require SYSTEM . 'functions.php'; require SYSTEM . 'functions.php';
require SYSTEM . 'init.php'; require SYSTEM . 'init.php';
require SYSTEM . 'login.php';
$error = ''; $error = '';
if(isset($_GET['account'])) if(isset($_GET['account']))
@ -54,14 +55,24 @@ else if(isset($_GET['email']))
} }
else if(isset($_GET['name'])) else if(isset($_GET['name']))
{ {
$name = strtolower(stripslashes($_GET['name'])); $name = $_GET['name'];
if(!admin()) {
$name = strtolower(stripslashes($name));
}
if(!Validator::characterName($name)) if(!Validator::characterName($name))
error_(Validator::getLastError()); error_(Validator::getLastError());
if(!Validator::newCharacterName($name)) if(!admin() && !Validator::newCharacterName($name))
error_(Validator::getLastError()); error_(Validator::getLastError());
success_('Good. Your name will be:<br /><b>' . ucwords($name) . '</b>'); $player = new OTS_Player();
$player->find($name);
if($player->isLoaded()) {
error_('Character with this name already exist.');
}
success_('Good. Your name will be:<br /><b>' . (admin() ? $name : ucwords($name)) . '</b>');
} }
else if(isset($_GET['password']) && isset($_GET['password2'])) { else if(isset($_GET['password']) && isset($_GET['password2'])) {
$password = $_GET['password']; $password = $_GET['password'];