-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleantalk_wordpress_sdk.php
742 lines (652 loc) · 22.7 KB
/
cleantalk_wordpress_sdk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<?php
/**
* DO NOT FORGET TO USE YOUR OWN NAMESPACE
*/
//namespace CleanTalkCustomNameSpaceSDK;
defined('ABSPATH') || exit;
/**
* CleanTalk WordPress SDK for spam protection
*
* Provides integration with CleanTalk's anti-spam services including:
* - API key management
* - Spam checking
* - Bot detection
*/
class CleanTalkSDK
{
/**
* SDK identifier name
*/
const SDK_NAME = 'cleantalk_wordpress_sdk';
/**
* Current SDK version
*/
const SDK_VERSION = '0.1.1';
/**
* WordPress option name for storing the access key
*/
const STORAGE_ACCES_KEY_NAME = 'cleantalk_wordpress_sdk_key';
/**
* URL for CleanTalk's bot detector script
*/
const BOT_DETECTOR_SCRIPT_URL = 'https://moderate.cleantalk.org/ct-bot-detector-wrapper.js';
/**
* CleanTalk API access key
* @var string
*/
private $access_key = '';
/**
* Message data transfer object
* @var CleantalkMessageSDK
*/
public $cleantalk_message;
/**
* Response data transfer object
* @var CleantalkResponseSDK
*/
public $cleantalk_response;
/**
* Vendor agent
* @var string
*/
protected $vendor_agent;
/**
* Constructor
*
* @param string $vendor_agent_prefix CleanTalk API access key
* @param string $vendor_version CleanTalk API access key
* @param string|null $access_key CleanTalk API access key
* @param bool $load_scripts Whether to load public scripts
*/
public function __construct($vendor_agent_prefix, $vendor_version, $access_key = null, $load_scripts = true)
{
add_action('wp_ajax_apbct_sdk_key_form', array($this, 'sync'));
$load_scripts && $this->wpLoadPublicScripts();
$this->cleantalk_response = new CleantalkResponseSDK();
$vendor_agent_prefix = !empty($vendor_agent_prefix) && is_string($vendor_agent_prefix) ? $vendor_agent_prefix : 'unknown_vendor';
$vendor_agent_version = !empty($vendor_version) && is_string($vendor_version) ? $vendor_version : '1.0.0';
$this->vendor_agent = 'wordpress-' . $vendor_agent_prefix . '-' . $vendor_agent_version;
$this->setAccessKey($access_key, ! empty($access_key));
}
/**
* Load public scripts if conditions are met
*/
public function wpLoadPublicScripts()
{
if ( static::storageGetAccessKey() && ! static::isCleantalkPluginActive() ) {
add_action('wp_head', array($this, 'printPublicScriptBotDetector'));
}
}
/**
* Set the access key for this instance, if second arg is true, this will save the key to storage
*
* @param string|null $access_key CleanTalk API access key
* @param bool $save_key_to_storage
*/
public function setAccessKey($access_key, $save_key_to_storage = true)
{
if ( is_string($access_key) && ! empty($access_key) ) {
$this->access_key = $access_key;
$save_key_to_storage && self::storageUpdateAccessKey($access_key);
}
}
/**
* Synchronize and validate the access key with CleanTalk servers
*
* @param string|null $input_key Key to validate
* @param bool $direct_call Whether this is a direct call (not AJAX)
* @param bool $save_key_to_storage Do save key
*
* @return array|null Returns array if direct_call, otherwise sends JSON response
*/
public function sync($input_key = null, $direct_call = false, $save_key_to_storage = true)
{
if ( empty($input_key) ) {
$key = isset($_POST[static::STORAGE_ACCES_KEY_NAME]) && is_string($_POST[static::STORAGE_ACCES_KEY_NAME])
? sanitize_text_field($_POST[static::STORAGE_ACCES_KEY_NAME])
: '';
} else {
$key = $input_key;
}
$result = [
'success' => false,
'message' => '',
];
if ( empty($key) ) {
$save_key_to_storage && self::storageUpdateAccessKey('');
$result['success'] = ! $direct_call;
$result['message'] = 'key is empty';
return self::formatSyncResult($result, $direct_call);
}
try {
if (
isset($_POST['apbct_sdk_key_form_nonce'])
&& is_string($_POST['apbct_sdk_key_form_nonce'])
&& ! wp_verify_nonce($_POST['apbct_sdk_key_form_nonce'], 'apbct_sdk_key_form')
) {
throw new \Exception('nonce is not valid');
}
$response = wp_remote_post('https://api.cleantalk.org/', array(
'body' => array(
'method_name' => 'notice_paid_till',
'auth_key' => $key,
),
));
if ( is_wp_error($response) || ! $response ) {
throw new \Exception(wp_remote_retrieve_response_message($response));
}
$body = wp_remote_retrieve_body($response);
if ( empty($body) ) {
throw new \Exception('content not found');
}
$response_code = wp_remote_retrieve_response_code($response);
if ( $response_code >= 400 ) {
$msg = '';
if ( $response instanceof \WP_Error ) {
$msg = $response->get_error_message();
} elseif ( is_object($response) && isset($response->detail) ) {
$msg = $response->detail;
}
throw new \Exception('response code error: ' . $response_code . ' - ' . $msg);
}
$response = json_decode($body);
if ( is_null($response) ) {
throw new \Exception('decoded response null');
}
if ( isset($response->data->valid) && $response->data->valid == 0 ) {
throw new \Exception('key is not valid');
}
if ( isset($response->data->product_id) && $response->data->product_id != 1 ) {
throw new \Exception('key is not for Anti-Spam service');
}
if ( isset($response->data->moderate) && $response->data->moderate == 0 ) {
throw new \Exception('service disabled, check key license');
}
} catch (\Exception $e) {
$result['message'] = $e->getMessage();
return self::formatSyncResult($result, $direct_call);
}
$save_key_to_storage && self::storageUpdateAccessKey($key);
$result['success'] = true;
return self::formatSyncResult($result, $direct_call);
}
/**
* Format the synchronization result for response
*
* @param array $result Result array with success and message
* @param bool $direct_call Whether this is a direct call
*
* @return array|void Returns array or sends JSON response
*/
private static function formatSyncResult($result, $direct_call)
{
if ( $direct_call ) {
return $result;
}
wp_send_json($result);
}
/**
* Render the API key form HTML
*
* @param string|null $vendor_name
* @return string HTML for the key form
*/
protected function renderKeyForm($vendor_name = null)
{
$key = (string)static::storageGetAccessKey();
$agitation = 'CleanTalk is cloud Anti-Spam service which focuses on a background scoring for websites visitors to highlight legitimate visitors and filter spambots.<br>Click here to get your key and start filter spam bots! <a href="' . static::getCleanTalkUTMLink($vendor_name, 'register') . '" target="_blank">https://cleantalk.org/register</a>';
$agitation = wp_kses(
$agitation,
array('a' => array('href' => array(), 'target' => array()), 'br' => array())
);
$key_is_ok_desc = 'Anti-Spam is active, use <a href="' . static::getCleanTalkUTMLink($vendor_name, 'my') . '" target="_blank">Dashboard</a> to tune the service.';
$key_is_ok_desc = wp_kses($key_is_ok_desc, array('a' => array('href' => array(), 'target' => array())));
$message = $key ? $key_is_ok_desc : $agitation;
return '<p><span class="apbct_sdk_description">' . $message . '</span>
. <form id="apbct_sdk-key-form" method="post">'
. '<input type="text" name="' . CleanTalkSDK::STORAGE_ACCES_KEY_NAME . '" value="' . $key . '" placeholder="API key"> <input type="submit" value="Save" class="apbct_sdk_submit">'
. wp_nonce_field(
'apbct_sdk_key_form',
'apbct_sdk_key_form_nonce'
) . '<input type="hidden" name="action" value="apbct_sdk_key_form">'
. '</form></p>'
. '<script> jQuery(document).ready(function($) {
$("form#apbct_sdk-key-form").submit(function(e) {
e.preventDefault();
$(".apbct_sdk_submit").attr("disabled","disabled").css("cursor", "wait");
$.post("' . admin_url('admin-ajax.php') . '", $(this).serialize(), function(response) {
$(".apbct_sdk-error").remove();
$(".apbct_sdk_submit").removeAttr("disabled").removeAttr("style");
if (response.success) {
const message = response.message === "key is empty" ? "' . addslashes($agitation) . '" : "' . addslashes($key_is_ok_desc) . '";
$("input[name=\'apbct_sdk_description\']").html(message);
} else {
$("input[name=\'apbct_sdk_description\']").html("' . addslashes($agitation) . '");
$("input[name=\'' . static::STORAGE_ACCES_KEY_NAME . '\']").parent().after("<div class=\'error apbct_sdk-error\'>" + response.message + "</div>");
}
});
});
});
</script>';
}
/**
* Print the bot detector script tag
*/
public static function printPublicScriptBotDetector()
{
echo static::getBotDetectorScriptTag();
}
/**
* Get the bot detector script tag HTML
*
* @return string Script tag HTML
*/
public static function getBotDetectorScriptTag()
{
return sprintf(
'<script src="%s" id="ct_bot_detector-js"></script>',
esc_attr(static::getBotDetectorScriptURL())
);
}
/**
* Get the URL for the bot detector script
*
* @return string Script URL
*/
public static function getBotDetectorScriptURL()
{
return static::BOT_DETECTOR_SCRIPT_URL;
}
/**
* Enqueue the bot detector script in WordPress
* @psalm-suppress InvalidArgument
*/
public static function wpEnqueuePublicScriptBotDetector()
{
wp_enqueue_script(
self::SDK_NAME . '--bot-detector',
static::getBotDetectorScriptURL(),
[],
self::SDK_VERSION,
array(
'in_footer' => false,
'strategy' => 'defer'
)
);
}
/**
* Check if data contains spam. A simple way to get if spam or not.
* Use if you need bool value as result, a do not need anything else.
* Remember, if anything goes wrong with submission, the result become false.
*
* @param CleantalkMessageSDK $cleantalk_sdk_message Data to check
*
* @return bool True if spam detected, false otherwise
*/
public function isSpam($cleantalk_sdk_message)
{
$response = $this->getCleanTalkResponse($cleantalk_sdk_message);
if ( isset($response->allow) && $response->allow == 0 ) {
return true;
}
return false;
}
/**
* Get response from CleanTalk servers for data check
*
* @param CleantalkMessageSDK $cleantalk_sdk_message Data to check
*
* @return CleantalkResponseSDK Response object
*/
public function getCleanTalkResponse($cleantalk_sdk_message)
{
global $cleantalk_executed;
if ( !empty($this->access_key) ) {
$work_key = $this->access_key;
} else if ( !empty($cleantalk_sdk_message->auth_key) ) {
$work_key = $cleantalk_sdk_message->auth_key;
} else {
$work_key = static::storageGetAccessKey();
}
try {
if ( ! $work_key ) {
$this->cleantalk_response->skip_reason = 'no access key';
throw new \Exception('Request skipped');
}
$skip_reason = apply_filters('cleantalk_sdk_skip_request', '');
if ( ! empty($skip_reason) ) {
$this->cleantalk_response->skip_reason = $skip_reason;
throw new \Exception('Request skipped');
}
if ( $cleantalk_executed ) {
$this->cleantalk_response->skip_reason = 'cleantalk already executed';
throw new \Exception('Request skipped');
}
if ( empty($cleantalk_sdk_message) ) {
$this->cleantalk_response->skip_reason = 'empty message';
throw new \Exception('Bad params');
}
if ( empty($cleantalk_sdk_message->event_token) ) {
$this->cleantalk_response->skip_reason = 'event token is empty';
throw new \Exception('Bad params');
}
$response = wp_remote_post('https://moderate.cleantalk.org/api2.0', array(
'body' => $cleantalk_sdk_message->getJson(),
));
if ( is_wp_error($response) ) {
$this->cleantalk_response->skip_reason = 'wp error occured';
throw new \Exception('Request failed');
}
$body = wp_remote_retrieve_body($response);
if ( empty($body) ) {
$this->cleantalk_response->skip_reason = 'empty response body';
throw new \Exception('Request failed');
}
$this->cleantalk_response->data = $body;
$response_code = wp_remote_retrieve_response_code($response);
if ( $response_code >= 400 ) {
$this->cleantalk_response->skip_reason = 'response code >400';
throw new \Exception('Request failed');
}
$response = json_decode($body);
if ( is_null($response) ) {
$this->cleantalk_response->skip_reason = 'cannot decode response JSON';
throw new \Exception('Request failed');
}
if ( ! (isset($response->allow, $response->comment)) ) {
$this->cleantalk_response->skip_reason = 'unknown response format';
throw new \Exception('Request failed');
}
$cleantalk_executed = true;
$this->cleantalk_response->allow = $response->allow;
$this->cleantalk_response->comment = $response->comment;
$this->cleantalk_response->success = true;
} catch (\Exception $e) {
$this->cleantalk_response->skip_reason = $e->getMessage() . ': ' . $this->cleantalk_response->skip_reason;
}
return $this->cleantalk_response;
}
/**
* Gather default parameters for CleanTalk API request
*
* @param array $data Input data
* @param string|null $work_key Optional API key
*
* @return CleantalkMessageSDK Parameters for API request in JSON format
*/
public function getDefaultHTTPMessage($data, $work_key)
{
$email_pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
$email = null;
array_walk_recursive($data, function ($value) use (&$email, $email_pattern) {
if ( is_string($value) && preg_match($email_pattern, $value, $matches) ) {
$email = $matches[0];
}
});
if ( function_exists('apache_request_headers') ) {
$all_headers = array_filter(
apache_request_headers(),
function ($value, $key) {
return strtolower($key) !== 'cookie';
},
ARRAY_FILTER_USE_BOTH
);
}
//message details
$message = [];
$message['sender_ip'] = isset($_SERVER['REMOTE_ADDR'])
? $_SERVER['REMOTE_ADDR']
: '';
$message['x_forwarded_for'] = isset($_SERVER['HTTP_X_FORWARDED_FOR'])
? $_SERVER['HTTP_X_FORWARDED_FOR']
: null;
$message['x_real_ip'] = isset($_SERVER['HTTP_X_REAL_IP'])
? $_SERVER['HTTP_X_REAL_IP']
: null;
$message['auth_key'] = ! empty($work_key)
? $work_key
: '';
$message['agent'] = empty($this->vendor_agent)
? self::SDK_NAME . '_' . self::SDK_VERSION
: $this->vendor_agent;
$message['sender_email'] = $email;
$message['event_token'] = ! empty($data['ct_bot_detector_event_token'])
? $data['ct_bot_detector_event_token']
: '';
$message['all_headers'] = ! empty($all_headers)
? json_encode($all_headers)
: '';
//sender info
$message['referrer'] = isset($_SERVER['HTTP_REFERER'])
? $_SERVER['HTTP_REFERER']
: '';
$message['user_agent'] = isset($_SERVER['HTTP_USER_AGENT'])
? $_SERVER['HTTP_USER_AGENT']
: '';
$message['sdk_version'] = self::SDK_VERSION;
$this->cleantalk_message = new CleantalkMessageSDK($message);
return $this->cleantalk_message;
}
/**
* Check if CleanTalk plugin is active
*
* @return bool True if plugin active, false otherwise
*/
public static function isCleantalkPluginActive()
{
return defined('APBCT_VERSION');
}
/**
* Update the stored access key
*
* @param string $access_key API key to store
*/
protected static function storageUpdateAccessKey($access_key)
{
update_option(static::STORAGE_ACCES_KEY_NAME, $access_key);
}
/**
* Get the stored access key
*
* @return string|false The stored key or false if not set
*/
protected static function storageGetAccessKey()
{
$key = get_option(static::STORAGE_ACCES_KEY_NAME, '');
if ( is_string($key) && ! empty($key) ) {
return $key;
}
return false;
}
/**
* @param string|null $utm_vendor_name UTM campaign source should contain vendor name.
*
* @return string
*/
public static function getCleanTalkUTMLink($utm_vendor_name, $get_path = '')
{
$utm = array(
'utm_campaign' => is_string($utm_vendor_name) ? $utm_vendor_name : 'unknown_sdk_vendor',
'utm_source' => 'admin_panel',
'utm_medium' => 'plugin',
'utm_content' => 'pluginsettings',
);
$utm = http_build_query($utm);
$href = 'https://cleantalk.org/' . $get_path . '/' . $utm;
return esc_url($href);
}
}
/**
* Data Transfer Object for messages to CleanTalk
*/
class CleantalkMessageSDK
{
/**
* Sender IP address
* @var string
*/
public $sender_ip = '';
/**
* X-Forwarded-For header value
* @var string
*/
public $x_forwarded_for = '';
/**
* X-Real-IP header value
* @var string
*/
public $x_real_ip = '';
/**
* API authentication key
* @var string
*/
public $auth_key = '';
/**
* User agent string
* @var string
*/
public $agent = '';
/**
* Sender email address
* @var string
*/
public $sender_email = '';
/**
* Sender nickname
* @var string
*/
public $sender_nickname = '';
/**
* Event token for bot detection
* @var string
*/
public $event_token = '';
/**
* All request headers as JSON
* @var string
*/
public $all_headers = '';
/**
* Sender info. Referrer.
* @var string
*/
public $referrer = '';
/**
* Sender info. UA.
* @var string
*/
public $user_agent = '';
/**
* Sender info. Visitor message.
* @var string
*/
public $message = '';
/**
* JSON encoded string of sender info, including referrer, user agent, and message.
* @var string
*/
private $sender_info = '';
/**
* @var bool is the request is registration
*/
public $registration_flag = false;
/**
* @var string SDK version
*/
private $sdk_version = '';
/**
* @var string misc service data
*/
private $post_info = '';
/**
* @var string the API method for spam checking
*/
private $method_name = 'check_message';
/**
* Constructor - initializes sender_info
*/
public function __construct($params)
{
foreach ( $params 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);
}
}
$this->prepareServiceData();
}
/**
* Service method. Prepare sender_info, post_info and method name.
* @return void
*/
public function prepareServiceData()
{
//prepare sender info
$this->sender_info = json_encode(
[
'REFFERRER' => $this->referrer,
'user_agent' => $this->user_agent,
'sdk_version' => $this->sdk_version,
]
);
//prepare method name
$this->method_name = $this->registration_flag ? 'check_newuser' : 'check_message';
//prepare post info
$comment_type = $this->registration_flag ? 'sdk_registration' : 'sdk_contact_form';
$this->post_info = json_encode(
[
'comment_type' => $comment_type . '_' . $this->agent
]
);
}
/**
* Get all properties as array
*
* @return array Message data as associative array
*/
public function getArray()
{
$this->prepareServiceData();
return get_object_vars($this);
}
/**
* @return string
*/
public function getJson()
{
$json = json_encode($this->getArray());
return false !== $json ? $json : '';
}
}
/**
* Data Transfer Object for CleanTalk response
*/
class CleantalkResponseSDK
{
/**
* Whether to allow the request (1 = allow, 0 = block)
* @var int
*/
public $allow = 1;
/**
* Response comment/message
* @var string
*/
public $comment = 'Not spam';
/**
* Raw response data
* @var string
*/
public $data = '';
/**
* Reason for skipping check
* @var string
*/
public $skip_reason = '';
/**
* Whether request succeeded
* @var bool
*/
public $success = false;
}