Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 25 additions & 160 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,186 +21,51 @@ API sends a comment's text and several previous approved comments to the servers
* PHP 5.6 and above
* CURL support

You can unpack the archive with the plugin to the root of the site or install it using the composer

## Sample SPAM test for user signup
```php
composer require cleantalk/php-antispam
```

### Sample SPAM test for text comment and user signup

```php
<?php

session_start();

//require_once "vendor/autoload.php"; -- Composer

require_once "lib/Cleantalk.php";
require_once "lib/CleantalkHelper.php";
require_once "lib/CleantalkRequest.php";

use Cleantalk\Cleantalk;
use Cleantalk\CleantalkRequest;

// Take params from config
$config_url = 'http://moderate.cleantalk.org/api2.0/';
$auth_key = 'enter key'; // Set Cleantalk auth key

if (count($_POST)) {
$sender_nickname = 'John Dow';
if (isset($_POST['login']) && $_POST['login'] != '')
$sender_nickname = $_POST['login'];

$sender_email = 'stop_email@example.com';
if (isset($_POST['email']) && $_POST['email'] != '')
$sender_email = $_POST['email'];

$sender_ip = null;
if (isset($_SERVER['REMOTE_ADDR']))
$sender_ip = $_SERVER['REMOTE_ADDR'];

$js_on = 0;
if (isset($_POST['js_on']) && $_POST['js_on'] == date("Y"))
$js_on = 1;

// The facility in which to store the query parameters
$ct_request = new CleantalkRequest();

$ct_request->auth_key = $auth_key;
$ct_request->agent = 'php-api';
$ct_request->sender_email = $sender_email;
$ct_request->sender_ip = $sender_ip;
$ct_request->sender_nickname = $sender_nickname;
$ct_request->js_on = $js_on;
$ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time'];

$ct = new Cleantalk();
$ct->server_url = $config_url;

// Check
$ct_result = $ct->isAllowUser($ct_request);

if ($ct_result->allow == 1) {
echo 'User allowed. Reason ' . $ct_result->comment;
} else {
echo 'User forbidden. Reason ' . $ct_result->comment;
}
echo '<br /><br />';
}
else
{
$_SESSION['ct_submit_time'] = time();
}
?>
$apikey = 'your_cleantalk_api_key';
$email_field = 'name_email_form_field';
$user_name_field = 'name_user_name_form_field';
$message_field = 'name_message_form_field';
$type_form = 'contact'; // use 'signup' for user signup form

<form method="post">
<label for="login">Login:<label>
<input type="text" name="login" id="login" />
<br />
<label for="email">Email:<label>
<input type="text" name="email" id="email" value="" />
<br />
<input type="hidden" name="js_on" id="js_on" value="0" />
<input type="submit" />
</form>
// if downloaded, unzip and include the app:
require_once 'php-antispam/cleantalk-antispam.php';
// if install the app by composer package:
use Cleantalk\CleantalkAntispam;

<script type="text/javascript">
var date = new Date();
//require_once "lib/cleantalk-php-patch.php"; -- PHP-FPM

document.getElementById("js_on").value = date.getFullYear();
</script>
```

## Sample SPAM test for text comment

```php
<?php

session_start();

//require_once "vendor/autoload.php"; -- Composer

require_once "lib/Cleantalk.php";
require_once "lib/CleantalkHelper.php";
require_once "lib/CleantalkRequest.php";

use Cleantalk\Cleantalk;
use Cleantalk\CleantalkRequest;

// Take params from config
$config_url = 'http://moderate.cleantalk.org/api2.0/';
$auth_key = 'enter key'; // Set Cleantalk auth key

if (count($_POST)) {
$sender_nickname = 'John Dow';
if (isset($_POST['login']) && $_POST['login'] != '')
$sender_nickname = $_POST['login'];

$sender_email = 'stop_email@example.com';
if (isset($_POST['email']) && $_POST['email'] != '')
$sender_email = $_POST['email'];

$sender_ip = null;
if (isset($_SERVER['REMOTE_ADDR']))
$sender_ip = $_SERVER['REMOTE_ADDR'];

$js_on = 0;
if (isset($_POST['js_on']) && $_POST['js_on'] == date("Y"))
$js_on = 1;

$message = null;
if (isset($_POST['message']) && $_POST['message'] != '')
$message = $_POST['message'];

// The facility in which to store the query parameters
$ct_request = new CleantalkRequest();

$ct_request->auth_key = $auth_key;
$ct_request->agent = 'php-api';
$ct_request->sender_email = $sender_email;
$ct_request->sender_ip = $sender_ip;
$ct_request->sender_nickname = $sender_nickname;
$ct_request->js_on = $js_on;
$ct_request->message = $message;
$ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time'];

$ct = new Cleantalk();
$ct->server_url = $config_url;

// Check
$ct_result = $ct->isAllowMessage($ct_request);

if ($ct_result->allow == 1) {
echo 'Message allowed. Reason ' . $ct_result->comment;
} else {
echo 'Message forbidden. Reason ' . $ct_result->comment;
}
echo '<br /><br />';
}
else
{
$_SESSION['ct_submit_time'] = time();
}
$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field, $user_name_field, $message_field, $type_form);
$cleantalk_antispam->handle();
?>

<form method="post">
<label for="login">Login:<label>
<input type="text" name="login" id="login" />
<label for="login">Login:</label>
<input type="text" name="name_user_name_form_field" id="login" />
<br />
<label for="email">Email:<label>
<input type="text" name="email" id="email" value="" />
<label for="email">Email:</label>
<input type="text" name="name_email_form_field" id="email" value="" />
<br />
<label for="message">Message:<label>
<textarea name="message" id="message"></textarea>
<label for="message">Message:</label>
<textarea name="name_message_form_field" id="message"></textarea>
<br />
<input type="hidden" name="js_on" id="js_on" value="0" />
<input type="submit" />
</form>

<script type="text/javascript">
var date = new Date();

document.getElementById("js_on").value = date.getFullYear();
</script>
<?php $cleantalk_antispam->frontendScript(); ?>
```


## API Response description
API returns PHP object:
* allow (0|1) - allow to publish or not, in other words spam or ham
Expand Down
7 changes: 7 additions & 0 deletions cleantalk-antispam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once "lib/Cleantalk.php";
require_once "lib/CleantalkHelper.php";
require_once "lib/CleantalkRequest.php";
require_once "lib/CleantalkResponse.php";
require_once "lib/CleantalkAntispam.php";
84 changes: 84 additions & 0 deletions lib/CleantalkAntispam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Cleantalk;

class CleantalkAntispam
{
private $apikey;
private $email_field;
private $user_name_field;
private $message_field;
private $type_form;

public function __construct(
$apikey,
$email_field,
$user_name_field = '',
$message_field = '',
$type_form = 'contact'
)
{
$this->apikey = $apikey;
$this->email_field = $email_field;
$this->user_name_field = $user_name_field;
$this->message_field = $message_field;
$this->type_form = $type_form;
}

public function handle()
{
if (count($_POST) === 0) {
$_SESSION['ct_submit_time'] = time();
return;
}

$sender_email = isset($_POST[$this->email_field]) ? $_POST[$this->email_field] : '';
$sender_nickname = isset($_POST[$this->user_name_field]) ? $_POST[$this->user_name_field] : '';
$message = isset($_POST[$this->message_field]) ? $_POST[$this->message_field] : '';
$sender_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;

$ct_request = new CleantalkRequest();

$ct_request->auth_key = $this->apikey;
$ct_request->agent = 'php-api';
$ct_request->sender_email = $sender_email;
$ct_request->sender_ip = $sender_ip;
$ct_request->sender_nickname = $sender_nickname;
$ct_request->message = $message;
$ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time'];
$ct_request->event_token = isset($_POST['ct_bot_detector_event_token']) ? $_POST['ct_bot_detector_event_token'] : null;

$ct = new Cleantalk();
$ct->server_url = $ct_request::CLEANTALK_API_URL;

// Check
if ($this->type_form === 'signup') {
$ct_result = $ct->isAllowUser($ct_request);
}
if ($this->type_form === 'contact') {
$ct_result = $ct->isAllowMessage($ct_request);
}

if ($ct_result->allow == 1) {
if ($this->type_form === 'signup') {
echo 'User allowed. Reason ' . $ct_result->comment;
}
if ($this->type_form === 'contact') {
echo 'Message allowed. Reason ' . $ct_result->comment;
}
} else {
if ($this->type_form === 'signup') {
echo 'User forbidden. Reason ' . $ct_result->comment;
}
if ($this->type_form === 'contact') {
echo 'Message forbidden. Reason ' . $ct_result->comment;
}
}
echo '<br /><br />';
}

public function frontendScript()
{
echo '<script type="text/javascript" src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js"></script>';
}
}
22 changes: 9 additions & 13 deletions lib/CleantalkRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
/**
* Request class
*/
class CleantalkRequest {
class CleantalkRequest
{
const CLEANTALK_API_URL = 'https://moderate.cleantalk.org/api2.0/';

/**
* All http request headers
Expand Down Expand Up @@ -120,17 +122,6 @@ class CleantalkRequest {
public $x_forwarded_for = '';
public $x_real_ip = '';

/**
* Is enable Java Script,
* valid are 0|1|2
* Status:
* null - JS html code not inserted into phpBB templates
* 0 - JS disabled at the client browser
* 1 - JS enabled at the client broswer
* @var int
*/
public $js_on = null;

/**
* user time zone
* @var string
Expand All @@ -154,7 +145,12 @@ class CleantalkRequest {
* Method name
* @var string
*/
public $method_name = 'check_message';
public $method_name = 'check_message';

/**
* @var string|null
*/
public $event_token;

/**
* Fill params with constructor
Expand Down