diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index d6d2315d..b4c23a1d 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -179,7 +179,19 @@ class AdminController extends Controller ->text('max_upload_file_size')->addon('KB') ->hint(trans('options.general.max_upload_file_size.hint', ['size' => ini_get('upload_max_filesize')])); - $form->checkbox('allow_chinese_playername')->label(); + $form->select('player_name_rule') + ->option('official', trans('options.general.player_name_rule.official')) + ->option('cjk', trans('options.general.player_name_rule.cjk')) + ->option('custom', trans('options.general.player_name_rule.custom')); + + $form->text('custom_player_name_regexp')->hint()->placeholder(); + + $form->group('player_name_length') + ->addon(trans('options.general.player_name_length.addon1')) + ->text('player_name_length_min') + ->addon(trans('options.general.player_name_length.addon2')) + ->text('player_name_length_max') + ->addon(trans('options.general.player_name_length.addon3')); $form->select('api_type') ->option('0', 'CustomSkinLoader API') @@ -305,7 +317,7 @@ class AdminController extends Controller } elseif ($action == "nickname") { $this->validate($request, [ - 'nickname' => 'required|nickname' + 'nickname' => 'required|no_special_chars' ]); $user->setNickName($request->input('nickname')); diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index c906dfab..d2a6b063 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -109,7 +109,7 @@ class AuthController extends Controller $this->validate($request, [ 'email' => 'required|email', 'password' => 'required|min:8|max:32', - 'nickname' => 'required|nickname|max:255' + 'nickname' => 'required|no_special_chars|max:255' ]); if (! option('user_can_register')) { diff --git a/app/Http/Controllers/PlayerController.php b/app/Http/Controllers/PlayerController.php index e0249cbc..ea1236fe 100644 --- a/app/Http/Controllers/PlayerController.php +++ b/app/Http/Controllers/PlayerController.php @@ -59,7 +59,7 @@ class PlayerController extends Controller public function add(Request $request) { $this->validate($request, [ - 'player_name' => 'required|'.(option('allow_chinese_playername') ? 'pname_chinese' : 'playername') + 'player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max') ]); event(new CheckPlayerExists($request->input('player_name'))); @@ -114,7 +114,7 @@ class PlayerController extends Controller public function rename(Request $request) { $this->validate($request, [ - 'new_player_name' => 'required|'.(option('allow_chinese_playername') ? 'pname_chinese' : 'playername') + 'new_player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max') ]); $newName = $request->input('new_player_name'); diff --git a/app/Providers/ValidatorExtendServiceProvider.php b/app/Providers/ValidatorExtendServiceProvider.php index 42136ced..a4be21ac 100644 --- a/app/Providers/ValidatorExtendServiceProvider.php +++ b/app/Providers/ValidatorExtendServiceProvider.php @@ -20,40 +20,63 @@ class ValidatorExtendServiceProvider extends ServiceProvider * @param $p parameters * @param $v validator */ - Validator::extend('username', function($a, $value, $p, $v) { - return preg_match("/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u", $value); + Validator::extend('no_special_chars', function ($a, $value, $p, $v) { + return $value === addslashes(trim($value)); }); - Validator::extend('nickname', function($a, $value, $p, $v) { - return $value == addslashes(trim($value)); + Validator::extend('player_name', function ($a, $value, $p, $v) { + $regexp = '/^(.*)$/'; + + switch (option('player_name_rule')) { + case 'official': + // Mojang's official username rule + $regexp = '/^([A-Za-z0-9_]+)$/'; + break; + + case 'cjk': + // CJK Unified Ideographs + $regexp = '/^([A-Za-z0-9_\x{4e00}-\x{9fff}]+)$/u'; + break; + + case 'custom': + $regexp = option('custom_player_name_regexp') ?: $regexp; + break; + } + + return preg_match($regexp, $value); }); - Validator::extend('no_special_chars', function($a, $value, $p, $v) { - return $value == addslashes(trim($value)); + Validator::extend('preference', function ($a, $value, $p, $v) { + return preg_match('/^(default|slim)$/', $value); }); - Validator::extend('playername', function($a, $value, $p, $v) { - return preg_match("/^([A-Za-z0-9_]+)$/", $value); + Validator::extend('model', function ($a, $value, $p, $v) { + return preg_match('/^(steve|alex|cape)$/', $value); }); - Validator::extend('pname_chinese', function($a, $value, $p, $v) { - return preg_match("/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u", $value); - }); - - Validator::extend('preference', function($a, $value, $p, $v) { - return preg_match("/^(default|slim)$/", $value); - }); - - Validator::extend('model', function($a, $value, $p, $v) { - return preg_match("/^(steve|alex|cape)$/", $value); - }); + $this->registerExpiredRules(); } /** - * Register any application services. + * Register these for the compatibility with plugins using old rules. * * @return void */ + protected function registerExpiredRules() + { + Validator::extend('nickname', function ($a, $value, $p, $v) { + return $value === addslashes(trim($value)); + }); + + Validator::extend('playername', function($a, $value, $p, $v) { + return preg_match('/^([A-Za-z0-9_]+)$/', $value); + }); + + Validator::extend('pname_chinese', function ($a, $value, $p, $v) { + return preg_match('/^([A-Za-z0-9_\x{4e00}-\x{9fff}]+)$/u', $value); + }); + } + public function register() { // diff --git a/config/options.php b/config/options.php index cf693459..18b8826f 100644 --- a/config/options.php +++ b/config/options.php @@ -13,7 +13,10 @@ return [ 'home_pic_url' => './resources/assets/dist/images/bg.jpg', 'custom_css' => '', 'custom_js' => '', - 'allow_chinese_playername' => 'true', + 'player_name_rule' => 'official', + 'custom_player_name_regexp' => '', + 'player_name_length_min' => '3', + 'player_name_length_max' => '16', 'comment_script' => '', 'allow_sending_statistics' => 'true', 'user_initial_score' => '1000', diff --git a/resources/lang/en/options.yml b/resources/lang/en/options.yml index 461aeb28..31350faa 100644 --- a/resources/lang/en/options.yml +++ b/resources/lang/en/options.yml @@ -82,9 +82,20 @@ general: max_upload_file_size: title: Max Upload Size hint: "Limit of PHP in php.ini: :size" - allow_chinese_playername: - title: Player Name - label: Allow chinese player names. + player_name_rule: + title: Player Name Rule + official: Letters, numbers and underscores (Mojang's official rule) + cjk: Allow CJK Unified Ideographs + custom: Use custom rules (regular expression) + custom_player_name_regexp: + title: Custom Player Name Rules + hint: Only takes effect when the above option is set to 'custom'. Leave empty to allow any character. + placeholder: Regular Expressions + player_name_length: + title: Player Name Length + addon1: Minimum + addon2: characters, maximum + addon3: characters api_type: Prefered JSON API auto_del_invalid_texture: title: Invalid Textures diff --git a/resources/lang/en/user.yml b/resources/lang/en/user.yml index 0b1f20be..056d034e 100644 --- a/resources/lang/en/user.yml +++ b/resources/lang/en/user.yml @@ -80,6 +80,13 @@ player: pname-rule: Could only contain letters, numbers and dashes. pname-rule-chinese: Could only contain chinese characters, letters, numbers and dashes. + player-name-rule: + official: Player name may only contains letters, numbers and underscores. + cjk: Player name may contains letters, numbers, underscores and CJK Unified Ideographs. + custom: Custom player name rules are applied on this site. Please contact admins for further information. + + player-name-length: The player name should be at least :min characters and not greater than :max characters. + add: repeated: The player name is already registered. lack-score: You don't have enough score to add a player. diff --git a/resources/lang/en/validation.yml b/resources/lang/en/validation.yml index 01efcb9d..502bf6f8 100644 --- a/resources/lang/en/validation.yml +++ b/resources/lang/en/validation.yml @@ -1,8 +1,6 @@ # Blessing Skin username: ':attribute format is invalid.' -nickname: ':attribute format is invalid.' -playername: 'The :attribute may only contain letters, numbers and dashes.' -pname_chinese: 'The :attribute may only contain letters, numbers, dashes and chinese characters.' +player_name: 'The :attribute contains invalid character.' no_special_chars: 'The :attribute must not contain special characters.' preference: 'The :attribute must be default or slim.' model: 'The :attribute must be steve, alex or cape.' diff --git a/resources/lang/zh_CN/options.yml b/resources/lang/zh_CN/options.yml index 93fd035b..a7b1e8b2 100644 --- a/resources/lang/zh_CN/options.yml +++ b/resources/lang/zh_CN/options.yml @@ -82,9 +82,20 @@ general: max_upload_file_size: title: 最大允许上传大小 hint: PHP 限制::size,定义在 php.ini 中。 - allow_chinese_playername: - title: 角色名 - label: 允许中文角色名 + player_name_rule: + title: 角色名规则 + official: 大小写字母数字下划线(Mojang 官方的用户名规则) + cjk: 允许中文字符(中日韩统一表意文字) + custom: 自定义(使用下方设置的正则表达式) + custom_player_name_regexp: + title: 自定义角色名规则 + hint: 正则表达式,仅当上一选项为「自定义」时生效。留空表示允许使用任意字符。 + placeholder: 正则表达式,不懂别乱填 + player_name_length: + title: 角色名长度 + addon1: 最少 + addon2: 个字符,最多 + addon3: 个字符 api_type: 首选 JSON API auto_del_invalid_texture: title: 失效材质 diff --git a/resources/lang/zh_CN/user.yml b/resources/lang/zh_CN/user.yml index d85b98b0..4ba8ef12 100644 --- a/resources/lang/zh_CN/user.yml +++ b/resources/lang/zh_CN/user.yml @@ -84,8 +84,12 @@ player: cape: 披风: empty: 未上传 - pname-rule: 只能包含数字、字母以及下划线 - pname-rule-chinese: 可使用汉字,字母数字以及下划线 + player-name-rule: + official: 角色名只能包含拉丁字母、数字以及下划线。 + cjk: 角色名可使用拉丁字母、数字、下划线以及汉字(中日韩统一表意文字)。 + custom: 本站使用了自定义的角色名规则,详情请咨询站点管理员。 + + player-name-length: 角色名最少要求 :min 个字符,最多不超过 :max 个字符。 add: repeated: 该角色名已经被其他人注册掉啦 diff --git a/resources/lang/zh_CN/validation.yml b/resources/lang/zh_CN/validation.yml index de4fb81e..ee1a4b86 100644 --- a/resources/lang/zh_CN/validation.yml +++ b/resources/lang/zh_CN/validation.yml @@ -1,8 +1,6 @@ # Blessing Skin username: ':attribute 格式错误。' -nickname: ':attribute 格式错误。' -playername: ':attribute 只能包含数字、字母以及下划线。' -pname_chinese: ':attribute 只能包含数字、字母、汉字以及下划线。' +player_name: ':attribute 不符合规则。' no_special_chars: ':attribute 不能包含特殊字母。' preference: ':attribute 的值必须为 default 或 slim。' model: ':attribute 的值必须为 steve、alex 或 cape。' diff --git a/resources/views/user/player.tpl b/resources/views/user/player.tpl index 8e96fcac..55157e5d 100644 --- a/resources/views/user/player.tpl +++ b/resources/views/user/player.tpl @@ -113,15 +113,20 @@ - - - +
{{ trans('user.player.player-name') }} - + {{ trans('user.player.player-name') }} +
+ +
+ +