COM-40 :: Add ZoneLockdown and UA Rules

This commit is contained in:
Junade Ali
2017-09-19 16:04:35 +01:00
parent 7d0c0c86dc
commit 1e8c8d6019
10 changed files with 840 additions and 5 deletions

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 19/09/2017
* Time: 15:23
*/
namespace Cloudflare\API\Configurations;
interface Configurations
{
public function getArray(): array;
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 19/09/2017
* Time: 15:22
*/
namespace Cloudflare\API\Configurations;
class UARules implements Configurations
{
private $configs = array();
public function addUA(string $value)
{
$object = new \stdClass();
$object->target = "ua";
$object->value = $value;
array_push($this->configs, $object);
}
public function getArray(): array
{
return $this->configs;
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 05/09/2017
* Time: 13:43
*/
namespace Cloudflare\API\Configurations;
class ZoneLockdown implements Configurations
{
private $configs = array();
public function addIP(string $value)
{
$object = new \stdClass();
$object->target = "ip";
$object->value = $value;
array_push($this->configs, $object);
}
public function addIPRange(string $value)
{
$object = new \stdClass();
$object->target = "ip_range";
$object->value = $value;
array_push($this->configs, $object);
}
public function getArray(): array
{
return $this->configs;
}
}

124
src/Endpoints/UARules.php Normal file
View File

@@ -0,0 +1,124 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 19/09/2017
* Time: 15:17
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
class UARules implements API
{
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function listRules(
string $zoneID,
int $page = 1,
int $perPage = 20
): \stdClass {
$options = [
'page' => $page,
'per_page' => $perPage
];
$query = http_build_query($options);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules?' . $query, []);
$body = json_decode($user->getBody());
$result = new \stdClass();
$result->result = $body->result;
$result->result_info = $body->result_info;
return $result;
}
public function createRule(
string $zoneID,
string $mode,
\Cloudflare\API\Configurations\Configurations $configuration,
string $id = null,
string $description = null
): bool {
$options = [
'mode' => $mode,
'configurations' => $configuration->getArray()
];
if ($id !== null) {
$options['id'] = $id;
}
if ($description !== null) {
$options['description'] = $description;
}
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/ua_rules', [], $options);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
return true;
}
return false;
}
public function getRuleDetails(string $zoneID, string $blockID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules/' . $blockID, []);
$body = json_decode($user->getBody());
return $body->result;
}
public function updateRule(
string $zoneID,
string $ruleID,
string $mode,
\Cloudflare\API\Configurations\UARules $configuration,
string $description = null
): bool {
$options = [
'mode' => $mode,
'id' => $ruleID,
'configurations' => $configuration->getArray()
];
if ($description !== null) {
$options['description'] = $description;
}
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, [], $options);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
return true;
}
return false;
}
public function deleteRule(string $zoneID, string $ruleID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, [], []);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 04/09/2017
* Time: 20:33
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
class ZoneLockdown implements API
{
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function listLockdowns(
string $zoneID,
int $page = 1,
int $perPage = 20
): \stdClass {
$options = [
'page' => $page,
'per_page' => $perPage
];
$query = http_build_query($options);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns?' . $query, []);
$body = json_decode($user->getBody());
$result = new \stdClass();
$result->result = $body->result;
$result->result_info = $body->result_info;
return $result;
}
public function createLockdown(
string $zoneID,
array $urls,
\Cloudflare\API\Configurations\ZoneLockdown $configuration,
string $id = null,
string $description = null
): bool {
$options = [
'urls' => $urls,
'configurations' => $configuration->getArray()
];
if ($id !== null) {
$options['id'] = $id;
}
if ($description !== null) {
$options['description'] = $description;
}
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/lockdowns', [], $options);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
return true;
}
return false;
}
public function getLockdownDetails(string $zoneID, string $lockdownID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, []);
$body = json_decode($user->getBody());
return $body->result;
}
public function updateLockdown(
string $zoneID,
string $lockdownID,
array $urls,
\Cloudflare\API\Configurations\ZoneLockdown $configuration,
string $description = null
): bool {
$options = [
'urls' => $urls,
'id' => $lockdownID,
'configurations' => $configuration->getArray()
];
if ($description !== null) {
$options['description'] = $description;
}
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, [], $options);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
return true;
}
return false;
}
public function deleteLockdown(string $zoneID, string $lockdownID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, [], []);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
return true;
}
return false;
}
}