Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35ce8eadf1 | ||
|
|
d95573976e | ||
|
|
3bfd5e17f1 | ||
|
|
19a4f481d3 | ||
|
|
9c56941516 | ||
|
|
e18a64e7e4 | ||
|
|
3585dde8ea | ||
|
|
730b0c97ee | ||
|
|
dc53414c2a | ||
|
|
a628cac283 | ||
|
|
0112425a61 | ||
|
|
fe854e1b21 | ||
|
|
45cb8ee539 | ||
|
|
cd2fd73cc3 | ||
|
|
bd83c6d48a | ||
|
|
4e3d307bdd |
@@ -18,7 +18,7 @@ Each API call is provided via a similarly named function within various classes
|
||||
- [x] [Page Rules](https://support.cloudflare.com/hc/en-us/articles/200168306-Is-there-a-tutorial-for-Page-Rules-)
|
||||
- [x] [Web Application Firewall (WAF)](https://www.cloudflare.com/waf/)
|
||||
- [ ] Virtual DNS Management
|
||||
- [ ] Custom hostnames
|
||||
- [x] Custom hostnames
|
||||
- [x] Zone Lockdown and User-Agent Block rules
|
||||
- [ ] Organization Administration
|
||||
- [x] [Railgun](https://www.cloudflare.com/railgun/) administration
|
||||
|
||||
@@ -132,9 +132,11 @@ class PageRulesActions implements Configurations
|
||||
throw new ConfigurationsException('Status Codes can only be 301 or 302.');
|
||||
}
|
||||
|
||||
$this->addConfigurationOption('forwarding_url', [
|
||||
'status_code' => $statusCode,
|
||||
'url' => $forwardingUrl,
|
||||
$this->addConfigurationOption("forwarding_url", [
|
||||
'value' => [
|
||||
'status_code' => $statusCode,
|
||||
'url' => $forwardingUrl,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -299,9 +301,19 @@ class PageRulesActions implements Configurations
|
||||
|
||||
private function addConfigurationOption(string $setting, array $configuration)
|
||||
{
|
||||
/**
|
||||
* Transforms an, optionally nested, array in to a collection of
|
||||
* stdClass objects.
|
||||
*
|
||||
* @var array $array
|
||||
*/
|
||||
$getArrayAsObject = function (array $array) {
|
||||
return json_decode(json_encode($array));
|
||||
};
|
||||
|
||||
$configuration['id'] = $setting;
|
||||
|
||||
$this->configs[] = (object) $configuration;
|
||||
array_push($this->configs, $getArrayAsObject($configuration));
|
||||
}
|
||||
|
||||
private function getBoolAsOnOrOff(bool $value): string
|
||||
|
||||
150
src/Endpoints/CustomHostnames.php
Normal file
150
src/Endpoints/CustomHostnames.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: junade
|
||||
* Date: 18/03/2018
|
||||
* Time: 21:46
|
||||
*/
|
||||
|
||||
namespace Cloudflare\API\Endpoints;
|
||||
|
||||
use Cloudflare\API\Adapter\Adapter;
|
||||
|
||||
class CustomHostnames implements API
|
||||
{
|
||||
private $adapter;
|
||||
|
||||
public function __construct(Adapter $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
*
|
||||
* @param string $zoneID
|
||||
* @param string $hostname
|
||||
* @param string $sslMethod
|
||||
* @param string $sslType
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv'): \stdClass
|
||||
{
|
||||
$options = [
|
||||
'hostname' => $hostname,
|
||||
'ssl' => (object)[
|
||||
'method' => $sslMethod,
|
||||
'type' => $sslType
|
||||
]
|
||||
];
|
||||
|
||||
$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', [], $options);
|
||||
$body = json_decode($zone->getBody());
|
||||
return $body->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $zoneID
|
||||
* @param string $hostname
|
||||
* @param string $id
|
||||
* @param int $page
|
||||
* @param int $perPage
|
||||
* @param string $order
|
||||
* @param string $direction
|
||||
* @param int $ssl
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function listHostnames(
|
||||
string $zoneID,
|
||||
string $hostname = '',
|
||||
string $hostnameID = '',
|
||||
int $page = 1,
|
||||
int $perPage = 20,
|
||||
string $order = '',
|
||||
string $direction = '',
|
||||
int $ssl = 0
|
||||
): \stdClass {
|
||||
$query = [
|
||||
'page' => $page,
|
||||
'per_page' => $perPage,
|
||||
'ssl' => $ssl
|
||||
];
|
||||
|
||||
if (!empty($hostname)) {
|
||||
$query['hostname'] = $hostname;
|
||||
}
|
||||
|
||||
if (!empty($hostnameID)) {
|
||||
$query['id'] = $hostnameID;
|
||||
}
|
||||
|
||||
if (!empty($order)) {
|
||||
$query['order'] = $order;
|
||||
}
|
||||
|
||||
if (!empty($direction)) {
|
||||
$query['direction'] = $direction;
|
||||
}
|
||||
|
||||
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query, []);
|
||||
$body = json_decode($zone->getBody());
|
||||
|
||||
return (object)['result' => $body->result, 'result_info' => $body->result_info];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $zoneID
|
||||
* @param string $hostnameID
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHostname(string $zoneID, string $hostnameID)
|
||||
{
|
||||
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
|
||||
$body = json_decode($zone->getBody());
|
||||
|
||||
return $body->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
*
|
||||
* @param string $zoneID
|
||||
* @param string $hostnameID
|
||||
* @param string $sslMethod
|
||||
* @param string $sslType
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = ''): \stdClass
|
||||
{
|
||||
$query = [];
|
||||
|
||||
if (!empty($sslMethod)) {
|
||||
$query['method'] = $sslMethod;
|
||||
}
|
||||
|
||||
if (!empty($sslType)) {
|
||||
$query['type'] = $sslType;
|
||||
}
|
||||
|
||||
$options = [
|
||||
'ssl' => (object)$query
|
||||
];
|
||||
|
||||
$zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], $options);
|
||||
$body = json_decode($zone->getBody());
|
||||
return $body->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $zoneID
|
||||
* @param string $hostnameID
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function deleteHostname(string $zoneID, string $hostnameID): \stdClass
|
||||
{
|
||||
$zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
|
||||
$body = json_decode($zone->getBody());
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ class DNS implements API
|
||||
* @param string $content
|
||||
* @param int $ttl
|
||||
* @param bool $proxied
|
||||
* @param string $priority
|
||||
* @return bool
|
||||
*/
|
||||
public function addRecord(
|
||||
@@ -36,7 +37,8 @@ class DNS implements API
|
||||
string $name,
|
||||
string $content,
|
||||
int $ttl = 0,
|
||||
bool $proxied = true
|
||||
bool $proxied = true,
|
||||
string $priority = ''
|
||||
): bool {
|
||||
$options = [
|
||||
'type' => $type,
|
||||
@@ -49,6 +51,10 @@ class DNS implements API
|
||||
$options['ttl'] = $ttl;
|
||||
}
|
||||
|
||||
if (!empty($priority)) {
|
||||
$options['priority'] = $priority;
|
||||
}
|
||||
|
||||
$user = $this->adapter->post('zones/' . $zoneID . '/dns_records', [], $options);
|
||||
|
||||
$body = json_decode($user->getBody());
|
||||
|
||||
@@ -23,15 +23,15 @@ class Zones implements API
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
*
|
||||
* @param string $name
|
||||
* @param bool $jumpstart
|
||||
* @param bool $jumpStart
|
||||
* @param string $organizationID
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function addZone(string $name, bool $jumpstart = false, string $organizationID = ''): \stdClass
|
||||
public function addZone(string $name, bool $jumpStart = false, string $organizationID = ''): \stdClass
|
||||
{
|
||||
$options = [
|
||||
'name' => $name,
|
||||
'jumpstart' => $jumpstart
|
||||
'jump_start' => $jumpStart
|
||||
];
|
||||
|
||||
if (!empty($organizationID)) {
|
||||
@@ -114,7 +114,7 @@ class Zones implements API
|
||||
*/
|
||||
public function getAnalyticsDashboard(string $zoneID, string $since = '-10080', string $until = '0', bool $continuous = true): \stdClass
|
||||
{
|
||||
$response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', [], ['since' => $since, 'until' => $until, 'continuous' => $continuous]);
|
||||
$response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', ['since' => $since, 'until' => $until, 'continuous' => var_export($continuous, true)], []);
|
||||
|
||||
return json_decode($response->getBody())->result;
|
||||
}
|
||||
|
||||
21
tests/Configurations/PageRulesActionTest.php
Normal file
21
tests/Configurations/PageRulesActionTest.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
use Cloudflare\API\Configurations\PageRulesActions;
|
||||
|
||||
class PageRulesActionTest extends TestCase
|
||||
{
|
||||
public function testForwardingURLConfigurationIsApplied()
|
||||
{
|
||||
$identifier = 'forwarding_url';
|
||||
$statusCode = 301;
|
||||
$forwardingURL = 'https://www.example.org/';
|
||||
|
||||
$actions = new PageRulesActions();
|
||||
$actions->setForwardingURL($statusCode, $forwardingURL);
|
||||
$configuration = $actions->getArray();
|
||||
|
||||
$this->assertCount(1, $configuration);
|
||||
$this->assertEquals($identifier, $configuration[0]->id);
|
||||
$this->assertEquals($statusCode, $configuration[0]->value->status_code);
|
||||
$this->assertEquals($forwardingURL, $configuration[0]->value->url);
|
||||
}
|
||||
}
|
||||
140
tests/Endpoints/CustomHostnamesTest.php
Normal file
140
tests/Endpoints/CustomHostnamesTest.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: junade
|
||||
* Date: 18/03/2018
|
||||
* Time: 22:23
|
||||
*/
|
||||
|
||||
use Cloudflare\API\Endpoints\CustomHostnames;
|
||||
|
||||
class CustomHostnamesTest extends TestCase
|
||||
{
|
||||
public function testAddHostname()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('post')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('post')
|
||||
->with(
|
||||
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([
|
||||
'hostname' => 'app.example.com',
|
||||
'ssl' => (object)[
|
||||
'method' => 'http',
|
||||
'type' => 'dv'
|
||||
]
|
||||
])
|
||||
);
|
||||
|
||||
$hostname = new CustomHostnames($mock);
|
||||
$hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv');
|
||||
}
|
||||
|
||||
public function testListHostnames()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listHostnames.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('get')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
|
||||
$this->equalTo([
|
||||
'hostname' => 'app.example.com',
|
||||
'id' => '0d89c70d-ad9f-4843-b99f-6cc0252067e9',
|
||||
'page' => 1,
|
||||
'per_page' => 20,
|
||||
'order' => 'ssl',
|
||||
'direction' => 'desc',
|
||||
'ssl' => 0
|
||||
]),
|
||||
$this->equalTo([])
|
||||
);
|
||||
|
||||
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
|
||||
$result = $zones->listHostnames('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 1, 20, 'ssl', 'desc', 0);
|
||||
|
||||
$this->assertObjectHasAttribute('result', $result);
|
||||
$this->assertObjectHasAttribute('result_info', $result);
|
||||
|
||||
$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id);
|
||||
$this->assertEquals(1, $result->result_info->page);
|
||||
}
|
||||
|
||||
public function testGetHostname()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getHostname.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('get')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([])
|
||||
);
|
||||
|
||||
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
|
||||
$result = $zones->getHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');
|
||||
|
||||
$this->assertObjectHasAttribute('id', $result);
|
||||
$this->assertObjectHasAttribute('hostname', $result);
|
||||
}
|
||||
|
||||
public function testUpdateHostname()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('patch')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('patch')
|
||||
->with(
|
||||
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([
|
||||
'ssl' => (object)[
|
||||
'method' => 'http',
|
||||
'type' => 'dv'
|
||||
]
|
||||
])
|
||||
);
|
||||
|
||||
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
|
||||
$result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv');
|
||||
|
||||
$this->assertObjectHasAttribute('id', $result);
|
||||
$this->assertObjectHasAttribute('hostname', $result);
|
||||
}
|
||||
|
||||
public function testDeleteHostname()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteHostname.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('delete')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('delete')
|
||||
->with(
|
||||
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([])
|
||||
);
|
||||
|
||||
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
|
||||
$result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');
|
||||
|
||||
$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class ZonesTest extends TestCase
|
||||
->with(
|
||||
$this->equalTo('zones'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo(['name' => 'example.com', 'jumpstart' => false])
|
||||
$this->equalTo(['name' => 'example.com', 'jump_start' => false])
|
||||
);
|
||||
|
||||
$zones = new \Cloudflare\API\Endpoints\Zones($mock);
|
||||
@@ -41,7 +41,7 @@ class ZonesTest extends TestCase
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([
|
||||
'name' => 'example.com',
|
||||
'jumpstart' => true,
|
||||
'jump_start' => true,
|
||||
'organization' => (object)['id' => '01a7362d577a6c3019a474fd6f485823']
|
||||
])
|
||||
);
|
||||
@@ -141,8 +141,8 @@ class ZonesTest extends TestCase
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/analytics/dashboard'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo(['since' => '-10080', 'until' => '0', 'continuous' => true])
|
||||
$this->equalTo(['since' => '-10080', 'until' => '0', 'continuous' => var_export(true, true)]),
|
||||
$this->equalTo([])
|
||||
);
|
||||
|
||||
$zones = new \Cloudflare\API\Endpoints\Zones($mock);
|
||||
|
||||
20
tests/Fixtures/Endpoints/createCustomHostname.json
Normal file
20
tests/Fixtures/Endpoints/createCustomHostname.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{}
|
||||
],
|
||||
"messages": [
|
||||
{}
|
||||
],
|
||||
"result": {
|
||||
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
|
||||
"hostname": "app.example.com",
|
||||
"ssl": {
|
||||
"status": "pending_validation",
|
||||
"method": "http",
|
||||
"type": "dv",
|
||||
"cname_target": "dcv.digicert.com",
|
||||
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
|
||||
}
|
||||
}
|
||||
}
|
||||
3
tests/Fixtures/Endpoints/deleteHostname.json
Normal file
3
tests/Fixtures/Endpoints/deleteHostname.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9"
|
||||
}
|
||||
20
tests/Fixtures/Endpoints/getHostname.json
Normal file
20
tests/Fixtures/Endpoints/getHostname.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{}
|
||||
],
|
||||
"messages": [
|
||||
{}
|
||||
],
|
||||
"result": {
|
||||
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
|
||||
"hostname": "app.example.com",
|
||||
"ssl": {
|
||||
"status": "pending_validation",
|
||||
"method": "http",
|
||||
"type": "dv",
|
||||
"cname_target": "dcv.digicert.com",
|
||||
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
|
||||
}
|
||||
}
|
||||
}
|
||||
28
tests/Fixtures/Endpoints/listHostnames.json
Normal file
28
tests/Fixtures/Endpoints/listHostnames.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{}
|
||||
],
|
||||
"messages": [
|
||||
{}
|
||||
],
|
||||
"result": [
|
||||
{
|
||||
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
|
||||
"hostname": "app.example.com",
|
||||
"ssl": {
|
||||
"status": "pending_validation",
|
||||
"method": "http",
|
||||
"type": "dv",
|
||||
"cname_target": "dcv.digicert.com",
|
||||
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
|
||||
}
|
||||
}
|
||||
],
|
||||
"result_info": {
|
||||
"page": 1,
|
||||
"per_page": 20,
|
||||
"count": 1,
|
||||
"total_count": 2000
|
||||
}
|
||||
}
|
||||
20
tests/Fixtures/Endpoints/updateHostname.json
Normal file
20
tests/Fixtures/Endpoints/updateHostname.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{}
|
||||
],
|
||||
"messages": [
|
||||
{}
|
||||
],
|
||||
"result": {
|
||||
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
|
||||
"hostname": "app.example.com",
|
||||
"ssl": {
|
||||
"status": "pending_validation",
|
||||
"method": "http",
|
||||
"type": "dv",
|
||||
"cname_target": "dcv.digicert.com",
|
||||
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user