diff --git a/src/Controller/Security.php b/src/Controller/Security.php index 59a37e4f39..551c7d3277 100644 --- a/src/Controller/Security.php +++ b/src/Controller/Security.php @@ -6,6 +6,7 @@ use App\Core\Controller; use App\Core\DB\DB; use App\Core\Form; use function App\Core\I18n\_m; +use App\Core\NoteScope; use App\Entity\Follow; use App\Entity\GSActor; use App\Entity\LocalUser; @@ -40,7 +41,7 @@ class Security extends Controller // last username entered by the user $last_username = $authenticationUtils->getLastUsername(); - return ['_template' => 'security/login.html.twig', 'last_username' => $last_username, 'error' => $error, 'notes' => Note::getAllNotes($this->instance_scope), ]; + return ['_template' => 'security/login.html.twig', 'last_username' => $last_username, 'error' => $error, 'notes' => Note::getAllNotes(NoteScope::$instance_scope)]; } public function logout() @@ -61,10 +62,10 @@ class Security extends Controller ['nickname', TextType::class, [ 'label' => _m('Nickname'), 'constraints' => [new Length([ - 'min' => 1, - 'minMessage' => _m('Your password should be at least {{ limit }} characters long'), - 'max' => 64, - 'maxMessage' => _m('Your password should be at most {{ limit }} characters long'), ]), + 'min' => Common::config('nickname', 'min_length'), + 'minMessage' => _m(['Your nickname must be at least # characters long'], ['count' => Common::config('nickname', 'min_length')]), + 'max' => Nickname::MAX_LEN, + 'maxMessage' => _m(['Your nickname must be at most # characters long'], ['count' => Nickname::MAX_LEN]), ]), ], ]], ['email', EmailType::class, ['label' => _m('Email')]], @@ -73,7 +74,8 @@ class Security extends Controller 'mapped' => false, 'constraints' => [ new NotBlank(['message' => _m('Please enter a password')]), - new Length(['min' => 6, 'minMessage' => _m('Your password should be at least {{ limit }} characters'), 'max' => 60]), + new Length(['min' => 6, 'minMessage' => _m(['Your password should be at least # characters'], ['count' => 6]), + 'max' => 60, 'maxMessage' => _m(['Your password should be at most # characters'], ['count' => 60]), ]), ], ]], ['register', SubmitType::class, ['label' => _m('Register')]], @@ -85,10 +87,7 @@ class Security extends Controller $data = $form->getData(); $data['password'] = $form->get('password')->getData(); - $valid_nickname = Nickname::isValid($data['nickname'], Nickname::CHECK_USED); - if (!$valid_nickname) { - throw new \Exception(_m('Invalid nickname')); - } + $valid_nickname = Nickname::normalize($data['nickname'], check_already_used: true); $actor = GSActor::create(['nickname' => $data['nickname']]); DB::persist($actor); @@ -134,7 +133,7 @@ class Security extends Controller return [ '_template' => 'security/register.html.twig', 'registration_form' => $form->createView(), - 'notes' => Note::getAllNotes($this->instance_scope), + 'notes' => Note::getAllNotes(NoteScope::$instance_scope), ]; } } diff --git a/src/Core/NoteScope.php b/src/Core/NoteScope.php index 48efc0422d..bd4a0bdb1f 100644 --- a/src/Core/NoteScope.php +++ b/src/Core/NoteScope.php @@ -29,4 +29,6 @@ class NoteScope extends Bitmap public const GROUP = 8; public const FOLLOWER = 16; public const MESSAGE = 32; + + public static int $instance_scope = self::PUBLIC | self::SITE; } diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 11cc307060..b48bb815e1 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -41,6 +41,7 @@ use DateTimeInterface; class Note extends Entity { // {{{ Autocode + private int $id; private int $gsactor_id; private ?string $content; @@ -199,7 +200,7 @@ class Note extends Entity Event::handle('GetAvatarUrl', [$this->getActorNickname(), &$url]); return $url; } - public function getAllNotes(int $noteScope): array + public static function getAllNotes(int $noteScope): array { return DB::sql('select * from note n ' . 'where n.reply_to is null and (n.scope & :notescope) <> 0 ' . diff --git a/src/Util/Common.php b/src/Util/Common.php index 31ca0bae01..aef285c5ea 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -94,14 +94,6 @@ abstract class Common self::ensureLoggedIn()->getNickname(); } - public function getAllNotes(int $noteScope): array - { - return DB::sql('select * from note n ' . - "where n.reply_to is null and (n.scope & {$noteScope}) <> 0 " . - 'order by n.created DESC', - ['n' => 'App\Entity\Note']); - } - public static function ensureLoggedIn(): LocalUser { if (($user = self::user()) == null) { diff --git a/src/Util/Exception/NicknameTooShortException.php b/src/Util/Exception/NicknameTooShortException.php index 61fe752fd5..42b358a041 100644 --- a/src/Util/Exception/NicknameTooShortException.php +++ b/src/Util/Exception/NicknameTooShortException.php @@ -42,13 +42,13 @@ namespace App\Util\Exception; use function App\Core\I18n\_m; -use App\Util\Nickname; +use App\Util\Common; class NicknameTooShortException extends NicknameInvalidException { protected function defaultMessage(): string { // TRANS: Validation error in form for registration, profile and group settings, etc. - return _m('Nickname cannot be more than # character long.', ['count' => Nickname::MAX_LEN]); + return _m(['Nickname cannot be less than # character long.'], ['count' => Common::config('nickname', 'min_length')]); } } diff --git a/src/Util/Nickname.php b/src/Util/Nickname.php index b5660e192b..60747595c5 100644 --- a/src/Util/Nickname.php +++ b/src/Util/Nickname.php @@ -145,21 +145,22 @@ class Nickname throw new NicknameTooLongException(); } - $nickname = trim($nickname); - $nickname = str_replace('_', '', $nickname); - $nickname = mb_strtolower($nickname); - $nickname = Normalizer::normalize($nickname, Normalizer::FORM_C); + $original_nickname = $nickname; + $nickname = trim($nickname); + $nickname = str_replace('_', '', $nickname); + $nickname = mb_strtolower($nickname); + $nickname = Normalizer::normalize($nickname, Normalizer::FORM_C); - if (mb_strlen($nickname) < 1) { + if (mb_strlen($original_nickname) < 1) { throw new NicknameEmptyException(); - } elseif (mb_strlen($nickname) < Common::config('nickname', 'min_length')) { + } elseif (mb_strlen($original_nickname) < Common::config('nickname', 'min_length')) { throw new NicknameTooShortException(); - } elseif (!self::isCanonical($nickname) && !filter_var($nickname, FILTER_VALIDATE_EMAIL)) { + } elseif (!self::isCanonical($original_nickname) && !filter_var($original_nickname, FILTER_VALIDATE_EMAIL)) { throw new NicknameInvalidException(); - } elseif ($check_reserved && self::isReserved($nickname) || Common::isSystemPath($nickname)) { + } elseif ($check_reserved && self::isReserved($original_nickname) || Common::isSystemPath($original_nickname)) { throw new NicknameReservedException(); } elseif ($check_already_used) { - $actor = self::isTaken($nickname); + $actor = self::isTaken($original_nickname); if ($actor instanceof GSActor) { throw new NicknameTakenException($actor); }