├── .gitignore ├── tests ├── Fixtures │ └── Endpoints │ │ ├── deleteHostname.json │ │ ├── disableTLS13.json │ │ ├── enableTLS13.json │ │ ├── getHTTPSRedirectSetting.json │ │ ├── getHTTPSRewritesSetting.json │ │ ├── getOnionRoutingSetting.json │ │ ├── listEmptyZoneSubscriptions.json │ │ ├── changeMinimumTLSVersion.json │ │ ├── updateHTTPSRedirectSetting.json │ │ ├── updateHTTPSRewritesSetting.json │ │ ├── updateOnionRoutingSetting.json │ │ ├── deletePool.json │ │ ├── revokeCertificate.json │ │ ├── cachePurge.json │ │ ├── deleteMembership.json │ │ ├── activationTest.json │ │ ├── cachePurgeHost.json │ │ ├── deleteLoadBalancer.json │ │ ├── deleteZoneTest.json │ │ ├── cachePurgeEverything.json │ │ ├── deleteFirewallRule.json │ │ ├── deleteRailgun.json │ │ ├── deleteAccessRule.json │ │ ├── deleteRule.json │ │ ├── updateSSLCertificatePackValidationMethod.json │ │ ├── deleteLockdown.json │ │ ├── deletePageRule.json │ │ ├── getSSLSetting.json │ │ ├── listIPs.json │ │ ├── updateSSLSetting.json │ │ ├── getTLSClientAuth.json │ │ ├── updateTLSClientAuth.json │ │ ├── getBrowserCacheTtlSetting.json │ │ ├── getServerSideExclude.json │ │ ├── updateBrowserCacheTtlSetting.json │ │ ├── updateServerSideExclude.json │ │ ├── getOpportunisticEncryptionSetting.json │ │ ├── updateOpportunisticEncryptionSetting.json │ │ ├── getChallengeTTLSetting.json │ │ ├── updateChallengeTTLSetting.json │ │ ├── getBrowserIntegrityCheckSetting.json │ │ ├── getSecurityLevelSetting.json │ │ ├── updateSecurityLevelSetting.json │ │ ├── updateBrowserIntegrityCheckSetting.json │ │ ├── createStandardAccount.json │ │ ├── createEnterpriseAccount.json │ │ ├── changeDevelopmentMode.json │ │ ├── getPackageInfo.json │ │ ├── listAccounts.json │ │ ├── addLockdown.json │ │ ├── createCertificate.json │ │ ├── getCertificate.json │ │ ├── getHostname.json │ │ ├── updateHostname.json │ │ ├── createCustomHostname.json │ │ ├── getCustomHostnameFallbackOrigin.json │ │ ├── getRecordDetails.json │ │ ├── updateLockdown.json │ │ ├── listCertificates.json │ │ ├── updateFirewallRule.json │ │ ├── getSSLVerificationStatus.json │ │ ├── getUserEmail.json │ │ ├── getUserId.json │ │ ├── getUserDetails.json │ │ ├── updateUserDetails.json │ │ ├── createRule.json │ │ ├── updateRule.json │ │ ├── createFirewallRule.json │ │ ├── getRuleDetails.json │ │ ├── addRecord.json │ │ ├── getDNSRecordDetails.json │ │ ├── getPackageRuleInfo.json │ │ ├── updatePackageRule.json │ │ ├── listPackages.json │ │ ├── getPackageGroupInfo.json │ │ ├── updatePackageGroup.json │ │ ├── getRailgun.json │ │ ├── createRailgun.json │ │ ├── updateRailgun.json │ │ ├── updateDNSRecord.json │ │ ├── listLockdowns.json │ │ ├── listHostnames.json │ │ ├── createPageRule.json │ │ ├── updatePageRule.json │ │ ├── getPageRuleDetails.json │ │ ├── listFirewallRules.json │ │ ├── listRules.json │ │ ├── listRecords.json │ │ ├── listRailgunZones.json │ │ ├── listPackageGroups.json │ │ ├── createAccessRule.json │ │ ├── updateAccessRule.json │ │ ├── getDNSAnalyticsReportByTime.json │ │ ├── getRecordId.json │ │ ├── listRailguns.json │ │ ├── createPool.json │ │ ├── updatePool.json │ │ ├── getPoolDetails.json │ │ ├── getDNSAnalyticsReportTable.json │ │ ├── listPageRules.json │ │ ├── listAccessRules.json │ │ ├── listPackageRules.json │ │ ├── listPools.json │ │ ├── createFirewallRules.json │ │ ├── getEnvironments.json │ │ ├── createZoneSubscription.json │ │ ├── listZoneSubscriptions.json │ │ ├── createLoadBalancer.json │ │ ├── updateLoadBalancer.json │ │ ├── getLoadBalancerDetails.json │ │ ├── listLoadBalancers.json │ │ ├── updateMembershipStatus.json │ │ ├── listAccountRoles.json │ │ ├── addZone.json │ │ ├── getZoneById.json │ │ ├── getZoneId.json │ │ ├── listZones.json │ │ ├── getMembershipDetails.json │ │ ├── createAccountMember.json │ │ ├── listMemberships.json │ │ └── listAccountMembers.json ├── Auth │ ├── NoneTest.php │ ├── APITokenTest.php │ ├── APIKeyTest.php │ └── UserServiceKeyTest.php ├── Configurations │ ├── PageRulesTargetTest.php │ ├── PageRulesActionTest.php │ ├── CertificateTest.php │ ├── ConfigurationsUARulesTest.php │ ├── ConfigurationsZoneLockdownTest.php │ ├── PoolTest.php │ └── LoadBalancerTest.php ├── Endpoints │ ├── ZoneDeleteTest.php │ ├── IPsTest.php │ ├── AccountRolesTest.php │ ├── AccountMembersTest.php │ ├── ZoneSettingsTest.php │ ├── UserTest.php │ ├── DNSAnalyticsTest.php │ ├── AccountsTest.php │ ├── CryptoTest.php │ ├── ZoneSubscriptionsTest.php │ ├── MembershipTest.php │ └── TLSTest.php ├── TestCase.php └── Adapter │ ├── GuzzleTest.php │ └── ResponseExceptionTest.php ├── src ├── Auth │ ├── Auth.php │ ├── None.php │ ├── APIToken.php │ ├── UserServiceKey.php │ └── APIKey.php ├── Traits │ └── BodyAccessorTrait.php ├── Adapter │ ├── JSONException.php │ ├── ResponseException.php │ ├── Adapter.php │ └── Guzzle.php ├── Endpoints │ ├── EndpointException.php │ ├── API.php │ ├── IPs.php │ ├── AccountRoles.php │ ├── User.php │ ├── AccountMembers.php │ ├── ZoneSubscriptions.php │ ├── Certificates.php │ ├── Membership.php │ ├── Accounts.php │ ├── Railgun.php │ ├── Crypto.php │ ├── UARules.php │ ├── Firewall.php │ ├── ZoneLockdown.php │ ├── TLS.php │ ├── FirewallSettings.php │ └── DNS.php └── Configurations │ ├── ConfigurationsException.php │ ├── Configurations.php │ ├── UARules.php │ ├── ZoneLockdown.php │ ├── PageRulesTargets.php │ ├── AccessRules.php │ ├── FirewallRuleOptions.php │ ├── DNSAnalytics.php │ ├── Certificate.php │ └── Pool.php ├── .php_cs ├── Makefile ├── phpunit.xml ├── .github └── workflows │ ├── semgrep.yml │ ├── lint.yml │ └── test.yml ├── CONTRIBUTING.md ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor/ -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deleteHostname.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9" 3 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/disableTLS13.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } 7 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/enableTLS13.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "on" 6 | } 7 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getHTTPSRedirectSetting.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getHTTPSRewritesSetting.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getOnionRoutingSetting.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [] 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/changeMinimumTLSVersion.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "1.1" 6 | } 7 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateHTTPSRedirectSetting.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateHTTPSRewritesSetting.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateOnionRoutingSetting.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": "off" 6 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deletePool.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "17b5962d775c646f3f9725cbc7a53df4" 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/revokeCertificate.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "11112222233333444455555" 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/cachePurge.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "023e105f4ecef8ad9ca31a8372d0c353" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deleteMembership.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "4536bcfad5faccb111b47003c79917fa" 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/activationTest.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "023e105f4ecef8ad9ca31a8372d0c353" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/cachePurgeHost.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "023e105f4ecef8ad9ca31a8372d0c353" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deleteLoadBalancer.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "699d98642c564d2e855e9661899b7252" 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deleteZoneTest.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "9a7806061c88ada191ed06f989cc3dac" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/cachePurgeEverything.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "023e105f4ecef8ad9ca31a8372d0c353" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deleteFirewallRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": { 3 | "id": "970b10321e3f4adda674c912b5f76591" 4 | }, 5 | "success": true, 6 | "errors": [], 7 | "messages": [] 8 | } 9 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/deleteRailgun.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "e928d310693a83094309acf9ead50448" 11 | } 12 | } -------------------------------------------------------------------------------- /src/Auth/Auth.php: -------------------------------------------------------------------------------- 1 | body; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Adapter/JSONException.php: -------------------------------------------------------------------------------- 1 | in(__DIR__ . '/src') 5 | ->in(__DIR__ . '/tests') 6 | ; 7 | 8 | return PhpCsFixer\Config::create() 9 | ->setUsingCache(false) 10 | ->setRules([ 11 | '@PSR2' => true, 12 | 'array_syntax' => ['syntax' => 'short'], 13 | ]) 14 | ->setFinder($finder) 15 | ; 16 | -------------------------------------------------------------------------------- /tests/Auth/NoneTest.php: -------------------------------------------------------------------------------- 1 | getHeaders(); 15 | 16 | $this->assertEquals([], $headers); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getPackageInfo.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "a25a9a7e9c00afc1fb2e0245519d725b", 11 | "name": "WordPress rules", 12 | "description": "Common WordPress exploit protections", 13 | "detection_mode": "traditional", 14 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 15 | "status": "active" 16 | } 17 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listAccounts.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 8 | "name": "Example Account", 9 | "settings": { 10 | "enforce_twofactor": true 11 | } 12 | } 13 | ], 14 | "result_info": { 15 | "page": 1, 16 | "per_page": 20, 17 | "count": 1, 18 | "total_count": 2000 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all fix lint test 2 | 3 | all: lint test 4 | 5 | fix: 6 | php vendor/bin/php-cs-fixer fix --config=.php_cs 7 | 8 | lint: 9 | php vendor/bin/php-cs-fixer fix --config=.php_cs --dry-run 10 | php vendor/bin/phpmd src/ text cleancode,codesize,controversial,design,naming,unusedcode 11 | php vendor/bin/phpmd tests/ text cleancode,codesize,controversial,design,naming,unusedcode 12 | 13 | test: 14 | php vendor/bin/phpunit --configuration phpunit.xml 15 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests/ 6 | 7 | 8 | 9 | 10 | ./src/ 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/addLockdown.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 7 | "description": "Restrict access to these endpoints to requests from a known IP address", 8 | "urls": [ 9 | "api.mysite.com/some/endpoint*" 10 | ], 11 | "configurations": [ 12 | { 13 | "target": "ip", 14 | "value": "1.2.3.4" 15 | } 16 | ] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createCertificate.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "12341234123412341234", 7 | "certificate": "some-cert-data-foofoo", 8 | "hostnames": [ 9 | "foo.example.com", 10 | "bar.example.com" 11 | ], 12 | "expires_on": "2014-01-01T05:20:00.12345Z", 13 | "request_type": "origin-ecc", 14 | "requested_validity": 365, 15 | "csr": "some-csr-data-barbar" 16 | } 17 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getCertificate.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "6666699999996666699999999966666666", 7 | "certificate": "some-cert-data-foobar", 8 | "hostnames": [ 9 | "foo.example.com", 10 | "bar.example.com" 11 | ], 12 | "expires_on": "2014-01-01T05:20:00.12345Z", 13 | "request_type": "origin-ecc", 14 | "requested_validity": 5475, 15 | "csr": "some-csr-data-foobar" 16 | } 17 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getHostname.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9", 11 | "hostname": "app.example.com", 12 | "ssl": { 13 | "status": "pending_validation", 14 | "method": "http", 15 | "type": "dv", 16 | "cname_target": "dcv.digicert.com", 17 | "cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com" 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateHostname.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9", 11 | "hostname": "app.example.com", 12 | "ssl": { 13 | "status": "pending_validation", 14 | "method": "http", 15 | "type": "dv", 16 | "cname_target": "dcv.digicert.com", 17 | "cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com" 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createCustomHostname.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9", 11 | "hostname": "app.example.com", 12 | "ssl": { 13 | "status": "pending_validation", 14 | "method": "http", 15 | "type": "dv", 16 | "cname_target": "dcv.digicert.com", 17 | "cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com" 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getCustomHostnameFallbackOrigin.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "origin": "fallback.example.com", 11 | "status": "pending_deployment", 12 | "errors": [ 13 | "DNS records are not setup correctly. Origin should be a proxied A/AAAA/CNAME dns record" 14 | ], 15 | "created_at": "2019-10-28T18:11:23.37411Z", 16 | "updated_at": "2020-03-16T18:11:23.531995Z" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Auth/APIToken.php: -------------------------------------------------------------------------------- 1 | apiToken = $apiToken; 17 | } 18 | 19 | public function getHeaders(): array 20 | { 21 | return [ 22 | 'Authorization' => 'Bearer ' . $this->apiToken 23 | ]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getRecordDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 11 | "description": "Restrict access to these endpoints to requests from a known IP address", 12 | "urls": [ 13 | "api.mysite.com/some/endpoint*" 14 | ], 15 | "configurations": [ 16 | { 17 | "target": "ip", 18 | "value": "1.2.3.4" 19 | } 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateLockdown.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 11 | "description": "Restrict access to these endpoints to requests from a known IP address", 12 | "urls": [ 13 | "api.mysite.com/some/endpoint*" 14 | ], 15 | "configurations": [ 16 | { 17 | "target": "ip", 18 | "value": "1.2.3.4" 19 | } 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listCertificates.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "328578533902268680212849205732770752308931942346", 8 | "certificate": "some-cert-data", 9 | "hostnames": [ 10 | "example.com", 11 | "*.example.com" 12 | ], 13 | "expires_on": "2014-01-01T05:20:00.12345Z", 14 | "request_type": "origin-rsa", 15 | "requested_validity": 5475, 16 | "csr": "some-csr-data" 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /src/Configurations/UARules.php: -------------------------------------------------------------------------------- 1 | configs[] = ['target' => 'ua', 'value' => $value]; 18 | } 19 | 20 | public function getArray(): array 21 | { 22 | return $this->configs; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateFirewallRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": { 3 | "id": "970b10321e3f4adda674c912b5f76591", 4 | "paused": false, 5 | "description": "Foo", 6 | "action": "block", 7 | "filter": { 8 | "id": "5def9c4297e0466cb0736b838345d910", 9 | "expression": "http.cookie eq \"foo\"", 10 | "paused": false 11 | }, 12 | "created_on": "2019-07-05T15:53:15Z", 13 | "modified_on": "2019-07-05T18:07:46Z", 14 | "index": 1 15 | }, 16 | "success": true, 17 | "errors": [], 18 | "messages": [] 19 | } 20 | -------------------------------------------------------------------------------- /src/Auth/UserServiceKey.php: -------------------------------------------------------------------------------- 1 | userServiceKey = $userServiceKey; 17 | } 18 | 19 | public function getHeaders(): array 20 | { 21 | return [ 22 | 'X-Auth-User-Service-Key' => $this->userServiceKey, 23 | ]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getSSLVerificationStatus.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": [ 3 | { 4 | "certificate_status": "active", 5 | "verification_type": "cname", 6 | "verification_status": true, 7 | "verification_info": { 8 | "record_name": "b3b90cfedd89a3e487d3e383c56c4267.example.com", 9 | "record_target": "6979be7e4cfc9e5c603e31df7efac9cc60fee82d.comodoca.com" 10 | }, 11 | "brand_check": false, 12 | "validation_method": "txt", 13 | "cert_pack_uuid": "a77f8bd7-3b47-46b4-a6f1-75cf98109948" 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getUserEmail.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "7c5dae5552338874e5053f2534d2767a", 7 | "email": "user@example.com", 8 | "first_name": "John", 9 | "last_name": "Appleseed", 10 | "username": "cfuser12345", 11 | "telephone": "+1 123-123-1234", 12 | "country": "US", 13 | "zipcode": "12345", 14 | "created_on": "2014-01-01T05:20:00Z", 15 | "modified_on": "2014-01-01T05:20:00Z", 16 | "two_factor_authentication_enabled": false 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getUserId.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "7c5dae5552338874e5053f2534d2767a", 7 | "email": "user@example.com", 8 | "first_name": "John", 9 | "last_name": "Appleseed", 10 | "username": "cfuser12345", 11 | "telephone": "+1 123-123-1234", 12 | "country": "US", 13 | "zipcode": "12345", 14 | "created_on": "2014-01-01T05:20:00Z", 15 | "modified_on": "2014-01-01T05:20:00Z", 16 | "two_factor_authentication_enabled": false 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getUserDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "7c5dae5552338874e5053f2534d2767a", 7 | "email": "user@example.com", 8 | "first_name": "John", 9 | "last_name": "Appleseed", 10 | "username": "cfuser12345", 11 | "telephone": "+1 123-123-1234", 12 | "country": "US", 13 | "zipcode": "12345", 14 | "created_on": "2014-01-01T05:20:00Z", 15 | "modified_on": "2014-01-01T05:20:00Z", 16 | "two_factor_authentication_enabled": false 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateUserDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "7c5dae5552338874e5053f2534d2767a", 7 | "email": "user@example.com", 8 | "first_name": "John", 9 | "last_name": "Appleseed", 10 | "username": "cfuser12345", 11 | "telephone": "+1 123-123-1234", 12 | "country": "US", 13 | "zipcode": "12345", 14 | "created_on": "2014-01-01T05:20:00Z", 15 | "modified_on": "2014-01-01T05:20:00Z", 16 | "two_factor_authentication_enabled": false 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 11 | "description": "Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack", 12 | "mode": "js_challenge", 13 | "configuration": { 14 | "target": "ua", 15 | "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 11 | "description": "Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack", 12 | "mode": "js_challenge", 13 | "configuration": { 14 | "target": "ua", 15 | "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createFirewallRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": [ 3 | { 4 | "id": "970b10321e3f4adda674c912b5f76591", 5 | "paused": false, 6 | "description": "Foobar", 7 | "action": "block", 8 | "filter": { 9 | "id": "70f39827184d487e97cc286b960f4cc3", 10 | "expression": "http.cookie eq \"foobar\"", 11 | "paused": false 12 | }, 13 | "created_on": "2019-07-05T15:53:15Z", 14 | "modified_on": "2019-07-05T15:53:15Z" 15 | } 16 | ], 17 | "success": true, 18 | "errors": [], 19 | "messages": [] 20 | } 21 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getRuleDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 11 | "description": "Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack", 12 | "mode": "js_challenge", 13 | "configuration": { 14 | "target": "ua", 15 | "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/addRecord.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 7 | "type": "A", 8 | "name": "example.com", 9 | "content": "1.2.3.4", 10 | "proxiable": true, 11 | "proxied": false, 12 | "ttl": 120, 13 | "locked": false, 14 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 15 | "zone_name": "example.com", 16 | "created_on": "2014-01-01T05:20:00.12345Z", 17 | "modified_on": "2014-01-01T05:20:00.12345Z", 18 | "data": {} 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getDNSRecordDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 7 | "type": "A", 8 | "name": "example.com", 9 | "content": "1.2.3.4", 10 | "proxiable": true, 11 | "proxied": false, 12 | "ttl": 120, 13 | "locked": false, 14 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 15 | "zone_name": "example.com", 16 | "created_on": "2014-01-01T05:20:00.12345Z", 17 | "modified_on": "2014-01-01T05:20:00.12345Z", 18 | "data": {} 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getPackageRuleInfo.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "f939de3be84e66e757adcdcb87908023", 11 | "description": "SQL injection prevention for SELECT statements", 12 | "priority": 5, 13 | "group": { 14 | "id": "de677e5818985db1285d0e80225f06e5", 15 | "name": "Project Honey Pot" 16 | }, 17 | "package_id": "a25a9a7e9c00afc1fb2e0245519d725b", 18 | "allowed_modes": [ 19 | "on", 20 | "off" 21 | ], 22 | "mode": "on" 23 | } 24 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updatePackageRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "f939de3be84e66e757adcdcb87908023", 11 | "description": "SQL injection prevention for SELECT statements", 12 | "priority": 5, 13 | "group": { 14 | "id": "de677e5818985db1285d0e80225f06e5", 15 | "name": "Project Honey Pot" 16 | }, 17 | "package_id": "a25a9a7e9c00afc1fb2e0245519d725b", 18 | "allowed_modes": [ 19 | "on", 20 | "off" 21 | ], 22 | "mode": "on" 23 | } 24 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listPackages.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "a25a9a7e9c00afc1fb2e0245519d725b", 12 | "name": "WordPress rules", 13 | "description": "Common WordPress exploit protections", 14 | "detection_mode": "traditional", 15 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 16 | "status": "active" 17 | } 18 | ], 19 | "result_info": { 20 | "page": 1, 21 | "per_page": 20, 22 | "count": 1, 23 | "total_count": 2000 24 | } 25 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getPackageGroupInfo.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "de677e5818985db1285d0e80225f06e5", 11 | "name": "Project Honey Pot", 12 | "description": "Group designed to protect against IP addresses that are a threat and typically used to launch DDoS attacks", 13 | "rules_count": 10, 14 | "modified_rules_count": 2, 15 | "package_id": "a25a9a7e9c00afc1fb2e0245519d725b", 16 | "mode": "on", 17 | "allowed_modes": [ 18 | "on", 19 | "off" 20 | ] 21 | } 22 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updatePackageGroup.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "de677e5818985db1285d0e80225f06e5", 11 | "name": "Project Honey Pot", 12 | "description": "Group designed to protect against IP addresses that are a threat and typically used to launch DDoS attacks", 13 | "rules_count": 10, 14 | "modified_rules_count": 2, 15 | "package_id": "a25a9a7e9c00afc1fb2e0245519d725b", 16 | "mode": "off", 17 | "allowed_modes": [ 18 | "on", 19 | "off" 20 | ] 21 | } 22 | } -------------------------------------------------------------------------------- /src/Auth/APIKey.php: -------------------------------------------------------------------------------- 1 | email = $email; 18 | $this->apiKey = $apiKey; 19 | } 20 | 21 | public function getHeaders(): array 22 | { 23 | return [ 24 | 'X-Auth-Email' => $this->email, 25 | 'X-Auth-Key' => $this->apiKey 26 | ]; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Auth/APITokenTest.php: -------------------------------------------------------------------------------- 1 | getHeaders(); 14 | 15 | $this->assertArrayHasKey('Authorization', $headers); 16 | 17 | $this->assertEquals('Bearer zKq9RDO6PbCjs6PRUXF3BoqFi3QdwY36C2VfOaRy', $headers['Authorization']); 18 | 19 | $this->assertCount(1, $headers); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getRailgun.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "e928d310693a83094309acf9ead50448", 11 | "name": "My Railgun", 12 | "status": "active", 13 | "enabled": true, 14 | "zones_connected": 2, 15 | "build": "b1234", 16 | "version": "2.1", 17 | "revision": "123", 18 | "activation_key": "e4edc00281cb56ebac22c81be9bac8f3", 19 | "activated_on": "2014-01-02T02:20:00Z", 20 | "created_on": "2014-01-01T05:20:00Z", 21 | "modified_on": "2014-01-01T05:20:00Z" 22 | } 23 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createRailgun.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "e928d310693a83094309acf9ead50448", 11 | "name": "My Railgun", 12 | "status": "active", 13 | "enabled": true, 14 | "zones_connected": 2, 15 | "build": "b1234", 16 | "version": "2.1", 17 | "revision": "123", 18 | "activation_key": "e4edc00281cb56ebac22c81be9bac8f3", 19 | "activated_on": "2014-01-02T02:20:00Z", 20 | "created_on": "2014-01-01T05:20:00Z", 21 | "modified_on": "2014-01-01T05:20:00Z" 22 | } 23 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateRailgun.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "e928d310693a83094309acf9ead50448", 11 | "name": "My Railgun", 12 | "status": "active", 13 | "enabled": true, 14 | "zones_connected": 2, 15 | "build": "b1234", 16 | "version": "2.1", 17 | "revision": "123", 18 | "activation_key": "e4edc00281cb56ebac22c81be9bac8f3", 19 | "activated_on": "2014-01-02T02:20:00Z", 20 | "created_on": "2014-01-01T05:20:00Z", 21 | "modified_on": "2014-01-01T05:20:00Z" 22 | } 23 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateDNSRecord.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 11 | "type": "A", 12 | "name": "example.com", 13 | "content": "1.2.3.4", 14 | "proxiable": true, 15 | "proxied": false, 16 | "ttl": 120, 17 | "locked": false, 18 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 19 | "zone_name": "example.com", 20 | "created_on": "2014-01-01T05:20:00.12345Z", 21 | "modified_on": "2014-01-01T05:20:00.12345Z", 22 | "data": {} 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listLockdowns.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 8 | "description": "Restrict access to these endpoints to requests from a known IP address", 9 | "urls": [ 10 | "api.mysite.com/some/endpoint*" 11 | ], 12 | "configurations": [ 13 | { 14 | "target": "ip", 15 | "value": "1.2.3.4" 16 | } 17 | ] 18 | } 19 | ], 20 | "result_info": { 21 | "page": 1, 22 | "per_page": 20, 23 | "count": 1, 24 | "total_count": 2000 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Configurations/PageRulesTargetTest.php: -------------------------------------------------------------------------------- 1 | getArray(); 17 | 18 | $this->assertCount(1, $array); 19 | $this->assertEquals('junade.com/*', $array[0]['constraint']['value']); 20 | $this->assertEquals('matches', $array[0]['constraint']['operator']); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listHostnames.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9", 12 | "hostname": "app.example.com", 13 | "ssl": { 14 | "status": "pending_validation", 15 | "method": "http", 16 | "type": "dv", 17 | "cname_target": "dcv.digicert.com", 18 | "cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com" 19 | } 20 | } 21 | ], 22 | "result_info": { 23 | "page": 1, 24 | "per_page": 20, 25 | "count": 1, 26 | "total_count": 2000 27 | } 28 | } -------------------------------------------------------------------------------- /src/Configurations/ZoneLockdown.php: -------------------------------------------------------------------------------- 1 | configs[] = ['target' => 'ip', 'value' => $value]; 18 | } 19 | 20 | public function addIPRange(string $value) 21 | { 22 | $this->configs[] = ['target' => 'ip_range', 'value' => $value]; 23 | } 24 | 25 | public function getArray(): array 26 | { 27 | return $this->configs; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /.github/workflows/semgrep.yml: -------------------------------------------------------------------------------- 1 | 2 | on: 3 | pull_request: {} 4 | workflow_dispatch: {} 5 | push: 6 | branches: 7 | - main 8 | - master 9 | schedule: 10 | - cron: '0 0 * * *' 11 | name: Semgrep config 12 | jobs: 13 | semgrep: 14 | name: semgrep/ci 15 | runs-on: ubuntu-20.04 16 | env: 17 | SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} 18 | SEMGREP_URL: https://cloudflare.semgrep.dev 19 | SEMGREP_APP_URL: https://cloudflare.semgrep.dev 20 | SEMGREP_VERSION_CHECK_URL: https://cloudflare.semgrep.dev/api/check-version 21 | container: 22 | image: returntocorp/semgrep 23 | steps: 24 | - uses: actions/checkout@v3 25 | - run: semgrep ci 26 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createPageRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "9a7806061c88ada191ed06f989cc3dac", 11 | "targets": [ 12 | { 13 | "target": "url", 14 | "constraint": { 15 | "operator": "matches", 16 | "value": "*example.com/images/*" 17 | } 18 | } 19 | ], 20 | "actions": [ 21 | { 22 | "id": "always_online", 23 | "value": "on" 24 | } 25 | ], 26 | "priority": 1, 27 | "status": "active", 28 | "modified_on": "2014-01-01T05:20:00.12345Z", 29 | "created_on": "2014-01-01T05:20:00.12345Z" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updatePageRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "9a7806061c88ada191ed06f989cc3dac", 11 | "targets": [ 12 | { 13 | "target": "url", 14 | "constraint": { 15 | "operator": "matches", 16 | "value": "*example.com/images/*" 17 | } 18 | } 19 | ], 20 | "actions": [ 21 | { 22 | "id": "always_online", 23 | "value": "on" 24 | } 25 | ], 26 | "priority": 1, 27 | "status": "active", 28 | "modified_on": "2014-01-01T05:20:00.12345Z", 29 | "created_on": "2014-01-01T05:20:00.12345Z" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getPageRuleDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "9a7806061c88ada191ed06f989cc3dac", 11 | "targets": [ 12 | { 13 | "target": "url", 14 | "constraint": { 15 | "operator": "matches", 16 | "value": "*example.com/images/*" 17 | } 18 | } 19 | ], 20 | "actions": [ 21 | { 22 | "id": "always_online", 23 | "value": "on" 24 | } 25 | ], 26 | "priority": 1, 27 | "status": "active", 28 | "modified_on": "2014-01-01T05:20:00.12345Z", 29 | "created_on": "2014-01-01T05:20:00.12345Z" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Endpoints/IPs.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 23 | } 24 | 25 | public function listIPs(): \stdClass 26 | { 27 | $ips = $this->adapter->get('ips'); 28 | $this->body = json_decode($ips->getBody()); 29 | 30 | return $this->body->result; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listFirewallRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": [ 3 | { 4 | "id": "970b10321e3f4adda674c912b5f76591", 5 | "paused": false, 6 | "description": "Foobar", 7 | "action": "block", 8 | "filter": { 9 | "id": "70f39827184d487e97cc286b960f4cc3", 10 | "expression": "http.cookie eq \"foobar\"", 11 | "paused": false 12 | }, 13 | "created_on": "2019-07-05T15:53:15Z", 14 | "modified_on": "2019-07-05T15:53:15Z" 15 | } 16 | ], 17 | "success": true, 18 | "errors": [], 19 | "messages": [], 20 | "result_info": { 21 | "page": 1, 22 | "per_page": 50, 23 | "count": 1, 24 | "total_count": 1, 25 | "total_pages": 1 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 12 | "description": "Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack", 13 | "mode": "js_challenge", 14 | "configuration": { 15 | "target": "ua", 16 | "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" 17 | } 18 | } 19 | ], 20 | "result_info": { 21 | "page": 1, 22 | "per_page": 20, 23 | "count": 1, 24 | "total_count": 2000 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Auth/APIKeyTest.php: -------------------------------------------------------------------------------- 1 | getHeaders(); 14 | 15 | $this->assertArrayHasKey('X-Auth-Key', $headers); 16 | $this->assertArrayHasKey('X-Auth-Email', $headers); 17 | 18 | $this->assertEquals('example@example.com', $headers['X-Auth-Email']); 19 | $this->assertEquals('1234567893feefc5f0q5000bfo0c38d90bbeb', $headers['X-Auth-Key']); 20 | 21 | $this->assertCount(2, $headers); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listRecords.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 8 | "type": "A", 9 | "name": "example.com", 10 | "content": "1.2.3.4", 11 | "proxiable": true, 12 | "proxied": false, 13 | "ttl": 120, 14 | "locked": false, 15 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 16 | "zone_name": "example.com", 17 | "created_on": "2014-01-01T05:20:00.12345Z", 18 | "modified_on": "2014-01-01T05:20:00.12345Z", 19 | "data": {} 20 | } 21 | ], 22 | "result_info": { 23 | "page": 1, 24 | "per_page": 20, 25 | "count": 1, 26 | "total_count": 2000 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Configurations/PageRulesTargets.php: -------------------------------------------------------------------------------- 1 | targets = [ 18 | [ 19 | 'target' => 'url', 20 | 'constraint' => [ 21 | 'operator' => 'matches', 22 | 'value' => $queryUrl 23 | ] 24 | ] 25 | ]; 26 | } 27 | 28 | public function getArray(): array 29 | { 30 | return $this->targets; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listRailgunZones.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 12 | "name": "example.com", 13 | "development_mode": 7200, 14 | "original_name_servers": [ 15 | "ns1.originaldnshost.com", 16 | "ns2.originaldnshost.com" 17 | ], 18 | "original_registrar": "GoDaddy", 19 | "original_dnshost": "NameCheap", 20 | "created_on": "2014-01-01T05:20:00.12345Z", 21 | "modified_on": "2014-01-01T05:20:00.12345Z" 22 | } 23 | ], 24 | "result_info": { 25 | "page": 1, 26 | "per_page": 20, 27 | "count": 1, 28 | "total_count": 2000 29 | } 30 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listPackageGroups.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "de677e5818985db1285d0e80225f06e5", 12 | "name": "Project Honey Pot", 13 | "description": "Group designed to protect against IP addresses that are a threat and typically used to launch DDoS attacks", 14 | "rules_count": 10, 15 | "modified_rules_count": 2, 16 | "package_id": "a25a9a7e9c00afc1fb2e0245519d725b", 17 | "mode": "on", 18 | "allowed_modes": [ 19 | "on", 20 | "off" 21 | ] 22 | } 23 | ], 24 | "result_info": { 25 | "page": 1, 26 | "per_page": 20, 27 | "count": 1, 28 | "total_count": 2000 29 | } 30 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createAccessRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "92f17202ed8bd63d69a66b86a49a8f6b", 11 | "notes": "This rule is on because of an event that occured on date X", 12 | "allowed_modes": [ 13 | "whitelist", 14 | "block", 15 | "challenge" 16 | ], 17 | "mode": "challenge", 18 | "configuration": { 19 | "target": "ip", 20 | "value": "1.2.3.4" 21 | }, 22 | "scope": { 23 | "id": "7c5dae5552338874e5053f2534d2767a", 24 | "email": "user@example.com", 25 | "type": "user" 26 | }, 27 | "created_on": "2014-01-01T05:20:00.12345Z", 28 | "modified_on": "2014-01-01T05:20:00.12345Z" 29 | } 30 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateAccessRule.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": { 10 | "id": "92f17202ed8bd63d69a66b86a49a8f6b", 11 | "notes": "This rule is on because of an event that occured on date X", 12 | "allowed_modes": [ 13 | "whitelist", 14 | "block", 15 | "challenge" 16 | ], 17 | "mode": "challenge", 18 | "configuration": { 19 | "target": "ip", 20 | "value": "1.2.3.4" 21 | }, 22 | "scope": { 23 | "id": "7c5dae5552338874e5053f2534d2767a", 24 | "email": "user@example.com", 25 | "type": "user" 26 | }, 27 | "created_on": "2014-01-01T05:20:00.12345Z", 28 | "modified_on": "2014-01-01T05:20:00.12345Z" 29 | } 30 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getDNSAnalyticsReportByTime.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": { 3 | "rows": 2, 4 | "data": [ 5 | { 6 | "metrics": [[911, 993]] 7 | } 8 | ], 9 | "data_lag": 0, 10 | "min": {}, 11 | "max": {}, 12 | "totals": { 13 | "queryCount": 455312 14 | }, 15 | "time_intervals": [ 16 | ["2020-02-10T11:19:00Z", "2020-02-10T11:19:59Z"], 17 | ["2020-02-10T11:20:00Z", "2020-02-10T11:20:59Z"] 18 | ], 19 | "query": { 20 | "dimensions": [], 21 | "metrics": ["queryCount"], 22 | "since": "2020-02-01T00:00:00Z", 23 | "until": "2020-02-28T23:59:59Z", 24 | "time_delta": "minute", 25 | "limit": 2 26 | } 27 | }, 28 | "success": true, 29 | "errors": [], 30 | "messages": [] 31 | } 32 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getRecordId.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "372e67954025e0ba6aaa6d586b9e0b59", 8 | "type": "A", 9 | "name": "example.com", 10 | "content": "198.51.100.4", 11 | "proxiable": true, 12 | "proxied": false, 13 | "ttl": {}, 14 | "locked": false, 15 | "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", 16 | "zone_name": "example.com", 17 | "created_on": "2014-01-01T05:20:00.12345Z", 18 | "modified_on": "2014-01-01T05:20:00.12345Z", 19 | "data": {} 20 | } 21 | ], 22 | "result_info": { 23 | "page": 1, 24 | "per_page": 20, 25 | "total_pages": 1, 26 | "count": 1, 27 | "total_count": 1 28 | } 29 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listRailguns.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "e928d310693a83094309acf9ead50448", 12 | "name": "My Railgun", 13 | "status": "active", 14 | "enabled": true, 15 | "zones_connected": 2, 16 | "build": "b1234", 17 | "version": "2.1", 18 | "revision": "123", 19 | "activation_key": "e4edc00281cb56ebac22c81be9bac8f3", 20 | "activated_on": "2014-01-02T02:20:00Z", 21 | "created_on": "2014-01-01T05:20:00Z", 22 | "modified_on": "2014-01-01T05:20:00Z" 23 | } 24 | ], 25 | "result_info": { 26 | "page": 1, 27 | "per_page": 20, 28 | "count": 1, 29 | "total_count": 2000 30 | } 31 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createPool.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "17b5962d775c646f3f9725cbc7a53df4", 7 | "created_on": "2014-01-01T05:20:00.12345Z", 8 | "modified_on": "2014-01-01T05:20:00.12345Z", 9 | "description": "Primary data center - Provider XYZ", 10 | "name": "primary-dc-1", 11 | "enabled": true, 12 | "minimum_origins": 2, 13 | "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", 14 | "check_regions": [ 15 | "WEU", 16 | "ENAM" 17 | ], 18 | "origins": [ 19 | { 20 | "name": "app-server-1", 21 | "address": "0.0.0.0", 22 | "enabled": true, 23 | "weight": 0.56 24 | } 25 | ], 26 | "notification_email": "someone@example.com" 27 | } 28 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updatePool.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "17b5962d775c646f3f9725cbc7a53df4", 7 | "created_on": "2014-01-01T05:20:00.12345Z", 8 | "modified_on": "2014-01-01T05:20:00.12345Z", 9 | "description": "Primary data center - Provider XYZ", 10 | "name": "primary-dc-1", 11 | "enabled": true, 12 | "minimum_origins": 2, 13 | "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", 14 | "check_regions": [ 15 | "WEU", 16 | "ENAM" 17 | ], 18 | "origins": [ 19 | { 20 | "name": "app-server-1", 21 | "address": "0.0.0.0", 22 | "enabled": true, 23 | "weight": 0.56 24 | } 25 | ], 26 | "notification_email": "someone@example.com" 27 | } 28 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getPoolDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "17b5962d775c646f3f9725cbc7a53df4", 7 | "created_on": "2014-01-01T05:20:00.12345Z", 8 | "modified_on": "2014-01-01T05:20:00.12345Z", 9 | "description": "Primary data center - Provider XYZ", 10 | "name": "primary-dc-1", 11 | "enabled": true, 12 | "minimum_origins": 2, 13 | "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", 14 | "check_regions": [ 15 | "WEU", 16 | "ENAM" 17 | ], 18 | "origins": [ 19 | { 20 | "name": "app-server-1", 21 | "address": "0.0.0.0", 22 | "enabled": true, 23 | "weight": 0.56 24 | } 25 | ], 26 | "notification_email": "someone@example.com" 27 | } 28 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Cloudflare PHP API Binding 2 | 3 | 👍🎉 First off, thanks for taking the time to contribute! 🎉👍 4 | 5 | ## How To Contribute 6 | 7 | We welcome community contribution to this repository. To help add functionality or address issues, please take the following steps: 8 | 9 | * Fork the repository from the master branch. 10 | * Create a new branch for your features / fixes. 11 | * Make the changes you wish to see. 12 | * Add tests for all changes. 13 | * Run `make fix` command to ensure the code is styled. 14 | * Run `make test` command to ensure the tests pass. 15 | * Create a pull request with details of what changes have been made and explanation of new behaviour. 16 | * Ensure documentation contains the correct information. 17 | * Pull requests will be reviewed and hopefully merged into a release. 18 | -------------------------------------------------------------------------------- /tests/Configurations/PageRulesActionTest.php: -------------------------------------------------------------------------------- 1 | setForwardingURL($statusCode, $forwardingURL); 14 | $configuration = $actions->getArray(); 15 | 16 | $this->assertCount(1, $configuration); 17 | $this->assertEquals($identifier, $configuration[0]['id']); 18 | $this->assertEquals($statusCode, $configuration[0]['value']['status_code']); 19 | $this->assertEquals($forwardingURL, $configuration[0]['value']['url']); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Configurations/AccessRules.php: -------------------------------------------------------------------------------- 1 | config = ['target' => 'ip', 'value' => $value]; 12 | } 13 | 14 | public function setIPRange(string $value) 15 | { 16 | $this->config = ['target' => 'ip_range', 'value' => $value]; 17 | } 18 | 19 | public function setCountry(string $value) 20 | { 21 | $this->config = ['target' => 'country', 'value' => $value]; 22 | } 23 | 24 | public function setASN(string $value) 25 | { 26 | $this->config = ['target' => 'asn', 'value' => $value]; 27 | } 28 | 29 | public function getArray(): array 30 | { 31 | return $this->config; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Endpoints/ZoneDeleteTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/deleteZoneTest.json'); 7 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 8 | $mock->method('delete')->willReturn($response); 9 | $mock->expects($this->once()) 10 | ->method('delete') 11 | ->with( 12 | $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353') 13 | ); 14 | $zones = new \Cloudflare\API\Endpoints\Zones($mock); 15 | $result = $zones->deleteZone('023e105f4ecef8ad9ca31a8372d0c353'); 16 | $this->assertTrue($result); 17 | $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getDNSAnalyticsReportTable.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": { 3 | "rows": 1, 4 | "data": [ 5 | { 6 | "dimensions": ["thrdld.sld.tld", "TXT", "NOERROR"], 7 | "metrics": [2] 8 | } 9 | ], 10 | "data_lag": 0, 11 | "min": {}, 12 | "max": {}, 13 | "totals": { 14 | "queryCount": 2 15 | }, 16 | "query": { 17 | "dimensions": ["queryName", "queryType", "responseCode"], 18 | "metrics": ["queryCount"], 19 | "filters": "responseCode==NOERROR AND queryType==TXT", 20 | "sort": [ 21 | { 22 | "Id": "queryCount", 23 | "Desc": true 24 | } 25 | ], 26 | "since": "2020-02-01T00:00:00Z", 27 | "until": "2020-02-28T23:59:59Z", 28 | "limit": 10000 29 | } 30 | }, 31 | "success": true, 32 | "errors": [], 33 | "messages": [] 34 | } 35 | -------------------------------------------------------------------------------- /src/Endpoints/AccountRoles.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 21 | } 22 | 23 | public function listAccountRoles(string $accountId): stdClass 24 | { 25 | $roles = $this->adapter->get('accounts/' . $accountId . '/roles'); 26 | $this->body = json_decode($roles->getBody()); 27 | 28 | return (object)[ 29 | 'result' => $this->body->result, 30 | 'result_info' => $this->body->result_info, 31 | ]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Configurations/CertificateTest.php: -------------------------------------------------------------------------------- 1 | setHostnames(['foo.com', '*.bar.com']); 12 | $certificate->setRequestType(Certificate::ORIGIN_ECC); 13 | $certificate->setRequestedValidity(365); 14 | $certificate->setCsr('some-csr-encoded-text'); 15 | 16 | $array = $certificate->getArray(); 17 | $this->assertEquals(['foo.com', '*.bar.com'], $array['hostnames']); 18 | $this->assertEquals('origin-ecc', $array['request_type']); 19 | $this->assertEquals(365, $array['requested_validity']); 20 | $this->assertEquals('some-csr-encoded-text', $array['csr']); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listPageRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "9a7806061c88ada191ed06f989cc3dac", 12 | "targets": [ 13 | { 14 | "target": "url", 15 | "constraint": { 16 | "operator": "matches", 17 | "value": "*example.com/images/*" 18 | } 19 | } 20 | ], 21 | "actions": [ 22 | { 23 | "id": "always_online", 24 | "value": "on" 25 | } 26 | ], 27 | "priority": 1, 28 | "status": "active", 29 | "modified_on": "2014-01-01T05:20:00.12345Z", 30 | "created_on": "2014-01-01T05:20:00.12345Z" 31 | } 32 | ], 33 | "result_info": { 34 | "page": 1, 35 | "per_page": 20, 36 | "count": 1, 37 | "total_count": 2000 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cloudflare/sdk", 3 | "description": "PHP binding for v4 of the Cloudflare Client API.", 4 | "type": "library", 5 | "require": { 6 | "guzzlehttp/guzzle": "^7.0.1", 7 | "php": ">=7.2.5", 8 | "psr/http-message": "~1.0", 9 | "ext-json": "*" 10 | }, 11 | "require-dev": { 12 | "phpunit/phpunit": "^5.7", 13 | "phpmd/phpmd" : "@stable", 14 | "friendsofphp/php-cs-fixer": "^2.6" 15 | }, 16 | "license": "BSD-3-Clause", 17 | "authors": [ 18 | { 19 | "name": "Junade Ali", 20 | "email": "junade@cloudflare.com" 21 | } 22 | ], 23 | "autoload": { 24 | "psr-4": { 25 | "Cloudflare\\API\\": "src/" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "classmap": [ 30 | "tests/" 31 | ] 32 | }, 33 | "config": { 34 | "preferred-install": "dist", 35 | "sort-packages": true, 36 | "optimize-autoloader": true 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listAccessRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "92f17202ed8bd63d69a66b86a49a8f6b", 12 | "notes": "This rule is on because of an event that occured on date X", 13 | "allowed_modes": [ 14 | "whitelist", 15 | "block", 16 | "challenge" 17 | ], 18 | "mode": "challenge", 19 | "configuration": { 20 | "target": "ip", 21 | "value": "1.2.3.4" 22 | }, 23 | "scope": { 24 | "id": "7c5dae5552338874e5053f2534d2767a", 25 | "email": "user@example.com", 26 | "type": "user" 27 | }, 28 | "created_on": "2014-01-01T05:20:00.12345Z", 29 | "modified_on": "2014-01-01T05:20:00.12345Z" 30 | } 31 | ], 32 | "result_info": { 33 | "page": 1, 34 | "per_page": 20, 35 | "count": 1, 36 | "total_count": 2000 37 | } 38 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listPackageRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [ 4 | {} 5 | ], 6 | "messages": [ 7 | {} 8 | ], 9 | "result": [ 10 | { 11 | "id": "92f17202ed8bd63d69a66b86a49a8f6b", 12 | "notes": "This rule is on because of an event that occured on date X", 13 | "allowed_modes": [ 14 | "whitelist", 15 | "block", 16 | "challenge" 17 | ], 18 | "mode": "challenge", 19 | "configuration": { 20 | "target": "ip", 21 | "value": "1.2.3.4" 22 | }, 23 | "scope": { 24 | "id": "7c5dae5552338874e5053f2534d2767a", 25 | "email": "user@example.com", 26 | "type": "user" 27 | }, 28 | "created_on": "2014-01-01T05:20:00.12345Z", 29 | "modified_on": "2014-01-01T05:20:00.12345Z" 30 | } 31 | ], 32 | "result_info": { 33 | "page": 1, 34 | "per_page": 20, 35 | "count": 1, 36 | "total_count": 2000 37 | } 38 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listPools.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "17b5962d775c646f3f9725cbc7a53df4", 8 | "created_on": "2014-01-01T05:20:00.12345Z", 9 | "modified_on": "2014-01-01T05:20:00.12345Z", 10 | "description": "Primary data center - Provider XYZ", 11 | "name": "primary-dc-1", 12 | "enabled": true, 13 | "minimum_origins": 2, 14 | "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", 15 | "check_regions": [ 16 | "WEU", 17 | "ENAM" 18 | ], 19 | "origins": [ 20 | { 21 | "name": "app-server-1", 22 | "address": "0.0.0.0", 23 | "enabled": true, 24 | "weight": 0.56 25 | } 26 | ], 27 | "notification_email": "someone@example.com" 28 | } 29 | ], 30 | "result_info": { 31 | "page": 1, 32 | "per_page": 20, 33 | "count": 1, 34 | "total_count": 2000 35 | } 36 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createFirewallRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": [ 3 | { 4 | "id": "970b10321e3f4adda674c912b5f76591", 5 | "paused": false, 6 | "description": "Foo", 7 | "action": "block", 8 | "filter": { 9 | "id": "70f39827184d487e97cc286b960f4cc3", 10 | "expression": "http.cookie eq \"foo\"", 11 | "paused": false 12 | }, 13 | "created_on": "2019-07-05T15:53:15Z", 14 | "modified_on": "2019-07-05T15:53:15Z" 15 | }, 16 | { 17 | "id": "42c05fd0e0af4d17a361d2d1423476bc", 18 | "paused": false, 19 | "description": "Bar", 20 | "action": "block", 21 | "filter": { 22 | "id": "246b4d9f5f51471485bdc95e1c6b53a7", 23 | "expression": "http.cookie eq \"bar\"", 24 | "paused": false 25 | }, 26 | "created_on": "2019-07-05T15:53:15Z", 27 | "modified_on": "2019-07-05T15:53:15Z" 28 | } 29 | ], 30 | "success": true, 31 | "errors": [], 32 | "messages": [] 33 | } 34 | -------------------------------------------------------------------------------- /tests/Configurations/ConfigurationsUARulesTest.php: -------------------------------------------------------------------------------- 1 | addUA('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4'); 15 | 16 | $array = $configuration->getArray(); 17 | $this->assertCount(1, $array); 18 | 19 | $this->assertArrayHasKey('target', $array[0]); 20 | $this->assertEquals('ua', $array[0]['target']); 21 | $this->assertArrayHasKey('value', $array[0]); 22 | $this->assertEquals( 23 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4', 24 | $array[0]['value'] 25 | ); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getEnvironments.json: -------------------------------------------------------------------------------- 1 | { 2 | "result": { 3 | "environments": [ 4 | { 5 | "name": "Production", 6 | "ref": "first", 7 | "version": 0, 8 | "expression": "expression_1", 9 | "locked_on_deployment": false, 10 | "position": { 11 | "before": "second" 12 | } 13 | }, 14 | { 15 | "name": "Staging", 16 | "ref": "second", 17 | "version": 0, 18 | "expression": "expression_2", 19 | "locked_on_deployment": false, 20 | "position": { 21 | "before": "third", 22 | "after": "first" 23 | } 24 | }, 25 | { 26 | "name": "Development", 27 | "ref": "third", 28 | "version": 0, 29 | "expression": "expression_3", 30 | "locked_on_deployment": false, 31 | "position": { 32 | "after": "second" 33 | } 34 | } 35 | ] 36 | }, 37 | "success": true, 38 | "errors": [], 39 | "messages": [] 40 | } 41 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createZoneSubscription.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "app": { 7 | "install_id": null 8 | }, 9 | "id": "506e3185e9c882d175a2d0cb0093d9f2", 10 | "state": "Paid", 11 | "price": 20, 12 | "currency": "USD", 13 | "component_values": [ 14 | { 15 | "name": "page_rules", 16 | "value": 20, 17 | "default": 5, 18 | "price": 5 19 | } 20 | ], 21 | "zone": { 22 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 23 | "name": "example.com" 24 | }, 25 | "frequency": "monthly", 26 | "rate_plan": { 27 | "id": "free", 28 | "public_name": "Business Plan", 29 | "currency": "USD", 30 | "scope": "zone", 31 | "sets": [ 32 | {} 33 | ], 34 | "is_contract": false, 35 | "externally_managed": false 36 | }, 37 | "current_period_end": "2014-03-31T12:20:00Z", 38 | "current_period_start": "2014-05-11T12:20:00Z" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: 4 | push: 5 | branches: [ '*' ] 6 | pull_request: 7 | branches: [ '*' ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4'] 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | 21 | - uses: shivammathur/setup-php@v2 22 | with: 23 | php-version: ${{ matrix.php-versions }} 24 | 25 | - name: Cache Composer dependencies 26 | uses: actions/cache@v2 27 | with: 28 | path: /tmp/composer-cache 29 | key: ${{ runner.os }}-php${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }} 30 | 31 | - name: Install dependencies 32 | uses: php-actions/composer@v5 33 | with: 34 | command: install 35 | args: --prefer-dist --no-progress --no-suggest --verbose 36 | php_version: ${{ matrix.php-versions }} 37 | version: 1 38 | 39 | - name: Run lint 40 | run: make lint 41 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: [ '*' ] 6 | pull_request: 7 | branches: [ '*' ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4'] 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | 21 | - uses: shivammathur/setup-php@v2 22 | with: 23 | php-version: ${{ matrix.php-versions }} 24 | 25 | - name: Cache Composer dependencies 26 | uses: actions/cache@v2 27 | with: 28 | path: /tmp/composer-cache 29 | key: ${{ runner.os }}-php${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }} 30 | 31 | - name: Install dependencies 32 | uses: php-actions/composer@v5 33 | with: 34 | command: install 35 | args: --prefer-dist --no-progress --no-suggest --verbose 36 | php_version: ${{ matrix.php-versions }} 37 | version: 1 38 | 39 | - name: Run tests 40 | run: make test 41 | -------------------------------------------------------------------------------- /tests/Endpoints/IPsTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/listIPs.json'); 14 | 15 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 16 | $mock->method('get')->willReturn($response); 17 | 18 | $mock->expects($this->once()) 19 | ->method('get') 20 | ->with( 21 | $this->equalTo('ips') 22 | ); 23 | 24 | $ipsMock = new \Cloudflare\API\Endpoints\IPs($mock); 25 | $ips = $ipsMock->listIPs(); 26 | $this->assertObjectHasAttribute('ipv4_cidrs', $ips); 27 | $this->assertObjectHasAttribute('ipv6_cidrs', $ips); 28 | $this->assertObjectHasAttribute('ipv4_cidrs', $ipsMock->getBody()->result); 29 | $this->assertObjectHasAttribute('ipv6_cidrs', $ipsMock->getBody()->result); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Configurations/FirewallRuleOptions.php: -------------------------------------------------------------------------------- 1 | false, 9 | 'action' => 'block' 10 | ]; 11 | 12 | public function getArray(): array 13 | { 14 | return $this->configs; 15 | } 16 | 17 | public function setPaused(bool $paused) 18 | { 19 | $this->configs['paused'] = $paused; 20 | } 21 | 22 | public function setActionBlock() 23 | { 24 | $this->configs['action'] = 'block'; 25 | } 26 | 27 | public function setActionAllow() 28 | { 29 | $this->configs['action'] = 'allow'; 30 | } 31 | 32 | public function setActionChallenge() 33 | { 34 | $this->configs['action'] = 'challenge'; 35 | } 36 | 37 | public function setActionJsChallenge() 38 | { 39 | $this->configs['action'] = 'js_challenge'; 40 | } 41 | 42 | public function setActionLog() 43 | { 44 | $this->configs['action'] = 'log'; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listZoneSubscriptions.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "app": { 8 | "install_id": null 9 | }, 10 | "id": "506e3185e9c882d175a2d0cb0093d9f2", 11 | "state": "Paid", 12 | "price": 20, 13 | "currency": "USD", 14 | "component_values": [ 15 | { 16 | "name": "page_rules", 17 | "value": 20, 18 | "default": 5, 19 | "price": 5 20 | } 21 | ], 22 | "zone": { 23 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 24 | "name": "example.com" 25 | }, 26 | "frequency": "monthly", 27 | "rate_plan": { 28 | "id": "free", 29 | "public_name": "Business Plan", 30 | "currency": "USD", 31 | "scope": "zone", 32 | "sets": [ 33 | {} 34 | ], 35 | "is_contract": false, 36 | "externally_managed": false 37 | }, 38 | "current_period_end": "2014-03-31T12:20:00Z", 39 | "current_period_start": "2014-05-11T12:20:00Z" 40 | } 41 | ] 42 | } -------------------------------------------------------------------------------- /tests/Auth/UserServiceKeyTest.php: -------------------------------------------------------------------------------- 1 | getHeaders(); 14 | 15 | $this->assertArrayHasKey('X-Auth-User-Service-Key', $headers); 16 | 17 | $this->assertEquals( 18 | 'v1.0-e24fd090c02efcfecb4de8f4ff246fd5c75b48946fdf0ce26c59f91d0d90797b-cfa33fe60e8e34073c149323454383fc9005d25c9b4c502c2f063457ef65322eade065975001a0b4b4c591c5e1bd36a6e8f7e2d4fa8a9ec01c64c041e99530c2-07b9efe0acd78c82c8d9c690aacb8656d81c369246d7f996a205fe3c18e9254a', 19 | $headers['X-Auth-User-Service-Key'] 20 | ); 21 | 22 | $this->assertCount(1, $headers); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Configurations/ConfigurationsZoneLockdownTest.php: -------------------------------------------------------------------------------- 1 | addIP('1.2.3.4'); 15 | 16 | $array = $configuration->getArray(); 17 | $this->assertCount(1, $array); 18 | 19 | $this->assertArrayHasKey('target', $array[0]); 20 | $this->assertEquals('ip', $array[0]['target']); 21 | $this->assertArrayHasKey('value', $array[0]); 22 | $this->assertEquals('1.2.3.4', $array[0]['value']); 23 | 24 | $configuration->addIPRange('1.2.3.4/24'); 25 | 26 | $array = $configuration->getArray(); 27 | $this->assertCount(2, $array); 28 | 29 | $this->assertArrayHasKey('target', $array[1]); 30 | $this->assertEquals('ip_range', $array[1]['target']); 31 | $this->assertArrayHasKey('value', $array[1]); 32 | $this->assertEquals('1.2.3.4/24', $array[1]['value']); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Endpoints/User.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 22 | } 23 | 24 | public function getUserDetails(): \stdClass 25 | { 26 | $user = $this->adapter->get('user'); 27 | $this->body = json_decode($user->getBody()); 28 | return $this->body->result; 29 | } 30 | 31 | public function getUserID(): string 32 | { 33 | return $this->getUserDetails()->id; 34 | } 35 | 36 | public function getUserEmail(): string 37 | { 38 | return $this->getUserDetails()->email; 39 | } 40 | 41 | public function updateUserDetails(array $details): \stdClass 42 | { 43 | $response = $this->adapter->patch('user', $details); 44 | $this->body = json_decode($response->getBody()); 45 | return $this->body; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Endpoints/AccountRolesTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/listAccountRoles.json'); 14 | 15 | $adapter = $this->getMockBuilder(Adapter::class)->getMock(); 16 | $adapter->method('get')->willReturn($response); 17 | 18 | $adapter->expects($this->once()) 19 | ->method('get') 20 | ->with($this->equalTo('accounts/023e105f4ecef8ad9ca31a8372d0c353/roles')); 21 | 22 | $roles = new AccountRoles($adapter); 23 | $result = $roles->listAccountRoles('023e105f4ecef8ad9ca31a8372d0c353'); 24 | 25 | $this->assertObjectHasAttribute('result', $result); 26 | $this->assertObjectHasAttribute('result_info', $result); 27 | 28 | $this->assertEquals('3536bcfad5faccb999b47003c79917fb', $result->result[0]->id); 29 | $this->assertEquals(1, $result->result_info->page); 30 | $this->assertEquals('3536bcfad5faccb999b47003c79917fb', $roles->getBody()->result[0]->id); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createLoadBalancer.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "699d98642c564d2e855e9661899b7252", 7 | "created_on": "2014-01-01T05:20:00.12345Z", 8 | "modified_on": "2014-01-01T05:20:00.12345Z", 9 | "description": "Load Balancer for www.example.com", 10 | "name": "www.example.com", 11 | "enabled": true, 12 | "ttl": 30, 13 | "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", 14 | "default_pools": [ 15 | "17b5962d775c646f3f9725cbc7a53df4", 16 | "9290f38c5d07c2e2f4df57b1f61d4196", 17 | "00920f38ce07c2e2f4df50b1f61d4194" 18 | ], 19 | "region_pools": { 20 | "WNAM": [ 21 | "de90f38ced07c2e2f4df50b1f61d4194", 22 | "9290f38c5d07c2e2f4df57b1f61d4196" 23 | ], 24 | "ENAM": [ 25 | "00920f38ce07c2e2f4df50b1f61d4194" 26 | ] 27 | }, 28 | "pop_pools": { 29 | "LAX": [ 30 | "de90f38ced07c2e2f4df50b1f61d4194", 31 | "9290f38c5d07c2e2f4df57b1f61d4196" 32 | ], 33 | "LHR": [ 34 | "abd90f38ced07c2e2f4df50b1f61d4194", 35 | "f9138c5d07c2e2f4df57b1f61d4196" 36 | ], 37 | "SJC": [ 38 | "00920f38ce07c2e2f4df50b1f61d4194" 39 | ] 40 | }, 41 | "proxied": true, 42 | "steering_policy": "dynamic_latency", 43 | "session_affinity": "cookie", 44 | "session_affinity_ttl": 5000 45 | } 46 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateLoadBalancer.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "699d98642c564d2e855e9661899b7252", 7 | "created_on": "2014-01-01T05:20:00.12345Z", 8 | "modified_on": "2014-01-01T05:20:00.12345Z", 9 | "description": "Load Balancer for www.example.com", 10 | "name": "www.example.com", 11 | "enabled": true, 12 | "ttl": 30, 13 | "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", 14 | "default_pools": [ 15 | "17b5962d775c646f3f9725cbc7a53df4", 16 | "9290f38c5d07c2e2f4df57b1f61d4196", 17 | "00920f38ce07c2e2f4df50b1f61d4194" 18 | ], 19 | "region_pools": { 20 | "WNAM": [ 21 | "de90f38ced07c2e2f4df50b1f61d4194", 22 | "9290f38c5d07c2e2f4df57b1f61d4196" 23 | ], 24 | "ENAM": [ 25 | "00920f38ce07c2e2f4df50b1f61d4194" 26 | ] 27 | }, 28 | "pop_pools": { 29 | "LAX": [ 30 | "de90f38ced07c2e2f4df50b1f61d4194", 31 | "9290f38c5d07c2e2f4df57b1f61d4196" 32 | ], 33 | "LHR": [ 34 | "abd90f38ced07c2e2f4df50b1f61d4194", 35 | "f9138c5d07c2e2f4df57b1f61d4196" 36 | ], 37 | "SJC": [ 38 | "00920f38ce07c2e2f4df50b1f61d4194" 39 | ] 40 | }, 41 | "proxied": true, 42 | "steering_policy": "dynamic_latency", 43 | "session_affinity": "cookie", 44 | "session_affinity_ttl": 5000 45 | } 46 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getLoadBalancerDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "699d98642c564d2e855e9661899b7252", 7 | "created_on": "2014-01-01T05:20:00.12345Z", 8 | "modified_on": "2014-01-01T05:20:00.12345Z", 9 | "description": "Load Balancer for www.example.com", 10 | "name": "www.example.com", 11 | "enabled": true, 12 | "ttl": 30, 13 | "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", 14 | "default_pools": [ 15 | "17b5962d775c646f3f9725cbc7a53df4", 16 | "9290f38c5d07c2e2f4df57b1f61d4196", 17 | "00920f38ce07c2e2f4df50b1f61d4194" 18 | ], 19 | "region_pools": { 20 | "WNAM": [ 21 | "de90f38ced07c2e2f4df50b1f61d4194", 22 | "9290f38c5d07c2e2f4df57b1f61d4196" 23 | ], 24 | "ENAM": [ 25 | "00920f38ce07c2e2f4df50b1f61d4194" 26 | ] 27 | }, 28 | "pop_pools": { 29 | "LAX": [ 30 | "de90f38ced07c2e2f4df50b1f61d4194", 31 | "9290f38c5d07c2e2f4df57b1f61d4196" 32 | ], 33 | "LHR": [ 34 | "abd90f38ced07c2e2f4df50b1f61d4194", 35 | "f9138c5d07c2e2f4df57b1f61d4196" 36 | ], 37 | "SJC": [ 38 | "00920f38ce07c2e2f4df50b1f61d4194" 39 | ] 40 | }, 41 | "proxied": true, 42 | "steering_policy": "dynamic_latency", 43 | "session_affinity": "cookie", 44 | "session_affinity_ttl": 5000 45 | } 46 | } -------------------------------------------------------------------------------- /src/Endpoints/AccountMembers.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 20 | } 21 | 22 | public function addAccountMember(string $accountId, string $email, array $roles): \stdClass 23 | { 24 | $options = [ 25 | 'email' => $email, 26 | 'roles' => $roles, 27 | ]; 28 | 29 | $account = $this->adapter->post('accounts/' . $accountId . '/members', $options); 30 | $this->body = json_decode($account->getBody()); 31 | 32 | return $this->body->result; 33 | } 34 | 35 | public function listAccountMembers(string $accountId, int $page = 1, int $perPage = 20): \stdClass 36 | { 37 | $query = [ 38 | 'page' => $page, 39 | 'per_page' => $perPage, 40 | ]; 41 | 42 | $zone = $this->adapter->get('accounts/' . $accountId . '/members', $query); 43 | $this->body = json_decode($zone->getBody()); 44 | 45 | return (object)[ 46 | 'result' => $this->body->result, 47 | 'result_info' => $this->body->result_info, 48 | ]; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Endpoints/ZoneSubscriptions.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 21 | } 22 | 23 | public function listZoneSubscriptions(string $zoneId): \stdClass 24 | { 25 | $user = $this->adapter->get('zones/' . $zoneId . '/subscriptions'); 26 | $this->body = json_decode($user->getBody()); 27 | 28 | return (object)[ 29 | 'result' => $this->body->result, 30 | ]; 31 | } 32 | 33 | public function addZoneSubscription(string $zoneId, string $ratePlanId = ''): stdClass 34 | { 35 | $options = []; 36 | 37 | if (empty($ratePlanId) === false) { 38 | $options['rate_plan'] = [ 39 | 'id' => $ratePlanId, 40 | ]; 41 | } 42 | 43 | $existingSubscription = $this->listZoneSubscriptions($zoneId); 44 | $method = empty($existingSubscription->result) ? 'post' : 'put'; 45 | 46 | $subscription = $this->adapter->{$method}('zones/' . $zoneId . '/subscription', $options); 47 | $this->body = json_decode($subscription->getBody()); 48 | 49 | return $this->body->result; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertFileExists($path); 22 | 23 | $stream = Psr7\stream_for(file_get_contents($path)); 24 | 25 | $this->assertInstanceOf(Psr7\Stream::class, $stream); 26 | 27 | return $stream; 28 | } 29 | 30 | /** 31 | * Returns a PSR7 Response (JSON) for a given fixture. 32 | * 33 | * @param string $fixture The fixture to create the response for. 34 | * @param integer $statusCode A HTTP Status Code for the response. 35 | * @return Psr7\Response 36 | */ 37 | protected function getPsr7JsonResponseForFixture($fixture, $statusCode = 200): Psr7\Response 38 | { 39 | $stream = $this->getPsr7StreamForFixture($fixture); 40 | 41 | $this->assertNotNull(json_decode($stream)); 42 | $this->assertEquals(JSON_ERROR_NONE, json_last_error()); 43 | 44 | return new Psr7\Response($statusCode, ['Content-Type' => 'application/json'], $stream); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015-2016, Cloudflare. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without modification, 4 | are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this 7 | list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation and/or 11 | other materials provided with the distribution. 12 | 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software without 15 | specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 21 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 24 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /src/Adapter/ResponseException.php: -------------------------------------------------------------------------------- 1 | hasResponse()) { 24 | return new ResponseException($err->getMessage(), 0, $err); 25 | } 26 | 27 | $response = $err->getResponse(); 28 | $contentType = $response->getHeaderLine('Content-Type'); 29 | 30 | // Attempt to derive detailed error from standard JSON response. 31 | if (strpos($contentType, 'application/json') !== false) { 32 | $json = json_decode($response->getBody()); 33 | if (json_last_error() !== JSON_ERROR_NONE) { 34 | return new ResponseException($err->getMessage(), 0, new JSONException(json_last_error_msg(), 0, $err)); 35 | } 36 | 37 | if (isset($json->errors) && count($json->errors) >= 1) { 38 | return new ResponseException($json->errors[0]->message, $json->errors[0]->code, $err); 39 | } 40 | } 41 | 42 | return new ResponseException($err->getMessage(), 0, $err); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listLoadBalancers.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "699d98642c564d2e855e9661899b7252", 8 | "created_on": "2014-01-01T05:20:00.12345Z", 9 | "modified_on": "2014-01-01T05:20:00.12345Z", 10 | "description": "Load Balancer for www.example.com", 11 | "name": "www.example.com", 12 | "enabled": true, 13 | "ttl": 30, 14 | "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", 15 | "default_pools": [ 16 | "17b5962d775c646f3f9725cbc7a53df4", 17 | "9290f38c5d07c2e2f4df57b1f61d4196", 18 | "00920f38ce07c2e2f4df50b1f61d4194" 19 | ], 20 | "region_pools": { 21 | "WNAM": [ 22 | "de90f38ced07c2e2f4df50b1f61d4194", 23 | "9290f38c5d07c2e2f4df57b1f61d4196" 24 | ], 25 | "ENAM": [ 26 | "00920f38ce07c2e2f4df50b1f61d4194" 27 | ] 28 | }, 29 | "pop_pools": { 30 | "LAX": [ 31 | "de90f38ced07c2e2f4df50b1f61d4194", 32 | "9290f38c5d07c2e2f4df57b1f61d4196" 33 | ], 34 | "LHR": [ 35 | "abd90f38ced07c2e2f4df50b1f61d4194", 36 | "f9138c5d07c2e2f4df57b1f61d4196" 37 | ], 38 | "SJC": [ 39 | "00920f38ce07c2e2f4df50b1f61d4194" 40 | ] 41 | }, 42 | "proxied": true, 43 | "steering_policy": "dynamic_latency", 44 | "session_affinity": "cookie", 45 | "session_affinity_ttl": 5000 46 | } 47 | ], 48 | "result_info": { 49 | "page": 1, 50 | "per_page": 20, 51 | "count": 1, 52 | "total_count": 2000 53 | } 54 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/updateMembershipStatus.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "4536bcfad5faccb111b47003c79917fa", 7 | "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", 8 | "status": "accepted", 9 | "account": { 10 | "id": "01a7362d577a6c3019a474fd6f485823", 11 | "name": "Demo Account", 12 | "settings": { 13 | "enforce_twofactor": false 14 | } 15 | }, 16 | "roles": [ 17 | "Account Administrator" 18 | ], 19 | "permissions": { 20 | "analytics": { 21 | "read": true, 22 | "write": true 23 | }, 24 | "billing": { 25 | "read": true, 26 | "write": true 27 | }, 28 | "cache_purge": { 29 | "read": true, 30 | "write": true 31 | }, 32 | "dns": { 33 | "read": true, 34 | "write": true 35 | }, 36 | "dns_records": { 37 | "read": true, 38 | "write": true 39 | }, 40 | "lb": { 41 | "read": true, 42 | "write": true 43 | }, 44 | "logs": { 45 | "read": true, 46 | "write": true 47 | }, 48 | "organization": { 49 | "read": true, 50 | "write": true 51 | }, 52 | "ssl": { 53 | "read": true, 54 | "write": true 55 | }, 56 | "waf": { 57 | "read": true, 58 | "write": true 59 | }, 60 | "zones": { 61 | "read": true, 62 | "write": true 63 | }, 64 | "zone_settings": { 65 | "read": true, 66 | "write": true 67 | } 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listAccountRoles.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "3536bcfad5faccb999b47003c79917fb", 8 | "name": "Account Administrator", 9 | "description": "Administrative access to the entire Account", 10 | "permissions": { 11 | "analytics": { 12 | "read": true, 13 | "write": true 14 | }, 15 | "billing": { 16 | "read": true, 17 | "write": true 18 | }, 19 | "cache_purge": { 20 | "read": true, 21 | "write": true 22 | }, 23 | "dns": { 24 | "read": true, 25 | "write": true 26 | }, 27 | "dns_records": { 28 | "read": true, 29 | "write": true 30 | }, 31 | "lb": { 32 | "read": true, 33 | "write": true 34 | }, 35 | "logs": { 36 | "read": true, 37 | "write": true 38 | }, 39 | "organization": { 40 | "read": true, 41 | "write": true 42 | }, 43 | "ssl": { 44 | "read": true, 45 | "write": true 46 | }, 47 | "waf": { 48 | "read": true, 49 | "write": true 50 | }, 51 | "zones": { 52 | "read": true, 53 | "write": true 54 | }, 55 | "zone_settings": { 56 | "read": true, 57 | "write": true 58 | } 59 | } 60 | } 61 | ], 62 | "result_info": { 63 | "page": 1, 64 | "per_page": 1, 65 | "total_pages": 1, 66 | "count": 1, 67 | "total_count": 1 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/addZone.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 7 | "name": "example.com", 8 | "development_mode": 7200, 9 | "original_name_servers": [ 10 | "ns1.originaldnshost.com", 11 | "ns2.originaldnshost.com" 12 | ], 13 | "original_registrar": "GoDaddy", 14 | "original_dnshost": "NameCheap", 15 | "created_on": "2014-01-01T05:20:00.12345Z", 16 | "modified_on": "2014-01-01T05:20:00.12345Z", 17 | "name_servers": [ 18 | "tony.ns.cloudflare.com", 19 | "woz.ns.cloudflare.com" 20 | ], 21 | "owner": { 22 | "id": "7c5dae5552338874e5053f2534d2767a", 23 | "email": "user@example.com", 24 | "owner_type": "user" 25 | }, 26 | "account": { 27 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 28 | "name": "Demo Account" 29 | }, 30 | "permissions": [ 31 | "#zone:read", 32 | "#zone:edit" 33 | ], 34 | "plan": { 35 | "id": "e592fd9519420ba7405e1307bff33214", 36 | "name": "Pro Plan", 37 | "price": 20, 38 | "currency": "USD", 39 | "frequency": "monthly", 40 | "legacy_id": "pro", 41 | "is_subscribed": true, 42 | "can_subscribe": true 43 | }, 44 | "plan_pending": { 45 | "id": "e592fd9519420ba7405e1307bff33214", 46 | "name": "Pro Plan", 47 | "price": 20, 48 | "currency": "USD", 49 | "frequency": "monthly", 50 | "legacy_id": "pro", 51 | "is_subscribed": true, 52 | "can_subscribe": true 53 | }, 54 | "status": "active", 55 | "paused": false, 56 | "type": "full" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getZoneById.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 7 | "name": "example.com", 8 | "development_mode": 7200, 9 | "original_name_servers": [ 10 | "ns1.originaldnshost.com", 11 | "ns2.originaldnshost.com" 12 | ], 13 | "original_registrar": "GoDaddy", 14 | "original_dnshost": "NameCheap", 15 | "created_on": "2014-01-01T05:20:00.12345Z", 16 | "modified_on": "2014-01-01T05:20:00.12345Z", 17 | "activated_on": "2014-01-02T00:01:00.12345Z", 18 | "owner": { 19 | "id": "7c5dae5552338874e5053f2534d2767a", 20 | "email": "user@example.com", 21 | "type": "user" 22 | }, 23 | "account": { 24 | "id": "01a7362d577a6c3019a474fd6f485823", 25 | "name": "Demo Account" 26 | }, 27 | "permissions": [ 28 | "#zone:read", 29 | "#zone:edit" 30 | ], 31 | "plan": { 32 | "id": "e592fd9519420ba7405e1307bff33214", 33 | "name": "Pro Plan", 34 | "price": 20, 35 | "currency": "USD", 36 | "frequency": "monthly", 37 | "legacy_id": "pro", 38 | "is_subscribed": true, 39 | "can_subscribe": true 40 | }, 41 | "plan_pending": { 42 | "id": "e592fd9519420ba7405e1307bff33214", 43 | "name": "Pro Plan", 44 | "price": 20, 45 | "currency": "USD", 46 | "frequency": "monthly", 47 | "legacy_id": "pro", 48 | "is_subscribed": true, 49 | "can_subscribe": true 50 | }, 51 | "status": "active", 52 | "paused": false, 53 | "type": "full", 54 | "name_servers": [ 55 | "tony.ns.cloudflare.com", 56 | "woz.ns.cloudflare.com" 57 | ] 58 | } 59 | } -------------------------------------------------------------------------------- /src/Configurations/DNSAnalytics.php: -------------------------------------------------------------------------------- 1 | configs; 12 | } 13 | 14 | public function setDimensions(array $dimensions) 15 | { 16 | if (count($dimensions) !== 0) { 17 | $this->configs['dimensions'] = implode(',', $dimensions); 18 | } 19 | } 20 | 21 | public function setMetrics(array $metrics) 22 | { 23 | if (count($metrics) !== 0) { 24 | $this->configs['metrics'] = implode(',', $metrics); 25 | } 26 | } 27 | 28 | public function setSince(string $since) 29 | { 30 | if ($since) { 31 | $this->configs['since'] = $since; 32 | } 33 | } 34 | 35 | public function setUntil(string $until) 36 | { 37 | if ($until) { 38 | $this->configs['until'] = $until; 39 | } 40 | } 41 | 42 | public function setSorting(array $sorting) 43 | { 44 | if (count($sorting) !== 0) { 45 | $this->configs['sort'] = implode(',', $sorting); 46 | } 47 | } 48 | 49 | public function setFilters(string $filters) 50 | { 51 | if ($filters) { 52 | $this->configs['filters'] = $filters; 53 | } 54 | } 55 | 56 | public function setLimit(int $limit) 57 | { 58 | if ($limit) { 59 | $this->configs['limit'] = $limit; 60 | } 61 | } 62 | 63 | public function setTimeDelta(string $timeDelta) 64 | { 65 | if ($timeDelta) { 66 | $this->configs['time_delta'] = $timeDelta; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Configurations/Certificate.php: -------------------------------------------------------------------------------- 1 | config; 16 | } 17 | 18 | /** 19 | * Array of hostnames or wildcard names (e.g., *.example.com) bound to the certificate 20 | * Example: $hostnames = ["example.com", "foo.example.com"] 21 | * @param array $hostnames 22 | */ 23 | public function setHostnames(array $hostnames) 24 | { 25 | $this->config['hostnames'] = $hostnames; 26 | } 27 | 28 | /** 29 | * The number of days for which the certificate should be valid 30 | * Default value: 5475 31 | * Valid values: 7, 30, 90, 365, 730, 1095, 5475 32 | * @param int $validity 33 | */ 34 | public function setRequestedValidity(int $validity) 35 | { 36 | $this->config['requested_validity'] = $validity; 37 | } 38 | 39 | /** 40 | * Signature type desired on certificate ("origin-rsa" (rsa), "origin-ecc" (ecdsa), or "keyless-certificate" (for Keyless SSL servers) 41 | * Valid values: origin-rsa, origin-ecc, keyless-certificate 42 | * @param string $type 43 | */ 44 | public function setRequestType(string $type) 45 | { 46 | $this->config['request_type'] = $type; 47 | } 48 | 49 | /** 50 | * The Certificate Signing Request (CSR). Must be newline-encoded. 51 | * 52 | * @param string $csr 53 | */ 54 | public function setCsr(string $csr) 55 | { 56 | $this->config['csr'] = $csr; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getZoneId.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 8 | "name": "example.com", 9 | "development_mode": 7200, 10 | "original_name_servers": [ 11 | "ns1.originaldnshost.com", 12 | "ns2.originaldnshost.com" 13 | ], 14 | "original_registrar": "GoDaddy", 15 | "original_dnshost": "NameCheap", 16 | "created_on": "2014-01-01T05:20:00.12345Z", 17 | "modified_on": "2014-01-01T05:20:00.12345Z", 18 | "name_servers": [ 19 | "tony.ns.cloudflare.com", 20 | "woz.ns.cloudflare.com" 21 | ], 22 | "owner": { 23 | "id": "7c5dae5552338874e5053f2534d2767a", 24 | "email": "user@example.com", 25 | "owner_type": "user" 26 | }, 27 | "permissions": [ 28 | "#zone:read", 29 | "#zone:edit" 30 | ], 31 | "plan": { 32 | "id": "e592fd9519420ba7405e1307bff33214", 33 | "name": "Pro Plan", 34 | "price": 20, 35 | "currency": "USD", 36 | "frequency": "monthly", 37 | "legacy_id": "pro", 38 | "is_subscribed": true, 39 | "can_subscribe": true 40 | }, 41 | "plan_pending": { 42 | "id": "e592fd9519420ba7405e1307bff33214", 43 | "name": "Pro Plan", 44 | "price": 20, 45 | "currency": "USD", 46 | "frequency": "monthly", 47 | "legacy_id": "pro", 48 | "is_subscribed": true, 49 | "can_subscribe": true 50 | }, 51 | "status": "active", 52 | "paused": false, 53 | "type": "full" 54 | } 55 | ], 56 | "result_info": { 57 | "page": 1, 58 | "per_page": 20, 59 | "count": 1, 60 | "total_count": 2000 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listZones.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "023e105f4ecef8ad9ca31a8372d0c353", 8 | "name": "example.com", 9 | "development_mode": 7200, 10 | "original_name_servers": [ 11 | "ns1.originaldnshost.com", 12 | "ns2.originaldnshost.com" 13 | ], 14 | "original_registrar": "GoDaddy", 15 | "original_dnshost": "NameCheap", 16 | "created_on": "2014-01-01T05:20:00.12345Z", 17 | "modified_on": "2014-01-01T05:20:00.12345Z", 18 | "name_servers": [ 19 | "tony.ns.cloudflare.com", 20 | "woz.ns.cloudflare.com" 21 | ], 22 | "owner": { 23 | "id": "7c5dae5552338874e5053f2534d2767a", 24 | "email": "user@example.com", 25 | "owner_type": "user" 26 | }, 27 | "permissions": [ 28 | "#zone:read", 29 | "#zone:edit" 30 | ], 31 | "plan": { 32 | "id": "e592fd9519420ba7405e1307bff33214", 33 | "name": "Pro Plan", 34 | "price": 20, 35 | "currency": "USD", 36 | "frequency": "monthly", 37 | "legacy_id": "pro", 38 | "is_subscribed": true, 39 | "can_subscribe": true 40 | }, 41 | "plan_pending": { 42 | "id": "e592fd9519420ba7405e1307bff33214", 43 | "name": "Pro Plan", 44 | "price": 20, 45 | "currency": "USD", 46 | "frequency": "monthly", 47 | "legacy_id": "pro", 48 | "is_subscribed": true, 49 | "can_subscribe": true 50 | }, 51 | "status": "active", 52 | "paused": false, 53 | "type": "full" 54 | } 55 | ], 56 | "result_info": { 57 | "page": 1, 58 | "per_page": 20, 59 | "count": 1, 60 | "total_count": 2000 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/getMembershipDetails.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "4536bcfad5faccb111b47003c79917fa", 7 | "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", 8 | "status": "accepted", 9 | "account": { 10 | "id": "01a7362d577a6c3019a474fd6f485823", 11 | "name": "Demo Account", 12 | "settings": { 13 | "enforce_twofactor": false 14 | } 15 | }, 16 | "roles": [ 17 | "Account Administrator" 18 | ], 19 | "permissions": { 20 | "analytics": { 21 | "read": true, 22 | "write": true 23 | }, 24 | "billing": { 25 | "read": true, 26 | "write": true 27 | }, 28 | "cache_purge": { 29 | "read": true, 30 | "write": true 31 | }, 32 | "dns": { 33 | "read": true, 34 | "write": true 35 | }, 36 | "dns_records": { 37 | "read": true, 38 | "write": true 39 | }, 40 | "lb": { 41 | "read": true, 42 | "write": true 43 | }, 44 | "logs": { 45 | "read": true, 46 | "write": true 47 | }, 48 | "organization": { 49 | "read": true, 50 | "write": true 51 | }, 52 | "ssl": { 53 | "read": true, 54 | "write": true 55 | }, 56 | "waf": { 57 | "read": true, 58 | "write": true 59 | }, 60 | "zones": { 61 | "read": true, 62 | "write": true 63 | }, 64 | "zone_settings": { 65 | "read": true, 66 | "write": true 67 | } 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /tests/Configurations/PoolTest.php: -------------------------------------------------------------------------------- 1 | 4 | * User: HemeraOne 5 | * Date: 13/05/2019 6 | */ 7 | 8 | use Cloudflare\API\Configurations\ConfigurationsException; 9 | use Cloudflare\API\Configurations\Pool; 10 | 11 | class PoolTest extends TestCase 12 | { 13 | /** 14 | * @dataProvider testArgumentsDataProvider 15 | */ 16 | public function testArguments($setFunction, $arguments, $getFunction, $invalid) 17 | { 18 | $pool = new Pool('bogus', []); 19 | foreach ($arguments as $argument) { 20 | if ($invalid) { 21 | try { 22 | $pool->{$setFunction}($argument); 23 | } catch (ConfigurationsException $e) { 24 | $this->assertNotEquals($argument, $pool->{$getFunction}()); 25 | } 26 | } elseif ($invalid === false) { 27 | $pool->{$setFunction}($argument); 28 | $this->assertEquals($argument, $pool->{$getFunction}()); 29 | } 30 | } 31 | } 32 | 33 | public function testArgumentsDataProvider() 34 | { 35 | return [ 36 | 'origins arguments valid' => [ 37 | 'setOrigins', [[['name' => 'test', 'address' => 'server1.example.com']]], 'getOrigins', false 38 | ], 39 | 'setNotificationEmail arguments valid' => [ 40 | 'setNotificationEmail', ['user@example.com'], 'getNotificationEmail', false 41 | ], 42 | 'origins arguments invalid no address' => [ 43 | 'setOrigins', [['name' => 'test']], 'getOrigins', true 44 | ], 45 | 'origins arguments invalid no name' => [ 46 | 'setOrigins', [['address' => 'server1.example.com']], 'getOrigins', true 47 | ], 48 | 'setNotificationEmail arguments invalid' => [ 49 | 'setNotificationEmail', ['userexample.com'], 'getNotificationEmail', true 50 | ] 51 | ]; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/createAccountMember.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": { 6 | "id": "4536bcfad5faccb111b47003c79917fa", 7 | "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", 8 | "user": { 9 | "id": "7c5dae5552338874e5053f2534d2767a", 10 | "first_name": "John", 11 | "last_name": "Appleseed", 12 | "email": "user@example.com", 13 | "two_factor_authentication_enabled": false 14 | }, 15 | "status": "accepted", 16 | "roles": [ 17 | { 18 | "id": "3536bcfad5faccb999b47003c79917fb", 19 | "name": "Account Administrator", 20 | "description": "Administrative access to the entire Account", 21 | "permissions": { 22 | "analytics": { 23 | "read": true, 24 | "write": true 25 | }, 26 | "billing": { 27 | "read": true, 28 | "write": true 29 | }, 30 | "cache_purge": { 31 | "read": true, 32 | "write": true 33 | }, 34 | "dns": { 35 | "read": true, 36 | "write": true 37 | }, 38 | "dns_records": { 39 | "read": true, 40 | "write": true 41 | }, 42 | "lb": { 43 | "read": true, 44 | "write": true 45 | }, 46 | "logs": { 47 | "read": true, 48 | "write": true 49 | }, 50 | "organization": { 51 | "read": true, 52 | "write": true 53 | }, 54 | "ssl": { 55 | "read": true, 56 | "write": true 57 | }, 58 | "waf": { 59 | "read": true, 60 | "write": true 61 | }, 62 | "zones": { 63 | "read": true, 64 | "write": true 65 | }, 66 | "zone_settings": { 67 | "read": true, 68 | "write": true 69 | } 70 | } 71 | } 72 | ] 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Adapter/Adapter.php: -------------------------------------------------------------------------------- 1 | 4 | * User: HemeraOne 5 | * Date: 13/05/2019 6 | */ 7 | 8 | use Cloudflare\API\Configurations\ConfigurationsException; 9 | use Cloudflare\API\Configurations\LoadBalancer; 10 | 11 | class LoadBalancerTest extends TestCase 12 | { 13 | /** 14 | * @dataProvider testArgumentsDataProvider 15 | */ 16 | public function testArguments($setFunction, $arguments, $getFunction, $invalid) 17 | { 18 | $loadBalancer = new LoadBalancer('bogus', [], 'bogus'); 19 | foreach ($arguments as $argument) { 20 | if ($invalid === true) { 21 | try { 22 | $loadBalancer->{$setFunction}($argument); 23 | } catch (ConfigurationsException $e) { 24 | $this->assertNotEquals($argument, $loadBalancer->{$getFunction}()); 25 | } 26 | } elseif ($invalid === false) { 27 | $loadBalancer->{$setFunction}($argument); 28 | $this->assertEquals($argument, $loadBalancer->{$getFunction}()); 29 | } 30 | } 31 | } 32 | 33 | public function testArgumentsDataProvider() 34 | { 35 | return [ 36 | 'steeringPolicy arguments valid' => [ 37 | 'setSteeringPolicy', ['off', 'geo', 'random', 'dynamic_latency', ''], 'getSteeringPolicy', false 38 | ], 39 | 'sessionAffinity arguments valid' => [ 40 | 'setSessionAffinity', ['none', 'cookie', 'ip_cookie', ''], 'getSessionAffinity', false 41 | ], 42 | 'sessionAffinityTtl arguments valid' => [ 43 | 'setSessionAffinityTtl', [3600], 'getSessionAffinityTtl', false 44 | ], 45 | 'steeringPolicy arguments invalid' => [ 46 | 'setSteeringPolicy', ['invalid'], 'getSteeringPolicy', true 47 | ], 48 | 'sessionAffinity arguments invalid' => [ 49 | 'setSessionAffinity', ['invalid'], 'getSessionAffinity', true 50 | ], 51 | 'sessionAffinityTtl arguments invalid' => [ 52 | 'setSessionAffinityTtl', [1337], 'getSessionAffinityTtl', true 53 | ], 54 | ]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/Fixtures/Endpoints/listAccountMembers.json: -------------------------------------------------------------------------------- 1 | { 2 | "success": true, 3 | "errors": [], 4 | "messages": [], 5 | "result": [ 6 | { 7 | "id": "4536bcfad5faccb111b47003c79917fa", 8 | "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", 9 | "user": { 10 | "id": "7c5dae5552338874e5053f2534d2767a", 11 | "first_name": "John", 12 | "last_name": "Appleseed", 13 | "email": "user@example.com", 14 | "two_factor_authentication_enabled": false 15 | }, 16 | "status": "accepted", 17 | "roles": [ 18 | { 19 | "id": "3536bcfad5faccb999b47003c79917fb", 20 | "name": "Account Administrator", 21 | "description": "Administrative access to the entire Account", 22 | "permissions": { 23 | "analytics": { 24 | "read": true, 25 | "write": true 26 | }, 27 | "billing": { 28 | "read": true, 29 | "write": true 30 | }, 31 | "cache_purge": { 32 | "read": true, 33 | "write": true 34 | }, 35 | "dns": { 36 | "read": true, 37 | "write": true 38 | }, 39 | "dns_records": { 40 | "read": true, 41 | "write": true 42 | }, 43 | "lb": { 44 | "read": true, 45 | "write": true 46 | }, 47 | "logs": { 48 | "read": true, 49 | "write": true 50 | }, 51 | "organization": { 52 | "read": true, 53 | "write": true 54 | }, 55 | "ssl": { 56 | "read": true, 57 | "write": true 58 | }, 59 | "waf": { 60 | "read": true, 61 | "write": true 62 | }, 63 | "zones": { 64 | "read": true, 65 | "write": true 66 | }, 67 | "zone_settings": { 68 | "read": true, 69 | "write": true 70 | } 71 | } 72 | } 73 | ] 74 | } 75 | ], 76 | "result_info": { 77 | "page": 1, 78 | "per_page": 20, 79 | "count": 1, 80 | "total_count": 200 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Endpoints/AccountMembersTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/createAccountMember.json'); 11 | 12 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 13 | $mock->method('post')->willReturn($response); 14 | 15 | $mock->expects($this->once()) 16 | ->method('post') 17 | ->with( 18 | $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/members'), 19 | $this->equalTo([ 20 | 'email' => 'user@example.com', 21 | 'roles' => [ 22 | '3536bcfad5faccb999b47003c79917fb', 23 | ], 24 | ]) 25 | ); 26 | 27 | $accountMembers = new AccountMembers($mock); 28 | $accountMembers->addAccountMember('01a7362d577a6c3019a474fd6f485823', 'user@example.com', ['3536bcfad5faccb999b47003c79917fb']); 29 | 30 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $accountMembers->getBody()->result->id); 31 | } 32 | 33 | public function testListAccountMembers() 34 | { 35 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/listAccountMembers.json'); 36 | 37 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 38 | $mock->method('get')->willReturn($response); 39 | 40 | $mock->expects($this->once()) 41 | ->method('get') 42 | ->with( 43 | $this->equalTo('accounts/023e105f4ecef8ad9ca31a8372d0c353/members'), 44 | $this->equalTo([ 45 | 'page' => 1, 46 | 'per_page' => 20, 47 | ]) 48 | ); 49 | 50 | $accountMembers = new AccountMembers($mock); 51 | $result = $accountMembers->listAccountMembers('023e105f4ecef8ad9ca31a8372d0c353', 1, 20); 52 | 53 | $this->assertObjectHasAttribute('result', $result); 54 | 55 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $result->result[0]->id); 56 | $this->assertEquals(1, $result->result_info->count); 57 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $accountMembers->getBody()->result[0]->id); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cloudflare SDK (v4 API Binding for PHP 7) 2 | 3 | [![Build Status](https://travis-ci.org/cloudflare/cloudflare-php.svg?branch=master)](https://travis-ci.org/cloudflare/cloudflare-php) 4 | 5 | ## Installation 6 | 7 | The recommended way to install this package is via the Packagist Dependency Manager ([cloudflare/sdk](https://packagist.org/packages/cloudflare/sdk)). You can get specific usage examples on the Cloudflare Knowledge Base under: [Cloudflare PHP API Binding](https://support.cloudflare.com/hc/en-us/articles/115001661191) 8 | 9 | ## Cloudflare API version 4 10 | 11 | The Cloudflare API can be found [here](https://api.cloudflare.com/). 12 | Each API call is provided via a similarly named function within various classes in the **Cloudflare\API\Endpoints** namespace: 13 | 14 | - [x] [DNS Records](https://www.cloudflare.com/dns/) 15 | - [x] [DNS Analytics](https://api.cloudflare.com/#dns-analytics-table) 16 | - [x] Zones 17 | - [x] User Administration (partial) 18 | - [x] [Cloudflare IPs](https://www.cloudflare.com/ips/) 19 | - [x] [Page Rules](https://support.cloudflare.com/hc/en-us/articles/200168306-Is-there-a-tutorial-for-Page-Rules-) 20 | - [x] [Web Application Firewall (WAF)](https://www.cloudflare.com/waf/) 21 | - [ ] Virtual DNS Management 22 | - [x] Custom hostnames 23 | - [x] Manage TLS settings 24 | - [x] Zone Lockdown and User-Agent Block rules 25 | - [ ] Organization Administration 26 | - [x] [Railgun](https://www.cloudflare.com/railgun/) administration 27 | - [ ] [Keyless SSL](https://blog.cloudflare.com/keyless-ssl-the-nitty-gritty-technical-details/) 28 | - [x] [Origin CA](https://blog.cloudflare.com/universal-ssl-encryption-all-the-way-to-the-origin-for-free/) 29 | - [x] Crypto 30 | - [x] Load Balancers 31 | - [x] Firewall Settings 32 | 33 | Note that this repository is currently under development, additional classes and endpoints being actively added. 34 | 35 | ## Getting Started 36 | 37 | ```php 38 | $key = new Cloudflare\API\Auth\APIKey('user@example.com', 'apiKey'); 39 | $adapter = new Cloudflare\API\Adapter\Guzzle($key); 40 | $user = new Cloudflare\API\Endpoints\User($adapter); 41 | 42 | echo $user->getUserID(); 43 | ``` 44 | 45 | ## Contributions 46 | 47 | We welcome community contribution to this repository. [CONTRIBUTING.md](CONTRIBUTING.md) will help you start contributing. 48 | 49 | ## Licensing 50 | 51 | Licensed under the 3-clause BSD license. See the [LICENSE](LICENSE) file for details. 52 | -------------------------------------------------------------------------------- /tests/Endpoints/ZoneSettingsTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/getServerSideExclude.json'); 13 | 14 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 15 | $mock->method('get')->willReturn($response); 16 | 17 | $mock->expects($this->once())->method('get'); 18 | 19 | $zones = new ZoneSettings($mock); 20 | $result = $zones->getServerSideExcludeSetting('023e105f4ecef8ad9ca31a8372d0c353'); 21 | 22 | $this->assertSame('on', $result); 23 | } 24 | 25 | public function testUpdateServerSideExcludeSetting() 26 | { 27 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateServerSideExclude.json'); 28 | 29 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 30 | $mock->method('patch')->willReturn($response); 31 | 32 | $mock->expects($this->once())->method('patch'); 33 | 34 | $zones = new ZoneSettings($mock); 35 | $result = $zones->updateServerSideExcludeSetting('023e105f4ecef8ad9ca31a8372d0c353', 'on'); 36 | 37 | $this->assertSame('on', $result); 38 | } 39 | 40 | public function testGetBrowserCacheTtlSetting() 41 | { 42 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/getBrowserCacheTtlSetting.json'); 43 | 44 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 45 | $mock->method('get')->willReturn($response); 46 | 47 | $mock->expects($this->once())->method('get'); 48 | 49 | $zones = new ZoneSettings($mock); 50 | $result = $zones->getBrowserCacheTtlSetting('023e105f4ecef8ad9ca31a8372d0c353'); 51 | 52 | $this->assertSame(14400, $result); 53 | } 54 | 55 | public function testUpdateBrowserCacheTtlSetting() 56 | { 57 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateBrowserCacheTtlSetting.json'); 58 | 59 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 60 | $mock->method('patch')->willReturn($response); 61 | 62 | $mock->expects($this->once())->method('patch'); 63 | 64 | $zones = new ZoneSettings($mock); 65 | $result = $zones->updateBrowserCacheTtlSetting('023e105f4ecef8ad9ca31a8372d0c353', 16070400); 66 | 67 | $this->assertTrue($result); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Endpoints/Certificates.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 18 | } 19 | 20 | /** 21 | * List all existing Origin CA certificates for a given zone 22 | * 23 | * @param string $zoneID 24 | * @return array 25 | */ 26 | public function listCertificates(string $zoneID): \stdClass 27 | { 28 | $certificates = $this->adapter->get('certificates', ['zone_id' => $zoneID]); 29 | $this->body = json_decode($certificates->getBody()); 30 | 31 | return (object)['result' => $this->body->result]; 32 | } 33 | 34 | /** 35 | * Get an existing Origin CA certificate by its serial number 36 | * 37 | * @param string $certificateID 38 | * @param string $zoneID 39 | * @return mixed 40 | */ 41 | public function getCertificate(string $certificateID, string $zoneID) 42 | { 43 | $certificates = $this->adapter->get('certificates/'.$certificateID, ['zone_id' => $zoneID]); 44 | $this->body = json_decode($certificates->getBody()); 45 | 46 | return (object)['result' => $this->body->result]; 47 | } 48 | 49 | /** 50 | * Revoke an existing Origin CA certificate by its serial number 51 | * 52 | * @param string $certificateID 53 | * @param string $zoneID 54 | * @return bool 55 | */ 56 | public function revokeCertificate(string $certificateID, string $zoneID): bool 57 | { 58 | $certificates = $this->adapter->delete('certificates/'.$certificateID, ['zone_id' => $zoneID]); 59 | $this->body = json_decode($certificates->getBody()); 60 | 61 | if (isset($this->body->result->id)) { 62 | return true; 63 | } 64 | 65 | return false; 66 | } 67 | 68 | /** 69 | * Create an Origin CA certificate 70 | * 71 | * @param CertificateConfig $config 72 | * @return bool 73 | */ 74 | public function createCertificate(CertificateConfig $config): bool 75 | { 76 | $certificate = $this->adapter->post('certificates', $config->getArray()); 77 | 78 | $this->body = json_decode($certificate->getBody()); 79 | 80 | if (isset($this->body->result->id)) { 81 | return true; 82 | } 83 | 84 | return false; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Endpoints/Membership.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 19 | } 20 | 21 | 22 | public function listMemberships( 23 | string $name = '', 24 | string $status = '', 25 | int $page = 1, 26 | int $perPage = 20, 27 | string $order = '', 28 | string $direction = '' 29 | ): \stdClass { 30 | $query = [ 31 | 'page' => $page, 32 | 'per_page' => $perPage 33 | ]; 34 | 35 | if (!empty($name)) { 36 | $query['account.name'] = $name; 37 | } 38 | 39 | if (!empty($status) && in_array($status, ['accepted', 'pending', 'rejected'], true)) { 40 | $query['status'] = $status; 41 | } 42 | 43 | if (!empty($order) && in_array($order, ['id', 'account.name', 'status'], true)) { 44 | $query['order'] = $order; 45 | } 46 | 47 | if (!empty($direction) && in_array($direction, ['asc', 'desc'], true)) { 48 | $query['direction'] = $direction; 49 | } 50 | 51 | $memberships = $this->adapter->get('memberships', $query); 52 | $this->body = json_decode($memberships->getBody()); 53 | 54 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 55 | } 56 | 57 | public function getMembershipDetails(string $membershipId): \stdClass 58 | { 59 | $membership = $this->adapter->get(sprintf('memberships/%s', $membershipId)); 60 | $this->body = json_decode($membership->getBody()); 61 | return $this->body->result; 62 | } 63 | 64 | public function updateMembershipStatus(string $membershipId, string $status): \stdClass 65 | { 66 | $response = $this->adapter->put(sprintf('memberships/%s', $membershipId), ['status' => $status]); 67 | $this->body = json_decode($response->getBody()); 68 | return $this->body; 69 | } 70 | 71 | public function deleteMembership(string $membershipId): bool 72 | { 73 | $response = $this->adapter->delete(sprintf('memberships/%s', $membershipId)); 74 | 75 | $this->body = json_decode($response->getBody()); 76 | 77 | if (isset($this->body->result->id)) { 78 | return true; 79 | } 80 | 81 | return false; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Adapter/Guzzle.php: -------------------------------------------------------------------------------- 1 | getHeaders(); 24 | 25 | $this->client = new Client([ 26 | 'base_uri' => $baseURI, 27 | 'headers' => $headers, 28 | 'Accept' => 'application/json' 29 | ]); 30 | } 31 | 32 | 33 | /** 34 | * @inheritDoc 35 | */ 36 | public function get(string $uri, array $data = [], array $headers = []): ResponseInterface 37 | { 38 | return $this->request('get', $uri, $data, $headers); 39 | } 40 | 41 | /** 42 | * @inheritDoc 43 | */ 44 | public function post(string $uri, array $data = [], array $headers = []): ResponseInterface 45 | { 46 | return $this->request('post', $uri, $data, $headers); 47 | } 48 | 49 | /** 50 | * @inheritDoc 51 | */ 52 | public function put(string $uri, array $data = [], array $headers = []): ResponseInterface 53 | { 54 | return $this->request('put', $uri, $data, $headers); 55 | } 56 | 57 | /** 58 | * @inheritDoc 59 | */ 60 | public function patch(string $uri, array $data = [], array $headers = []): ResponseInterface 61 | { 62 | return $this->request('patch', $uri, $data, $headers); 63 | } 64 | 65 | /** 66 | * @inheritDoc 67 | */ 68 | public function delete(string $uri, array $data = [], array $headers = []): ResponseInterface 69 | { 70 | return $this->request('delete', $uri, $data, $headers); 71 | } 72 | 73 | /** 74 | * @SuppressWarnings(PHPMD.StaticAccess) 75 | */ 76 | public function request(string $method, string $uri, array $data = [], array $headers = []) 77 | { 78 | if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { 79 | throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete'); 80 | } 81 | 82 | try { 83 | $response = $this->client->$method($uri, [ 84 | 'headers' => $headers, 85 | ($method === 'get' ? 'query' : 'json') => $data, 86 | ]); 87 | } catch (RequestException $err) { 88 | throw ResponseException::fromRequestException($err); 89 | } 90 | 91 | return $response; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/Endpoints/Accounts.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 22 | } 23 | 24 | public function addAccount(string $name, string $type = 'standard'): \stdClass 25 | { 26 | $options = [ 27 | 'name' => $name, 28 | 'type' => $type, 29 | ]; 30 | 31 | $account = $this->adapter->post('accounts', $options); 32 | $this->body = json_decode($account->getBody()); 33 | 34 | return $this->body->result; 35 | } 36 | 37 | public function listAccounts( 38 | int $page = 1, 39 | int $perPage = 20, 40 | string $direction = '' 41 | ): \stdClass { 42 | $query = [ 43 | 'page' => $page, 44 | 'per_page' => $perPage 45 | ]; 46 | 47 | if (!empty($direction)) { 48 | $query['direction'] = $direction; 49 | } 50 | 51 | $user = $this->adapter->get('accounts', $query); 52 | $this->body = json_decode($user->getBody()); 53 | 54 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 55 | } 56 | 57 | public function getDomains(string $accountID): array 58 | { 59 | $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains'); 60 | 61 | $this->body = json_decode($response->getBody()); 62 | 63 | return $this->body->result; 64 | } 65 | 66 | public function getDomainDetails(string $accountID, string $domainName): \stdClass 67 | { 68 | $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains/' . $domainName); 69 | 70 | $this->body = json_decode($response->getBody()); 71 | 72 | return $this->body->result; 73 | } 74 | 75 | public function lockDomain(string $accountID, string $domainName): \stdClass 76 | { 77 | $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ['locked' => true]); 78 | $this->body = json_decode($response->getBody()); 79 | return $this->body; 80 | } 81 | 82 | public function unlockDomain(string $accountID, string $domainName): \stdClass 83 | { 84 | $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ['locked' => false]); 85 | $this->body = json_decode($response->getBody()); 86 | return $this->body; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /tests/Endpoints/UserTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/getUserDetails.json'); 13 | 14 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 15 | $mock->method('get')->willReturn($response); 16 | 17 | $user = new \Cloudflare\API\Endpoints\User($mock); 18 | $details = $user->getUserDetails(); 19 | 20 | $this->assertObjectHasAttribute('id', $details); 21 | $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $details->id); 22 | $this->assertObjectHasAttribute('email', $details); 23 | $this->assertEquals('user@example.com', $details->email); 24 | $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id); 25 | } 26 | 27 | public function testGetUserID() 28 | { 29 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/getUserId.json'); 30 | 31 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 32 | $mock->method('get')->willReturn($response); 33 | 34 | $user = new \Cloudflare\API\Endpoints\User($mock); 35 | $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getUserID()); 36 | $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id); 37 | } 38 | 39 | public function testGetUserEmail() 40 | { 41 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/getUserEmail.json'); 42 | 43 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 44 | $mock->method('get')->willReturn($response); 45 | 46 | $mock->expects($this->once())->method('get'); 47 | 48 | $user = new \Cloudflare\API\Endpoints\User($mock); 49 | $this->assertEquals('user@example.com', $user->getUserEmail()); 50 | $this->assertEquals('user@example.com', $user->getBody()->result->email); 51 | } 52 | 53 | public function testUpdateUserDetails() 54 | { 55 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateUserDetails.json'); 56 | 57 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 58 | $mock->method('patch')->willReturn($response); 59 | 60 | $mock->expects($this->once()) 61 | ->method('patch') 62 | ->with($this->equalTo('user'), $this->equalTo(['email' => 'user2@example.com'])); 63 | 64 | $user = new \Cloudflare\API\Endpoints\User($mock); 65 | $user->updateUserDetails(['email' => 'user2@example.com']); 66 | $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Endpoints/Railgun.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 23 | } 24 | 25 | public function create( 26 | string $name 27 | ): \stdClass { 28 | $query = [ 29 | 'name' => $name, 30 | ]; 31 | 32 | $user = $this->adapter->post('railguns', $query); 33 | $this->body = json_decode($user->getBody()); 34 | 35 | return $this->body; 36 | } 37 | 38 | public function list( 39 | int $page = 1, 40 | int $perPage = 20, 41 | string $direction = '' 42 | ): \stdClass { 43 | $query = [ 44 | 'page' => $page, 45 | 'per_page' => $perPage 46 | ]; 47 | 48 | if (!empty($direction)) { 49 | $query['direction'] = $direction; 50 | } 51 | 52 | $user = $this->adapter->get('railguns', $query); 53 | $this->body = json_decode($user->getBody()); 54 | 55 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 56 | } 57 | 58 | public function get( 59 | string $railgunID 60 | ): \stdClass { 61 | $user = $this->adapter->get('railguns/' . $railgunID); 62 | $this->body = json_decode($user->getBody()); 63 | 64 | return $this->body->result; 65 | } 66 | 67 | public function getZones( 68 | string $railgunID 69 | ): \stdClass { 70 | $user = $this->adapter->get('railguns/' . $railgunID . '/zones'); 71 | $this->body = json_decode($user->getBody()); 72 | 73 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 74 | } 75 | 76 | public function update( 77 | string $railgunID, 78 | bool $status 79 | ): \stdClass { 80 | $query = [ 81 | 'enabled' => $status 82 | ]; 83 | 84 | $user = $this->adapter->patch('railguns/' . $railgunID, $query); 85 | $this->body = json_decode($user->getBody()); 86 | 87 | return $this->body->result; 88 | } 89 | 90 | public function delete( 91 | string $railgunID 92 | ): bool { 93 | $user = $this->adapter->delete('railguns/' . $railgunID); 94 | $this->body = json_decode($user->getBody()); 95 | 96 | if (isset($this->body->result->id)) { 97 | return true; 98 | } 99 | 100 | return false; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/Endpoints/Crypto.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 14 | } 15 | 16 | /** 17 | * Get the Opportunistic Encryption feature for a zone. 18 | * 19 | * @param string $zoneID The ID of the zone 20 | * @return string|false 21 | */ 22 | public function getOpportunisticEncryptionSetting(string $zoneID) 23 | { 24 | $return = $this->adapter->get( 25 | 'zones/' . $zoneID . '/settings/opportunistic_encryption' 26 | ); 27 | $body = json_decode($return->getBody()); 28 | if (isset($body->result)) { 29 | return $body->result->value; 30 | } 31 | return false; 32 | } 33 | 34 | /** 35 | * Get the Onion Routing feature for a zone. 36 | * 37 | * @param string $zoneID The ID of the zone 38 | * @return string|false 39 | */ 40 | public function getOnionRoutingSetting(string $zoneID) 41 | { 42 | $return = $this->adapter->get( 43 | 'zones/' . $zoneID . '/settings/opportunistic_onion' 44 | ); 45 | $body = json_decode($return->getBody()); 46 | if (isset($body->result)) { 47 | return $body->result; 48 | } 49 | return false; 50 | } 51 | 52 | /** 53 | * Update the Oppurtunistic Encryption setting for the zone 54 | * 55 | * @param string $zoneID The ID of the zone 56 | * @param string $value The value of the zone setting 57 | * @return bool 58 | */ 59 | public function updateOpportunisticEncryptionSetting(string $zoneID, string $value) 60 | { 61 | $return = $this->adapter->patch( 62 | 'zones/' . $zoneID . '/settings/opportunistic_encryption', 63 | [ 64 | 'value' => $value, 65 | ] 66 | ); 67 | $body = json_decode($return->getBody()); 68 | if (isset($body->success) && $body->success == true) { 69 | return true; 70 | } 71 | return false; 72 | } 73 | 74 | /** 75 | * Update the Onion Routing setting for the zone 76 | * 77 | * @param string $zoneID The ID of the zone 78 | * @param string $value The value of the zone setting 79 | * @return bool 80 | */ 81 | public function updateOnionRoutingSetting(string $zoneID, string $value) 82 | { 83 | $return = $this->adapter->patch( 84 | 'zones/' . $zoneID . '/settings/opportunistic_onion', 85 | [ 86 | 'value' => $value, 87 | ] 88 | ); 89 | $body = json_decode($return->getBody()); 90 | if (isset($body->success) && $body->success == true) { 91 | return true; 92 | } 93 | return false; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/Endpoints/DNSAnalyticsTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture( 14 | 'Endpoints/getDNSAnalyticsReportTable.json' 15 | ); 16 | 17 | $mock = $this->getMockBuilder( 18 | \Cloudflare\API\Adapter\Adapter::class 19 | )->getMock(); 20 | $mock->method('get')->willReturn($response); 21 | 22 | $mock 23 | ->expects($this->once()) 24 | ->method('get') 25 | ->with( 26 | $this->equalTo( 27 | 'zones/023e105f4ecef8ad9ca31a8372d0c353/dns_analytics/report' 28 | ) 29 | ); 30 | 31 | $analytics = new \Cloudflare\API\Endpoints\DNSAnalytics($mock); 32 | $since = '2020-02-01T00:00:00Z'; 33 | $until = '2020-02-28T23:59:59Z'; 34 | $filters = 'responseCode==NOERROR AND queryType==A'; 35 | 36 | $result = $analytics->getReportTable( 37 | '023e105f4ecef8ad9ca31a8372d0c353', 38 | ['queryName', 'queryType', 'responseCode'], 39 | ['queryCount'], 40 | ['-queryCount'], 41 | $filters, 42 | $since, 43 | $until 44 | ); 45 | 46 | $this->assertEquals(1, $result->rows); 47 | $this->assertEquals($since, $result->query->since); 48 | $this->assertEquals($until, $result->query->until); 49 | } 50 | 51 | public function testGetDNSAnalyticsReportByTime() 52 | { 53 | $response = $this->getPsr7JsonResponseForFixture( 54 | 'Endpoints/getDNSAnalyticsReportByTime.json' 55 | ); 56 | 57 | $mock = $this->getMockBuilder( 58 | \Cloudflare\API\Adapter\Adapter::class 59 | )->getMock(); 60 | $mock->method('get')->willReturn($response); 61 | 62 | $mock 63 | ->expects($this->once()) 64 | ->method('get') 65 | ->with( 66 | $this->equalTo( 67 | 'zones/023e105f4ecef8ad9ca31a8372d0c353/dns_analytics/report/bytime' 68 | ) 69 | ); 70 | 71 | $analytics = new \Cloudflare\API\Endpoints\DNSAnalytics($mock); 72 | $since = '2020-02-01T00:00:00Z'; 73 | $until = '2020-02-28T23:59:59Z'; 74 | $filters = 'responseCode==NOERROR AND queryType==A'; 75 | 76 | $result = $analytics->getReportByTime( 77 | '023e105f4ecef8ad9ca31a8372d0c353', 78 | ['queryName', 'queryType', 'responseCode'], 79 | ['queryCount'], 80 | ['-queryCount'], 81 | $filters, 82 | $since, 83 | $until, 84 | 2 85 | ); 86 | 87 | $this->assertEquals(2, $result->rows); 88 | $this->assertEquals($since, $result->query->since); 89 | $this->assertEquals($until, $result->query->until); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /tests/Endpoints/AccountsTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/listAccounts.json'); 15 | 16 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 17 | $mock->method('get')->willReturn($response); 18 | 19 | $mock->expects($this->once()) 20 | ->method('get') 21 | ->with( 22 | $this->equalTo('accounts'), 23 | $this->equalTo([ 24 | 'page' => 1, 25 | 'per_page' => 20, 26 | 'direction' => 'desc', 27 | ]) 28 | ); 29 | 30 | $accounts = new Accounts($mock); 31 | $result = $accounts->listAccounts(1, 20, 'desc'); 32 | 33 | $this->assertObjectHasAttribute('result', $result); 34 | $this->assertObjectHasAttribute('result_info', $result); 35 | 36 | $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->result[0]->id); 37 | $this->assertEquals(1, $result->result_info->page); 38 | $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $accounts->getBody()->result[0]->id); 39 | } 40 | 41 | public function testAddAccountWithDefaultType() 42 | { 43 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/createStandardAccount.json'); 44 | 45 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 46 | $mock->method('post')->willReturn($response); 47 | 48 | $mock->expects($this->once()) 49 | ->method('post') 50 | ->with( 51 | $this->equalTo('accounts'), 52 | $this->equalTo([ 53 | 'name' => 'Foo Bar', 54 | 'type' => 'standard', 55 | ]) 56 | ); 57 | 58 | $accounts = new Accounts($mock); 59 | 60 | $accounts->addAccount('Foo Bar'); 61 | $this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id); 62 | } 63 | 64 | public function testAddAccountWithCustomType() 65 | { 66 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/createEnterpriseAccount.json'); 67 | 68 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 69 | $mock->method('post')->willReturn($response); 70 | 71 | $mock->expects($this->once()) 72 | ->method('post') 73 | ->with( 74 | $this->equalTo('accounts'), 75 | $this->equalTo([ 76 | 'name' => 'Foo Bar', 77 | 'type' => 'enterprise', 78 | ]) 79 | ); 80 | 81 | $accounts = new Accounts($mock); 82 | 83 | $accounts->addAccount('Foo Bar', 'enterprise'); 84 | $this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Configurations/Pool.php: -------------------------------------------------------------------------------- 1 | 4 | * User: HemeraOne 5 | * Date: 13/05/2019 6 | */ 7 | 8 | namespace Cloudflare\API\Configurations; 9 | 10 | class Pool implements Configurations 11 | { 12 | private $configs = []; 13 | 14 | public function __construct(string $name, array $origins) 15 | { 16 | $this->setName($name); 17 | $this->setOrigins($origins); 18 | } 19 | 20 | public function setName(string $name) 21 | { 22 | $this->configs['name'] = $name; 23 | } 24 | 25 | public function getName():string 26 | { 27 | return $this->configs['name'] ?? ''; 28 | } 29 | 30 | public function setOrigins(array $origins) 31 | { 32 | foreach ($origins as $origin) { 33 | if (!isset($origin['name'])) { 34 | throw new ConfigurationsException('name is required for origin'); 35 | } 36 | if (!isset($origin['address'])) { 37 | throw new ConfigurationsException('address is required for origin'); 38 | } 39 | } 40 | $this->configs['origins'] = $origins; 41 | } 42 | 43 | public function getOrigins():array 44 | { 45 | return $this->configs['origins'] ?? []; 46 | } 47 | 48 | public function setDescription(string $description = '') 49 | { 50 | $this->configs['description'] = $description; 51 | } 52 | 53 | public function getDescription():string 54 | { 55 | return $this->configs['description'] ?? ''; 56 | } 57 | 58 | public function enable() 59 | { 60 | $this->configs['enabled'] = true; 61 | } 62 | 63 | public function isEnabled():bool 64 | { 65 | return $this->configs['enabled'] ?? true; 66 | } 67 | 68 | public function disable() 69 | { 70 | $this->configs['enabled'] = false; 71 | } 72 | 73 | public function isDisabled():bool 74 | { 75 | return !$this->configs['enabled'] ?? false; 76 | } 77 | 78 | public function getEnabled():bool 79 | { 80 | return $this->configs['enabled'] ?? true; 81 | } 82 | 83 | public function setMonitor(string $monitor) 84 | { 85 | $this->configs['monitor'] = $monitor; 86 | } 87 | 88 | public function getMonitor():string 89 | { 90 | return $this->configs['monitor'] ?? ''; 91 | } 92 | 93 | public function setCheckRegions(array $checkRegions) 94 | { 95 | $this->configs['check_regions'] = $checkRegions; 96 | } 97 | 98 | public function getCheckRegions():array 99 | { 100 | return $this->configs['check_regions'] ?? []; 101 | } 102 | 103 | public function setNotificationEmail(string $email) 104 | { 105 | if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 106 | throw new ConfigurationsException('Invalid notification email given'); 107 | } 108 | 109 | $this->configs['notification_email'] = $email; 110 | } 111 | 112 | public function getNotificationEmail():string 113 | { 114 | return $this->configs['notification_email'] ?? ''; 115 | } 116 | 117 | public function getArray(): array 118 | { 119 | return $this->configs; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /tests/Endpoints/CryptoTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/getOpportunisticEncryptionSetting.json'); 8 | 9 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 10 | $mock->method('get')->willReturn($response); 11 | 12 | $mock->expects($this->once()) 13 | ->method('get') 14 | ->with( 15 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_encryption') 16 | ); 17 | 18 | $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock); 19 | $result = $cryptoMock->getOpportunisticEncryptionSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); 20 | 21 | $this->assertEquals('off', $result); 22 | } 23 | 24 | public function testGetOnionRoutingSetting() 25 | { 26 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/getOnionRoutingSetting.json'); 27 | 28 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 29 | $mock->method('get')->willReturn($response); 30 | 31 | $mock->expects($this->once()) 32 | ->method('get') 33 | ->with( 34 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_onion') 35 | ); 36 | 37 | $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock); 38 | $result = $cryptoMock->getOnionRoutingSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); 39 | 40 | $this->assertEquals('off', $result); 41 | } 42 | 43 | public function testUpdateOpportunisticEncryptionSetting() 44 | { 45 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateOpportunisticEncryptionSetting.json'); 46 | 47 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 48 | $mock->method('patch')->willReturn($response); 49 | 50 | $mock->expects($this->once()) 51 | ->method('patch') 52 | ->with( 53 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_encryption'), 54 | $this->equalTo(['value' => 'off']) 55 | ); 56 | 57 | $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock); 58 | $result = $cryptoMock->updateOpportunisticEncryptionSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off'); 59 | 60 | $this->assertTrue($result); 61 | } 62 | 63 | public function testUpdateOnionRoutingSetting() 64 | { 65 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateOnionRoutingSetting.json'); 66 | 67 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 68 | $mock->method('patch')->willReturn($response); 69 | 70 | $mock->expects($this->once()) 71 | ->method('patch') 72 | ->with( 73 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_onion'), 74 | $this->equalTo(['value' => 'off']) 75 | ); 76 | 77 | $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock); 78 | $result = $cryptoMock->updateOnionRoutingSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off'); 79 | 80 | $this->assertTrue($result); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Adapter/GuzzleTest.php: -------------------------------------------------------------------------------- 1 | getMockBuilder(\Cloudflare\API\Auth\Auth::class) 12 | ->setMethods(['getHeaders']) 13 | ->getMock(); 14 | 15 | $auth->method('getHeaders') 16 | ->willReturn(['X-Testing' => 'Test']); 17 | 18 | $this->client = new \Cloudflare\API\Adapter\Guzzle($auth, 'https://httpbin.org/'); 19 | } 20 | 21 | public function testGet() 22 | { 23 | $response = $this->client->get('https://httpbin.org/get'); 24 | 25 | $headers = $response->getHeaders(); 26 | $this->assertEquals('application/json', $headers['Content-Type'][0]); 27 | 28 | $body = json_decode($response->getBody()); 29 | $this->assertEquals('Test', $body->headers->{'X-Testing'}); 30 | 31 | $response = $this->client->get('https://httpbin.org/get', [], ['X-Another-Test' => 'Test2']); 32 | $body = json_decode($response->getBody()); 33 | $this->assertEquals('Test2', $body->headers->{'X-Another-Test'}); 34 | } 35 | 36 | public function testPost() 37 | { 38 | $response = $this->client->post('https://httpbin.org/post', ['X-Post-Test' => 'Testing a POST request.']); 39 | 40 | $headers = $response->getHeaders(); 41 | $this->assertEquals('application/json', $headers['Content-Type'][0]); 42 | 43 | $body = json_decode($response->getBody()); 44 | $this->assertEquals('Testing a POST request.', $body->json->{'X-Post-Test'}); 45 | } 46 | 47 | public function testPut() 48 | { 49 | $response = $this->client->put('https://httpbin.org/put', ['X-Put-Test' => 'Testing a PUT request.']); 50 | 51 | $headers = $response->getHeaders(); 52 | $this->assertEquals('application/json', $headers['Content-Type'][0]); 53 | 54 | $body = json_decode($response->getBody()); 55 | $this->assertEquals('Testing a PUT request.', $body->json->{'X-Put-Test'}); 56 | } 57 | 58 | public function testPatch() 59 | { 60 | $response = $this->client->patch( 61 | 'https://httpbin.org/patch', 62 | ['X-Patch-Test' => 'Testing a PATCH request.'] 63 | ); 64 | 65 | $headers = $response->getHeaders(); 66 | $this->assertEquals('application/json', $headers['Content-Type'][0]); 67 | 68 | $body = json_decode($response->getBody()); 69 | $this->assertEquals('Testing a PATCH request.', $body->json->{'X-Patch-Test'}); 70 | } 71 | 72 | public function testDelete() 73 | { 74 | $response = $this->client->delete( 75 | 'https://httpbin.org/delete', 76 | ['X-Delete-Test' => 'Testing a DELETE request.'] 77 | ); 78 | 79 | $headers = $response->getHeaders(); 80 | $this->assertEquals('application/json', $headers['Content-Type'][0]); 81 | 82 | $body = json_decode($response->getBody()); 83 | $this->assertEquals('Testing a DELETE request.', $body->json->{'X-Delete-Test'}); 84 | } 85 | 86 | public function testNotFound() 87 | { 88 | $this->expectException(ResponseException::class); 89 | $this->client->get('https://httpbin.org/status/404'); 90 | } 91 | 92 | public function testServerError() 93 | { 94 | $this->expectException(ResponseException::class); 95 | $this->client->get('https://httpbin.org/status/500'); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/Endpoints/UARules.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 24 | } 25 | 26 | public function listRules( 27 | string $zoneID, 28 | int $page = 1, 29 | int $perPage = 20 30 | ): \stdClass { 31 | $query = [ 32 | 'page' => $page, 33 | 'per_page' => $perPage 34 | ]; 35 | 36 | $user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules', $query); 37 | $this->body = json_decode($user->getBody()); 38 | 39 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 40 | } 41 | 42 | public function createRule( 43 | string $zoneID, 44 | string $mode, 45 | Configurations $configuration, 46 | string $ruleID = null, 47 | string $description = null 48 | ): bool { 49 | $options = [ 50 | 'mode' => $mode, 51 | 'configurations' => $configuration->getArray() 52 | ]; 53 | 54 | if ($ruleID !== null) { 55 | $options['id'] = $ruleID; 56 | } 57 | 58 | if ($description !== null) { 59 | $options['description'] = $description; 60 | } 61 | 62 | $user = $this->adapter->post('zones/' . $zoneID . '/firewall/ua_rules', $options); 63 | 64 | $this->body = json_decode($user->getBody()); 65 | 66 | if (isset($this->body->result->id)) { 67 | return true; 68 | } 69 | 70 | return false; 71 | } 72 | 73 | public function getRuleDetails(string $zoneID, string $blockID): \stdClass 74 | { 75 | $user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules/' . $blockID); 76 | $this->body = json_decode($user->getBody()); 77 | return $this->body->result; 78 | } 79 | 80 | public function updateRule( 81 | string $zoneID, 82 | string $ruleID, 83 | string $mode, 84 | \Cloudflare\API\Configurations\UARules $configuration, 85 | string $description = null 86 | ): bool { 87 | $options = [ 88 | 'mode' => $mode, 89 | 'id' => $ruleID, 90 | 'configurations' => $configuration->getArray() 91 | ]; 92 | 93 | if ($description !== null) { 94 | $options['description'] = $description; 95 | } 96 | 97 | $user = $this->adapter->put('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, $options); 98 | 99 | $this->body = json_decode($user->getBody()); 100 | 101 | if (isset($this->body->result->id)) { 102 | return true; 103 | } 104 | 105 | return false; 106 | } 107 | 108 | public function deleteRule(string $zoneID, string $ruleID): bool 109 | { 110 | $user = $this->adapter->delete('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID); 111 | 112 | $this->body = json_decode($user->getBody()); 113 | 114 | if (isset($this->body->result->id)) { 115 | return true; 116 | } 117 | 118 | return false; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/Endpoints/Firewall.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 15 | } 16 | 17 | public function createFirewallRules( 18 | string $zoneID, 19 | array $rules 20 | ): bool { 21 | $query = $this->adapter->post('zones/' . $zoneID . '/firewall/rules', $rules); 22 | $body = json_decode($query->getBody()); 23 | 24 | foreach ($body->result as $result) { 25 | if (!isset($result->id)) { 26 | return false; 27 | } 28 | } 29 | 30 | return true; 31 | } 32 | 33 | public function createFirewallRule( 34 | string $zoneID, 35 | string $expression, 36 | FirewallRuleOptions $options, 37 | string $description = null, 38 | int $priority = null 39 | ): bool { 40 | $rule = array_merge([ 41 | 'filter' => [ 42 | 'expression' => $expression, 43 | 'paused' => false 44 | ] 45 | ], $options->getArray()); 46 | 47 | if ($description !== null) { 48 | $rule['description'] = $description; 49 | } 50 | 51 | if ($priority !== null) { 52 | $rule['priority'] = $priority; 53 | } 54 | 55 | return $this->createFirewallRules($zoneID, [$rule]); 56 | } 57 | 58 | public function listFirewallRules( 59 | string $zoneID, 60 | int $page = 1, 61 | int $perPage = 50 62 | ): \stdClass { 63 | $query = [ 64 | 'page' => $page, 65 | 'per_page' => $perPage, 66 | ]; 67 | 68 | $rules = $this->adapter->get('zones/' . $zoneID . '/firewall/rules', $query); 69 | $body = json_decode($rules->getBody()); 70 | 71 | return (object)['result' => $body->result, 'result_info' => $body->result_info]; 72 | } 73 | 74 | public function deleteFirewallRule( 75 | string $zoneID, 76 | string $ruleID 77 | ): bool { 78 | $rule = $this->adapter->delete('zones/' . $zoneID . '/firewall/rules/' . $ruleID); 79 | 80 | $body = json_decode($rule->getBody()); 81 | 82 | if (isset($body->result->id)) { 83 | return true; 84 | } 85 | 86 | return false; 87 | } 88 | 89 | public function updateFirewallRule( 90 | string $zoneID, 91 | string $ruleID, 92 | string $filterID, 93 | string $expression, 94 | FirewallRuleOptions $options, 95 | string $description = null, 96 | int $priority = null 97 | ): \stdClass { 98 | $rule = array_merge([ 99 | 'id' => $ruleID, 100 | 'filter' => [ 101 | 'id' => $filterID, 102 | 'expression' => $expression, 103 | 'paused' => false 104 | ] 105 | ], $options->getArray()); 106 | 107 | if ($description !== null) { 108 | $rule['description'] = $description; 109 | } 110 | 111 | if ($priority !== null) { 112 | $rule['priority'] = $priority; 113 | } 114 | 115 | $rule = $this->adapter->put('zones/' . $zoneID . '/firewall/rules/' . $ruleID, $rule); 116 | $body = json_decode($rule->getBody()); 117 | 118 | return $body->result; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /tests/Adapter/ResponseExceptionTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(ResponseException::class, $respErr); 20 | $this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); 21 | $this->assertEquals(0, $respErr->getCode()); 22 | $this->assertEquals($reqErr, $respErr->getPrevious()); 23 | } 24 | 25 | public function testFromRequestExceptionEmptyContentType() 26 | { 27 | $resp = new Response(404); 28 | $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); 29 | $respErr = ResponseException::fromRequestException($reqErr); 30 | 31 | $this->assertInstanceOf(ResponseException::class, $respErr); 32 | $this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); 33 | $this->assertEquals(0, $respErr->getCode()); 34 | $this->assertEquals($reqErr, $respErr->getPrevious()); 35 | } 36 | 37 | 38 | public function testFromRequestExceptionUnknownContentType() 39 | { 40 | $resp = new Response(404, ['Content-Type' => ['application/octet-stream']]); 41 | $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); 42 | $respErr = ResponseException::fromRequestException($reqErr); 43 | 44 | $this->assertInstanceOf(ResponseException::class, $respErr); 45 | $this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); 46 | $this->assertEquals(0, $respErr->getCode()); 47 | $this->assertEquals($reqErr, $respErr->getPrevious()); 48 | } 49 | 50 | public function testFromRequestExceptionJSONDecodeError() 51 | { 52 | $resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], '[what]'); 53 | $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); 54 | $respErr = ResponseException::fromRequestException($reqErr); 55 | 56 | $this->assertInstanceOf(ResponseException::class, $respErr); 57 | $this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); 58 | $this->assertEquals(0, $respErr->getCode()); 59 | $this->assertInstanceOf(JSONException::class, $respErr->getPrevious()); 60 | $this->assertEquals($reqErr, $respErr->getPrevious()->getPrevious()); 61 | } 62 | 63 | public function testFromRequestExceptionJSONWithErrors() 64 | { 65 | $body = '{ 66 | "result": null, 67 | "success": false, 68 | "errors": [{"code":1003, "message":"This is an error"}], 69 | "messages": [] 70 | }'; 71 | 72 | $resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], $body); 73 | $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); 74 | $respErr = ResponseException::fromRequestException($reqErr); 75 | 76 | $this->assertInstanceOf(ResponseException::class, $respErr); 77 | $this->assertEquals('This is an error', $respErr->getMessage()); 78 | $this->assertEquals(1003, $respErr->getCode()); 79 | $this->assertEquals($reqErr, $respErr->getPrevious()); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Endpoints/ZoneLockdown.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 23 | } 24 | 25 | public function listLockdowns( 26 | string $zoneID, 27 | int $page = 1, 28 | int $perPage = 20 29 | ): \stdClass { 30 | $query = [ 31 | 'page' => $page, 32 | 'per_page' => $perPage 33 | ]; 34 | 35 | $user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns', $query); 36 | $this->body = json_decode($user->getBody()); 37 | 38 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 39 | } 40 | 41 | public function createLockdown( 42 | string $zoneID, 43 | array $urls, 44 | \Cloudflare\API\Configurations\ZoneLockdown $configuration, 45 | string $lockdownID = null, 46 | string $description = null 47 | ): bool { 48 | $options = [ 49 | 'urls' => $urls, 50 | 'configurations' => $configuration->getArray() 51 | ]; 52 | 53 | if ($lockdownID !== null) { 54 | $options['id'] = $lockdownID; 55 | } 56 | 57 | if ($description !== null) { 58 | $options['description'] = $description; 59 | } 60 | 61 | $user = $this->adapter->post('zones/' . $zoneID . '/firewall/lockdowns', $options); 62 | 63 | $this->body = json_decode($user->getBody()); 64 | 65 | if (isset($this->body->result->id)) { 66 | return true; 67 | } 68 | 69 | return false; 70 | } 71 | 72 | public function getLockdownDetails(string $zoneID, string $lockdownID): \stdClass 73 | { 74 | $user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID); 75 | $this->body = json_decode($user->getBody()); 76 | return $this->body->result; 77 | } 78 | 79 | public function updateLockdown( 80 | string $zoneID, 81 | string $lockdownID, 82 | array $urls, 83 | \Cloudflare\API\Configurations\ZoneLockdown $configuration, 84 | string $description = null 85 | ): bool { 86 | $options = [ 87 | 'urls' => $urls, 88 | 'id' => $lockdownID, 89 | 'configurations' => $configuration->getArray() 90 | ]; 91 | 92 | if ($description !== null) { 93 | $options['description'] = $description; 94 | } 95 | 96 | $user = $this->adapter->put('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, $options); 97 | 98 | $this->body = json_decode($user->getBody()); 99 | 100 | if (isset($this->body->result->id)) { 101 | return true; 102 | } 103 | 104 | return false; 105 | } 106 | 107 | public function deleteLockdown(string $zoneID, string $lockdownID): bool 108 | { 109 | $user = $this->adapter->delete('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID); 110 | 111 | $this->body = json_decode($user->getBody()); 112 | 113 | if (isset($this->body->result->id)) { 114 | return true; 115 | } 116 | 117 | return false; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/Endpoints/TLS.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 20 | } 21 | 22 | /** 23 | * Get the TLS Client Auth setting for the zone 24 | * 25 | * @param string $zoneID The ID of the zone 26 | * @return string|false 27 | */ 28 | public function getTLSClientAuth($zoneID) 29 | { 30 | $return = $this->adapter->get( 31 | 'zones/' . $zoneID . '/settings/tls_client_auth' 32 | ); 33 | $body = json_decode($return->getBody()); 34 | if (isset($body->result)) { 35 | return $body->result->value; 36 | } 37 | return false; 38 | } 39 | 40 | /** 41 | * Enable TLS 1.3 for the zone 42 | * 43 | * @param string $zoneID The ID of the zone 44 | * @return bool 45 | */ 46 | public function enableTLS13($zoneID) 47 | { 48 | $return = $this->adapter->patch( 49 | 'zones/' . $zoneID . '/settings/tls_1_3', 50 | ['value' => 'on'] 51 | ); 52 | $body = json_decode($return->getBody()); 53 | if (isset($body->success) && $body->success == true) { 54 | return true; 55 | } 56 | return false; 57 | } 58 | 59 | /** 60 | * Disable TLS 1.3 for the zone 61 | * 62 | * @param string $zoneID The ID of the zone 63 | * @return bool 64 | */ 65 | public function disableTLS13($zoneID) 66 | { 67 | $return = $this->adapter->patch( 68 | 'zones/' . $zoneID . '/settings/tls_1_3', 69 | ['value' => 'off'] 70 | ); 71 | $body = json_decode($return->getBody()); 72 | if (isset($body->success) && $body->success == true) { 73 | return true; 74 | } 75 | return false; 76 | } 77 | 78 | /** 79 | * Update the minimum TLS version setting for the zone 80 | * 81 | * @param string $zoneID The ID of the zone 82 | * @param string $minimumVersion The version to update to 83 | * @return bool 84 | */ 85 | public function changeMinimumTLSVersion($zoneID, $minimumVersion) 86 | { 87 | $return = $this->adapter->patch( 88 | 'zones/' . $zoneID . '/settings/min_tls_version', 89 | [ 90 | 'value' => $minimumVersion, 91 | ] 92 | ); 93 | $body = json_decode($return->getBody()); 94 | if (isset($body->success) && $body->success == true) { 95 | return true; 96 | } 97 | return false; 98 | } 99 | 100 | /** 101 | * Update the TLS Client Auth setting for the zone 102 | * 103 | * @param string $zoneID The ID of the zone 104 | * @param string $value The value of the zone setting 105 | * @return bool 106 | */ 107 | public function updateTLSClientAuth($zoneID, $value) 108 | { 109 | $return = $this->adapter->patch( 110 | 'zones/' . $zoneID . '/settings/tls_client_auth', 111 | [ 112 | 'value' => $value, 113 | ] 114 | ); 115 | $body = json_decode($return->getBody()); 116 | if (isset($body->success) && $body->success == true) { 117 | return true; 118 | } 119 | return false; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /tests/Endpoints/ZoneSubscriptionsTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json'); 11 | 12 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 13 | $mock->method('get')->willReturn($response); 14 | 15 | $mock->expects($this->once()) 16 | ->method('get') 17 | ->with( 18 | $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscriptions') 19 | ); 20 | 21 | $zoneSubscriptions = new ZoneSubscriptions($mock); 22 | $zoneSubscriptions->listZoneSubscriptions('023e105f4ecef8ad9ca31a8372d0c353'); 23 | 24 | $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result[0]->id); 25 | $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result[0]->zone->id); 26 | } 27 | 28 | public function testAddZoneSubscriptionIfMissing() 29 | { 30 | $postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json'); 31 | $getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listEmptyZoneSubscriptions.json'); 32 | 33 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 34 | $mock->method('post')->willReturn($postResponse); 35 | $mock->method('get')->willReturn($getResponse); 36 | 37 | $mock->expects($this->once()) 38 | ->method('post') 39 | ->with( 40 | $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'), 41 | $this->equalTo([ 42 | 'rate_plan' => [ 43 | 'id' => 'PARTNER_PRO', 44 | ], 45 | ]) 46 | ); 47 | 48 | $zoneSubscriptions = new ZoneSubscriptions($mock); 49 | $zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO'); 50 | 51 | $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id); 52 | $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id); 53 | } 54 | 55 | public function testAddZoneSubscriptionIfExisting() 56 | { 57 | $postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json'); 58 | $getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json'); 59 | 60 | $mock = $this->getMockBuilder(Adapter::class)->getMock(); 61 | $mock->method('put')->willReturn($postResponse); 62 | $mock->method('get')->willReturn($getResponse); 63 | 64 | $mock->expects($this->once()) 65 | ->method('put') 66 | ->with( 67 | $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'), 68 | $this->equalTo([ 69 | 'rate_plan' => [ 70 | 'id' => 'PARTNER_PRO', 71 | ], 72 | ]) 73 | ); 74 | 75 | $zoneSubscriptions = new ZoneSubscriptions($mock); 76 | $zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO'); 77 | 78 | $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id); 79 | $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tests/Endpoints/MembershipTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/listMemberships.json'); 9 | 10 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 11 | $mock->method('get')->willReturn($response); 12 | 13 | $mock->expects($this->once()) 14 | ->method('get') 15 | ->with( 16 | $this->equalTo('memberships'), 17 | $this->equalTo([ 18 | 'page' => 1, 19 | 'per_page' => 20, 20 | 'account.name' => 'Demo Account', 21 | 'status' => 'accepted', 22 | 'order' => 'status', 23 | 'direction' => 'desc', 24 | ]) 25 | ); 26 | 27 | $zones = new \Cloudflare\API\Endpoints\Membership($mock); 28 | $result = $zones->listMemberships('Demo Account', 'accepted', 1, 20, 'status', 'desc'); 29 | 30 | $this->assertObjectHasAttribute('result', $result); 31 | $this->assertObjectHasAttribute('result_info', $result); 32 | 33 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $result->result[0]->id); 34 | $this->assertEquals(1, $result->result_info->page); 35 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $zones->getBody()->result[0]->id); 36 | } 37 | 38 | public function testGetMembershipDetails() 39 | { 40 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/getMembershipDetails.json'); 41 | 42 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 43 | $mock->method('get')->willReturn($response); 44 | 45 | $membership = new \Cloudflare\API\Endpoints\Membership($mock); 46 | $details = $membership->getMembershipDetails('4536bcfad5faccb111b47003c79917fa'); 47 | 48 | $this->assertObjectHasAttribute('id', $details); 49 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $details->id); 50 | $this->assertObjectHasAttribute('code', $details); 51 | $this->assertEquals('05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0', $details->code); 52 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id); 53 | } 54 | 55 | public function testUpdateMembershipDetails() 56 | { 57 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateMembershipStatus.json'); 58 | 59 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 60 | $mock->method('put')->willReturn($response); 61 | 62 | $mock->expects($this->once()) 63 | ->method('put') 64 | ->with( 65 | $this->equalTo('memberships/4536bcfad5faccb111b47003c79917fa'), 66 | $this->equalTo([ 67 | 'status' => 'accepted' 68 | ]) 69 | ); 70 | 71 | $membership = new \Cloudflare\API\Endpoints\Membership($mock); 72 | $membership->updateMembershipStatus('4536bcfad5faccb111b47003c79917fa', 'accepted'); 73 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id); 74 | } 75 | 76 | public function testDeleteMembership() 77 | { 78 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteMembership.json'); 79 | 80 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 81 | $mock->method('delete')->willReturn($response); 82 | 83 | $mock->expects($this->once()) 84 | ->method('delete') 85 | ->with($this->equalTo('memberships/4536bcfad5faccb111b47003c79917fa')); 86 | 87 | $membership = new \Cloudflare\API\Endpoints\Membership($mock); 88 | 89 | $membership->deleteMembership('4536bcfad5faccb111b47003c79917fa'); 90 | 91 | $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/Endpoints/FirewallSettings.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 14 | } 15 | 16 | /** 17 | * Get the Security Level feature for a zone. 18 | * 19 | * @param string $zoneID The ID of the zone 20 | * @return string|false 21 | */ 22 | public function getSecurityLevelSetting(string $zoneID) 23 | { 24 | $return = $this->adapter->get( 25 | 'zones/' . $zoneID . '/settings/security_level' 26 | ); 27 | $body = json_decode($return->getBody()); 28 | if (isset($body->result)) { 29 | return $body->result->value; 30 | } 31 | return false; 32 | } 33 | 34 | /** 35 | * Get the Challenge TTL feature for a zone. 36 | * 37 | * @param string $zoneID The ID of the zone 38 | * @return integer|false 39 | */ 40 | public function getChallengeTTLSetting(string $zoneID) 41 | { 42 | $return = $this->adapter->get( 43 | 'zones/' . $zoneID . '/settings/challenge_ttl' 44 | ); 45 | $body = json_decode($return->getBody()); 46 | if (isset($body->result)) { 47 | return $body->result->value; 48 | } 49 | return false; 50 | } 51 | 52 | /** 53 | * Get the Browser Integrity Check feature for a zone. 54 | * 55 | * @param string $zoneID The ID of the zone 56 | * @return string|false 57 | */ 58 | public function getBrowserIntegrityCheckSetting(string $zoneID) 59 | { 60 | $return = $this->adapter->get( 61 | 'zones/' . $zoneID . '/settings/browser_check' 62 | ); 63 | $body = json_decode($return->getBody()); 64 | if (isset($body->result)) { 65 | return $body->result->value; 66 | } 67 | return false; 68 | } 69 | 70 | /** 71 | * Update the Security Level setting for the zone 72 | * 73 | * @param string $zoneID The ID of the zone 74 | * @param string $value The value of the zone setting 75 | * @return bool 76 | */ 77 | public function updateSecurityLevelSetting(string $zoneID, string $value) 78 | { 79 | $return = $this->adapter->patch( 80 | 'zones/' . $zoneID . '/settings/security_level', 81 | [ 82 | 'value' => $value, 83 | ] 84 | ); 85 | $body = json_decode($return->getBody()); 86 | if (isset($body->success) && $body->success == true) { 87 | return true; 88 | } 89 | return false; 90 | } 91 | 92 | /** 93 | * Update the Challenge TTL setting for the zone 94 | * 95 | * @param string $zoneID The ID of the zone 96 | * @param int $value The value of the zone setting 97 | * @return bool 98 | */ 99 | public function updateChallengeTTLSetting(string $zoneID, int $value) 100 | { 101 | $return = $this->adapter->patch( 102 | 'zones/' . $zoneID . '/settings/challenge_ttl', 103 | [ 104 | 'value' => $value, 105 | ] 106 | ); 107 | $body = json_decode($return->getBody()); 108 | if (isset($body->success) && $body->success == true) { 109 | return true; 110 | } 111 | return false; 112 | } 113 | 114 | /** 115 | * Update the Browser Integrity Check setting for the zone 116 | * 117 | * @param string $zoneID The ID of the zone 118 | * @param string $value The value of the zone setting 119 | * @return bool 120 | */ 121 | public function updateBrowserIntegrityCheckSetting(string $zoneID, string $value) 122 | { 123 | $return = $this->adapter->patch( 124 | 'zones/' . $zoneID . '/settings/browser_check', 125 | [ 126 | 'value' => $value, 127 | ] 128 | ); 129 | $body = json_decode($return->getBody()); 130 | if (isset($body->success) && $body->success == true) { 131 | return true; 132 | } 133 | return false; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /tests/Endpoints/TLSTest.php: -------------------------------------------------------------------------------- 1 | getPsr7JsonResponseForFixture('Endpoints/getTLSClientAuth.json'); 15 | 16 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 17 | $mock->method('get')->willReturn($response); 18 | 19 | $mock->expects($this->once()) 20 | ->method('get') 21 | ->with( 22 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/tls_client_auth') 23 | ); 24 | 25 | $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock); 26 | $result = $tlsMock->getTLSClientAuth('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); 27 | 28 | $this->assertEquals('off', $result); 29 | } 30 | 31 | public function testEnableTLS13() 32 | { 33 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/enableTLS13.json'); 34 | 35 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 36 | $mock->method('patch')->willReturn($response); 37 | 38 | $mock->expects($this->once()) 39 | ->method('patch') 40 | ->with( 41 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/tls_1_3'), 42 | $this->equalTo(['value' => 'on']) 43 | ); 44 | 45 | $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock); 46 | $result = $tlsMock->enableTLS13('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); 47 | 48 | $this->assertTrue($result); 49 | } 50 | 51 | public function testDisableTLS13() 52 | { 53 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/disableTLS13.json'); 54 | 55 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 56 | $mock->method('patch')->willReturn($response); 57 | 58 | $mock->expects($this->once()) 59 | ->method('patch') 60 | ->with( 61 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/tls_1_3'), 62 | $this->equalTo(['value' => 'off']) 63 | ); 64 | 65 | $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock); 66 | $result = $tlsMock->disableTLS13('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); 67 | 68 | $this->assertTrue($result); 69 | } 70 | 71 | public function testChangeMinimimTLSVersion() 72 | { 73 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/changeMinimumTLSVersion.json'); 74 | 75 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 76 | $mock->method('patch')->willReturn($response); 77 | 78 | $mock->expects($this->once()) 79 | ->method('patch') 80 | ->with( 81 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/min_tls_version'), 82 | $this->equalTo(['value' => '1.1']) 83 | ); 84 | 85 | $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock); 86 | $result = $tlsMock->changeMinimumTLSVersion('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', '1.1'); 87 | 88 | $this->assertTrue($result); 89 | } 90 | 91 | public function testUpdateTLSClientAuth() 92 | { 93 | $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateTLSClientAuth.json'); 94 | 95 | $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); 96 | $mock->method('patch')->willReturn($response); 97 | 98 | $mock->expects($this->once()) 99 | ->method('patch') 100 | ->with( 101 | $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/tls_client_auth'), 102 | $this->equalTo(['value' => 'off']) 103 | ); 104 | 105 | $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock); 106 | $result = $tlsMock->updateTLSClientAuth('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off'); 107 | 108 | $this->assertTrue($result); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/Endpoints/DNS.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 23 | } 24 | 25 | /** 26 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) 27 | * 28 | * @param string $zoneID 29 | * @param string $type 30 | * @param string $name 31 | * @param string $content 32 | * @param int $ttl 33 | * @param bool $proxied 34 | * @param string $priority 35 | * @param array $data 36 | * @return bool 37 | */ 38 | public function addRecord( 39 | string $zoneID, 40 | string $type, 41 | string $name, 42 | string $content, 43 | int $ttl = 0, 44 | bool $proxied = true, 45 | string $priority = '', 46 | array $data = [] 47 | ): bool { 48 | $options = [ 49 | 'type' => $type, 50 | 'name' => $name, 51 | 'content' => $content, 52 | 'proxied' => $proxied 53 | ]; 54 | 55 | if ($ttl > 0) { 56 | $options['ttl'] = $ttl; 57 | } 58 | 59 | if (is_numeric($priority)) { 60 | $options['priority'] = (int)$priority; 61 | } 62 | 63 | if (!empty($data)) { 64 | $options['data'] = $data; 65 | } 66 | 67 | $user = $this->adapter->post('zones/' . $zoneID . '/dns_records', $options); 68 | 69 | $this->body = json_decode($user->getBody()); 70 | 71 | if (isset($this->body->result->id)) { 72 | return true; 73 | } 74 | 75 | return false; 76 | } 77 | 78 | public function listRecords( 79 | string $zoneID, 80 | string $type = '', 81 | string $name = '', 82 | string $content = '', 83 | int $page = 1, 84 | int $perPage = 20, 85 | string $order = '', 86 | string $direction = '', 87 | string $match = 'all' 88 | ): \stdClass { 89 | $query = [ 90 | 'page' => $page, 91 | 'per_page' => $perPage, 92 | 'match' => $match 93 | ]; 94 | 95 | if (!empty($type)) { 96 | $query['type'] = $type; 97 | } 98 | 99 | if (!empty($name)) { 100 | $query['name'] = $name; 101 | } 102 | 103 | if (!empty($content)) { 104 | $query['content'] = $content; 105 | } 106 | 107 | if (!empty($order)) { 108 | $query['order'] = $order; 109 | } 110 | 111 | if (!empty($direction)) { 112 | $query['direction'] = $direction; 113 | } 114 | 115 | $user = $this->adapter->get('zones/' . $zoneID . '/dns_records', $query); 116 | $this->body = json_decode($user->getBody()); 117 | 118 | return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; 119 | } 120 | 121 | public function getRecordDetails(string $zoneID, string $recordID): \stdClass 122 | { 123 | $user = $this->adapter->get('zones/' . $zoneID . '/dns_records/' . $recordID); 124 | $this->body = json_decode($user->getBody()); 125 | return $this->body->result; 126 | } 127 | 128 | public function getRecordID(string $zoneID, string $type = '', string $name = ''): string 129 | { 130 | $records = $this->listRecords($zoneID, $type, $name); 131 | if (isset($records->result[0]->id)) { 132 | return $records->result[0]->id; 133 | } 134 | return false; 135 | } 136 | 137 | public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass 138 | { 139 | $response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, $details); 140 | $this->body = json_decode($response->getBody()); 141 | return $this->body; 142 | } 143 | 144 | public function deleteRecord(string $zoneID, string $recordID): bool 145 | { 146 | $user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID); 147 | 148 | $this->body = json_decode($user->getBody()); 149 | 150 | if (isset($this->body->result->id)) { 151 | return true; 152 | } 153 | 154 | return false; 155 | } 156 | } 157 | --------------------------------------------------------------------------------