| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use Cleantalk\Antispam\CleantalkRequest; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class CleanTalkHandler |
| 9 |
{ |
| 10 |
public static function isEnabled() |
| 11 |
{ |
| 12 |
if (!self::isPluginEnabled()) { |
| 13 |
return false; |
| 14 |
} |
| 15 |
|
| 16 |
$settings = get_option('_fluentform_global_form_settings'); |
| 17 |
return $settings && 'yes' == ArrayHelper::get($settings, 'misc.cleantalk_status'); |
| 18 |
} |
| 19 |
|
| 20 |
public static function isPluginEnabled() |
| 21 |
{ |
| 22 |
$exists = method_exists('Cleantalk\Antispam\Cleantalk', 'isAllowMessage'); |
| 23 |
if ($exists) { |
| 24 |
global $apbct; |
| 25 |
return !!$apbct->data['key_is_ok']; |
| 26 |
} |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
public static function isSpamSubmission($formData, $form) |
| 31 |
{ |
| 32 |
$cleanTalkRequest = self::getCleanTalkRequest($formData, $form); |
| 33 |
$cleanTalk = new \Cleantalk\Antispam\Cleantalk(); |
| 34 |
$cleanTalk->server_url = 'https://moderate.cleantalk.org'; |
| 35 |
$response = $cleanTalk->isAllowMessage($cleanTalkRequest); |
| 36 |
|
| 37 |
return 0 == $response->allow; |
| 38 |
} |
| 39 |
|
| 40 |
protected static function getCleanTalkRequest($data, $form) |
| 41 |
{ |
| 42 |
global $apbct; |
| 43 |
$app = wpFluentForm(); |
| 44 |
$ip = $app->request->getIp(); |
| 45 |
|
| 46 |
$info = [ |
| 47 |
'auth_key' => $apbct->settings['apikey'], |
| 48 |
'sender_ip' => $ip, |
| 49 |
'contact_form_subject' => $form->title, |
| 50 |
'referrer' => urlencode($data['_wp_http_referer']), |
| 51 |
'page_url' => htmlspecialchars(@$_SERVER['SERVER_NAME'] . @$_SERVER['REQUEST_URI']), |
| 52 |
'submit_time' => isset($_SESSION['ct_submit_time']) ? time() - (int)$_SESSION['ct_submit_time'] : null, //@todo Improve this |
| 53 |
'agent' => 'php-api', |
| 54 |
'js_on' => 1, |
| 55 |
'sender_nickname' => '', |
| 56 |
'sender_email' => '', |
| 57 |
'message' => '', |
| 58 |
'phone' => '' |
| 59 |
]; |
| 60 |
|
| 61 |
$maps = [ |
| 62 |
'input_name' => 'sender_nickname', |
| 63 |
'input_email' => 'sender_email', |
| 64 |
'textarea' => 'message', |
| 65 |
'phone' => 'phone', |
| 66 |
]; |
| 67 |
$inputs = FormFieldsParser::getInputs($form, ['attributes']); |
| 68 |
|
| 69 |
foreach ($inputs as $input) { |
| 70 |
$element = ArrayHelper::get($input, 'element'); |
| 71 |
$key = ArrayHelper::get($input, 'attributes.name'); |
| 72 |
if (isset($maps[$element]) && !$info[$maps[$element]]) { |
| 73 |
$value = ArrayHelper::get($data, $key); |
| 74 |
if ($value) { |
| 75 |
if (is_array($value)) { |
| 76 |
$value = implode(' ', $value); |
| 77 |
} |
| 78 |
$info[$maps[$element]] = $value; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$info = apply_filters('fluentform/cleantalk_fields', $info, $data, $form); |
| 84 |
|
| 85 |
return new CleantalkRequest($info); |
| 86 |
} |
| 87 |
} |
| 88 |
|