2016-09-10 19:06:54 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
|
|
use Validator;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
|
|
|
class ValidatorExtendServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param $a attribute
|
|
|
|
* @param $value value
|
|
|
|
* @param $p parameters
|
|
|
|
* @param $v validator
|
|
|
|
*/
|
2018-06-19 11:51:34 +08:00
|
|
|
Validator::extend('no_special_chars', function ($a, $value, $p, $v) {
|
|
|
|
return $value === addslashes(trim($value));
|
2016-09-10 19:06:54 +08:00
|
|
|
});
|
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
Validator::extend('player_name', function ($a, $value, $p, $v) {
|
|
|
|
$regexp = '/^(.*)$/';
|
2016-09-10 19:06:54 +08:00
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
switch (option('player_name_rule')) {
|
|
|
|
case 'official':
|
|
|
|
// Mojang's official username rule
|
|
|
|
$regexp = '/^([A-Za-z0-9_]+)$/';
|
|
|
|
break;
|
2016-09-10 19:06:54 +08:00
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
case 'cjk':
|
|
|
|
// CJK Unified Ideographs
|
|
|
|
$regexp = '/^([A-Za-z0-9_\x{4e00}-\x{9fff}]+)$/u';
|
|
|
|
break;
|
2016-09-10 19:06:54 +08:00
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
case 'custom':
|
|
|
|
$regexp = option('custom_player_name_regexp') ?: $regexp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return preg_match($regexp, $value);
|
2016-09-10 19:06:54 +08:00
|
|
|
});
|
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
Validator::extend('preference', function ($a, $value, $p, $v) {
|
|
|
|
return preg_match('/^(default|slim)$/', $value);
|
2016-09-10 19:06:54 +08:00
|
|
|
});
|
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
Validator::extend('model', function ($a, $value, $p, $v) {
|
|
|
|
return preg_match('/^(steve|alex|cape)$/', $value);
|
2016-09-10 19:06:54 +08:00
|
|
|
});
|
2018-06-19 11:51:34 +08:00
|
|
|
|
|
|
|
$this->registerExpiredRules();
|
2016-09-10 19:06:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-19 11:51:34 +08:00
|
|
|
* Register these for the compatibility with plugins using old rules.
|
2016-09-10 19:06:54 +08:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-19 11:51:34 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-10 19:06:54 +08:00
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|