Skip to content

Commit 3ef1d15

Browse files
authored
Upd. Code. Added suggestion module. (#43)
* Upd. Code. Added suggestion module. * Upd. Examples. Update example of use. * fix psalm * Update readme.
1 parent e359541 commit 3ef1d15

20 files changed

+2210
-2076
lines changed

README.md

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,91 +4,70 @@ php-antispam
44

55
[![Latest Stable Version](https://poser.pugx.org/cleantalk/php-antispam/v)](https://packagist.org/packages/cleantalk/php-antispam)
66

7-
A PHP API for antispam service cleantalk.org. Invisible protection from spam, no captches, no puzzles, no animals and no math.
7+
## The Invisible protection from spam, no captcha, no recaptcha, no puzzles, no math captcha.
8+
_API for antispam service cleantalk.org_
89

9-
## How API stops spam?
10-
API uses several simple tests to stop spammers.
11-
* Spam bots signatures.
12-
* Blacklists checks by Email, IP, web-sites domain names.
13-
* JavaScript availability.
14-
* Relevance test for the comment.
10+
#### Requirements
11+
* PHP 5.6 and above
12+
* CURL support
1513

16-
## How API works?
17-
API sends a comment's text and several previous approved comments to the servers. Servers evaluates the relevance of the comment's text on the topic, tests on spam and finaly provides a solution - to publish or put on manual moderation of comments. If a comment is placed on manual moderation, the plugin adds to the text of a comment explaining the reason for the ban server publishing.
14+
### How we stop spam?
15+
PHP Anti-Spam library providing invisible spam protection for your websites, registration forms, and comment sections. CleanTalk API offers an effective CAPTCHA alternative that silently blocks spam without interrupting your users' experience.
1816

19-
## Requirements
17+
When users submit forms on your website form, the form data is securely sent to CleanTalk’s cloud servers. CleanTalk analyzes submissions using advanced heuristics. CleanTalk then returns a real-time verdict— legitimate requests or spam.
2018

21-
* PHP 5.6 and above
22-
* CURL support
19+
You are free to do anything with spam, or just allow as to block spam (we will interrupt desirable request).
2320

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

22+
## Interesting? Let's make some settings (it will take few minutes)
23+
24+
25+
### Step 1 - install our SDK (2 variants ability)
26+
27+
Through composer install **OR** through download zip arhive and unzip it to root directory (with your index.php)
2628
```php
2729
composer require cleantalk/php-antispam
2830
```
29-
30-
### Sample SPAM test for text comment
31-
32-
Notice: You can use the example PHP file from <code>./examples/form_with_handler</code>
3331

34-
```php
35-
<?php
36-
session_start();
3732

38-
$apikey = '';
39-
$email_field = 'name_email_form_field';
40-
$user_name_field = 'name_user_name_form_field';
41-
$message_field = 'name_message_form_field';
42-
$type_form = 'contact'; // use 'signup' for user signup form
33+
### Step 2 - add CleantalkAntispam handler (middleware/interception) to your form handler (action)
4334

44-
// if downloaded, unzip and include the app, take your own relative path:
45-
require_once 'php-antispam/cleantalk-antispam.php';
46-
// if install the app by composer package:
47-
use Cleantalk\CleantalkAntispam;
35+
```php
36+
$apikey = ''; // get it here cleantalk.org (free trial)
37+
$email_field = $_POST['email']; // get it from your form
38+
$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field);
39+
$api_result = $cleantalk_antispam->handle();
40+
```
4841

49-
//require_once "lib/cleantalk-php-patch.php"; -- PHP-FPM
42+
### Step 2.1 - add js lib to your html template
43+
```html
44+
<script src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js"></script>
45+
```
46+
_Need for gathering frontend data._
5047

51-
$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field, $user_name_field, $message_field, $type_form);
52-
$api_result = $cleantalk_antispam->handle();
53-
if ($api_result) { // the check fired
54-
if ($api_result->account_status !== 1) {
55-
// something wrong with your key or license, to know why read $api_result->codes
56-
echo 'Allowed. Spam protection disabled.'; // or do nothing
57-
} else {
58-
if ($api_result->allow === 1) {
59-
echo 'Allowed. Spam protection OK.'; // or do nothing
60-
} else {
61-
die('Blocked. Spam protection OK. Reason: ' . $api_result->comment); // or make your own handler
62-
}
63-
}
48+
### Step 3 - do whatever you want with cloud result
49+
For example add die block for spam.
50+
```php
51+
if ($api_result && $api_result->allow === 0) {
52+
die('Blocked. Spam protection OK. Reason: ' . $api_result->comment);
53+
// or make your own actions/logs/messages ...
6454
}
65-
// your further code flow here
66-
?>
67-
68-
<form method="post">
69-
<label for="login">Login:</label>
70-
<input type="text" name="name_user_name_form_field" id="login" />
71-
<br />
72-
<label for="email">Email:</label>
73-
<input type="text" name="name_email_form_field" id="email" value="" />
74-
<br />
75-
<label for="message">Message:</label>
76-
<textarea name="name_message_form_field" id="message"></textarea>
77-
<br />
78-
<input type="submit" />
79-
</form>
80-
81-
<?php $cleantalk_antispam->frontendScript(); ?>
8255
```
8356

84-
## API Response description
85-
API returns (`$api_result`) PHP object:
86-
* allow (0|1) - allow to publish or not, in other words spam or ham
87-
* comment (string) - server comment for requests.
88-
* id (string MD5 HEX hash) - unique request idenifier.
89-
* errno (int) - error number. errno == 0 if requests successfull.
90-
* errstr (string) - comment for error issue, errstr == null if requests successfull.
91-
* account_status - 0 account disabled, 1 account enabled, -1 unknown status.
57+
### Step 4 (not required) - we prepare for you special troubleshooting method
58+
To find possible problems, just add follow snippet after getVerdict method.
59+
```php
60+
// TROUBLESHOOTING: logging the suggestions
61+
error_log($cleantalk_antispam->whatsWrong(true));
62+
```
63+
In [example file](https://github.com/CleanTalk/php-antispam/blob/dev/examples/form_with_handler/form_with_handler.php) you can see context.
64+
65+
### Step 5 (not required) - if you have any question, please, feel free to ask it in issue here or in our tiket system
66+
67+
## Examples
68+
- [api response description](https://github.com/CleanTalk/php-antispam/tree/dev/examples/api_response_description.md)
69+
- [example with form handler](https://github.com/CleanTalk/php-antispam/blob/dev/examples/form_with_handler/form_with_handler.php)
70+
9271

9372
## Don't want to deal with all this?
9473
Universal solution for any CMS or custom website: https://github.com/CleanTalk/php-uni

cleantalk-antispam.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
require_once "lib/Cleantalk.php";
4-
require_once "lib/CleantalkHelper.php";
5-
require_once "lib/CleantalkRequest.php";
6-
require_once "lib/CleantalkResponse.php";
7-
require_once "lib/CleantalkAntispam.php";
8-
require_once "lib/CleantalkAPI.php";
3+
require_once 'lib/HTTP/Helper.php';
4+
require_once 'lib/HTTP/Request.php';
5+
require_once 'lib/HTTP/Response.php';
6+
require_once 'lib/HTTP/CleantalkResponse.php';
7+
require_once 'lib/CleantalkVerdict.php';
8+
require_once 'lib/CleantalkAntispam.php';

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"keywords": ["spam", "anti-spam", "antispam", "api"],
1010
"autoload": {
1111
"psr-4": {
12-
"Cleantalk\\": "lib"
12+
"CleanTalk\\": "lib"
1313
},
1414
"files": [
1515
"lib/cleantalk-php-patch.php"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## API Response description
2+
API returns (`$api_result`) PHP object:
3+
* allow (0|1) - allow to publish or not, in other words spam or ham
4+
* comment (string) - server comment for requests.
5+
* id (string MD5 HEX hash) - unique request idenifier.
6+
* errno (int) - error number. errno == 0 if requests successfull.
7+
* errstr (string) - comment for error issue, errstr == null if requests successfull.
8+
* account_status - 0 account disabled, 1 account enabled, -1 unknown status.

examples/direct_php/example.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)