From f62e025e5dfc47d9ed48c229cdfd89e35d3c7e05 Mon Sep 17 00:00:00 2001 From: John Wineman Date: Tue, 19 Jul 2016 15:04:26 +0100 Subject: [PATCH 1/3] PI-552: Partial Zone Set Enable/Disable throws error if cPanel Advanced Zone Editor is disabled. --- src/Cpanel/ClientActions.php | 4 ++++ src/Cpanel/CpanelAPI.php | 8 ++++++++ src/Cpanel/HostActions.php | 3 +++ src/Cpanel/Zone/Partial.php | 1 + 4 files changed, 16 insertions(+) diff --git a/src/Cpanel/ClientActions.php b/src/Cpanel/ClientActions.php index 50e9f97b..d3213bb5 100644 --- a/src/Cpanel/ClientActions.php +++ b/src/Cpanel/ClientActions.php @@ -143,6 +143,10 @@ public function patchDNSRecord() */ public function deleteZone() { + if(!$this->cpanelAPI->isAdvancedZoneEditEnabled()) { + return $this->api->createAPIError(Partial::ADVANCED_ZONE_EDIT_DISABLED_ERROR); + } + $path_array = explode('/', $this->request->getUrl()); $zone_tag = $path_array[1]; diff --git a/src/Cpanel/CpanelAPI.php b/src/Cpanel/CpanelAPI.php index 25f9fbd5..20cfe7bf 100644 --- a/src/Cpanel/CpanelAPI.php +++ b/src/Cpanel/CpanelAPI.php @@ -286,4 +286,12 @@ public function get_cpanel_dns_record_name($cloudflare_dns_record_name) //cpanel uses the trailing dot for all record names return $cloudflare_dns_record_name . "."; } + + public function isAdvancedZoneEditEnabled() { + /* Advanced Zone Editor is required to edit DNS records for partial zone provisioning. + * https://documentation.cpanel.net/display/SDK/Guide+to+the+LiveAPI+System+-+The+cpanelfeature%28%29+Method + * Returns 1 = enabled, 0 = disabled + */ + return $this->cpanel_api->cpanelfeature("zoneedit"); + } } diff --git a/src/Cpanel/HostActions.php b/src/Cpanel/HostActions.php index 9bca2912..68ac59ed 100644 --- a/src/Cpanel/HostActions.php +++ b/src/Cpanel/HostActions.php @@ -46,6 +46,9 @@ public function setPartialZoneSet(Partial $partialZoneSet) { */ public function partialZoneSet() { + if(!$this->cpanelAPI->isAdvancedZoneEditEnabled()) { + return $this->api->createAPIError(Partial::ADVANCED_ZONE_EDIT_DISABLED_ERROR); + } $bodyParameters = $this->request->getBody(); if (isset($bodyParameters["zone_name"])) { if ($this->partialZoneSet->partialZoneSet("www." . $bodyParameters["zone_name"] . ".", $bodyParameters["zone_name"])) { diff --git a/src/Cpanel/Zone/Partial.php b/src/Cpanel/Zone/Partial.php index 140c3834..f9c9b152 100644 --- a/src/Cpanel/Zone/Partial.php +++ b/src/Cpanel/Zone/Partial.php @@ -16,6 +16,7 @@ class Partial const FORWARD_TO_SUFFIX = "cdn.cloudflare.net"; const RESOLVE_TO_PREFIX = "cloudflare-resolve-to."; + const ADVANCED_ZONE_EDIT_DISABLED_ERROR = "CloudFlare cPanel Plugin configuration issue! Please contact your hosting provider to enable \"Advanced DNS Zone Editor\""; /** * @param CpanelAPI $cpanel_api From aca963ad93fc5edab7858dd80a96527adeb7c708 Mon Sep 17 00:00:00 2001 From: John Wineman Date: Tue, 19 Jul 2016 15:21:29 +0100 Subject: [PATCH 2/3] PI-552: Tests for CF\Cpanel\CpanelAPI->isAdvancedZoneEditEnabled(). --- src/Cpanel/CpanelAPI.php | 2 +- src/Test/Cpanel/CpanelAPITest.php | 36 +++++++++++++++++++++++++++++++ src/Test/Cpanel/DataStoreTest.php | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/Test/Cpanel/CpanelAPITest.php diff --git a/src/Cpanel/CpanelAPI.php b/src/Cpanel/CpanelAPI.php index 20cfe7bf..748ea56c 100644 --- a/src/Cpanel/CpanelAPI.php +++ b/src/Cpanel/CpanelAPI.php @@ -292,6 +292,6 @@ public function isAdvancedZoneEditEnabled() { * https://documentation.cpanel.net/display/SDK/Guide+to+the+LiveAPI+System+-+The+cpanelfeature%28%29+Method * Returns 1 = enabled, 0 = disabled */ - return $this->cpanel_api->cpanelfeature("zoneedit"); + return ($this->cpanel_api->cpanelfeature("zoneedit") === 1); } } diff --git a/src/Test/Cpanel/CpanelAPITest.php b/src/Test/Cpanel/CpanelAPITest.php new file mode 100644 index 00000000..a5d86dc1 --- /dev/null +++ b/src/Test/Cpanel/CpanelAPITest.php @@ -0,0 +1,36 @@ +mockCpanelLiveAPI = $this->getMockBuilder('CF\Cpanel\Test\MockCpanelLiveAPI') + ->disableOriginalConstructor() + ->getMock(); + $this->mockLogger = $this->getMockBuilder('CF\Integration\DefaultLogger') + ->disableOriginalConstructor() + ->getMock(); + $this->cpanelAPI = new CpanelAPI($this->mockCpanelLiveAPI, $this->mockLogger); + } + + public function testIsAdvancedZoneEditEnabledReturnsTrueIfEnabled() { + $this->mockCpanelLiveAPI->method('cpanelfeature')->with("zoneedit")->willReturn(1); + $this->assertTrue($this->cpanelAPI->isAdvancedZoneEditEnabled()); + } + + public function testIsAdvancedZoneEditEnabledReturnsFalseIfDisabled() { + $this->mockCpanelLiveAPI->method('cpanelfeature')->with("zoneedit")->willReturn(0); + $this->assertFalse($this->cpanelAPI->isAdvancedZoneEditEnabled()); + } +} \ No newline at end of file diff --git a/src/Test/Cpanel/DataStoreTest.php b/src/Test/Cpanel/DataStoreTest.php index 670219cb..1a91b42e 100644 --- a/src/Test/Cpanel/DataStoreTest.php +++ b/src/Test/Cpanel/DataStoreTest.php @@ -5,7 +5,7 @@ use CF\Cpanel\DataStore; -class PartialTest extends \PHPUnit_Framework_TestCase +class DataStoreTest extends \PHPUnit_Framework_TestCase { private $mockCpanelAPI; private $mockLogger; From c7bd24ca039732702b322c4dbbb470c8744fa061 Mon Sep 17 00:00:00 2001 From: John Wineman Date: Tue, 19 Jul 2016 16:14:18 +0100 Subject: [PATCH 3/3] PI-552: Updating CF\Cpanel\CpanelAPI->isAdvancedZoneEditEnabled() docs --- src/Cpanel/CpanelAPI.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Cpanel/CpanelAPI.php b/src/Cpanel/CpanelAPI.php index 748ea56c..9c48cfc0 100644 --- a/src/Cpanel/CpanelAPI.php +++ b/src/Cpanel/CpanelAPI.php @@ -287,10 +287,13 @@ public function get_cpanel_dns_record_name($cloudflare_dns_record_name) return $cloudflare_dns_record_name . "."; } + /** + * @return bool + */ public function isAdvancedZoneEditEnabled() { /* Advanced Zone Editor is required to edit DNS records for partial zone provisioning. * https://documentation.cpanel.net/display/SDK/Guide+to+the+LiveAPI+System+-+The+cpanelfeature%28%29+Method - * Returns 1 = enabled, 0 = disabled + * cPanel returns 1 = enabled, 0 = disabled */ return ($this->cpanel_api->cpanelfeature("zoneedit") === 1); }