Example #1
0
 /**
  * @return void
  */
 protected function renderHoneypot()
 {
     $renderingOptions = $this->formDefinition->getRenderingOptions();
     if (!isset($renderingOptions['honeypot']['enable']) || $renderingOptions['honeypot']['enable'] === false || TYPO3_MODE === 'BE') {
         return;
     }
     FormArrayUtility::assertAllArrayKeysAreValid($renderingOptions['honeypot'], ['enable', 'formElementToUse']);
     if (!$this->isAfterLastPage()) {
         $elementsCount = count($this->currentPage->getElements());
         if ($elementsCount === 0) {
             return;
         }
         if (!$this->isFirstRequest()) {
             $honeypotNameFromSession = $this->getHoneypotNameFromSession($this->lastDisplayedPage);
             if ($honeypotNameFromSession) {
                 $honeypotElement = $this->formDefinition->getElementByIdentifier($honeypotNameFromSession);
                 if ($honeypotElement instanceof FormElementInterface) {
                     $this->lastDisplayedPage->removeElement($honeypotElement);
                 }
             }
         }
         $elementsCount = count($this->currentPage->getElements());
         $randomElementNumber = mt_rand(0, $elementsCount - 1);
         $honeypotName = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, mt_rand(5, 26));
         $referenceElement = $this->currentPage->getElements()[$randomElementNumber];
         $honeypotElement = $this->currentPage->createElement($honeypotName, $renderingOptions['honeypot']['formElementToUse']);
         $validator = $this->objectManager->get(EmptyValidator::class);
         $honeypotElement->addValidator($validator);
         if (mt_rand(0, 1) === 1) {
             $this->currentPage->moveElementAfter($honeypotElement, $referenceElement);
         } else {
             $this->currentPage->moveElementBefore($honeypotElement, $referenceElement);
         }
         $this->setHoneypotNameInSession($this->currentPage, $honeypotName);
     }
 }