Skip to content

Commit

Permalink
Added anti-spam checks (#357)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gabriel-BW authored Aug 28, 2024
1 parent 3dd41a0 commit ed00a8c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function addComment(
if (
$commentModel->checkCommentSpam($loggedInMember, $comment)
|| $commentModel->checkForEmailAddress($comment)
|| $commentModel->checkForPhoneNumber($comment)
) {
$form->addError(new FormError($this->translator->trans('commentsomethingwentwrong')));
} else {
Expand Down
31 changes: 25 additions & 6 deletions src/Controller/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ public function add(Request $request, Member $member, Mailer $mailer): Response
if ($form->isSubmitted() && $form->isValid()) {
/** @var Relation $relation */
$relation = $form->getData();
$relation->setOwner($loggedInMember);
$relation->setReceiver($member);
if (!checkForEmailAddress($relation) && !checkForPhoneNumber($relation))
{
$relation->setOwner($loggedInMember);
$relation->setReceiver($member);

$this->entityManager->persist($relation);
$this->entityManager->flush();
$this->entityManager->persist($relation);
$this->entityManager->flush();

$mailer->sendRelationNotification($relation);
$mailer->sendRelationNotification($relation);

return $this->redirectToRoute('relations', ['username' => $loggedInMember->getUsername()]);
return $this->redirectToRoute('relations', ['username' => $loggedInMember->getUsername()]);
}
}

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

return $relationRepository->findRelationBetween($loggedInMember, $member);
}

private function checkForEmailAddress(Relation $relation): bool
{
$relationText = $relation->getCommentText();
$found = preg_match("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $relationText);

return $found > 0;
}

private function checkForPhoneNumber(Relation $relation): bool
{
$relationText = $relation->getCommentText();
$found = preg_match("/([0-9][\. \)-]*){8,}/", $relationText);

return $found > 0;
}
}
8 changes: 8 additions & 0 deletions src/Model/CommentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,12 @@ public function checkForEmailAddress(Comment $comment): bool

return $count > 0;
}

public function checkForPhoneNumber(Comment $comment): bool
{
$commentText = $comment->getTextfree();
$found = preg_match("/([0-9][\. \)-]*){8,}/", $commentText);

return $found > 0;
}
}

0 comments on commit ed00a8c

Please sign in to comment.