Skip to content

Commit af69d81

Browse files
Merge pull request #42 from CleanTalk/dev
3.2 final merge.
2 parents 520abf5 + 9abc3d5 commit af69d81

19 files changed

+1606
-1521
lines changed

CleantalkValidate.php

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

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ You can unpack the archive with the plugin to the root of the site or install it
2727
composer require cleantalk/php-antispam
2828
```
2929

30-
### Sample SPAM test for text comment and user signup
30+
### Sample SPAM test for text comment
31+
32+
Notice: You can use the example PHP file from <code>./examples/form_with_handler</code>
3133

3234
```php
3335
<?php
3436
session_start();
3537

36-
$apikey = 'your_cleantalk_api_key';
38+
$apikey = '';
3739
$email_field = 'name_email_form_field';
3840
$user_name_field = 'name_user_name_form_field';
3941
$message_field = 'name_message_form_field';
4042
$type_form = 'contact'; // use 'signup' for user signup form
4143

42-
// if downloaded, unzip and include the app:
44+
// if downloaded, unzip and include the app, take your own relative path:
4345
require_once 'php-antispam/cleantalk-antispam.php';
4446
// if install the app by composer package:
4547
use Cleantalk\CleantalkAntispam;
@@ -48,6 +50,19 @@ use Cleantalk\CleantalkAntispam;
4850

4951
$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field, $user_name_field, $message_field, $type_form);
5052
$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+
}
64+
}
65+
// your further code flow here
5166
?>
5267

5368
<form method="post">

cleantalk-antispam.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
require_once "lib/CleantalkHelper.php";
55
require_once "lib/CleantalkRequest.php";
66
require_once "lib/CleantalkResponse.php";
7-
require_once "lib/CleantalkAntispam.php";
7+
require_once "lib/CleantalkAntispam.php";
8+
require_once "lib/CleantalkApi.php";

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,33 @@
1414
"files": [
1515
"lib/cleantalk-php-patch.php"
1616
]
17+
},
18+
"require-dev": {
19+
"vimeo/psalm": "^4.8",
20+
"phpunit/phpunit": "^7.5",
21+
"squizlabs/php_codesniffer": "3.*",
22+
"phpcompatibility/php-compatibility": "@dev",
23+
"yoast/phpunit-polyfills": "^1.0",
24+
"ext-dom": "*",
25+
"wp-cli/wp-cli-bundle": "*"
26+
},
27+
"scripts": {
28+
"test": [
29+
"vendor/bin/phpunit --configuration tests/phpunit.xml",
30+
"vendor/bin/phpcs --config-set installed_paths vendor/phpcompatibility/php-compatibility",
31+
"vendor/bin/phpcs --standard=tests/.phpcs.xml",
32+
"vendor/bin/psalm --no-cache --config=psalm.xml"
33+
],
34+
"psalm_l1": [
35+
"vendor/bin/psalm --no-cache --config=psalm.xml"
36+
],
37+
"just_phpunit": [
38+
"vendor/bin/phpunit --configuration tests/phpunit.xml --debug"
39+
]
40+
},
41+
"config": {
42+
"allow-plugins": {
43+
"dealerdirect/phpcodesniffer-composer-installer": true
44+
}
1745
}
1846
}

example.php

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

examples/direct_php/example.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
require_once "../../cleantalk-antispam.php";
4+
5+
/**
6+
* Cleantalk PHP example
7+
*
8+
* @package Cleantalk Example
9+
* @copyright (C) 2011 - 2025 CleanTalk team (https://cleantalk.org)
10+
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
11+
* @see https://cleantalk.org/help/api-check-message
12+
*
13+
*/
14+
15+
use Cleantalk\CleantalkRequest;
16+
use Cleantalk\Cleantalk;
17+
use Cleantalk\CleantalkAPI;
18+
19+
// Take params from config
20+
$config_url = 'https://moderate.cleantalk.org';
21+
$auth_key = ''; // Set Cleantalk auth key
22+
23+
/**
24+
* Key validation example.
25+
*/
26+
$validation = CleantalkAPI::method__notice_validate_key($auth_key, 'php-api');
27+
$validation = json_decode($validation) ? json_decode($validation) : false;
28+
$is_valid = is_object($validation) && $validation->valid;
29+
30+
echo "Access key validation result:";
31+
echo CleantalkAPI::method__notice_validate_key($auth_key, 'php-api');
32+
echo "\n";
33+
34+
if (!$is_valid) {
35+
echo "Access key is not valid. Please check access key in the config.\n";
36+
exit;
37+
}
38+
39+
// The facility in which to store the query parameters
40+
$ct_request = new CleantalkRequest();
41+
42+
$ct_request->auth_key = $auth_key;
43+
$ct_request->message = 'stop_word';
44+
$ct_request->sender_email = 'stop_email@example.com';
45+
$ct_request->sender_nickname = 'John Dow';
46+
$ct_request->example = str_repeat('Just text ', 10);
47+
$ct_request->agent = 'php-api';
48+
$ct_request->sender_ip = '178.32.183.43';
49+
$ct_request->event_token = isset($_POST['ct_bot_detector_event_token']) ? $_POST['ct_bot_detector_event_token'] : null;
50+
51+
$ct = new Cleantalk();
52+
$ct->server_url = $config_url;
53+
54+
// Check
55+
$ct_result = $ct->isAllowMessage($ct_request);
56+
57+
if ( $ct_result->allow == 1 ) {
58+
echo 'Comment allowed. Reason ' . $ct_result->comment;
59+
} else {
60+
echo 'Comment blocked. Reason ' . $ct_result->comment;
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
session_start();
3+
4+
$apikey = '';
5+
$email_field = 'name_email_form_field';
6+
$user_name_field = 'name_user_name_form_field';
7+
$message_field = 'name_message_form_field';
8+
$type_form = 'contact'; // use 'signup' for user signup form
9+
10+
// if downloaded, unzip and include the app, take your own relative path:
11+
require_once '../../cleantalk-antispam.php';
12+
// if install the app by composer package:
13+
use Cleantalk\CleantalkAntispam;
14+
15+
//require_once "lib/cleantalk-php-patch.php"; -- PHP-FPM
16+
17+
$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field, $user_name_field, $message_field, $type_form);
18+
$api_result = $cleantalk_antispam->handle();
19+
if ($api_result) { // the check fired
20+
if ($api_result->account_status !== 1) {
21+
// something wrong with your key or license, to know why read $api_result->codes
22+
echo 'Allowed. Spam protection disabled.'; // or do nothing
23+
} else {
24+
if ($api_result->allow === 1) {
25+
echo 'Allowed. Spam protection OK.'; // or do nothing
26+
} else {
27+
die('Blocked. Spam protection OK. Reason: ' . $api_result->comment); // or make your own handler
28+
}
29+
}
30+
}
31+
// your further code flow here
32+
?>
33+
34+
<form method="post">
35+
<label for="login">Login:</label>
36+
<input type="text" name="name_user_name_form_field" id="login" />
37+
<br />
38+
<label for="email">Email:</label>
39+
<input type="text" name="name_email_form_field" id="email" value="" />
40+
<br />
41+
<label for="message">Message:</label>
42+
<textarea name="name_message_form_field" id="message"></textarea>
43+
<br />
44+
<input type="submit" />
45+
</form>
46+
47+
<?php $cleantalk_antispam->frontendScript(); ?>

0 commit comments

Comments
 (0)