-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate.bots.inc
More file actions
178 lines (160 loc) · 4.63 KB
/
rate.bots.inc
File metadata and controls
178 lines (160 loc) · 4.63 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
<?php
/**
* Check if the given IP is a local IP-address.
*
* @param string $ip
* @return bool
*/
function rate_bots_is_local($ip) {
if (preg_match('/^([012]?[0-9]{2})\\./', $ip, $match)) {
switch ($match[1]) {
case 10:
case 127:
case 172:
case 192:
return TRUE;
}
}
return FALSE;
}
/**
* Check if the current user is blocked.
*
* This function will first check if the user is already known to be a bot.
* If not, it will check if we have valid reasons to assume the user is a bot.
*
* @return bool
*/
function rate_bots_is_blocked() {
$ip = ip_address();
$agent = $_SERVER['HTTP_USER_AGENT'];
if (rate_bots_is_local($ip)) {
// The IP-address is a local IP-address. This is probably because of
// misconfigured proxy servers. Do only the user agent check.
return rate_bots_check_agent($agent);
}
if (rate_bots_check_ip($ip)) {
return TRUE;
}
if (rate_bots_check_agent($agent)) {
// Identified as a bot by its user agent. Register this bot by IP-address
// as well, in case this bots uses multiple agent strings.
rate_bots_register_bot($ip);
return TRUE;
}
// TODO This variable was probably removed in Backdrop without replacement.
$threshold = config_get('rate.settings', RATE_VAR_BOT_MINUTE_THRESHOLD, 25);
if ($threshold && (rate_bots_check_threshold($ip, 60) > $threshold)) {
rate_bots_register_bot($ip);
return TRUE;
}
// TODO This variable was probably removed in Backdrop without replacement.
$threshold = config_get('rate.settings', RATE_VAR_BOT_HOUR_THRESHOLD, 250);
// Always count, even if threshold is disabled. This is to determine if we
// can skip the BotScout check.
$count = rate_bots_check_threshold($ip, 3600);
if ($threshold && ($count > $threshold)) {
rate_bots_register_bot($ip);
return TRUE;
}
if (!$count && rate_bots_check_botscout($ip)) {
rate_bots_register_bot($ip);
return TRUE;
}
return FALSE;
}
/**
* Register new bot.
*
* @param string $ip
*/
function rate_bots_register_bot($ip) {
db_insert('rate_bot_ip')->fields(array('ip' => $ip))->execute();
}
/**
* Check if the IP-address exists in the local bot database.
*
* @param string $ip
* @return bool
*/
function rate_bots_check_ip($ip) {
return (bool) db_select('rate_bot_ip', 'rbi')
->fields('rbi', array('id'))
->condition('rbi.ip', $ip)
->range(0, 1)
->execute()
->fetchField();
}
/**
* Check if the given user agent matches the local bot database.
*
* @param string $agent
* @return bool
*/
function rate_bots_check_agent($agent) {
$sql = 'SELECT 1 FROM {rate_bot_agent} WHERE :agent LIKE pattern LIMIT 1';
return (bool) db_query($sql, array(':agent' => $agent))->fetchField();
}
/**
* Check the number of votes between now and $interval seconds ago.
*
* @param string $ip
* @param int $interval
* @return int
*/
function rate_bots_check_threshold($ip, $interval) {
$sql = 'SELECT COUNT(*) FROM {votingapi_vote} WHERE vote_source = :ip AND timestamp > :time';
return db_query($sql, array(':ip' => $ip, ':time' => REQUEST_TIME - $interval))->fetchField();
}
/**
* Check if the IP is in the BotScout database.
*
* @param string $ip
* @return bool
*/
function rate_bots_check_botscout($ip) {
// TODO This variable was probably removed in Backdrop without replacement.
$key = config_get('rate.settings', RATE_VAR_BOT_BOTSCOUT_KEY, '');
if ($key) {
$url = "http://botscout.com/test/?ip=$ip&key=$key";
$data = backdrop_http_request($url, array('timeout' => 2));
if ($data->code == 200) {
if ($data->data[0] == 'Y') {
return TRUE;
}
}
}
return FALSE;
}
/**
* Delete votes from bots.
*/
function rate_bots_delete_votes() {
// TODO This variable was probably removed in Backdrop without replacement.
$limit = config_get('rate.settings', RATE_VAR_CRON_DELETE_LIMIT, 25);
$queue = BackdropQueue::get('rate_delete_votes');
while ($item = $queue->claimItem()) {
$ip = $item->data;
$votes = db_select('votingapi_vote', 'v')
->fields('v', array('vote_id', 'entity_type', 'entity_id'))
->condition('v.vote_source', $ip)
->range(0, $limit)
->execute()
->fetchAll();
foreach ($votes as $vote) {
db_delete('votingapi_vote')
->condition('vote_id', $vote->vote_id)
->execute();
votingapi_recalculate_results($vote->entity_type, $vote->entity_id, TRUE);
}
$queue->deleteItem($item);
if (count($votes) == $limit) {
// There may be more votes from this IP which needs to be deleted.
$queue->createItem($ip);
}
$limit -= count($votes) + 1;
if ($limit <= 0) {
break;
}
}
}