1: <?php
2:
3: namespace Cloudflare;
4:
5: use Exception;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class Api
17: {
18: 19: 20: 21: 22:
23: protected $permission_level = ['read' => null, 'edit' => null];
24:
25: 26: 27: 28: 29:
30: public $email;
31:
32: 33: 34: 35: 36:
37: public $auth_key;
38:
39: 40: 41: 42: 43:
44: public $curl_options;
45:
46: 47: 48: 49: 50:
51: private $permissions = null;
52:
53: 54: 55: 56: 57:
58: public function __construct()
59: {
60: $num_args = func_num_args();
61: if ($num_args === 1) {
62: $parameters = func_get_args();
63: $client = $parameters[0];
64: $this->email = $client->email;
65: $this->auth_key = $client->auth_key;
66: $this->curl_options = $client->curl_options;
67: $this->permissions = $client->permissions;
68: } elseif ($num_args === 2) {
69: $parameters = func_get_args();
70: $this->email = $parameters[0];
71: $this->auth_key = $parameters[1];
72: }
73: }
74:
75: 76: 77: 78: 79:
80: public function setEmail($email)
81: {
82: $this->email = $email;
83: }
84:
85: 86: 87: 88: 89:
90: public function setAuthKey($token)
91: {
92: $this->auth_key = $token;
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function setCurlOption($key, $value)
102: {
103: $this->curl_options[$key] = $value;
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function get($path, array $data = null)
113: {
114: return $this->request($path, $data, 'get', 'read');
115: }
116:
117: 118: 119: 120: 121: 122:
123: public function post($path, array $data = null)
124: {
125: return $this->request($path, $data, 'post', 'edit');
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function put($path, array $data = null)
135: {
136: return $this->request($path, $data, 'put', 'edit');
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function delete($path, array $data = null)
146: {
147: return $this->request($path, $data, 'delete', 'edit');
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function patch($path, array $data = null)
157: {
158: return $this->request($path, $data, 'patch', 'edit');
159: }
160:
161: 162: 163:
164: public function permissions()
165: {
166: if (!$this->permissions) {
167: $api = new User($this->email, $this->auth_key);
168: $user = $api->user();
169: if (!$user->result->organizations[0]) {
170: $this->permissions = ['read' => true, 'write' => true];
171: } else {
172: $this->permissions = $user->result->organizations[0]->permissions;
173: }
174: }
175:
176: return $this->permissions;
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186: 187: 188:
189: protected function request($path, array $data = null, $method = null, $permission_level = null)
190: {
191: if (!isset($this->email) || !isset($this->auth_key)) {
192: throw new Exception('Authentication information must be provided');
193:
194: return false;
195: }
196: $data = (is_null($data) ? [] : $data);
197: $method = (is_null($method) ? 'get' : $method);
198: $permission_level = (is_null($permission_level) ? 'read' : $permission_level);
199:
200: if (!is_null($this->permission_level[$permission_level])) {
201: if (!$this->permissions) {
202: $this->permissions();
203: }
204: if (!isset($this->permissions) || !in_array($this->permission_level[$permission_level], $this->permissions)) {
205: throw new Exception('You do not have permission to perform this request');
206:
207: return false;
208: }
209: }
210:
211:
212:
213: $data = array_filter($data, function ($val) {
214: return !is_null($val);
215: });
216:
217:
218:
219: $url = 'https://api.cloudflare.com/client/v4/'.$path;
220:
221: $default_curl_options = [
222: CURLOPT_VERBOSE => false,
223: CURLOPT_FORBID_REUSE => true,
224: CURLOPT_RETURNTRANSFER => 1,
225: CURLOPT_HEADER => false,
226: CURLOPT_TIMEOUT => 30,
227: CURLOPT_SSL_VERIFYPEER => false,
228: CURLOPT_FOLLOWLOCATION => true,
229: ];
230:
231: $curl_options = $default_curl_options;
232: if (isset($this->curl_options) && is_array($this->curl_options)) {
233: $curl_options = array_replace($default_curl_options, $this->curl_options);
234: }
235:
236: $headers = ["X-Auth-Email: {$this->email}", "X-Auth-Key: {$this->auth_key}"];
237:
238: $ch = curl_init();
239: curl_setopt_array($ch, $curl_options);
240:
241: $headers[] = 'Content-type: application/json';
242: $json_data = json_encode($data);
243:
244: if ($method === 'post') {
245: curl_setopt($ch, CURLOPT_POST, true);
246: curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
247: } elseif ($method === 'put') {
248: curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
249: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
250: } elseif ($method === 'delete') {
251: curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
252: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
253: } elseif ($method === 'patch') {
254: curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
255: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
256: } else {
257: $url .= '?'.http_build_query($data);
258: }
259:
260: curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
261: curl_setopt($ch, CURLOPT_URL, $url);
262:
263: $http_result = curl_exec($ch);
264: $error = curl_error($ch);
265: $information = curl_getinfo($ch);
266: $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
267: $response = json_decode($http_result);
268:
269: curl_close($ch);
270: if ($response->success !== true) {
271: $response->http_code = $http_code;
272: $response->method = $method;
273: $response->information = $information;
274: }
275:
276: return $response;
277: }
278: }
279: