Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
NoteHandlerPlugin
n/a
0 / 0
n/a
0 / 0
8
n/a
0 / 0
 noteActionHandle
n/a
0 / 0
8
n/a
0 / 0
1<?php
2
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
20namespace App\Core\Modules;
21
22use App\Entity\Note;
23use App\Util\Common;
24use Symfony\Component\Form\Form;
25use Symfony\Component\HttpFoundation\Request;
26
27class NoteHandlerPlugin extends Plugin
28{
29    /**
30     * Handle the $form submission for the note action for note if
31     * $note->getId() == $data['note_id']
32     *
33     * This function is called when a user interacts with a note, such as through favouriting or commenting
34     *
35     * @codeCoverageIgnore
36     *
37     * @param Request  $request
38     * @param Form     $form
39     * @param Note     $note
40     * @param string   $form_name
41     * @param callable $handle
42     *
43     * @throws InvalidFormException
44     * @throws NoSuchNoteException
45     *
46     * @return bool|void
47     */
48    public static function noteActionHandle(Request $request, Form $form, Note $note, string $form_name, callable $handle)
49    {
50        if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
51            $form->handleRequest($request);
52            if ($form->isSubmitted()) {
53                $data = $form->getData();
54                // Loose comparison
55                if ($data['note_id'] != $note->getId()) {
56                    return Event::next;
57                } else {
58                    $user = Common::user();
59                    if (!$note->isVisibleTo($user)) {
60                        // ^ Ensure user isn't trying to trip us up
61                        Log::warning('Suspicious activity: user ' . $user->getNickname() .
62                                   ' tried to interact with note ' . $note->getId() .
63                                   ', but they shouldn\'t have access to it');
64                        throw new NoSuchNoteException();
65                    } else {
66                        if ($form->isValid()) {
67                            $ret = $handle($note, $data, $user);
68                            if ($ret != null) {
69                                return $ret;
70                            }
71                        } else {
72                            throw new InvalidFormException();
73                        }
74                    }
75                }
76            }
77        }
78    }
79}