Skip to content

Instantly share code, notes, and snippets.

@cybersholt
Forked from andrieslouw/cfban.php
Created May 31, 2017 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybersholt/e1f0fd70ba7ca53daaad9ce49f0945a9 to your computer and use it in GitHub Desktop.
Save cybersholt/e1f0fd70ba7ca53daaad9ce49f0945a9 to your computer and use it in GitHub Desktop.
Cloudflare V4 PHP API - block / unblock IP in firewall
<?php
/**
* @author https://github.com/andrieslouw
* @copyright 2016
**/
function cfban($ipaddr){
$cfheaders = array(
'Content-Type: application/json',
'X-Auth-Email: your@email.com',
'X-Auth-Key: yourauthkeyhere'
);
$data = array(
'mode' => 'block',
'configuration' => array('target' => 'ip', 'value' => $ipaddr),
'notes' => 'Banned on '.date('Y-m-d H:i:s').' by PHP-script'
);
$json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $cfheaders);
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules');
$return = curl_exec($ch);
curl_close($ch);
if ($return === false){
return false;
}else{
$return = json_decode($return,true);
if(isset($return['success']) && $return['success'] == true){
return $return['result']['id'];
}else{
return false;
}
}
}
function cfunban($block_rule_id){
$cfheaders = array(
'Content-Type: application/json',
'X-Auth-Email: your@email.com',
'X-Auth-Key: yourauthkeyhere'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $cfheaders);
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules/'.$block_rule_id);
$return = curl_exec($ch);
curl_close($ch);
if ($return === false){
return false;
}else{
$return = json_decode($return,true);
if(isset($return['success']) && $return['success'] == true){
return $return['result']['id'];
}else{
return false;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment