Source of file SimplestSpamField.php
Size: 5,502 Bytes - Last Modified: 2021-12-23T10:46:34+00:00
/var/www/docs.ssmods.com/process/src/src/Form/SimplestSpamField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | <?php namespace Sunnysideup\SimplestSpam\Form; use SilverStripe\Forms\FormField; use SilverStripe\SpamProtection\EditableSpamProtectionField; use Sunnysideup\SimplestSpam\Model\SimplestSpamFieldQuestion; use SilverStripe\Control\Controller; use SilverStripe\SiteConfig\SiteConfig; /** * Provides an {@link FormField} which allows form to validate for non-bot submissions * by giving them a challenge answer a question * * @module SimplestSpam */ class SimplestSpamField extends EditableSpamProtectionField { private static $table_name = 'SimplestSpamField'; private static $questions_and_answers = array(); private static $has_been_initialised = false; protected $error = ''; protected function initialise() { if (!self::$has_been_initialised) { if (!count(self::$questions_and_answers)) { self::$questions_and_answers = SimplestSpamFieldQuestion::get(); } $questionCount = 0; if (self::$questions_and_answers->count()) { $questionCount = self::$questions_and_answers->count(); } if (!isset($_REQUEST['SimplestSpam_challenge_field']) && $questionCount) { $randomNumber = rand(0, $questionCount - 1); Controller::curr()->getRequest()->getSession()->set("SimplestSpamQuestion", $randomNumber + 1); // adding one to make it easier to work out if anything has been entered, i.e. 0 could be nothing or first question } } self::$has_been_initialised = true; } public function Field($properties = array()) { $this->initialise(); $html = '<input type="text" name="SimplestSpam_challenge_field" class="text requiredField" />'; return $html; } public function FieldHolder($properties = []) { $this->initialise(); $obj = $this->getQuestionAnswerObject(); if (!$obj) { return ""; } $question = $obj->Question; $siteConfig = SiteConfig::get()->First(); $explanation = $siteConfig->SimplestSpamExplanation; if ($explanation) { $explanation = $explanation; } $Title = $this->XML_val('Title'); $Message = $this->XML_val('Message'); $MessageType = $this->XML_val('MessageType'); $Type = $this->XML_val('Type'); $extraClass = $this->XML_val('extraClass'); $Name = $this->XML_val('Name'); $Field = $this->XML_val('Field'); $messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : ""; return <<<HTML <div id="$Name" class="field $Type $extraClass"> <label class="left spamquestion">{$question} </label> <div class="middleColumn"> {$Field} <label class="required">{$explanation} {$messageBlock}</label> </div> </div> HTML; } public function validate() { $validator = parent::validate(); $siteConfig = SiteConfig::get()->First(); // don't bother querying the SimplestSpam-service if fields were empty if ( !isset($_REQUEST['SimplestSpam_challenge_field']) || empty($_REQUEST['SimplestSpam_challenge_field']) ) { $validator->validationError( $this->name, $siteConfig->SimplestSpamWrongAnswerFieldMessage, "validation", false ); Controller::curr()->getRequest()->getSession()->set("FormField.{$this->form->FormName()}.{$this->getName()}", $siteConfig->SimplestSpamWrongAnswerFieldMessage); $this->form->sessionMessage($siteConfig->SimplestSpamWrongAnswerFormMessage, "bad"); return false; } $response = $_REQUEST['SimplestSpam_challenge_field']; $obj = $this->getQuestionAnswerObject(); if (!$obj || !isset($obj->Answer)) { user_error("SimplestSpamField::validate(): could not find answer - sorry, please try again'", E_USER_ERROR); return false; } $answer = $obj->Answer; if ($this->cleanupAnswer($answer) != $this->cleanupAnswer($response)) { $validator->validationError( $this->name, $siteConfig->SimplestSpamWrongAnswerFormMessage, "validation", false ); Controller::curr()->getRequest()->getSession()->set("FormField.{$this->form->FormName()}.{$this->getName()}", $siteConfig->SimplestSpamWrongAnswerFieldMessage); $this->form->sessionMessage($siteConfig->SimplestSpamWrongAnswerFormMessage, "bad"); return false; } return true; } protected function cleanupAnswer($v) { return trim(strtolower($v)); } protected function getQuestionAnswerObject() { $this->initialise(); $number = Controller::curr()->getRequest()->getSession()->get("SimplestSpamQuestion"); if ($number > 0) { $number = $number - 1; if (($dos = SimplestSpamFieldQuestion::get()->limit(1, $number))->count()) { return $dos->first(); } else { $this->error = _t("SimplestSpamField.QUESTIONNOTFOUND", "Selected question not found."); } } else { $this->error = _t("SimplestSpamField.QUESTIONSELECTIONNOTAVAILABLE", "No question selection made."); } return false; } } |