/[drupal]/contributions/modules/httpbl/httpbl.module
ViewVC logotype

Contents of /contributions/modules/httpbl/httpbl.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.22 - (show annotations) (download) (as text)
Thu Sep 17 21:36:26 2009 UTC (2 months, 1 week ago) by praseodym
Branch: MAIN
CVS Tags: HEAD
Changes since 1.21: +4 -4 lines
File MIME type: text/x-php
#576376 by bryrock: bug in link code in httpbl_boot()
1 <?php
2
3 // $Id: httpbl.module,v 1.21 2009/09/17 21:30:27 praseodym Exp $
4
5 /**
6 * @file
7 * Implementation of http:BL for Drupal. It provides IP-based
8 * blacklisting through http:BL and allows linking to a honeypot.
9 *
10 * @author Mark Janssen (praseodym)
11 * @link http://drupal.org/project/httpbl
12 * @link http://httpbl.org/
13 */
14
15 /**
16 *
17 * Version 6.x-dev
18 * Contact: praseodym (at) gmail (dot) com
19 *
20 * Feel free to improve this module, but please contact the author with any
21 * changes you make so they can be implemented into the 'official' version.
22 *
23 */
24
25 /**
26 * Cache levels.
27 */
28 define('HTTPBL_CACHE_OFF', 0); // Cache is off
29 define('HTTPBL_CACHE_DB', 1); // Only database cache
30 define('HTTPBL_CACHE_DBDRUPAL', 2); // Both database cache and Drupal access table
31
32 /**
33 * Blacklist levels.
34 */
35 define('HTTPBL_LIST_SAFE', 0); // Not listed (or very low threat)
36 define('HTTPBL_LIST_GREY', 2); // Greylisted: session whitelist request permitted
37 define('HTTPBL_LIST_BLACK', 1); // Blacklisted: all requests blocked.
38
39 /**
40 * Threshold levels.
41 */
42 define('HTTPBL_THRESHOLD_GREY', 1); // Threshold under which a user is allowed, and above which a user is greylisted
43 define('HTTPBL_THRESHOLD_BLACK', 50); // Threshold under which a user is greylisted, and above which a user is blacklisted
44
45 /**
46 * Check levels.
47 */
48 define('HTTPBL_CHECK_NONE', 0); // Check nowhere
49 define('HTTPBL_CHECK_COMMENTS', 1); // Check only comments
50 define('HTTPBL_CHECK_ALL', 2); // Check all requests
51
52
53 /**
54 * Implementation of hook_menu().
55 */
56 function httpbl_menu() {
57 $items['httpbl/whitelist'] = array(
58 'title' => 'Request whitelisting',
59 'page callback' => 'drupal_get_form',
60 'page arguments' => array('httpbl_request_whitelist'),
61 'access callback' => 'httpbl_whitelist_access',
62 'access arguments' => array(),
63 'type' => MENU_CALLBACK,
64 );
65 $items['admin/settings/httpbl'] = array(
66 'title' => 'http:BL',
67 'description' => 'Manage http:BL settings.',
68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('httpbl_admin_settings'),
70 'access arguments' => array('administer site configuration'),
71 );
72
73 return $items;
74 }
75
76 /**
77 * Implementation of hook_boot().
78 */
79 function httpbl_boot() {
80 // Only continue when checks are enabled
81 if (variable_get('httpbl_check', HTTPBL_CHECK_NONE) == HTTPBL_CHECK_NONE) {
82 return;
83 }
84
85 $result = httpbl_check();
86 if ($result) {
87 if ($result == HTTPBL_LIST_GREY) {
88 if ($_GET['q'] == 'httpbl/whitelist') {
89 return;
90 }
91 $message = 'Sorry, %ip has been greylisted by <a href="%ipurl">http:BL</a>.<br>You may try whitelisting on <a href="%whitelisturl">%whitelisturl</a>.%honeypot';
92 }
93 else if ($result == HTTPBL_LIST_BLACK) {
94 $message = 'Sorry, %ip has been blacklisted by <a href="%ipurl">http:BL</a>.%honeypot';
95 }
96
97 if ($link = variable_get('httpbl_link', NULL)) {
98 $word = variable_get('httpbl_word', 'randomness');
99 $link = httpbl_honeylink($link, $word);
100 }
101
102 $message = strtr($message, array('%ip' => ip_address(), '%ipurl' => _httpbl_ipdata(ip_address(), FALSE), '%honeypot' => $link, '%whitelisturl' => _httpbl_url('/httpbl/whitelist')));
103
104 header('HTTP/1.1 403 Forbidden');
105 echo $message;
106 exit();
107 }
108 }
109
110 /**
111 * Implementation of hook_comment().
112 */
113 function httpbl_comment($comment, $op) {
114 if (variable_get('httpbl_check', HTTPBL_CHECK_NONE) != HTTPBL_CHECK_COMMENTS) {
115 return;
116 }
117
118 switch ($op) {
119 case 'insert':
120 case 'update':
121 $comment = (object)$comment;
122
123 if (httpbl_check()) {
124 // Unpublish and inform the user.
125 $operation = comment_operations('unpublish');
126 $query = $operation['unpublish'][1];
127 db_query($query, $comment->cid);
128 drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
129
130 // Add to our statistics
131 if (variable_get('httpbl_stats', TRUE)) {
132 variable_set('httpbl_stat_comment', variable_get('httpbl_stat_comment', 0)+1);
133 }
134 }
135 return;
136 }
137 }
138
139
140 /**
141 * Check if an IP should be banned
142 *
143 * @return constant: HTTP_LIST_*
144 */
145
146 function httpbl_check() {
147 static $result;
148 // Result was already calculated -- return.
149 if (is_int($result)) {
150 return $result;
151 }
152
153 $ip = ip_address();
154
155 // Check if user is whitelisted in any way
156 if (_httpbl_whitelisted($ip)) {
157 $result = HTTPBL_LIST_SAFE;
158 }
159 // Cache is enabled - do a cache lookup
160 else if ($cache = variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL)) {
161 $result = _httpbl_cache_get($ip);
162 }
163
164 if (!(is_numeric($result))) {
165 // Do a DNS lookup, and continue if lookup was succesful
166 if ($response = httpbl_dnslookup($ip)) {
167 $stats = variable_get('httpbl_stats', TRUE);
168
169 // Blacklist?
170 if ($response['threat'] > HTTPBL_THRESHOLD_BLACK && $response['type']) {
171 if (variable_get('httpbl_log', FALSE)) {
172 watchdog('httpbl', '%ip was blacklisted (%response)', array('%ip' => $ip, '%response' => $response['raw']), WATCHDOG_WARNING, _httpbl_ipdata($ip));
173 }
174 if ($stats) {
175 variable_set('httpbl_stat_black', variable_get('httpbl_stat_black', 0)+1);
176 }
177 $result = HTTPBL_LIST_BLACK;
178 }
179 // Greylist?
180 else if ($response['threat'] > HTTPBL_THRESHOLD_GREY && $response['type']) {
181 if (variable_get('httpbl_log', FALSE)) {
182 watchdog('httpbl', '%ip was greylisted (%response)', array('%ip' => $ip, '%response' => $response['raw']), WATCHDOG_WARNING, _httpbl_ipdata($ip));
183 }
184 if ($stats) {
185 variable_set('httpbl_stat_grey', variable_get('httpbl_stat_grey', 0)+1);
186 }
187 $result = HTTPBL_LIST_GREY;
188 }
189 // Listed, but safe
190 else {
191 $result = HTTPBL_LIST_SAFE;
192 }
193
194 // Cache results
195 if ($cache) {
196 _httpbl_cache_set($ip, $result, 7200);
197 }
198 }
199 // No result, safe
200 else {
201 if ($cache) {
202 _httpbl_cache_set($ip, HTTPBL_LIST_SAFE, 10800);
203 }
204 $result = HTTPBL_LIST_SAFE;
205 }
206 }
207
208 return $result;
209 }
210
211 /**
212 * Implementation of hook_settings().
213 */
214 function httpbl_admin_settings() {
215 if (!$_POST && (!variable_get('httpbl_accesskey', NULL) || !variable_get('httpbl_check', HTTPBL_CHECK_NONE))) {
216 drupal_set_message(t('Blacklist lookups are currently disabled; enter your access key below and enable checks to enable blacklist lookups.'), 'error');
217 }
218
219 $form['core'] = array(
220 '#type' => 'fieldset',
221 '#title' => t('http:BL'),
222 '#description' => t('For more information about http:BL, see the <a href="http://www.projecthoneypot.org/httpbl.php">http:BL homepage</a>.'),
223 '#collapsible' => TRUE,
224 );
225
226 $form['core']['httpbl_accesskey'] = array(
227 '#type' => 'textfield',
228 '#title' => t('http:BL Access Key'),
229 '#default_value' => variable_get('httpbl_accesskey', NULL),
230 '#description' => t('Your http:BL <a href="http://www.projecthoneypot.org/httpbl_configure.php">Access Key</a>.'),
231 '#size' => 20,
232 '#maxlength' => 12,
233 );
234
235 $form['core']['httpbl_check'] = array(
236 '#type' => 'radios',
237 '#title' => t('Blacklist checks'),
238 '#default_value' => variable_get('httpbl_check', HTTPBL_CHECK_NONE),
239 '#options' => array(
240 t('Disabled'),
241 t('On comment submissions'),
242 t('On all requests'),
243 ),
244 '#description' => t('At what times the blacklist should be checked.'),
245 );
246
247 $form['honeypot'] = array(
248 '#type' => 'fieldset',
249 '#title' => t('Honeypot'),
250 '#description' => t('Your Honeypot (spam trap) settings. For more information, see the <a href="http://www.projecthoneypot.org/">Project Honey Pot homepage</a>.'),
251 '#collapsible' => TRUE,
252 );
253
254 $form['honeypot']['httpbl_footer'] = array(
255 '#type' => 'checkbox',
256 '#title' => t('Add link to footer'),
257 '#default_value' => variable_get('httpbl_footer', FALSE),
258 '#description' => t('Whether to add your honeypot link to the footer of every page.'),
259 );
260
261 $form['honeypot']['httpbl_link'] = array(
262 '#type' => 'textfield',
263 '#title' => t('Honeypot link'),
264 '#default_value' => variable_get('httpbl_link', NULL),
265 '#description' => t('Your Honeypot (spam trap) link. This can be one of your <a href="http://www.projecthoneypot.org/manage_honey_pots.php">own Honey Pots</a> or a <a href="http://www.projecthoneypot.org/manage_quicklink.php">QuickLink</a>.'),
266 );
267
268 $form['honeypot']['httpbl_word'] = array(
269 '#type' => 'textfield',
270 '#title' => t('Link word'),
271 '#default_value' => variable_get('httpbl_word', 'randomness'),
272 '#description' => t('A random word which will be used as a link.'),
273 );
274
275 $form['advanced'] = array(
276 '#type' => 'fieldset',
277 '#title' => t('Advanced'),
278 '#collapsible' => TRUE,
279 '#collapsed' => TRUE,
280 );
281
282 $form['advanced']['httpbl_log'] = array(
283 '#type' => 'checkbox',
284 '#title' => t('Verbose watchdog logging'),
285 '#default_value' => variable_get('httpbl_log', FALSE),
286 '#description' => t('Logs all positive lookups in the watchdog.'),
287 );
288
289 $form['advanced']['httpbl_stats'] = array(
290 '#type' => 'checkbox',
291 '#title' => t('Enable statistics'),
292 '#default_value' => variable_get('httpbl_stats', TRUE),
293 '#description' => t('Whether to enable counting of positive lookups. Statistics are show on the Drupal <a href="@statusreport">Status report page</a>.', array('@statusreport' => url('admin/reports/status'))),
294 );
295
296 $form['advanced']['httpbl_cache'] = array(
297 '#type' => 'radios',
298 '#title' => t('Caching'),
299 '#default_value' => variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL),
300 '#options' => array(
301 t('Off'),
302 t('Database cache'),
303 t('Database cache and Drupal access table'),
304 ),
305 '#description' => t('Whether to enable database-based caching. Note that when this is disabled, IPs that fail the session whitelist test cannot be banned. The Drupal access table allows visitors to be blocked in the early stages of the bootstrap.'),
306 );
307
308 return system_settings_form($form);
309 }
310
311 /**
312 * Form API callback to validate the httpbl settings form.
313 */
314 function httpbl_admin_settings_validate($form, &$form_state) {
315 $key = $form_state['values']['httpbl_accesskey'];
316
317 if ($form_state['values']['httpbl_check'] && !$key) {
318 form_set_error('httpbl_accesskey', t('You must enter an access key to enable blacklist checks.'));
319 }
320
321 if ($form_state['values']['httpbl_footer'] && !$form_state['values']['httpbl_link']) {
322 form_set_error('httpbl_link', t('You must enter a link to be able to add it to the footer.'));
323 }
324
325 if ($key) {
326 // Key should be 12 lowercase alpha characters.
327 // There's no unicode allowed, so we're not using drupal_strlen().
328 if (ereg('[^a-z]', $key) || strlen($key) != 12) {
329 form_set_error('httpbl_accesskey', t('Your access key is formatted incorrectly.'));
330 }
331 else if (!count(form_get_errors())) {
332 // Do a test lookup (with known result).
333 $lookup = httpbl_dnslookup('127.1.80.1', $key);
334 if (!$lookup || $lookup['threat'] != 80) {
335 form_set_error('httpbl_accesskey', t('Testcase failed. This either means that your access key is incorrect or that there is a problem in your DNS system.'));
336 }
337 else {
338 drupal_set_message('http:BL test completed successfully.');
339 }
340 }
341 }
342
343 }
344
345 /**
346 * Implementation of hook_footer().
347 *
348 * Adds a Project Honeypot link to the footer.
349 */
350 function httpbl_footer($main = 0) {
351 if (variable_get('httpbl_footer', FALSE)) {
352 $link = variable_get('httpbl_link', NULL);
353 $word = variable_get('httpbl_word', 'randomness');
354 return httpbl_honeylink($link, $word);
355 }
356 }
357
358 /**
359 * Implementation of hook_requirements().
360 *
361 * Shows some basic usage statistics for the module.
362 */
363 function httpbl_requirements($phase) {
364 $requirements = array();
365
366 if ($phase == 'runtime') {
367 if (!variable_get('httpbl_accesskey', NULL) || !variable_get('httpbl_check', HTTPBL_CHECK_NONE)) {
368 $requirements['httpbl'] = array(
369 'description' => t('IP blacklist lookups are currently disabled; enter your access key <a href="@settings">on the settings page</a> and enable checks to enable blacklist lookups.', array('@settings' => url('admin/settings/httpbl'))),
370 'severity' => REQUIREMENT_ERROR,
371 'value' => t('Disabled'),
372 );
373 if (variable_get('httpbl_footer', FALSE)) {
374 $requirements['httpbl']['severity'] = REQUIREMENT_WARNING;
375 }
376 }
377 else {
378 $stat_black = variable_get('httpbl_stat_black', 0);
379 $stat_comment = variable_get('httpbl_stat_comment', 0);
380 $stat_grey = variable_get('httpbl_stat_grey', 0);
381
382 if (!variable_get('httpbl_stats', TRUE)) {
383 $requirements['httpbl'] = array(
384 'description' => t('http:BL is enabled.'),
385 'severity' => REQUIREMENT_OK,
386 'value' => t('Enabled'),
387 );
388 }
389 else if (variable_get('httpbl_check', HTTPBL_CHECK_NONE) == HTTPBL_CHECK_COMMENTS) {
390 $requirements['httpbl'] = array(
391 'description' => t('http:BL is enabled and has blocked @c comments.', array('@c' => $stat_comment)),
392 'severity' => REQUIREMENT_OK,
393 'value' => t('Enabled'),
394 );
395 }
396 else if (variable_get('httpbl_check', HTTPBL_CHECK_NONE) == HTTPBL_CHECK_ALL) {
397 $requirements['httpbl'] = array(
398 'description' => t('http:BL is enabled and has blocked @t visits (@b blacklisted and @g greylisted).', array('@t' => $stat_grey+$stat_black, '@b' => $stat_black, '@g' => $stat_grey)),
399 'severity' => REQUIREMENT_OK,
400 'value' => t('Enabled'),
401 );
402 }
403 if (!variable_get('httpbl_footer', FALSE)) {
404 $requirements['httpbl']['severity'] = REQUIREMENT_WARNING;
405 $requirements['httpbl']['description'] .= ' '. t('However, the honeypot link is not enabled.');
406 }
407 }
408
409 $requirements['httpbl']['title'] = t('http:BL');
410 }
411
412 return $requirements;
413 }
414
415 /**
416 * Implementation of hook_cron().
417 *
418 * Cleans old results from the cache table and also Drupal access table if appropriate.
419 */
420 function httpbl_cron() {
421 // Only continue when caching is enabled
422 if (variable_get('httpbl_cache', HTTPBL_CACHE_OFF)) {
423 if (variable_get('httpbl_cache', HTTPBL_CACHE_OFF) == HTTPBL_CACHE_DBDRUPAL) {
424 // Also check status so that we do not accidentally delete the site's custom access rules.
425 db_query("DELETE FROM {access} WHERE mask IN (SELECT hostname FROM {httpbl} WHERE status > 0 AND expire <= %d)", time());
426 }
427 db_query("DELETE FROM {httpbl} WHERE expire <= %d", time());
428 }
429 }
430
431 /**
432 * Return HTML code with hidden Honeypot link
433 * in one of the many styles.
434 *
435 * @param string $link
436 * @param string $word
437 * @return string
438 */
439 function httpbl_honeylink($link, $word) {
440 if (!$link) {
441 return;
442 }
443 switch (mt_rand(0, 7)) {
444 case 0:
445 return '<div><a href="'. $link .'"><!-- '. $word .' --></a></div>';
446 case 1:
447 return '<div><a href="'. $link .'" style="display: none;">'. $word .'</a></div>';
448 case 2:
449 return '<div style="display: none;"><a href="'. $link .'">'. $word .'</a></div>';
450 case 3:
451 return '<div><a href="'. $link .'"></a></div>';
452 case 4:
453 return '<!-- <a href="'. $link .'">'. $word .'</a> -->';
454 case 5:
455 return '<div style="position: absolute; top: -250px; left: -250px;"><a href="'. $link .'">'. $word .'</a></div>';
456 case 6:
457 return '<div><a href="'. $link .'"><span style="display: none;">'. $word .'</span></a></div>';
458 case 7:
459 return '<div style="height: 0px; width: 0px;"><a href="'. $link .'">'. $word .'</a></div>';
460 }
461 }
462
463 /**
464 * Check if an IP is known to belong to a search engine
465 *
466 * @param string $ip
467 * @return bool
468 */
469
470 function httpbl_se($ip = NULL) {
471 if ($response = httpbl_dnslookup($ip)) {
472 $iplink = _httpbl_ipdata($ip);
473 return ($response['search_engine'] === TRUE);
474 }
475 else {
476 return FALSE;
477 }
478 }
479
480 /**
481 * Reverse IP octets
482 *
483 * @param string $ip
484 * @return string
485 */
486 function _httpbl_reverse_ip($ip) {
487 if (!is_numeric(str_replace('.', '', $ip))) {
488 return NULL;
489 }
490
491 $ip = explode('.', $ip);
492
493 if (count($ip) != 4) {
494 return NULL;
495 }
496
497 return $ip[3] .'.'. $ip[2] .'.'. $ip[1] .'.'. $ip[0];
498 }
499
500 /**
501 * Determine whether a user has access to the session whitelist functionality.
502 */
503 function httpbl_whitelist_access() {
504 return (httpbl_check() == HTTPBL_LIST_GREY);
505 }
506
507 /**
508 * Form to request a session whitelist.
509 */
510 function httpbl_request_whitelist() {
511 $form['#redirect'] = 'node';
512
513 $form['reason'] = array(
514 '#type' => 'textarea',
515 '#title' => t('Reason you were blocked'),
516 '#size' => 60,
517 '#required' => TRUE,
518 );
519
520 $form['block'] = array(
521 '#type' => 'textfield',
522 '#title' => t('Leave this blank'),
523 '#size' => 15,
524 );
525
526 $form['leave'] = array(
527 '#type' => 'textfield',
528 '#size' => 30,
529 '#attributes' => array('style' => 'display: none')
530 );
531
532 $form['submit'] = array(
533 '#type' => 'submit',
534 '#value' => t('Whitelist request'),
535 );
536
537 return $form;
538 }
539
540 /**
541 * Validate session whitelist request.
542 * Ban the user if one of the 'forbidden' fields were filled.
543 */
544 function httpbl_request_whitelist_validate($form, &$form_state) {
545 $ip = ip_address();
546 $iplink = _httpbl_ipdata($ip);
547
548 if ($form_state['values']['block'] || $form_state['values']['leave']) {
549 if (variable_get('httpbl_cache', HTTPBL_CACHE_OFF)) {
550 _httpbl_cache_update($ip, 1, 86400);
551 watchdog('httpbl', '%ip failed session whitelist request, blacklisted for 24 hours.', array('%ip' => $ip), WATCHDOG_WARNING, $iplink);
552 print t('Whitelist request failed; your IP has been blacklisted for 24 hours.');
553 exit();
554 }
555 else {
556 watchdog('httpbl', '%ip failed session whitelist request.', array('%ip' => $ip), WATCHDOG_WARNING, $iplink);
557 print t('Whitelist request failed.');
558 exit();
559 }
560 }
561
562 if (variable_get('httpbl_log', FALSE)) {
563 watchdog('httpbl', '%ip tried a whitelist request', array('%ip' => $ip), WATCHDOG_NOTICE, $iplink);
564 }
565 }
566
567 /**
568 * Grant the session whitelist.
569 */
570 function httpbl_request_whitelist_submit($form, &$form_state) {
571 $ip = ip_address();
572 $iplink = _httpbl_ipdata($ip);
573
574 watchdog('httpbl', 'Session from %ip whitelisted. Reason for block: @reason', array('%ip' => $ip, '@reason' => check_plain($form_state['values']['reason'])), WATCHDOG_WARNING, $iplink);
575 drupal_set_message(t('The current session has been whitelisted.'));
576 $_SESSION['httpbl_status'] = 'white';
577 drupal_goto('/');
578 }
579
580 /**
581 * Do http:BL DNS lookup
582 *
583 * @param string $ip
584 * @param string $key
585 * @return array
586 */
587
588 function httpbl_dnslookup($ip, $key = NULL) {
589 // Thanks to J.Wesley2 at
590 // http://www.projecthoneypot.org/board/read.php?f=10&i=1&t=1
591
592 if (!$ip = _httpbl_reverse_ip($ip)) {
593 return FALSE;
594 }
595
596 if (!$key && !$key = variable_get('httpbl_accesskey', NULL)) {
597 return FALSE;
598 }
599
600 $query = $key .'.'. $ip .'.dnsbl.httpbl.org.';
601 $response = gethostbyname($query);
602
603 if ($response == $query) {
604 // if the domain does not resolve then it will be the same thing we passed to gethostbyname
605 return FALSE;
606 }
607
608 $values = array();
609 $values['raw'] = $response;
610 $response = explode('.', $response);
611
612 if ($response[0] != '127') {
613 // if the first octet is not 127, the response should be considered invalid
614 watchdog('httpbl', 'Lookup failed for %ip, response was %response', array('%ip' => $ip, '%response' => $values['raw']), WATCHDOG_ERROR);
615 return FALSE;
616 }
617
618 $values['last_activity'] = $response[1];
619 $values['threat'] = $response[2];
620 $values['type'] = $response[3];
621 if ($response[3] == 0) {
622 //if it's 0 then there's only one thing it can be
623 $values['search_engine'] = TRUE;
624 }
625
626 if ($response[3] & 1) {
627 //does it have the same bits as 1 set
628 $values['suspicious'] = TRUE;
629 }
630
631 if ($response[3] & 2) {
632 //does it have the same bits as 2 set
633 $values['harvester'] = TRUE;
634 }
635
636 if ($response[3] & 4) {
637 //does it have the same bits as 4 set
638 $values['comment_spammer'] = TRUE;
639 }
640
641 return $values;
642 }
643
644 /**
645 * Check if an IP is whitelisted through the access table or a session whitelist.
646 */
647 function _httpbl_whitelisted($ip) {
648 return (isset($_SESSION['httpbl_status']) && $_SESSION['httpbl_status'] == 'white') || (db_result(db_query_range("SELECT status FROM {access} WHERE type = 'host' AND LOWER('%s') LIKE LOWER(mask) ORDER BY status DESC", $ip, 0, 1)) == '1');
649 }
650
651 /**
652 * Write status value into cache table
653 */
654 function _httpbl_cache_set($ip, $status, $offset = 0) {
655 db_query("DELETE FROM {httpbl} WHERE hostname = '%s'", $ip);
656 db_query("INSERT INTO {httpbl} (hostname, status, expire) VALUES ('%s', %d, %d)", $ip, $status, time() + $offset);
657
658 if ($status == HTTPBL_LIST_BLACK && variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL)) {
659 db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $ip, 'host', 0);
660 }
661 }
662
663 /**
664 * Update cache table
665 */
666 function _httpbl_cache_update($ip, $status, $offset = 0) {
667 db_query("UPDATE {httpbl} SET status = %d, expire = %d WHERE hostname = '%s'", $status, time() + $offset, $ip);
668
669 // Note that IP addresses will not be deleted from the {access} table, but this is currently not needed.
670 // TODO: check for duplicates
671 //if ($status == HTTPBL_BLACKLIST && variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL)) {
672 // db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $ip, 'host', 0);
673 //}
674 }
675
676 /**
677 * Get status value from cache table
678 */
679 function _httpbl_cache_get($ip) {
680 return db_result(db_query("SELECT status FROM {httpbl} WHERE hostname = '%s'", $ip));
681 }
682
683 /**
684 * Generate a link with Project Honeypot information for a given IP address.
685 */
686 function _httpbl_ipdata($ip, $anchor = TRUE) {
687 if ($anchor) {
688 return '<a href="http://www.projecthoneypot.org/search_ip.php?ip='. $ip .'">IP data</a>';
689 }
690 else {
691 return 'http://www.projecthoneypot.org/search_ip.php?ip='. $ip;
692 }
693 }
694
695 /**
696 * Lightweight url function, since url() isn't available yet.
697 */
698 function _httpbl_url($path = NULL) {
699 global $base_url;
700 $script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE) ? 'index.php' : '';
701
702 if (variable_get('clean_url', '0')) {
703 return $base_url . $path;
704 }
705 else {
706 return $base_url . $script .'?q='. $path;
707 }
708 }

  ViewVC Help
Powered by ViewVC 1.1.2