DeleteNote.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\DeleteNote\Controller;
  20. use App\Core\Controller;
  21. use App\Core\DB\DB;
  22. use App\Core\Form;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Log;
  25. use App\Core\Router\Router;
  26. use App\Entity\Note;
  27. use App\Util\Common;
  28. use App\Util\Exception\ClientException;
  29. use App\Util\Exception\NoLoggedInUser;
  30. use App\Util\Exception\NoSuchNoteException;
  31. use App\Util\Exception\RedirectException;
  32. use App\Util\Exception\ServerException;
  33. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class DeleteNote extends Controller
  36. {
  37. /**
  38. * Create delete note view
  39. *
  40. * @throws ClientException
  41. * @throws NoLoggedInUser
  42. * @throws RedirectException
  43. * @throws ServerException
  44. */
  45. public function __invoke(Request $request)
  46. {
  47. $user = Common::ensureLoggedIn();
  48. $note_id = (int) $request->get('note_id');
  49. $note = Note::getByPK($note_id);
  50. if (\is_null($note) || !$note->isVisibleTo($user)) {
  51. throw new NoSuchNoteException();
  52. }
  53. $form_delete = Form::create([
  54. ['delete_note', SubmitType::class,
  55. [
  56. 'label' => _m('Delete it'),
  57. 'attr' => [
  58. 'title' => _m('Press to delete this note'),
  59. ],
  60. ],
  61. ],
  62. ]);
  63. $form_delete->handleRequest($request);
  64. if ($form_delete->isSubmitted()) {
  65. if (!\is_null(\Plugin\DeleteNote\DeleteNote::deleteNote(note: $note_id, actor: $user->getId()))) {
  66. DB::flush();
  67. } else {
  68. throw new ClientException(_m('Note already deleted!'));
  69. }
  70. // Redirect user to where they came from
  71. // Prevent open redirect
  72. if (!\is_null($from = $this->string('from'))) {
  73. if (Router::isAbsolute($from)) {
  74. Log::warning("Actor {$user->getId()} attempted to delete to a note and then get redirected to another host, or the URL was invalid ({$from})");
  75. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  76. } else {
  77. // TODO anchor on element id
  78. throw new RedirectException(url: $from);
  79. }
  80. } else {
  81. // If we don't have a URL to return to, go to the instance root
  82. throw new RedirectException('root');
  83. }
  84. }
  85. return [
  86. '_template' => 'delete_note/delete_note.html.twig',
  87. 'note' => $note,
  88. 'delete' => $form_delete->createView(),
  89. ];
  90. }
  91. }