-
-
Save umeshdepale/0d1385c979642eefb448de2de1f705cb to your computer and use it in GitHub Desktop.
Block / unBlock IPs in firewall via Cloudflare V4 PHP API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @ author https://www.kevinpareek.com | |
* @ Monday, 23 December 2019 | |
**/ | |
class cloudflare_ip_ban{ | |
public function __construct($email, $apiKey, $by){ | |
$this->cfEmail = $email; | |
$this->apiKey = $apiKey; | |
$this->by = $by; | |
} | |
public function cloudflare_ipBan($ipaddr){ | |
$modulesData = $this->modulesData('cloudflare'); | |
$cfheaders = array( | |
'Content-Type: application/json', | |
'X-Auth-Email: '.$this->cfEmail.'', | |
'X-Auth-Key: '.$this->apiKey.'' | |
); | |
$data = array('mode' => 'block', 'configuration' => array('target' => 'ip', 'value' => $ipaddr), 'notes' => 'Banned on '.date('d-m-Y H:i:s').' by '.$this->by); | |
$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; | |
} | |
} | |
} | |
public function cloudflare_ipunBan($block_rule_id){ | |
$modulesData = $this->modulesData('cloudflare'); | |
$cfheaders = array( | |
'Content-Type: application/json', | |
'X-Auth-Email: '.$this->cfEmail.'', | |
'X-Auth-Key: '.$this->apiKey.'' | |
); | |
$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; | |
} | |
} | |
} | |
} | |
$email = ''; | |
$apiKey = ''; | |
$by = 'PHP Script'; | |
$ip = ''; | |
$cf = new cloudflare_ip_ban($email, $apiKey, $by); | |
$ipBan = $cf->cloudflare_ipBan($ip); | |
print_r($ipBan); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment