-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleantalk_api.php
127 lines (97 loc) · 2.91 KB
/
cleantalk_api.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
<?php
require_once("cleantalk.class.php");
require_once("JSON.php");
/**
* Get ct_get_checkjs_value
* @return string
*/
function ct_get_checkjs_value_plugin($random_key = false) {
$ct_data=get_option("cleantalk_data");
if ($random_key) {
$keys = $ct_data['js_keys'];
$keys_checksum = md5(json_encode($keys));
$key = null;
$latest_key_time = 0;
foreach ($keys as $k => $t) {
// Removing key if it's to old
if (time() - $t > $ct_data['js_keys_store_days'] * 86400) {
unset($keys[$k]);
continue;
}
if ($t > $latest_key_time) {
$latest_key_time = $t;
$key = $k;
}
}
// Get new key if the latest key is too old
if (time() - $latest_key_time > $ct_data['js_key_lifetime']) {
$key = rand();
$keys[$key] = time();
}
if (md5(json_encode($keys)) != $keys_checksum) {
$ct_data['js_keys'] = $keys;
update_option('cleantalk_data', $ct_data);
}
} else {
$key = md5($ct_options['apikey'] . '+' . get_option('admin_email'));
}
return $key;
}
/**
* Validates JavaScript anti-spam test
*
*/
function js_test_plugin($field_name = 'ct_checkjs', $data = null, $random_key = false) {
$ct_data=get_option("cleantalk_data");
$checkjs = null;
$js_post_value = null;
if (!$data)
return $checkjs;
if (isset($data[$field_name])) {
$js_post_value = $data[$field_name];
if (isset($keys[$js_post_value])) {
//
// Random key check
//
if ($random_key) {
$keys = $ct_data['js_keys'];
$checkjs = 1;
} else {
$checkjs = 0;
}
} else {
$ct_challenge = ct_get_checkjs_value();
if(preg_match("/$ct_challenge/", $js_post_value)) {
$checkjs = 1;
} else {
$checkjs = 0;
}
}
}
return $checkjs;
}
/**
* Check messages for external plugins
* @return array with checking result;
*/
function ct_test_message($nickname, $email, $ip, $text){
$checkjs = js_test_plugin('ct_checkjs', $_COOKIE, true);
$post_info['comment_type'] = 'feedback_plugin_check';
$post_info = json_encode($post_info);
$ct_base_call_result = ct_base_call(array(
'message' => $text,
'example' => null,
'sender_email' => $email,
'sender_nickname' => $nickname,
'post_info' => $post_info,
'sender_info' => get_sender_info(),
'checkjs' => $checkjs
));
$ct_result = $ct_base_call_result['ct_result'];
$result=Array(
'allow' => $ct_result->allow,
'comment' => $ct_result->comment,
);
return $result;
}
?>