Skip to content

Commit ed00a8c

Browse files
authored
Added anti-spam checks (#357)
* Add check for phone number in CommentModel * Add check for phone number in CommentController * Add anti-spam check in RelationsController Added functions to check for email address and phone number, and only add the comment if none are detected
1 parent 3dd41a0 commit ed00a8c

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/Controller/CommentController.php

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public function addComment(
171171
if (
172172
$commentModel->checkCommentSpam($loggedInMember, $comment)
173173
|| $commentModel->checkForEmailAddress($comment)
174+
|| $commentModel->checkForPhoneNumber($comment)
174175
) {
175176
$form->addError(new FormError($this->translator->trans('commentsomethingwentwrong')));
176177
} else {

src/Controller/RelationController.php

+25-6
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ public function add(Request $request, Member $member, Mailer $mailer): Response
7272
if ($form->isSubmitted() && $form->isValid()) {
7373
/** @var Relation $relation */
7474
$relation = $form->getData();
75-
$relation->setOwner($loggedInMember);
76-
$relation->setReceiver($member);
75+
if (!checkForEmailAddress($relation) && !checkForPhoneNumber($relation))
76+
{
77+
$relation->setOwner($loggedInMember);
78+
$relation->setReceiver($member);
7779

78-
$this->entityManager->persist($relation);
79-
$this->entityManager->flush();
80+
$this->entityManager->persist($relation);
81+
$this->entityManager->flush();
8082

81-
$mailer->sendRelationNotification($relation);
83+
$mailer->sendRelationNotification($relation);
8284

83-
return $this->redirectToRoute('relations', ['username' => $loggedInMember->getUsername()]);
85+
return $this->redirectToRoute('relations', ['username' => $loggedInMember->getUsername()]);
86+
}
8487
}
8588

8689
return $this->render('relation/add.html.twig', [
@@ -235,4 +238,20 @@ private function findRelationBetween(Member $loggedInMember, Member $member): ?R
235238

236239
return $relationRepository->findRelationBetween($loggedInMember, $member);
237240
}
241+
242+
private function checkForEmailAddress(Relation $relation): bool
243+
{
244+
$relationText = $relation->getCommentText();
245+
$found = preg_match("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $relationText);
246+
247+
return $found > 0;
248+
}
249+
250+
private function checkForPhoneNumber(Relation $relation): bool
251+
{
252+
$relationText = $relation->getCommentText();
253+
$found = preg_match("/([0-9][\. \)-]*){8,}/", $relationText);
254+
255+
return $found > 0;
256+
}
238257
}

src/Model/CommentModel.php

+8
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,12 @@ public function checkForEmailAddress(Comment $comment): bool
175175

176176
return $count > 0;
177177
}
178+
179+
public function checkForPhoneNumber(Comment $comment): bool
180+
{
181+
$commentText = $comment->getTextfree();
182+
$found = preg_match("/([0-9][\. \)-]*){8,}/", $commentText);
183+
184+
return $found > 0;
185+
}
178186
}

0 commit comments

Comments
 (0)