Issues (186)

includes/Pages/PageFlagComment.php (1 issue)

Severity
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages;
11
12
use Waca\DataObjects\Comment;
13
use Waca\DataObjects\Request;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\AccessDeniedException;
16
use Waca\Exceptions\ApplicationLogicException;
17
use Waca\Helpers\Logger;
18
use Waca\Tasks\InternalPageBase;
19
use Waca\WebRequest;
20
21
class PageFlagComment extends InternalPageBase
22
{
23
    /**
24
     * @inheritDoc
25
     */
26
    protected function main()
27
    {
28
        if (!WebRequest::wasPosted()) {
29
            throw new ApplicationLogicException('This page does not support GET methods.');
30
        }
31
32
        $this->validateCSRFToken();
33
34
        $flagState = WebRequest::postInt('flag');
35
        $commentId = WebRequest::postInt('comment');
36
        $updateVersion = WebRequest::postInt('updateversion');
37
38
        if ($flagState !== 0 && $flagState !== 1) {
39
            throw new ApplicationLogicException('Flag status not valid');
40
        }
41
42
        $database = $this->getDatabase();
43
44
        /** @var Comment|false $comment */
45
        $comment = Comment::getById($commentId, $database);
46
        if ($comment === false) {
0 ignored issues
show
The condition $comment === false is always false.
Loading history...
47
            throw new ApplicationLogicException('Unknown comment');
48
        }
49
50
        $currentUser = User::getCurrent($database);
51
52
        if ($comment->getFlagged() && !$this->barrierTest('unflag', $currentUser)) {
53
            // user isn't allowed to unflag comments
54
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
55
        }
56
57
        /** @var Request $request */
58
        $request = Request::getById($comment->getRequest(), $database);
59
60
        if ($comment->getFlagged()
61
            && !$this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')
62
            && $request->getReserved() !== $currentUser->getId()
63
        ) {
64
            // can't unflag if you can't see it.
65
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
66
        }
67
68
        $comment->setFlagged($flagState == 1);
69
        $comment->setUpdateVersion($updateVersion);
70
        $comment->save();
71
72
        if ($flagState === 1) {
73
            Logger::flaggedComment($database, $comment, $request->getDomain());
74
        }
75
        else {
76
            Logger::unflaggedComment($database, $comment, $request->getDomain());
77
        }
78
79
        if (WebRequest::postString('return') == 'list') {
80
            $this->redirect('flaggedComments');
81
        }
82
        else {
83
            $this->redirect('viewRequest', null, ['id' => $comment->getRequest()]);
84
        }
85
    }
86
}