This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Ara\Comment; |
||
4 | |||
5 | use \Anax\DI\InjectionAwareInterface; |
||
6 | use \Anax\DI\InjectionAwareTrait; |
||
7 | use \LRC\Form\ModelForm as Modelform; |
||
8 | |||
9 | /** |
||
10 | * A controller for the comment system. |
||
11 | */ |
||
12 | class CommentController implements InjectionAwareInterface |
||
13 | { |
||
14 | use InjectionAwareTrait; |
||
15 | |||
16 | |||
17 | |||
18 | /** |
||
19 | * @var \LRC\Repository\SoftDbRepository Comment repository. |
||
20 | */ |
||
21 | private $comments; |
||
22 | |||
23 | |||
24 | |||
25 | /** |
||
26 | * Configuration. |
||
27 | */ |
||
28 | 2 | public function init() |
|
29 | { |
||
30 | 2 | $commentRepository = $this->di->manager->createRepository(Comment::class, [ |
|
31 | 2 | 'db' => $this->di->db, |
|
32 | 2 | 'type' => 'db-soft', |
|
33 | 2 | 'table' => 'rv1_Comment' |
|
34 | ]); |
||
35 | 2 | $this->comments = $commentRepository; |
|
36 | 2 | return $commentRepository; |
|
37 | } |
||
38 | |||
39 | |||
40 | |||
41 | public function showComments($postid) |
||
42 | { |
||
43 | $loggedInUser = $this->di->userController->getLoggedInUserId(); |
||
44 | |||
45 | $newForm = new ModelForm('new-comment-form', Comment::class); |
||
46 | |||
47 | View Code Duplication | if ($this->di->request->getMethod() == 'POST' && $loggedInUser) { |
|
0 ignored issues
–
show
|
|||
48 | $comment = $newForm->populateModel(); |
||
49 | $comment->user = $loggedInUser; |
||
50 | $newForm->validate(); |
||
51 | if ($newForm->isValid()) { |
||
52 | $this->comments->save($comment); |
||
53 | $this->di->response->redirect("comment/$postid#{$comment->id}"); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | $comments = $this->getComments($postid, $loggedInUser); |
||
58 | $sortBy = $this->sortBy(); |
||
59 | |||
60 | $viewData = [ |
||
61 | "comments" => $this->buildCommentTree($comments, $sortBy), |
||
62 | "textfilter" => $this->di->textfilter, |
||
63 | "postid" => $postid, |
||
64 | "action" => "", |
||
65 | "actionID" => "", |
||
66 | "newForm" => $newForm, |
||
67 | "isLoggedIn" => $loggedInUser |
||
68 | ]; |
||
69 | |||
70 | $this->di->view->add("comment/comment-section", $viewData, "main", 2); |
||
71 | } |
||
72 | |||
73 | |||
74 | |||
75 | public function replyComment($postid) |
||
76 | { |
||
77 | $loggedInUser = $this->di->userController->getLoggedInUserId(); |
||
78 | if (is_null($loggedInUser)) { |
||
79 | $this->di->response->redirect("comment/$postid"); |
||
80 | } |
||
81 | |||
82 | $actionID = (int)$this->di->request->getGet("id"); |
||
83 | if (!$this->comments->findSoft('id', $actionID)) { |
||
84 | $this->di->response->redirect("comment/$postid"); |
||
85 | } |
||
86 | |||
87 | $replyForm = new ModelForm('reply-comment-form', Comment::class); |
||
88 | |||
89 | View Code Duplication | if ($this->di->request->getMethod() == 'POST') { |
|
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. ![]() |
|||
90 | $comment = $replyForm->populateModel(); |
||
91 | $comment->user = $loggedInUser; |
||
92 | $replyForm->validate(); |
||
93 | if ($replyForm->isValid()) { |
||
94 | $this->comments->save($comment); |
||
95 | $this->di->response->redirect("comment/$postid#{$comment->id}"); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $comments = $this->getComments($postid, $loggedInUser); |
||
100 | $sortBy = $this->sortBy(); |
||
101 | |||
102 | $viewData = [ |
||
103 | "comments" => $this->buildCommentTree($comments, $sortBy), |
||
104 | "textfilter" => $this->di->textfilter, |
||
105 | "postid" => $postid, |
||
106 | "action" => "reply", |
||
107 | "actionID" => $actionID, |
||
108 | "newForm" => new ModelForm('new-comment-form', Comment::class), |
||
109 | "replyForm" => $replyForm, |
||
110 | "isLoggedIn" => $loggedInUser |
||
111 | ]; |
||
112 | |||
113 | $this->di->view->add("comment/comment-section", $viewData, "main", 2); |
||
114 | } |
||
115 | |||
116 | |||
117 | |||
118 | public function editComment($postid) |
||
119 | { |
||
120 | $actionID = (int)$this->di->request->getGet("id"); |
||
121 | $currentComment = $this->comments->findSoft('id', $actionID); |
||
122 | if (!$currentComment) { |
||
123 | $this->di->response->redirect("comment/$postid"); |
||
124 | } |
||
125 | |||
126 | $loggedInUser = $this->di->userController->getLoggedInUserId(); |
||
127 | if (!$this->canComment($loggedInUser, $currentComment)) { |
||
128 | $this->di->response->redirect("comment/$postid"); |
||
129 | } |
||
130 | |||
131 | $editForm = new ModelForm('edit-comment-form', $currentComment); |
||
132 | |||
133 | if ($this->di->request->getMethod() == 'POST') { |
||
134 | $comment = $editForm->populateModel(null, ['id', 'post_id', 'parent_id']); |
||
135 | //Prevent edited column from being set to NULL |
||
136 | unset($comment->edited); |
||
137 | $editForm->validate(); |
||
138 | if ($editForm->isValid()) { |
||
139 | $this->comments->save($comment); |
||
140 | $this->di->response->redirect("comment/$postid#{$comment->id}"); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $comments = $this->getComments($postid, $loggedInUser); |
||
145 | $sortBy = $this->sortBy(); |
||
146 | |||
147 | $viewData = [ |
||
148 | "comments" => $this->buildCommentTree($comments, $sortBy), |
||
149 | "textfilter" => $this->di->textfilter, |
||
150 | "postid" => $postid, |
||
151 | "action" => "edit", |
||
152 | "actionID" => $actionID, |
||
153 | "newForm" => new ModelForm('new-comment-form', Comment::class), |
||
154 | "editForm" => $editForm, |
||
155 | "isLoggedIn" => $loggedInUser |
||
156 | ]; |
||
157 | |||
158 | $this->di->view->add("comment/comment-section", $viewData, "main", 2); |
||
159 | } |
||
160 | |||
161 | |||
162 | |||
163 | public function deleteComment($postid) |
||
164 | { |
||
165 | $actionID = (int)$this->di->request->getGet("id"); |
||
166 | $currentComment = $this->comments->findSoft('id', $actionID); |
||
167 | if (!$currentComment) { |
||
168 | $this->di->response->redirect("comment/$postid"); |
||
169 | } |
||
170 | |||
171 | $loggedInUser = $this->di->userController->getLoggedInUserId(); |
||
172 | if (!$this->canComment($loggedInUser, $currentComment)) { |
||
173 | $this->di->response->redirect("comment/$postid"); |
||
174 | } |
||
175 | |||
176 | if ($this->di->request->getMethod() == 'POST') { |
||
177 | if ($this->di->request->getPost('delete') == 'delete') { |
||
178 | $this->comments->deleteSoft($currentComment); |
||
179 | } |
||
180 | $this->di->response->redirect("comment/$postid#{$currentComment->id}"); |
||
181 | } |
||
182 | |||
183 | $comments = $this->getComments($postid, $loggedInUser); |
||
184 | $sortBy = $this->sortBy(); |
||
185 | |||
186 | $viewData = [ |
||
187 | "comments" => $this->buildCommentTree($comments, $sortBy), |
||
188 | "textfilter" => $this->di->textfilter, |
||
189 | "postid" => $postid, |
||
190 | "action" => "delete", |
||
191 | "actionID" => $actionID, |
||
192 | "newForm" => new ModelForm('new-comment-form', Comment::class), |
||
193 | "isLoggedIn" => $loggedInUser |
||
194 | ]; |
||
195 | |||
196 | $this->di->view->add("comment/comment-section", $viewData, "main", 2); |
||
197 | } |
||
198 | |||
199 | |||
200 | |||
201 | public function voteComment($postid) |
||
202 | { |
||
203 | if (is_null($this->di->userController->getLoggedInUserId())) { |
||
204 | $this->di->response->redirect("comment/$postid"); |
||
205 | } |
||
206 | |||
207 | $actionID = (int)$this->di->request->getGet("id"); |
||
208 | $currentComment = $this->comments->findSoft('id', $actionID); |
||
209 | if (!$currentComment) { |
||
210 | $this->di->response->redirect("comment/$postid"); |
||
211 | } |
||
212 | |||
213 | if ($this->di->request->getPost("upvote")) { |
||
214 | $currentComment->upvote += 1; |
||
215 | } elseif ($this->di->request->getPost("downvote")) { |
||
216 | $currentComment->downvote += 1; |
||
217 | } |
||
218 | $this->comments->save($currentComment); |
||
219 | $this->di->response->redirect("comment/$postid#{$currentComment->id}"); |
||
220 | } |
||
221 | |||
222 | |||
223 | |||
224 | 1 | public function getComments($postid, $loggedInUser) |
|
225 | { |
||
226 | 1 | $comments = $this->comments->getAll('post_id = ?', [$postid]); |
|
227 | |||
228 | 1 | foreach ($comments as $comment) { |
|
229 | 1 | $comment->isUserOwner = ($loggedInUser == $comment->userObject->id); |
|
230 | 1 | $comment->isUserAdmin = $this->di->session->has("admin"); |
|
231 | } |
||
232 | |||
233 | 1 | return $comments; |
|
234 | } |
||
235 | |||
236 | |||
237 | |||
238 | 1 | public function sortBy() |
|
239 | { |
||
240 | 1 | $sortRequest = $this->di->request->getGet("sort"); |
|
241 | 1 | $sortRules = ["best", "old", "new"]; |
|
242 | 1 | return in_array($sortRequest, $sortRules) ? $sortRequest : "best"; |
|
243 | } |
||
244 | |||
245 | |||
246 | |||
247 | 1 | public function buildCommentTree(array &$elements, $sortBy, $parentId = 0) |
|
248 | { |
||
249 | 1 | $branch = array(); |
|
250 | |||
251 | 1 | foreach ($elements as $element) { |
|
252 | 1 | if ($element->parent_id == $parentId) { |
|
253 | 1 | $element->children = $this->buildCommentTree($elements, $sortBy, $element->id); |
|
254 | 1 | $branch[$element->id] = $element; |
|
255 | } |
||
256 | } |
||
257 | 1 | return $this->sortBranchComments($branch, $sortBy); |
|
258 | } |
||
259 | |||
260 | |||
261 | |||
262 | 2 | public function sortBranchComments(array &$branch, $sortBy = "best") |
|
263 | { |
||
264 | 2 | $sortOrder = SORT_DESC; |
|
265 | 2 | $sortArray = array(); |
|
266 | 2 | foreach ($branch as $key => $comment) { |
|
267 | switch ($sortBy) { |
||
268 | 2 | case 'old': |
|
269 | 1 | $sortOrder = SORT_ASC; |
|
270 | //Intentional fall through |
||
271 | 2 | case 'new': |
|
272 | 1 | $sortArray[$key] = $comment->created; |
|
273 | 1 | break; |
|
274 | 2 | case 'best': |
|
275 | //Intentional fall through |
||
276 | default: |
||
277 | 2 | $sortArray[$key] = ($comment->upvote - $comment->downvote); |
|
278 | 2 | break; |
|
279 | } |
||
280 | } |
||
281 | 2 | array_multisort($sortArray, $sortOrder, $branch); |
|
282 | 2 | return $branch; |
|
283 | } |
||
284 | |||
285 | |||
286 | |||
287 | 1 | public function canComment($user, $comment) |
|
288 | { |
||
289 | 1 | return $user == $comment->user || $this->di->session->has("admin"); |
|
290 | } |
||
291 | } |
||
292 |
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.