Issues (51)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Controller/CommentController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Controller;
13
14
use FOS\RestBundle\Controller\Annotations\Get;
15
use FOS\RestBundle\Controller\Annotations\Post;
16
use FOS\RestBundle\Routing\ClassResourceInterface;
17
use Sulu\Bundle\CommentBundle\Entity\CommentInterface;
18
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface;
19
use Sulu\Component\Rest\Exception\EntityNotFoundException;
20
use Sulu\Component\Rest\Exception\RestException;
21
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface;
22
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
23
use Sulu\Component\Rest\RequestParametersTrait;
24
use Sulu\Component\Rest\RestController;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
28
/**
29
 * Provides an api for comments.
30
 */
31
class CommentController extends RestController implements ClassResourceInterface
32
{
33
    use RequestParametersTrait;
34
35
    /**
36
     * Returns list of field-descriptors.
37
     *
38
     * @Get("/comments/fields")
39
     *
40
     * @return Response
41
     */
42
    public function getFieldsAction()
43
    {
44
        return $this->handleView($this->view($this->getFieldDescriptors()));
45
    }
46
47
    /**
48
     * Returns list of comments.
49
     *
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54 3
    public function cgetAction(Request $request)
55
    {
56 3
        $restHelper = $this->get('sulu_core.doctrine_rest_helper');
57 3
        $factory = $this->get('sulu_core.doctrine_list_builder_factory');
58 3
        $listBuilder = $factory->create($this->getParameter('sulu.model.comment.class'));
59
60 3
        $fieldDescriptors = $this->getFieldDescriptors();
61 3
        $restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
62
63 3
        if ($request->query->get('threadType')) {
64 1
            $listBuilder->in(
65 1
                $fieldDescriptors['threadType'],
66 1
                array_filter(explode(',', $request->query->get('threadType')))
67
            );
68
69 1
            $request->query->remove('threadType');
70
        }
71
72 3 View Code Duplication
        foreach ($request->query->all() as $filterKey => $filterValue) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73 1
            if (isset($fieldDescriptors[$filterKey])) {
74 1
                $listBuilder->where($fieldDescriptors[$filterKey], $filterValue);
75
            }
76
        }
77
78 3
        $results = $listBuilder->execute();
79 3
        $list = new ListRepresentation(
80
            $results,
81 3
            'comments',
82 3
            'get_comments',
83 3
            $request->query->all(),
84 3
            $listBuilder->getCurrentPage(),
85 3
            $listBuilder->getLimit(),
86 3
            $listBuilder->count()
87
        );
88
89 3
        return $this->handleView($this->view($list, 200));
90
    }
91
92
    /**
93
     * Returns single comment.
94
     *
95
     * @param int $id
96
     *
97
     * @return Response
98
     *
99
     * @throws EntityNotFoundException
100
     */
101 1
    public function getAction($id)
102
    {
103 1
        $comment = $this->get('sulu.repository.comment')->findCommentById($id);
104 1
        if (!$comment) {
105
            throw new EntityNotFoundException(CommentInterface::class, $id);
106
        }
107
108 1
        return $this->handleView($this->view($comment));
109
    }
110
111
    /**
112
     * Update comment.
113
     *
114
     * @param int $id
115
     * @param Request $request
116
     *
117
     * @return Response
118
     *
119
     * @throws EntityNotFoundException
120
     */
121 1 View Code Duplication
    public function putAction($id, Request $request)
122
    {
123
        /** @var CommentInterface $comment */
124 1
        $comment = $this->get('sulu.repository.comment')->findCommentById($id);
125 1
        if (!$comment) {
126
            throw new EntityNotFoundException(CommentInterface::class, $id);
127
        }
128
129 1
        $comment->setMessage($request->request->get('message'));
130
131 1
        $this->get('sulu_comment.manager')->update($comment);
132 1
        $this->get('doctrine.orm.entity_manager')->flush();
133
134 1
        return $this->handleView($this->view($comment));
135
    }
136
137
    /**
138
     * Delete comment identified by id.
139
     *
140
     * @param int $id
141
     *
142
     * @return Response
143
     */
144 1 View Code Duplication
    public function deleteAction($id)
145
    {
146 1
        $this->get('sulu_comment.manager')->delete([$id]);
147 1
        $this->get('doctrine.orm.entity_manager')->flush();
148
149 1
        return $this->handleView($this->view(null, 204));
150
    }
151
152
    /**
153
     * Delete multiple comments identified by ids parameter.
154
     *
155
     * @param Request $request
156
     *
157
     * @return Response
158
     */
159 1 View Code Duplication
    public function cdeleteAction(Request $request)
160
    {
161 1
        $ids = array_filter(explode(',', $request->get('ids', '')));
162
163 1
        if (0 === count($ids)) {
164
            return $this->handleView($this->view(null, 204));
165
        }
166
167 1
        $this->get('sulu_comment.manager')->delete($ids);
168 1
        $this->get('doctrine.orm.entity_manager')->flush();
169
170 1
        return $this->handleView($this->view(null, 204));
171
    }
172
173
    /**
174
     * trigger a action for given comment specified over action get-parameter
175
     * - publish: Publish a comment
176
     * - unpublish: Unpublish a comment.
177
     *
178
     * @Post("/comments/{id}")
179
     *
180
     * @param int $id
181
     * @param Request $request
182
     *
183
     * @return Response
184
     *
185
     * @throws RestException
186
     */
187 2
    public function postTriggerAction($id, Request $request)
188
    {
189 2
        $action = $this->getRequestParameter($request, 'action', true);
190
191
        /** @var CommentRepositoryInterface $commentRepository */
192 2
        $commentRepository = $this->get('sulu.repository.comment');
193 2
        $commentManager = $this->get('sulu_comment.manager');
194 2
        $comment = $commentRepository->findCommentById($id);
195
196
        switch ($action) {
197 2
            case 'unpublish':
198 1
                $commentManager->unpublish($comment);
199 1
200 1
                break;
201 1
            case 'publish':
202 1
                $commentManager->publish($comment);
203
204
                break;
205
            default:
206
                throw new RestException('Unrecognized action: ' . $action);
207 2
        }
208
209 2
        $this->get('doctrine.orm.entity_manager')->flush();
210
211
        return $this->handleView($this->view($comment));
212
    }
213
214
    /**
215
     * Returns array of field-descriptors.
216
     *
217 3
     * @return FieldDescriptorInterface[]
218
     */
219 3
    private function getFieldDescriptors()
220 3
    {
221
        return $this->get('sulu_core.list_builder.field_descriptor_factory')
222
            ->getFieldDescriptorForClass($this->getParameter('sulu.model.comment.class'));
223
    }
224
}
225