You are here

function _cloudflare_ban_ip in CloudFlare 7

Same name and namespace in other branches
  1. 6 cloudflare.module \_cloudflare_ban_ip()

File

./cloudflare.module, line 234

Code

function _cloudflare_ban_ip($ip) {
  $result = _cloudflare_threat_api('ban', $ip);

  // Get the first line only
  list($status_code) = explode("\n", $result);
  if ($status_code == "OK") {
    drupal_set_message(t("You have successfully added %ip to your Cloudflare block list.", array(
      '%ip' => $ip,
    )), 'status', FALSE);

    // record a message noting the action taken
    watchdog('cloudflare', t('You have successfully added %ip to your Cloudflare block list.'), array(
      '%ip' => $ip,
    ));
  }
  else {
    switch ($status_code) {
      case "E_UNAUTH":
        $message_user = "Cloudflare response: Authorization could not be completed.";
        $message_watchdog = t("Cloudflare response: Authorization could not be completed.");
        break;
      case "E_INVLDIP":
        $message_user = "Cloudflare response: Malformed IPv4 address passed in. (IP: %ip)";
        $message_watchdog = t("Cloudflare response: Malformed IPv4 address passed in. (IP: %ip)");
        break;
      case "E_INVLDINPUT":
        $message_user = "Cloudflare response: Some other input was not valid.";
        $message_watchdog = t("Cloudflare response: Some other input was not valid.");
        break;
      case "E_MAXAPI":
        $message_user = "Cloudflare response: You have exceeded your allowed number of API calls.";
        $message_watchdog = t("Cloudflare response: You have exceeded your allowed number of API calls.");
        break;
      case "CF_CIDR":
        $message_user = "Sorry, %ip belongs to Cloudflare and cannot be banned.";
        $message_watchdog = t("Sorry, %ip belongs to Cloudflare and cannot be banned.");
        break;
      case "MY_IP":
        $message_user = "You dork.  %ip belongs to you!";
        $message_watchdog = t("You dork.  %ip belongs to you!");
        break;
    }
    drupal_set_message(t($message_user, array(
      '%ip' => $ip,
    )), 'warning', FALSE);

    // record a message noting the action taken
    watchdog('cloudflare', $message_watchdog, array(
      '%ip' => $ip,
    ));
  }
  return $status_code;
}