Skip to content
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
.idea
/vendor/
/composer.lock
54 changes: 28 additions & 26 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
This guideline will help you to use CleanTalk check_bot API method via the special library that can be downloaded from this repo.
## Usage
1. Download the lib and unpack it to your website filesystem. Look at the suggested files structure with unpacked data highlighted:
<br><br>![files_structure.png](files_structure.png)
<br><br>
2. Add the CleanTalk Bot-detector JS library wrapper `https://fd.cleantalk.org/ct-bot-detector-wrapper.js` as `<script>`tag to the HTML page contains the form you want to protect.
1. Install the package via composer

`composer install cleantalk/php-checkbot`

2. Include the composer autoloader into your app

`require_once 'vendor/autoloader.php';`

3. Add the CleanTalk Bot-detector JS library wrapper `https://fd.cleantalk.org/ct-bot-detector-wrapper.js` as `<script>`tag to the HTML page contains the form you want to protect.

```html
<script src="https://fd.cleantalk.org/ct-bot-detector-wrapper.js"></script>
```
3. Fill the [config.php](src%2FCleantalk%2Fconfig.php).
```php
src/FCleantalk/config.php
```
&nbsp;&nbsp;&nbsp;&nbsp;Obligatory properties are:
* access_key - your CleanTalk Anti-Spam access key. If you do not have a key, [register the new account](https://cleantalk.org/register?utm_source=github&utm_medium=article&utm_campaign=instructions&utm_content=link&utm_term=create+account) or [access dashboard](https://cleantalk.org/my?utm_source=github&utm_medium=referral&utm_campaign=bot_detector&utm_content=link&utm_term=dashboard) to an existing account to get it.
* trust_cleantalk_decision - set this to true if you do not want to set custom checking settings

4. To start use CheckBot library, include "yourpath/src/autoloader.php" into the top of codepage where you want to perform the check.
```php
require_once 'yourpath/src/autoloader.php';
```
5. Then create a new CleanTalk\CheckBot object, provide $_POST or filtered POST data to the constructor.
4. Prepare config and create a new CleanTalk\CheckBot object, provide $_POST or filtered POST data to the constructor.

```php
$bot_checker = new \Cleantalk\CheckBot($_POST);
$check_bot_config = array(
'access_key' => "YOUR-CLEANTALK-API-KEY"
);
$config = new \Cleantalk\CheckBot\CheckBotConfig($check_bot_config);
$bot_checker = new \Cleantalk\CheckBot\CheckBot($config, $_POST);
```

6. Then perform the check:
5. Then perform the check:
```php
$is_bot = $bot_checker->check()->getVerdict();
```

7. Then do the actions depending on the verdict.
6. Then do the actions depending on the verdict.
```php
if ( $is_bot ) {
die ($bot_checker->getBlockMessage());
}
```

8. How it looks in the suggested files structure:
7. How it looks in the suggested files structure:
- `index.html`:
```html
<!DOCTYPE html>
Expand All @@ -60,8 +59,8 @@ if ( $is_bot ) {
- `your_form_handler.php`:
```php
<?php
//your_libs\cleantalk_check_bot\src
require_once 'your_libs/cleantalk_check_bot/src/autoloader.php';

require_once 'vendor/autoload.php';

if ( empty($_POST) ) {
return;
Expand All @@ -81,7 +80,11 @@ function handle_search_form($post)
}

//create a new CheckBot object
$bot_checker = new \Cleantalk\CheckBot($post);
$check_bot_config = array(
'access_key' => "YOUR-CLEANTALK-API-KEY"
);
$config = new \Cleantalk\CheckBot\CheckBotConfig($check_bot_config);
$bot_checker = new \Cleantalk\CheckBot\CheckBot($config, $_POST);

//call visitor check and make decision
$is_bot = $bot_checker->check()->getVerdict();
Expand All @@ -95,14 +98,13 @@ function handle_search_form($post)
```
## Config setup explanation

### Default config.php
### Available config array elements
```php
<?php
global $check_bot_config;
$check_bot_config = array(
'access_key' => "",
'trust_cleantalk_decision' => true,
'block_no_js_visitors' => true,
'block_no_js_visitors' => false,
'common_block_message' => 'Visitor blocked. It seems to be a bot.',
'bot_expectation' => 0.5,
'ip_frequency_24hour' => 50,
Expand Down
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "cleantalk/php-checkbot",
"description": "The best app to filter bots during the simple page viewing",
"license": "GPL-3.0-or-later",
"autoload": {
"psr-4": {
"Cleantalk\\CheckBot\\": "src/"
}
},
"authors": [
{
"name": "CleanTalk Team",
"email": "plugins@cleantalk.org"
}
],
"require": {
"cleantalk/antispam": "*"
}
}
Binary file removed files_structure.png
Binary file not shown.
15 changes: 8 additions & 7 deletions src/Cleantalk/CheckBot.php → src/CheckBot.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

namespace Cleantalk;
namespace Cleantalk\CheckBot;

use Cleantalk\Common\Antispam\Cleantalk;
use Cleantalk\Common\Antispam\CleantalkRequest;
use Cleantalk\Common\Antispam\CleantalkResponse;

class CheckBot
{
Expand Down Expand Up @@ -32,12 +36,10 @@ class CheckBot

private $request_success = true;

public function __construct(array $post_data)
public function __construct(CheckBotConfig $config, $post_data = [])
{
$this->config = $config;
$this->post = $post_data;
$this->config = new CheckBotConfig();
$load_config_result = $this->config->loadConfig();
$this->writeLog($load_config_result['msg']);
}

/**
Expand Down Expand Up @@ -70,7 +72,6 @@ private function setEventToken($event_token)
*/
private function checkBotApiCall()
{

$ct_request = new CleantalkRequest();
$ct_request->event_token = $this->event_token;
$ct_request->auth_key = $this->config->access_key;
Expand All @@ -80,7 +81,7 @@ private function checkBotApiCall()
}

$ct = new Cleantalk();
$ct->server_url = $ct_request::CLEANTALK_API_URL;
$ct->server_url = 'https://moderate.cleantalk.org';
$ct_result = $ct->checkBot($ct_request);
$this->writeLog('raw result: ' . var_export($ct_result, true));

Expand Down
39 changes: 13 additions & 26 deletions src/Cleantalk/CheckBotConfig.php → src/CheckBotConfig.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cleantalk;
namespace Cleantalk\CheckBot;

class CheckBotConfig
{
Expand All @@ -16,41 +16,28 @@ class CheckBotConfig

public $access_key = '';
public $trust_cleantalk_decision = true;
public $block_no_js_visitors = true;
public $block_no_js_visitors = false;
public $common_block_message = 'Visitor blocked. It seems to be a bot.';
public $do_log = true;
public $do_log = false;
public $bot_expectation = 0.5;
public $ip_frequency_24hour = 50;
public $ip_frequency_1hour = 15;
public $ip_frequency_10min = 5;

public function __construct()
public function __construct($config_array)
{
}

public function loadConfig()
{
try {
require_once 'config.php';
global $check_bot_config;

if (empty($check_bot_config) || !$this->isObligatoryParamsPresented($check_bot_config) ) {
throw new \Exception('CheckBot config: not enough params set. Load defaults.');
}
if (empty($config_array) || !$this->isObligatoryParamsPresented($config_array) ) {
throw new \Exception('CheckBot config: not enough params set. Load defaults.');
}

foreach ( $check_bot_config as $param_name => $param ) {
if ( property_exists(static::class, $param_name) ) {
$type = gettype($this->$param_name);
$this->$param_name = $param;
settype($this->$param_name, $type);
}
foreach ( $config_array as $param_name => $param ) {
if ( property_exists(static::class, $param_name) ) {
$type = gettype($this->$param_name);
$this->$param_name = $param;
settype($this->$param_name, $type);
}
} catch (\Exception $e) {
return array('success' => false, 'msg' => $e->getMessage());
}

return array('success' => true, 'msg' => 'custom config loaded.' . var_export($this, true));


}

public function __get($name)
Expand Down
Loading