Skip to content

Commit

Permalink
MINOR: Account for undocumented exception in Akismet::getIsSpam (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliot-sawyer authored Apr 20, 2020
1 parent 3ed76c9 commit ac053b5
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/AkismetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace SilverStripe\Akismet;

use Exception;
use Psr\Log\LoggerInterface;
use SilverStripe\Akismet\Service\AkismetService;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\Validator;
use SilverStripe\ORM\ValidationResult;
Expand All @@ -20,6 +23,13 @@
*/
class AkismetField extends FormField
{
/**
* If the Akismet network response fails, it is neither true or false
* This is the value assigned on a 400
*
* @var boolean
*/
private static $is_spam_when_response_fails = false;
/**
* @var array
*/
Expand All @@ -30,7 +40,7 @@ class AkismetField extends FormField
* @var boolean
*/
protected $isSpam = null;

/**
* Get the nested confirmation checkbox field
*
Expand All @@ -43,7 +53,7 @@ protected function confirmationField()
if (empty($requireConfirmation)) {
return null;
}

// If confirmation is required then return a checkbox
return CheckboxField::create(
$this->getName(),
Expand All @@ -56,23 +66,23 @@ protected function confirmationField()
->setMessage($this->getMessage(), $this->getMessageType())
->setForm($this->getForm());
}

public function Field($properties = array())
{
$checkbox = $this->confirmationField();
if ($checkbox) {
return $checkbox->Field($properties);
}
}

public function FieldHolder($properties = array())
{
$checkbox = $this->confirmationField();
if ($checkbox) {
return $checkbox->FieldHolder($properties);
}
}

/**
* @return array
*/
Expand All @@ -81,7 +91,7 @@ public function getSpamMappedData()
if (empty($this->fieldMapping)) {
return null;
}

$result = array();
$data = $this->form->getData();

Expand All @@ -91,7 +101,7 @@ public function getSpamMappedData()

return $result;
}

/**
* This function first gets values from mapped fields and then checks these values against
* akismet and then notifies callback object with the spam checking result.
Expand All @@ -102,7 +112,7 @@ public function getSpamMappedData()
*/
public function validate($validator)
{

// Check that, if necessary, the user has given permission to check for spam
$requireConfirmation = Config::inst()->get(AkismetSpamProtector::class, 'require_confirmation');
if ($requireConfirmation && !$this->Value()) {
Expand All @@ -116,7 +126,7 @@ public function validate($validator)
);
return false;
}

// Check result
$isSpam = $this->getIsSpam();
if (!$isSpam) {
Expand Down Expand Up @@ -184,10 +194,28 @@ public function getIsSpam()
// Check result
/** @var AkismetService $api */
$api = AkismetSpamProtector::singleton()->getService();
$this->isSpam = $api && $api->isSpam($content, $author, $email, $url);
$this->isSpam = false;
try {
$this->isSpam = $api && $api->isSpam($content, $author, $email, $url);
} catch (Exception $e) {
//if the network response fails, it still needs to be true/false
$this->isSpam = (bool) $this->config()->is_spam_when_response_fails;
$errorMessage = sprintf(
"%s: %s",
$e->getMessage(),
_t(
__CLASS__ . '.SPAM',
"Your submission has been rejected because it was treated as spam."
)
);
Injector::inst()
->get(LoggerInterface::class)
->error($errorMessage);
}

return $this->isSpam;
}

/**
* Get the fields to map spam protection too
*
Expand Down

0 comments on commit ac053b5

Please sign in to comment.