You are here

function _cloudflare_threat_api in CloudFlare 7

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

Perform an action using Cloudflare's Threat API.

2 calls to _cloudflare_threat_api()
_cloudflare_ban_ip in ./cloudflare.module
_cloudflare_whitelist_ip in ./cloudflare.module

File

./cloudflare.module, line 364

Code

function _cloudflare_threat_api($action, $ip) {

  // Retrieve the settings.
  $cf_settings = _cloudflare_settings();
  $cf_api_email = $cf_settings['cf_api_email'];
  $cf_api_key = $cf_settings['cf_api_key'];
  $cf_ip_ranges = $cf_settings['cf_ip_ranges'];
  $my_ip = $cf_settings['my_ip'];

  // if the IP being banned is known to belong to Cloudflare, disallow it.
  foreach ($cf_ip_ranges as $cidr) {
    if (_cidr_match($ip, $cidr) && $action == "ban") {
      return "CF_CIDR";
    }
  }

  // if the IP being banned belongs to the person submitting this request, disallow it.
  if ($ip == $my_ip && $action == "ban") {
    return "MY_IP";
  }
  $url = "/api.html?a={$action}&key={$ip}&u={$cf_api_email}&tkn={$cf_api_key}";
  $opts = array(
    'http' => array(
      'method' => "GET",
      'header' => array(
        "Host: www.cloudflare.com",
        "Connection: Close",
      ),
    ),
  );
  $context = stream_context_create($opts);

  // Open the file using the HTTP headers set above
  $fc = check_plain(file_get_contents($cf_settings['cf_api_https_host'] . $url, false, $context));
  return $fc;
}