Posting.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Component\Posting;
  19. use App\Core\Cache;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\Form;
  23. use App\Core\GSFile;
  24. use function App\Core\I18n\_m;
  25. use App\Core\Modules\Component;
  26. use App\Core\Security;
  27. use App\Entity\Attachment;
  28. use App\Entity\AttachmentToNote;
  29. use App\Entity\Note;
  30. use App\Util\Common;
  31. use App\Util\Exception\InvalidFormException;
  32. use App\Util\Exception\RedirectException;
  33. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  34. use Symfony\Component\Form\Extension\Core\Type\FileType;
  35. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  36. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  37. class Posting extends Component
  38. {
  39. /**
  40. * "Perfect URL Regex", courtesy of https://urlregex.com/
  41. */
  42. const URL_REGEX = <<<END
  43. %(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@|\\d{1,3}(?:\\.\\d{1,3}){3}|(?:(?:[a-z\\d\\x{00a1}-\\x{ffff}]+-?)*[a-z\\d\\x{00a1}-\\x{ffff}]+)(?:\\.(?:[a-z\\d\\x{00a1}-\\x{ffff}]+-?)*[a-z\\d\\x{00a1}-\\x{ffff}]+)*(?:\\.[a-z\\x{00a1}-\\x{ffff}]{2,6}))(?::\\d+)?(?:[^\\s]*)?%iu
  44. END;
  45. /**
  46. * HTML render event handler responsible for adding and handling
  47. * the result of adding the note submission form, only if a user is logged in
  48. */
  49. public function onStartTwigPopulateVars(array &$vars): bool
  50. {
  51. if (($user = Common::user()) == null) {
  52. return Event::next;
  53. }
  54. $actor_id = $user->getId();
  55. $to_tags = [];
  56. $tags = Cache::get("actor-tags-{$actor_id}", function () use ($actor_id) {
  57. return DB::dql('select c.tag from App\Entity\GSActorCircle c where c.tagger = :tagger', ['tagger' => $actor_id]);
  58. });
  59. foreach ($tags as $t) {
  60. $t = $t['tag'];
  61. $to_tags[$t] = $t;
  62. }
  63. $placeholder_string = ['How are you feeling?', 'Have something to share?', 'How was your day?'];
  64. $rand_key = array_rand($placeholder_string);
  65. $request = $vars['request'];
  66. $form = Form::create([
  67. ['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => _m($placeholder_string[$rand_key])]]],
  68. ['attachments', FileType::class, ['label' => ' ', 'data' => null, 'multiple' => true, 'required' => false]],
  69. ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]],
  70. ['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => true, 'expanded' => true, 'choices' => $to_tags]],
  71. ['post', SubmitType::class, ['label' => _m('Post')]],
  72. ]);
  73. $form->handleRequest($request);
  74. if ($form->isSubmitted()) {
  75. $data = $form->getData();
  76. if ($form->isValid()) {
  77. self::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true);
  78. throw new RedirectException();
  79. } else {
  80. throw new InvalidFormException();
  81. }
  82. }
  83. $vars['post_form'] = $form->createView();
  84. return Event::next;
  85. }
  86. /**
  87. * Store the given note with $content and $attachments, created by
  88. * $actor_id, possibly as a reply to note $reply_to and with flag
  89. * $is_local. Sanitizes $content and $attachments
  90. */
  91. public static function storeNote(int $actor_id, ?string $content, array $attachments, bool $is_local, ?int $reply_to = null, ?int $repeat_of = null)
  92. {
  93. $note = Note::create([
  94. 'gsactor_id' => $actor_id,
  95. 'content' => $content,
  96. 'is_local' => $is_local,
  97. 'reply_to' => $reply_to,
  98. 'repeat_of' => $repeat_of,
  99. ]);
  100. $processed_attachments = [];
  101. foreach ($attachments as $f) {
  102. $processed_attachments[] = GSFile::validateAndStoreFileAsAttachment(
  103. $f, Common::config('attachments', 'dir'),
  104. Security::sanitize($f->getClientOriginalName()),
  105. is_local: true, actor_id: $actor_id
  106. );
  107. }
  108. $matched_urls = [];
  109. preg_match_all(self::URL_REGEX, $content, $matched_urls, PREG_SET_ORDER);
  110. foreach ($matched_urls as $match) {
  111. $processed_attachments[] = GSFile::validateAndStoreURLAsAttachment($match[0]);
  112. }
  113. DB::persist($note);
  114. // Need file and note ids for the next step
  115. DB::flush();
  116. if ($processed_attachments != []) {
  117. foreach ($processed_attachments as $a) {
  118. DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId()]));
  119. }
  120. DB::flush();
  121. }
  122. }
  123. /**
  124. * Get a unique representation of a file on disk
  125. *
  126. * This can be used in the future to deduplicate images by visual content
  127. */
  128. public function onHashFile(string $filename, ?string &$out_hash)
  129. {
  130. $out_hash = hash_file(Attachment::FILEHASH_ALGO, $filename);
  131. return Event::stop;
  132. }
  133. /**
  134. * Fill the list of allowed sizes for an attachment, to prevent potential DoS'ing by requesting thousands of different thumbnail sizes
  135. */
  136. public function onGetAllowedThumbnailSizes(?array &$sizes)
  137. {
  138. $sizes[] = ['width' => Common::config('thumbnail', 'width'), 'height' => Common::config('thumbnail', 'height')];
  139. return Event::next;
  140. }
  141. }