From 39c0bf0c5e7e9da5803685043cd6682eb08c9b84 Mon Sep 17 00:00:00 2001 From: Danny Bessems Date: Tue, 11 Aug 2020 12:32:00 +0000 Subject: [PATCH] Add 'class/Cloudflare.php' --- class/Cloudflare.php | 270 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 class/Cloudflare.php diff --git a/class/Cloudflare.php b/class/Cloudflare.php new file mode 100644 index 0000000..99d3371 --- /dev/null +++ b/class/Cloudflare.php @@ -0,0 +1,270 @@ +email = $email; + $this->apiKey = $apiKey; + } + + /** + * Issues an HTTPS request and returns the result + * + * @param string $method + * @param string $endpoint + * @param array $params + * + * @throws Exception + * + * @return mixed + */ + public function request($method, $endpoint, $params = []) + { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, False); + + $headers = [ + 'X-Auth-Email: ' . $this->email, + 'X-Auth-Key: ' . $this->apiKey, + 'User-Agent: cloudflare-php' + ]; + + $url = self::ENDPOINT . ltrim($endpoint, '/'); + switch ($method) + { + case self::POST : + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $params); + break; + case self::PUT : + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); + $headers[] = 'Content-type: application/json'; + break; + case self::PATCH : + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); + $headers[] = 'Content-type: application/json'; + break; + case self::DELETE : + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); + $headers[] = 'Content-type: application/json'; + break; + default: + if ($params) + { + $url .= '?' . http_build_query($params); + } + } + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $response = json_decode(curl_exec($curl), true); + if (!$response) + { + throw new CloudflareException(curl_error($curl)); + } + elseif (false == $response['success']) + { + throw new CloudflareException($response['errors'][0]['message']); + } + curl_close($curl); + + $this->resultInfo = isset($response['result_info']) ? $response['result_info'] : null; + + return $response['result']; + } + + /** + * Issues an HTTP GET request + * + * @param string $endpoint + * @param array $params + */ + public function get($endpoint, $params = []) + { + return $this->request(self::GET, $endpoint, $params); + } + + /** + * Issues an HTTP POST request + * + * @param string $endpoint + * @param array $params + */ + public function post($endpoint, $params = []) + { + return $this->request(self::POST, $endpoint, $params); + } + + /** + * Issues an HTTP PUT request + * + * @param string $endpoint + * @param array $params + */ + public function put($endpoint, $params = []) + { + return $this->request(self::PUT, $endpoint, $params); + } + + /** + * Issues an HTTP PATCH request + * + * @param string $endpoint + * @param array $params + */ + public function patch($endpoint, $params = []) + { + return $this->request(self::PATCH, $endpoint, $params); + } + + /** + * Issues an HTTP DELETE request + * + * @param string $endpoint + * @param array $params + */ + public function delete($endpoint, $params = []) + { + return $this->request(self::DELETE, $endpoint, $params); + } + + public function getZones(array $params = []) + { + return $this->get('/zones', $params); + } + + public function getResultInfo() + { + return $this->resultInfo; + } + + public function getZone($name) + { + $zones = $this->getZones([ + 'name' => $name + ]); + foreach ($zones as $zone) + { + if ($zone['name'] == $name) + { + return $zone; + } + } + return null; + } + + public function getZoneDnsRecords($zoneId, array $params = []) + { + return $this->get('/zones/' . $zoneId . '/dns_records', $params); + } + + public function createDnsZone($name, array $params = []) + { + $params['name'] = $name; + return $this->post('/zones', $params); + } + + /** + * Creates a zone if it doesn't already exist. + * + * Returns information about the zone + */ + public function registerDnsZone($name, $params = []) + { + if ($res = $this->getZone($name)) + { + return $res; + } + return $this->createDnsZone($name, $params); + } + + public function setDnsZoneSsl($zoneId, $type) + { + $allowedTypes = [ + 'off', + 'flexible', + 'full', + 'full_strict' + ]; + + if (!in_array($type, $allowedTypes)) + { + throw new Exception('SSL type not allowed. valid types are ' . join(', ', $allowedTypes)); + } + + return $this->patch('/zones/' . $zoneId . '/settings/ssl', ['value' => $type]); + } + + public function setDnsZoneCache($zoneId, $type) + { + $allowedTypes = [ + 'aggressive', + 'basic', + 'simplified', + ]; + + if (!in_array($type, $allowedTypes)) + { + throw new Exception('Cache type not allowed. valid types are ' . join(', ', $allowedTypes)); + } + + return $this->patch('/zones/' . $zoneId . '/settings/cache_level', ['value' => $type]); + } + + public function clearZoneCache($zoneId) + { + return $this->delete('/zones/' . $zoneId . '/purge_cache', ['purge_everything' => true]); + } + + public function setDnsZoneMinify($zoneId, $settings) + { + return $this->patch('/zones/' . $zoneId . '/settings/minify', ['value' => $settings]); + } + + public function createDnsRecord($zoneId, $type, $name, $content, array $params = []) + { + $params = array_merge($params, [ + 'type' => $type, + 'name' => $name, + 'content' => $content, + ]); + return $this->post('/zones/' . $zoneId . '/dns_records', $params); + } + + public function updateDnsRecord($zoneId, $recordId, array $params = []) + { + return $this->put('/zones/' . $zoneId . '/dns_records/' . $recordId, $params); + } + + public function deleteDnsRecord($zoneId, $recordId) + { + return $this->delete('/zones/' . $zoneId . '/dns_records/' . $recordId); + } +} \ No newline at end of file