You are here

badbehavior.module in Bad Behavior 6

Same filename and directory in other branches
  1. 5.2 badbehavior.module
  2. 6.2 badbehavior.module
  3. 7.2 badbehavior.module

File

badbehavior.module
View source
<?php

define('BB2_CWD', './sites/all/libraries');

/**
 * Implements hook_help().
 */
function badbehavior_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/reports/badbehavior":
      $output .= '<p>' . t("The Bad Behavior module examines HTTP requests of visits to your web site, and any suspicious requests are logged for later review. The suspicious visit is shown an error page with instructions on how to view the site without triggering the bad behavior error message.") . '</p>';
      break;
  }
  return $output;
}

/**
 * Implements hook_perm().
 */
function badbehavior_perm() {
  return array(
    'administer bad behavior',
    'bypass bad behavior protection',
  );
}

/**
 * Implements hook_menu().
 */
function badbehavior_menu() {
  $items['admin/settings/badbehavior'] = array(
    'title' => 'Bad Behavior',
    'description' => 'Configure automatic spam blocking for your site.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'badbehavior_settings_form',
    ),
    'access arguments' => array(
      'administer bad behavior',
    ),
    'file' => 'badbehavior.admin.inc',
  );
  $items['admin/reports/badbehavior'] = array(
    'title' => 'Bad Behavior',
    'description' => 'Examine the spam blocking logs for your web site.',
    'page callback' => 'badbehavior_overview',
    'access arguments' => array(
      'administer bad behavior',
    ),
    'file' => 'badbehavior.admin.inc',
  );
  $items['admin/reports/badbehavior/event'] = array(
    'title' => 'Details',
    'page callback' => 'badbehavior_event',
    'access arguments' => array(
      'administer bad behavior',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'badbehavior.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_boot().
 */
function badbehavior_boot() {
  if (!$GLOBALS['user']->uid) {
    badbehavior_start_badbehavior();
  }
}

/**
 * Implements hook_init().
 */
function badbehavior_init() {
  if ($GLOBALS['user']->uid) {
    badbehavior_start_badbehavior();
  }
}
function badbehavior_start_badbehavior() {
  if (function_exists('user_access') && user_access('bypass bad behavior protection')) {
    return;
  }
  elseif (badbehavior_load_includes()) {
    bb2_install();
    bb2_start(bb2_read_settings());
  }
}

/**
 * Load BadBehavior library files.
 *
 * On first run, this will also include the required badbehavior.inc,
 * core.inc.php, and version.inc.php files.
 *
 * @param $files
 *   An array of BadBehavior files in the BB2_CWD/bad-behavior directory to
 *   include. The file type (.inc.php) will automatically be added to the file
 *   name.
 * @return
 *   TRUE if the files were included, or FALSE otherwise.
 */
function badbehavior_load_includes($files = array()) {
  static $included;
  if (!isset($included)) {
    $included = TRUE;
    array_unshift($files, 'core', 'version');
  }
  foreach ($files as $file) {
    $file = BB2_CWD . "/bad-behavior/{$file}.inc.php";
    if (is_file($file)) {
      require_once $file;
    }
    else {
      return FALSE;
    }
  }
  return TRUE;
}

// Return current time in the format preferred by your database.
function bb2_db_date() {
  return gmdate('Y-m-d H:i:s');

  // Example is MySQL format
}

// Return affected rows from most recent query.
function bb2_db_affected_rows() {
  return db_affected_rows();
}

// Escape a string for database usage
function bb2_db_escape($string) {
  return db_escape_string($string);
}

// Return the number of rows in a particular query.
function bb2_db_num_rows($result) {
  if ($result != FALSE) {
    return count($result);
  }
  return 0;
}
function badbehavior_db_errortrap($errno, $string) {
}

// Run a query and return the results, if any.
function bb2_db_query($query) {
  set_error_handler('badbehavior_db_errortrap');
  $result = db_query($query);
  restore_error_handler();
  if ($result == FALSE) {
    return FALSE;
  }
  return db_affected_rows();
}

// Return all rows in a particular query.
function bb2_db_rows($result) {
  return $result;
}

// Return emergency contact email address.
function bb2_email() {
  return variable_get('badbehavior_mail', variable_get('site_mail', ini_get('sendmail_from')));
}

// write settings to database
function bb2_write_settings($settings) {
  return;
}

// retrieve settings from database
function bb2_read_settings() {
  $logging = variable_get('badbehavior_logging', 1);
  return array(
    'log_table' => '{bad_behavior_log}',
    'display_stats' => FALSE,
    'strict' => variable_get('badbehavior_strict', 0),
    'verbose' => $logging == 'verbose',
    'logging' => !empty($logging),
    'httpbl_key' => variable_get('badbehavior_httpbl_key', ''),
    'httpbl_threat' => '25',
    'httpbl_maxage' => '30',
    'offsite_forms' => FALSE,
  );
}

// installation
function bb2_install() {
  if (variable_get('badbehavior_db_installed', 0) != BB2_VERSION) {
    bb2_db_query(bb2_table_structure('{bad_behavior_log}'));
    variable_set('badbehavior_db_installed', BB2_VERSION);
  }
}

// Return the top-level relative path of wherever we are (for cookies)
function bb2_relative_path() {
  global $base_path;
  return $base_path;
}
function bb2_convertdate($bbdate) {
  $timestamp = strtotime($bbdate . ' UTC');
  return format_date($timestamp, 'small');
}