Skip to content

Commit 9abc3d5

Browse files
committed
Code. Examples refactored.
1 parent 7ea8312 commit 9abc3d5

File tree

7 files changed

+79
-85
lines changed

7 files changed

+79
-85
lines changed

CleantalkValidate.php

Lines changed: 0 additions & 72 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
require_once "lib/CleantalkRequest.php";
66
require_once "lib/CleantalkResponse.php";
77
require_once "lib/CleantalkAntispam.php";
8+
require_once "lib/CleantalkApi.php";

example.php renamed to examples/direct_php/example.php

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

3-
require_once "lib/Cleantalk.php";
4-
require_once "lib/CleantalkRequest.php";
5-
require_once "lib/CleantalkResponse.php";
6-
require_once "lib/CleantalkHelper.php";
7-
require_once "lib/CleantalkAPI.php";
8-
require_once "lib/cleantalk-php-patch.php";
3+
require_once "../../cleantalk-antispam.php";
94

105
/**
11-
* Cleantalk example
6+
* Cleantalk PHP example
127
*
138
* @package Cleantalk Example
149
* @copyright (C) 2011 - 2025 CleanTalk team (https://cleantalk.org)
@@ -24,6 +19,10 @@
2419
// Take params from config
2520
$config_url = 'https://moderate.cleantalk.org';
2621
$auth_key = ''; // Set Cleantalk auth key
22+
23+
/**
24+
* Key validation example.
25+
*/
2726
$validation = CleantalkAPI::method__notice_validate_key($auth_key, 'php-api');
2827
$validation = json_decode($validation) ? json_decode($validation) : false;
2928
$is_valid = is_object($validation) && $validation->valid;
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(); ?>

lib/Cleantalk.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ class Cleantalk
9292
public $ssl_path = '';
9393

9494
/**
95-
* Minimal server response in miliseconds to catch the server
95+
* Minimal server response in milliseconds to catch the server
9696
*
9797
*/
9898
public $min_server_timeout = 50;
9999

100100
/**
101-
* Maximal server response in miliseconds to catch the server
101+
* Maximal server response in milliseconds to catch the server
102102
*
103103
*/
104104
public $max_server_timeout = 1500;

lib/CleantalkAntispam.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ public function __construct(
2424
$this->type_form = $type_form;
2525
}
2626

27+
/**
28+
* @return CleantalkResponse|null
29+
* @throws TransportException
30+
*/
2731
public function handle()
2832
{
2933
if ( count($_POST) === 0 ) {
3034
$_SESSION['ct_submit_time'] = time();
3135

32-
return;
36+
return null;
3337
}
3438

3539
$sender_email = isset($_POST[$this->email_field]) ? $_POST[$this->email_field] : '';

0 commit comments

Comments
 (0)