From b7d9da8ae6f43e9bcecb5d95b57c83c007a3ba7e Mon Sep 17 00:00:00 2001 From: Eliseu Amaro Date: Sun, 24 Oct 2021 15:32:28 +0100 Subject: [PATCH] [Posting] Add Content Length constraint to form validation --- components/Posting/Posting.php | 64 +++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index f455fe6bfe..432809e9b1 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -1,6 +1,6 @@ getId(); - $to_tags = []; - $tags = Cache::get( + $to_tags = []; + $tags = Cache::get( "actor-circle-{$actor_id}", - fn () => DB::dql('select c.tag from App\Entity\ActorCircle c where c.tagger = :tagger', ['tagger' => $actor_id]), + fn() => DB::dql('select c.tag from App\Entity\ActorCircle c where c.tagger = :tagger', ['tagger' => $actor_id]), ); foreach ($tags as $t) { - $t = $t['tag']; + $t = $t['tag']; $to_tags[$t] = $t; } @@ -82,45 +85,48 @@ class Posting extends Component $initial_content = ''; Event::handle('PostingInitialContent', [&$initial_content]); - $available_content_types = ['Plain Text' => 'text/plain']; + $available_content_types = [ + 'Plain Text' => 'text/plain', + ]; Event::handle('PostingAvailableContentTypes', [&$available_content_types]); - $request = $vars['request']; + $request = $vars['request']; $form_params = [ - ['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => false, 'expanded' => false, 'choices' => $to_tags]], - ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'multiple' => false, 'expanded' => false, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]], - ['content', TextareaType::class, ['label' => _m('Content:'), 'data' => $initial_content, 'attr' => ['placeholder' => _m($placeholder)]]], - ['attachments', FileType::class, [ + ['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => false, 'expanded' => false, 'choices' => $to_tags]], + ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'multiple' => false, 'expanded' => false, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]], + ['content', TextareaType::class, ['label' => _m('Content:'), 'data' => $initial_content, 'attr' => ['placeholder' => _m($placeholder)], 'constraints' => [new Length(['max' => Common::config('site', 'text_limit')])],]], + ['attachments', FileType::class, [ 'label' => _m('Attachments:'), 'multiple' => true, 'required' => false, 'invalid_message' => _m('Attachment not valid.'), ] - ]]; + ] + ]; - if (\count($available_content_types) > 1) { + if (count($available_content_types) > 1) { $form_params[] = ['content_type', ChoiceType::class, [ - 'label' => _m('Text format:'), 'multiple' => false, 'expanded' => false, - 'data' => $available_content_types[array_key_first($available_content_types)], + 'label' => _m('Text format:'), 'multiple' => false, 'expanded' => false, + 'data' => $available_content_types[array_key_first($available_content_types)], 'choices' => $available_content_types, ], ]; } - $form_params[] = ['post_note', SubmitType::class, ['label' => _m('Post')]]; - $form = Form::create($form_params); + $form_params[] = ['post_note', SubmitType::class, ['label' => _m('Post')]]; + $form = Form::create($form_params); $form->handleRequest($request); if ($form->isSubmitted()) { try { - $data = $form->getData(); if ($form->isValid()) { + $data = $form->getData(); $content_type = $data['content_type'] ?? $available_content_types[array_key_first($available_content_types)]; self::storeLocalNote($user->getActor(), $data['content'], $content_type, $data['attachments']); throw new RedirectException(); } } catch (FormSizeFileException $sizeFileException) { - throw new FormSizeFileException(_m($sizeFileException)); + throw new FormSizeFileException(); } catch (InvalidFormException $invalidFormException) { throw new InvalidFormException(); } @@ -144,21 +150,21 @@ class Posting extends Component $rendered = null; Event::handle('RenderNoteContent', [$content, $content_type, &$rendered, $actor, $reply_to]); $note = Note::create([ - 'actor_id' => $actor->getId(), - 'content' => $content, + 'actor_id' => $actor->getId(), + 'content' => $content, 'content_type' => $content_type, - 'rendered' => $rendered, - 'is_local' => true, + 'rendered' => $rendered, + 'is_local' => true, ]); $processed_attachments = []; - /** @var \Symfony\Component\HttpFoundation\File\UploadedFile[] $attachments */ + /** @var UploadedFile[] $attachments */ foreach ($attachments as $f) { - $filesize = $f->getSize(); + $filesize = $f->getSize(); $max_file_size = Common::getUploadLimit(); if ($max_file_size < $filesize) { - throw new FormSizeFileException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' - . 'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize])); + throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' + . 'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize],)); } Event::handle('EnforceUserFileQuota', [$filesize, $actor->getId()]); $processed_attachments[] = [GSFile::storeFileAsAttachment($f), $f->getClientOriginalName()];