Skip to content

Commit

Permalink
feat: remove unsupported thirdparty dependancies
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Nov 25, 2023
1 parent 8e50409 commit 43cccfc
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 60 deletions.
3 changes: 1 addition & 2 deletions .upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ mappings:
AkismetConfig: SilverStripe\Akismet\Config\AkismetConfig
AkismetProcessor: SilverStripe\Akismet\Config\AkismetProcessor
AkismetService: SilverStripe\Akismet\Service\AkismetService
AkismetServiceBackend: SilverStripe\Akismet\Service\AkismetServiceBackend
AkismetField: SilverStripe\Akismet\AkismetField
AkismetSpamProtector: SilverStripe\Akismet\AkismetSpamProtector
AkismetSpamProtector: SilverStripe\Akismet\AkismetSpamProtector
2 changes: 0 additions & 2 deletions _config/akismet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
default_spam_protector: SilverStripe\Akismet\AkismetSpamProtector

SilverStripe\Core\Injector\Injector:
SilverStripe\Akismet\Service\AkismetService:
class: SilverStripe\Akismet\Service\AkismetServiceBackend
SilverStripe\Control\Director:
properties:
Middlewares:
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
},
"require": {
"silverstripe/framework": "^5",
"tijsverkoyen/akismet": "^1.1",
"silverstripe/spamprotection": "^4"
},
"require-dev": {
Expand Down
4 changes: 3 additions & 1 deletion src/AkismetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ protected function confirmationField()
->setForm($this->getForm());
}

public function Field($properties = []))

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


public function FieldHolder($properties = [])
{
$checkbox = $this->confirmationField();
Expand Down
4 changes: 2 additions & 2 deletions src/AkismetSpamProtector.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ public function getService()
{
// Get API key and URL
$key = $this->getApiKey();

if (empty($key)) {
user_error("AkismetSpamProtector is incorrectly configured. Please specify an API key.", E_USER_WARNING);
return null;
}
$url = Director::protocolAndHost();

// Generate API object
return Injector::inst()->get(AkismetService::class, false, [$key, $url]);
return Injector::inst()->get(AkismetService::class, false, [$key]);
}

public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null)
Expand Down
163 changes: 128 additions & 35 deletions src/Service/AkismetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,133 @@

namespace SilverStripe\Akismet\Service;

/**
* Describes TijsVerkoyen\Akismet\Akismet
*/
interface AkismetService
use SilverStripe\Control\Director;
use Exception;

class AkismetService
{
/**
* Check if the comment is spam or not
* This is basically the core of everything. This call takes a number of
* arguments and characteristics about the submitted content and then
* returns a thumbs up or thumbs down.
* Almost everything is optional, but performance can drop dramatically if
* you exclude certain elements.
* REMARK: If you are having trouble triggering you can send
* "viagra-test-123" as the author and it will trigger a true response,
* always.
*
* @param string[optional] $content The content that was submitted.
* @param string[optional] $author The name.
* @param string[optional] $email The email address.
* @param string[optional] $url The URL.
* @param string[optional] $permalink The permanent location of the entry
* the comment was submitted to.
* @param string[optional] $type The type, can be blank, comment,
* trackback, pingback, or a made up
* value like "registration".
* @return bool If the comment is spam true will be
* returned, otherwise false.
*/
public function isSpam(
$content,
$author = null,
$email = null,
$url = null,
$permalink = null,
$type = null
);
private $apiKey;

private $endpoint;

public function __construct($apiKey)
{
$this->apiKey = $apiKey;
$this->endpoint = sprintf('https://%s.rest.akismet.com/1.1/', $apiKey);
}


public function verifyKey()
{
$response = $this->post($this->endpoint . 'verify-key', [
'key' => $this->apiKey,
'blog' => Director::protocolAndHost()
]);

return 'valid' == trim(strtolower($response['body']));
}


public function buildData($content, $author = null, $email = null, $url = null, $permalink = null)
{
$data = [
'blog' => Director::protocolAndHost(),
'user_ip' => $_SERVER['REMOTE_ADDR'],
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'referrer' => (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '',
'permalink' => $permalink,
'comment_type' => 'comment',
'comment_author' => $author,
'comment_author_email' => $email,
'comment_author_url' => $url,
'comment_content' => $content,
];

return $data;
}


public function isSpam($content, $author = null, $email = null, $url = null, $permalink = null)
{
$data = $this->buildData($content, $author, $email, $url, $permalink);
$response = $this->checkSpam($data);

return (isset($response['spam']) && $response['spam']);
}


public function submitSpam($content, $author = null, $email = null, $url = null, $permalink = null)
{
$data = $this->buildData($content, $author, $email, $url, $permalink);
$this->post($this->endpoint . 'submit-spam', $data);
}


public function checkSpam($data)
{
$keys = array_intersect_key($_SERVER, array_fill_keys([
'HTTP_HOST', 'HTTP_USER_AGENT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_ENCODING',
'HTTP_ACCEPT_CHARSET', 'HTTP_KEEP_ALIVE', 'HTTP_REFERER', 'HTTP_CONNECTION', 'HTTP_FORWARDED',
'HTTP_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP',
'REMOTE_ADDR', 'REMOTE_HOST', 'REMOTE_PORT', 'SERVER_PROTOCOL', 'REQUEST_METHOD'],
0
));

$data = array_merge($keys, $data);
$response = $this->post($this->endpoint . 'comment-check', $data);
$response['error'] = $response['discard'] = $response['spam'] = null;

$body = trim(strtolower($response['body']));

if ('true' == $body) {
$response['spam'] = true;

if ( array_key_exists('x-akismet-pro-tip', $response['akismet_headers']) && $response['akismet_headers']['x-akismet-pro-tip'] == 'discard' ) {
$response['discard'] = true;
}
} else if ('false' == $body) {
$response['spam'] = false;
} else if (array_key_exists('x-akismet-debug-help', $response['akismet_headers'])) {
$response['error'] = $response['akismet_headers']['x-akismet-debug-help'];
}

return $response;
}

protected function post($endpoint, $data)
{
$response = [];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$curl_response = curl_exec($ch);

if (false === $curl_response) {
throw new Exception('There was an error sending the Akismet request.');
}

$response['info'] = curl_getinfo($ch);
$response['info']['request_header'] .= http_build_query($data);
$response['header'] = substr($curl_response, 0, $response['info']['header_size']);
$response['body'] = substr($curl_response, $response['info']['header_size']);

$response['akismet_headers'] = [];

foreach (explode("\n", $response['header']) as $header) {
if (stripos($header, 'x-akismet') === 0) {
list($key, $value) = explode(':', $header, 2);
$response['akismet_headers'][strtolower($key)] = $value;
}
}

curl_close($ch);

return $response;
}
}
12 changes: 0 additions & 12 deletions src/Service/AkismetServiceBackend.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/AkismetTestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
use SilverStripe\Dev\TestOnly;
use SilverStripe\Akismet\Service\AkismetService;

class AkismetTestService implements TestOnly, AkismetService
class AkismetTestService extends AkismetService implements TestOnly
{
public function __construct($apiKey, $url)
public function __construct($apiKey)
{
if ($apiKey !== 'dummykey') {
throw new Exception("Invalid key");
}
}

public function isSpam($content, $author = null, $email = null, $url = null, $permalink = null, $type = null)
{
// This dummy service only checks the content
Expand Down
4 changes: 2 additions & 2 deletions tests/AkismetTestSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

class AkismetTestSubmission extends DataObject implements TestOnly
{
private static $db = array(
private static $db = [
'Name' => 'Varchar',
'Email' => 'Varchar',
'Content' => 'Text',
'IsSpam' => 'Boolean',
);
];

private static $default_sort = 'ID';
}

0 comments on commit 43cccfc

Please sign in to comment.