-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEmailEncoder.php
More file actions
414 lines (378 loc) · 17 KB
/
Copy pathEmailEncoder.php
File metadata and controls
414 lines (378 loc) · 17 KB
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
<?php
namespace Cleantalk\ApbctWP\Antispam;
use Cleantalk\ApbctWP\Helper;
use Cleantalk\Common\TT;
use Cleantalk\Variables\Post;
use Cleantalk\Antispam\CleantalkRequest;
use Cleantalk\Antispam\Cleantalk;
class EmailEncoder extends \Cleantalk\Antispam\EmailEncoder
{
/**
* @var null|string Comment from API response
*/
private $comment;
/**
* Check if the decoding is allowed
*
* Set properties
* this->api_response
* this->comment
*
* @return bool
*/
protected function checkRequest()
{
global $apbct;
$post_browser_sign = TT::toString(Post::get('browser_signature_params'));
$post_event_javascript_data = TT::toString(Post::get('event_javascript_data'));
$browser_sign = hash('sha256', $post_browser_sign);
$event_javascript_data = Helper::isJson($post_event_javascript_data)
? $post_event_javascript_data
: stripslashes($post_event_javascript_data);
$params = array(
'auth_key' => $apbct->api_key, // Access key
'agent' => APBCT_AGENT,
'event_token' => null, // Unique event ID
'event_javascript_data' => $event_javascript_data, // JSON-string params to analysis
'browser_sign' => $browser_sign, // Browser ID
'sender_ip' => Helper::ipGet('real', false), // IP address
'event_type' => 'CONTACT_DECODING', // 'GENERAL_BOT_CHECK' || 'CONTACT_DECODING'
'message_to_log' => json_encode(array_values($this->decoded_emails_array), JSON_FORCE_OBJECT), // Custom message
'page_url' => Post::get('post_url'),
'sender_info' => array(
'site_referrer' => Post::get('referrer'),
),
);
$ct_request = new CleantalkRequest($params);
$ct = new Cleantalk();
$this->has_connection_error = false;
// Options store url without scheme because of DB error with ''://'
$config = ct_get_server();
$ct->server_url = APBCT_MODERATE_URL;
$config_work_url = TT::getArrayValueAsString($config, 'ct_work_url');
$ct->work_url = preg_match('/https:\/\/.+/', $config_work_url)
? $config_work_url
: '';
$ct->server_ttl = TT::getArrayValueAsInt($config, 'ct_server_ttl');
$ct->server_changed = TT::getArrayValueAsInt($config, 'ct_server_changed');
$api_response = $ct->checkBot($ct_request);
// Allow to see to the decoded contact if error occurred
// Send error as comment in this case
if ( ! empty($api_response->errstr)) {
$this->comment = $api_response->errstr;
$this->has_connection_error = true;
return true;
}
$stub_comment = $api_response->allow
? esc_html__('Allowed', 'cleantalk-spam-protect')
: esc_html__('Blocked', 'cleantalk-spam-protect');
$this->comment = ! empty($api_response->comment) ? $api_response->comment : $stub_comment;
return $api_response->allow === 1;
}
/**
* Compile the response to pass it further
*
* @param $decoded_emails_array
* @param $is_allowed
*
* @return array
*/
protected function compileResponse($decoded_emails_array, $is_allowed)
{
$result = array();
foreach ( $decoded_emails_array as $encoded_email => $decoded_email ) {
$result[] = array(
'is_allowed' => $is_allowed,
'show_comment' => !$is_allowed,
'comment' => $this->comment,
'encoded_email' => strip_tags($encoded_email, '<a>'),
'decoded_email' => $is_allowed ? strip_tags($decoded_email, '<a>') : '',
);
}
return $result;
}
public static function getEncoderOptionDescription()
{
$common_description = __(
'Option encodes emails/phones on public pages of the site. This prevents robots from collecting and including your emails/phones in lists to spam.',
'cleantalk-spam-protect'
);
return $common_description;
}
public static function getExampleOfEncodedEmail($example_email)
{
$example_encoded = '';
if ( !empty($example_email) && is_string($example_email)) {
$example_encoded = sprintf(
'%s <span style="margin-left: 5px">%s.</span>',
__('Here is a sample of encoded email, click the email to decode it and see the effect', 'cleantalk-spam-protect'),
TT::toString($example_email)
);
}
return $example_encoded;
}
/**
* @return string|null
*/
public static function getBufferUsageOptionDescription()
{
return __('Use this option only if no encoding occurs when the "Encode contact data" option is enabled.', 'cleantalk-spam-protect');
}
/**
* @return string|null
*/
public static function getObfuscationModesDescription()
{
return __('This options manage the visual effect for encoded email.', 'cleantalk-spam-protect');
}
/**
* @return array[]
*/
public static function getObfuscationModesOptionsArray()
{
return array(
array('val' => 'blur', 'label' => __('Blur effect', 'cleantalk-spam-protect'),),
array('val' => 'obfuscate', 'label' => __('Replace with * symbol', 'cleantalk-spam-protect'),),
array('val' => 'replace', 'label' => __('Replace with the custom text', 'cleantalk-spam-protect'),),
);
}
public static function getPhonesEncodingDescription()
{
return __('Encode phone numbers', 'cleantalk-spam-protect');
}
public static function getEmailsEncodingDescription()
{
return __('Encode email addresses', 'cleantalk-spam-protect');
}
public static function getPhonesEncodingLongDescription()
{
$tmp = '
<p>%s</p>
<p>%s</p>
<p class="apbct-icon-right-dir" style="padding-left: 10px">%s</p>
<p class="apbct-icon-right-dir" style="padding-left: 10px">%s</p>
<p class="apbct-icon-right-dir" style="padding-left: 10px">%s</p>
<p class="apbct-icon-right-dir" style="padding-left: 10px">%s</p>
<p>%s</p>
<p class="apbct-icon-ok" style="padding-left: 10px">%s</p>
<p class="apbct-icon-ok" style="padding-left: 10px">%s</p>
<p class="apbct-icon-ok" style="padding-left: 10px">%s</p>
<p>%s</p>
<p>%s</p>
';
$tmp = sprintf(
$tmp,
__('Enable this option to encode contact phone numbers', 'cleantalk-spam-protect'),
__('There are a few requirements to the number format:', 'cleantalk-spam-protect'),
__('Should starting with "+" symbol', 'cleantalk-spam-protect'),
__('At least 8 digit numbers', 'cleantalk-spam-protect'),
__('Less than 13 digit numbers', 'cleantalk-spam-protect'),
__('Spaces, braces and dashes between digits are allowed', 'cleantalk-spam-protect'),
__('Examples of format', 'cleantalk-spam-protect'),
esc_html('+1 (234) 567-8901'),
esc_html('+12345678901'),
esc_html('+12 34 5678901'),
__('Complied numbers in the "a" tag with "tel" property will be also encoded', 'cleantalk-spam-protect'),
esc_html('<a href="tel:+11234567890">Call +1 (123) 456-7890</a>')
);
return $tmp;
}
public static function getEmailEncoderCommonLongDescription()
{
$tmp = '
<p>%s</p>
<p>%s</p>
<p>%s</p>
<p>%s</p>
<p class="apbct-icon-mail-alt" style="padding-left: 10px">%s</p>
<p class="apbct-icon-mobile" style="padding-left: 10px">%s</p>
<p>%s</p>
<p><a href="#apbct_setting_group__contact_data_encoding" onclick="handleAnchorDetection(\'apbct_setting_group__contact_data_encoding\')">%s</a> %s</p>
';
$tmp = sprintf(
$tmp,
__('This option encodes email addresses and phone numbers on your website to make them unreadable to spambots that scan public pages for contact information.', 'cleantalk-spam-protect'),
__('By hiding emails and phone numbers, it prevents your data from being harvested and added to spam mailing lists or used for unwanted calls.', 'cleantalk-spam-protect'),
__('Visitors will still be able to see and use your contact details, but bots will see only encoded or partially hidden versions.', 'cleantalk-spam-protect'),
__('Examples:', 'cleantalk-spam-protect'),
__('contact@example.com → co*****@********.com', 'cleantalk-spam-protect'),
__('+1 (234) 567-8901 → +1 (***) ***-****', 'cleantalk-spam-protect'),
__('You can also customize how emails are displayed before they are decoded by users — blurred, replaced with stars, or shown as a custom message. ', 'cleantalk-spam-protect'),
__('Click', 'cleantalk-spam-protect'),
__('to proceed to encoder settings', 'cleantalk-spam-protect')
);
return $tmp;
}
/**
* @return string
*/
public static function getObfuscationModesLongDescription()
{
$tmp = '
<p>%s</p>
<p>%s yourmail@yourmaildomain.com, %s +1 234 5678910</p>
<span>1. %s</span>
<div style="margin: 0 0 20px 10px">
<p>
%s
<p class="apbct-icon-eye"><span class="apbct-email-encoder">yo<span class="apbct-blur">******</span>@<span class="apbct-blur">************</span>in.com</span></p>
<p class="apbct-icon-eye"><span class="apbct-email-encoder">+1 23<span class="apbct-blur">********</span>10</span></p>
</p>
</div>
<span>2. %s</span>
<div style="margin: 0 0 20px 10px">
<p>
%s
<p class="apbct-icon-eye">yo******@************in.com</p>
<p class="apbct-icon-eye">+1 23*******01</p>
</p>
</div>
<span>3. %s</span>
<div style="margin: 0 0 20px 10px">
<p>%s</p>
</div>
';
$tmp = sprintf(
$tmp,
__('This option sets up how the hidden email/phone is visible on the site before decoding.', 'cleantalk-spam-protect'),
__('Example original email is', 'cleantalk-spam-protect'),
__('phone is', 'cleantalk-spam-protect'),
__('Blur effect', 'cleantalk-spam-protect'),
__('The contact will be partially replaced with blur effect:', 'cleantalk-spam-protect'),
__('Replace with "*"', 'cleantalk-spam-protect'),
__('The contact will be partially replaced with * symbols:', 'cleantalk-spam-protect'),
__('Replace with the custom text', 'cleantalk-spam-protect'),
__('The contact will be totally replaced with the custom text from the appropriate setting field.', 'cleantalk-spam-protect')
);
return $tmp;
}
public static function getDefaultReplacingText()
{
return __('Click to show encoded email', 'cleantalk-spam-protect');
}
public static function getLocalizationText()
{
/**
* Filter for the text of the decoding process
*
* @param string $decoding_text
*
* @return string
* @apbct_filter
*/
$decoding_text = apply_filters(
'apbct__ee_wait_for_decoding_text',
__('The magic is on the way!', 'cleantalk-spam-protect')
);
/**
* Filter for the text of the original email
*
* @param string $original_contact_text
*
* @return string
* @apbct_filter
*/
$original_contact_text = apply_filters(
'apbct__ee_original_email_text',
__('The complete one is', 'cleantalk-spam-protect')
);
/**
* Filter for the text of the decoding process
*
* @param string $decoding_process_text
*
* @return string
* @apbct_filter
*/
$decoding_process_text = apply_filters(
'apbct__ee_decoding_process_text',
__('Please wait a few seconds while we decode the contact data.', 'cleantalk-spam-protect')
);
return array(
'text__ee_click_to_select' => __('Click to select the whole data', 'cleantalk-spam-protect'),
'text__ee_original_email' => $original_contact_text,
'text__ee_got_it' => __('Got it', 'cleantalk-spam-protect'),
'text__ee_blocked' => __('Blocked', 'cleantalk-spam-protect'),
'text__ee_cannot_connect' => __('Cannot connect', 'cleantalk-spam-protect'),
'text__ee_cannot_decode' => __('Can not decode email. Unknown reason', 'cleantalk-spam-protect'),
'text__ee_email_decoder' => __('CleanTalk email decoder', 'cleantalk-spam-protect'),
'text__ee_wait_for_decoding' => $decoding_text,
'text__ee_decoding_process' => $decoding_process_text,
);
}
/**
* Return status table for settings hat.
* @param array $data array of required data to construct
* @return string
*/
public static function getEncoderStatusForSettingsHat($data)
{
$template = '
<div id="apbct_encoder_status__title_wrapper">
<span class="apbct-icon-eye-off"></span>
<span style="margin: 0 3px">%s</span>
<i setting="%s" class="apbct_settings-long_description---show apbct-icon-help-circled"></i>
</div>
<div class="apcbt_contact_data_encoder__line">
<span>%s </span>
<span>%s</span>
</div>
<div class="apcbt_contact_data_encoder__line">
<span>%s </span>
<span>%s </span><span><b>%s, </b></span>
<span>%s </span><span><b>%s, </b></span>
<span>%s </span><span><b>%s. </b></span>
<span>%s <a class="apbct_color--gray" href="#apbct_setting_group__contact_data_encoding" onclick="handleAnchorDetection(\'apbct_setting_group__contact_data_encoding\')">%s </a>%s</span>
</div>
';
$ancor_to_section = 'Advanced settings';
$description = __(
'This feature prevents robots from collecting and including your emails/phones in lists to spam.',
'cleantalk-spam-protect'
);
if (!empty($data['phones_on']) && !empty($data['encoder_enabled_global'])) {
$phone_status = __('On', 'cleantalk_spam_protect');
} else {
$phone_status = __('Off', 'cleantalk_spam_protect');
}
if (!empty($data['emails_on']) && !empty($data['encoder_enabled_global'])) {
$email_status = __('On', 'cleantalk_spam_protect');
} else {
$email_status = __('Off', 'cleantalk_spam_protect');
}
if (!empty($data['obfuscation_mode']) && !empty($data['encoder_enabled_global'])) {
if ($data['obfuscation_mode'] === 'blur') {
$obfuscation_mode = __('Blur', 'cleantalk_spam_protect');
}
if ($data['obfuscation_mode'] === 'obfuscate') {
$obfuscation_mode = __('Replace with * ', 'cleantalk_spam_protect');
}
if ($data['obfuscation_mode'] === 'replace') {
$obfuscation_mode = __('Replace with text', 'cleantalk_spam_protect');
}
$obfuscation_mode = empty($obfuscation_mode) ? 'n/a' : '' . $obfuscation_mode . '';
} else {
$obfuscation_mode = __('Disabled', 'cleantalk_spam_protect');
}
$email = isset($data['current_user_email']) && is_string($data['current_user_email'])
? $data['current_user_email']
: 'example@mail.com';
return sprintf(
$template,
__('Encoding contact data', 'cleantalk_spam_protect'),
'data__email_decoder',
$description,
static::getExampleOfEncodedEmail($email),
__('By now,', 'cleantalk_spam_protect'),
__('Phones encoding are', 'cleantalk_spam_protect'),
$phone_status,
__('Emails encoding are', 'cleantalk_spam_protect'),
$email_status,
__('Current Obfuscation mode are', 'cleantalk_spam_protect'),
$obfuscation_mode,
__('Use', 'cleantalk_spam_protect'),
$ancor_to_section,
__('to tune the encoding.', 'cleantalk_spam_protect')
);
}
}