diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index 920868408..7dfb23e00 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -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 { diff --git a/src/Controller/RelationController.php b/src/Controller/RelationController.php index 9258eb2f5..9fe19958a 100644 --- a/src/Controller/RelationController.php +++ b/src/Controller/RelationController.php @@ -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', [ @@ -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; + } } diff --git a/src/Model/CommentModel.php b/src/Model/CommentModel.php index 62231a352..9bb74c5cc 100644 --- a/src/Model/CommentModel.php +++ b/src/Model/CommentModel.php @@ -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; + } }