Skip to content

Commit 7e2f151

Browse files
authored
Refactoring and event_token support added (#35)
* Added event_token to CleantalkRequest * Simplified the use of the library * Updated readme.md * updated instruction and added handle for user signup form * added installation via composer * refactoring cleantalk-antispam.php * fixed readme.md
1 parent ff2360c commit 7e2f151

File tree

4 files changed

+125
-173
lines changed

4 files changed

+125
-173
lines changed

README.md

Lines changed: 25 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -21,186 +21,51 @@ API sends a comment's text and several previous approved comments to the servers
2121
* PHP 5.6 and above
2222
* CURL support
2323

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

25-
## Sample SPAM test for user signup
26+
```php
27+
composer require cleantalk/php-antispam
28+
```
29+
30+
### Sample SPAM test for text comment and user signup
2631

2732
```php
2833
<?php
29-
3034
session_start();
3135

32-
//require_once "vendor/autoload.php"; -- Composer
33-
34-
require_once "lib/Cleantalk.php";
35-
require_once "lib/CleantalkHelper.php";
36-
require_once "lib/CleantalkRequest.php";
37-
38-
use Cleantalk\Cleantalk;
39-
use Cleantalk\CleantalkRequest;
40-
41-
// Take params from config
42-
$config_url = 'http://moderate.cleantalk.org/api2.0/';
43-
$auth_key = 'enter key'; // Set Cleantalk auth key
44-
45-
if (count($_POST)) {
46-
$sender_nickname = 'John Dow';
47-
if (isset($_POST['login']) && $_POST['login'] != '')
48-
$sender_nickname = $_POST['login'];
49-
50-
$sender_email = '[email protected]';
51-
if (isset($_POST['email']) && $_POST['email'] != '')
52-
$sender_email = $_POST['email'];
53-
54-
$sender_ip = null;
55-
if (isset($_SERVER['REMOTE_ADDR']))
56-
$sender_ip = $_SERVER['REMOTE_ADDR'];
57-
58-
$js_on = 0;
59-
if (isset($_POST['js_on']) && $_POST['js_on'] == date("Y"))
60-
$js_on = 1;
61-
62-
// The facility in which to store the query parameters
63-
$ct_request = new CleantalkRequest();
64-
65-
$ct_request->auth_key = $auth_key;
66-
$ct_request->agent = 'php-api';
67-
$ct_request->sender_email = $sender_email;
68-
$ct_request->sender_ip = $sender_ip;
69-
$ct_request->sender_nickname = $sender_nickname;
70-
$ct_request->js_on = $js_on;
71-
$ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time'];
72-
73-
$ct = new Cleantalk();
74-
$ct->server_url = $config_url;
75-
76-
// Check
77-
$ct_result = $ct->isAllowUser($ct_request);
78-
79-
if ($ct_result->allow == 1) {
80-
echo 'User allowed. Reason ' . $ct_result->comment;
81-
} else {
82-
echo 'User forbidden. Reason ' . $ct_result->comment;
83-
}
84-
echo '<br /><br />';
85-
}
86-
else
87-
{
88-
$_SESSION['ct_submit_time'] = time();
89-
}
90-
?>
36+
$apikey = 'your_cleantalk_api_key';
37+
$email_field = 'name_email_form_field';
38+
$user_name_field = 'name_user_name_form_field';
39+
$message_field = 'name_message_form_field';
40+
$type_form = 'contact'; // use 'signup' for user signup form
9141

92-
<form method="post">
93-
<label for="login">Login:<label>
94-
<input type="text" name="login" id="login" />
95-
<br />
96-
<label for="email">Email:<label>
97-
<input type="text" name="email" id="email" value="" />
98-
<br />
99-
<input type="hidden" name="js_on" id="js_on" value="0" />
100-
<input type="submit" />
101-
</form>
42+
// if downloaded, unzip and include the app:
43+
require_once 'php-antispam/cleantalk-antispam.php';
44+
// if install the app by composer package:
45+
use Cleantalk\CleantalkAntispam;
10246

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

106-
document.getElementById("js_on").value = date.getFullYear();
107-
</script>
108-
```
109-
110-
## Sample SPAM test for text comment
111-
112-
```php
113-
<?php
114-
115-
session_start();
116-
117-
//require_once "vendor/autoload.php"; -- Composer
118-
119-
require_once "lib/Cleantalk.php";
120-
require_once "lib/CleantalkHelper.php";
121-
require_once "lib/CleantalkRequest.php";
122-
123-
use Cleantalk\Cleantalk;
124-
use Cleantalk\CleantalkRequest;
125-
126-
// Take params from config
127-
$config_url = 'http://moderate.cleantalk.org/api2.0/';
128-
$auth_key = 'enter key'; // Set Cleantalk auth key
129-
130-
if (count($_POST)) {
131-
$sender_nickname = 'John Dow';
132-
if (isset($_POST['login']) && $_POST['login'] != '')
133-
$sender_nickname = $_POST['login'];
134-
135-
$sender_email = '[email protected]';
136-
if (isset($_POST['email']) && $_POST['email'] != '')
137-
$sender_email = $_POST['email'];
138-
139-
$sender_ip = null;
140-
if (isset($_SERVER['REMOTE_ADDR']))
141-
$sender_ip = $_SERVER['REMOTE_ADDR'];
142-
143-
$js_on = 0;
144-
if (isset($_POST['js_on']) && $_POST['js_on'] == date("Y"))
145-
$js_on = 1;
146-
147-
$message = null;
148-
if (isset($_POST['message']) && $_POST['message'] != '')
149-
$message = $_POST['message'];
150-
151-
// The facility in which to store the query parameters
152-
$ct_request = new CleantalkRequest();
153-
154-
$ct_request->auth_key = $auth_key;
155-
$ct_request->agent = 'php-api';
156-
$ct_request->sender_email = $sender_email;
157-
$ct_request->sender_ip = $sender_ip;
158-
$ct_request->sender_nickname = $sender_nickname;
159-
$ct_request->js_on = $js_on;
160-
$ct_request->message = $message;
161-
$ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time'];
162-
163-
$ct = new Cleantalk();
164-
$ct->server_url = $config_url;
165-
166-
// Check
167-
$ct_result = $ct->isAllowMessage($ct_request);
168-
169-
if ($ct_result->allow == 1) {
170-
echo 'Message allowed. Reason ' . $ct_result->comment;
171-
} else {
172-
echo 'Message forbidden. Reason ' . $ct_result->comment;
173-
}
174-
echo '<br /><br />';
175-
}
176-
else
177-
{
178-
$_SESSION['ct_submit_time'] = time();
179-
}
49+
$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field, $user_name_field, $message_field, $type_form);
50+
$cleantalk_antispam->handle();
18051
?>
18152

18253
<form method="post">
183-
<label for="login">Login:<label>
184-
<input type="text" name="login" id="login" />
54+
<label for="login">Login:</label>
55+
<input type="text" name="name_user_name_form_field" id="login" />
18556
<br />
186-
<label for="email">Email:<label>
187-
<input type="text" name="email" id="email" value="" />
57+
<label for="email">Email:</label>
58+
<input type="text" name="name_email_form_field" id="email" value="" />
18859
<br />
189-
<label for="message">Message:<label>
190-
<textarea name="message" id="message"></textarea>
60+
<label for="message">Message:</label>
61+
<textarea name="name_message_form_field" id="message"></textarea>
19162
<br />
192-
<input type="hidden" name="js_on" id="js_on" value="0" />
19363
<input type="submit" />
19464
</form>
19565

196-
<script type="text/javascript">
197-
var date = new Date();
198-
199-
document.getElementById("js_on").value = date.getFullYear();
200-
</script>
66+
<?php $cleantalk_antispam->frontendScript(); ?>
20167
```
20268

203-
20469
## API Response description
20570
API returns PHP object:
20671
* allow (0|1) - allow to publish or not, in other words spam or ham

cleantalk-antispam.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
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";

lib/CleantalkAntispam.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Cleantalk;
4+
5+
class CleantalkAntispam
6+
{
7+
private $apikey;
8+
private $email_field;
9+
private $user_name_field;
10+
private $message_field;
11+
private $type_form;
12+
13+
public function __construct(
14+
$apikey,
15+
$email_field,
16+
$user_name_field = '',
17+
$message_field = '',
18+
$type_form = 'contact'
19+
)
20+
{
21+
$this->apikey = $apikey;
22+
$this->email_field = $email_field;
23+
$this->user_name_field = $user_name_field;
24+
$this->message_field = $message_field;
25+
$this->type_form = $type_form;
26+
}
27+
28+
public function handle()
29+
{
30+
if (count($_POST) === 0) {
31+
$_SESSION['ct_submit_time'] = time();
32+
return;
33+
}
34+
35+
$sender_email = isset($_POST[$this->email_field]) ? $_POST[$this->email_field] : '';
36+
$sender_nickname = isset($_POST[$this->user_name_field]) ? $_POST[$this->user_name_field] : '';
37+
$message = isset($_POST[$this->message_field]) ? $_POST[$this->message_field] : '';
38+
$sender_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
39+
40+
$ct_request = new CleantalkRequest();
41+
42+
$ct_request->auth_key = $this->apikey;
43+
$ct_request->agent = 'php-api';
44+
$ct_request->sender_email = $sender_email;
45+
$ct_request->sender_ip = $sender_ip;
46+
$ct_request->sender_nickname = $sender_nickname;
47+
$ct_request->message = $message;
48+
$ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time'];
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 = $ct_request::CLEANTALK_API_URL;
53+
54+
// Check
55+
if ($this->type_form === 'signup') {
56+
$ct_result = $ct->isAllowUser($ct_request);
57+
}
58+
if ($this->type_form === 'contact') {
59+
$ct_result = $ct->isAllowMessage($ct_request);
60+
}
61+
62+
if ($ct_result->allow == 1) {
63+
if ($this->type_form === 'signup') {
64+
echo 'User allowed. Reason ' . $ct_result->comment;
65+
}
66+
if ($this->type_form === 'contact') {
67+
echo 'Message allowed. Reason ' . $ct_result->comment;
68+
}
69+
} else {
70+
if ($this->type_form === 'signup') {
71+
echo 'User forbidden. Reason ' . $ct_result->comment;
72+
}
73+
if ($this->type_form === 'contact') {
74+
echo 'Message forbidden. Reason ' . $ct_result->comment;
75+
}
76+
}
77+
echo '<br /><br />';
78+
}
79+
80+
public function frontendScript()
81+
{
82+
echo '<script type="text/javascript" src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js"></script>';
83+
}
84+
}

lib/CleantalkRequest.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
/**
55
* Request class
66
*/
7-
class CleantalkRequest {
7+
class CleantalkRequest
8+
{
9+
const CLEANTALK_API_URL = 'https://moderate.cleantalk.org/api2.0/';
810

911
/**
1012
* All http request headers
@@ -120,17 +122,6 @@ class CleantalkRequest {
120122
public $x_forwarded_for = '';
121123
public $x_real_ip = '';
122124

123-
/**
124-
* Is enable Java Script,
125-
* valid are 0|1|2
126-
* Status:
127-
* null - JS html code not inserted into phpBB templates
128-
* 0 - JS disabled at the client browser
129-
* 1 - JS enabled at the client broswer
130-
* @var int
131-
*/
132-
public $js_on = null;
133-
134125
/**
135126
* user time zone
136127
* @var string
@@ -154,7 +145,12 @@ class CleantalkRequest {
154145
* Method name
155146
* @var string
156147
*/
157-
public $method_name = 'check_message';
148+
public $method_name = 'check_message';
149+
150+
/**
151+
* @var string|null
152+
*/
153+
public $event_token;
158154

159155
/**
160156
* Fill params with constructor

0 commit comments

Comments
 (0)