This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Cloudflare; |
||
4 | |||
5 | use Cloudflare\Exception\AuthenticationException; |
||
6 | use Cloudflare\Exception\UnauthorizedException; |
||
7 | |||
8 | /** |
||
9 | * CloudFlare API wrapper |
||
10 | * |
||
11 | * A work in progress library for the Cloudflare API. The documentation for the API can be found at https://www.cloudflare.com/docs/. |
||
12 | * |
||
13 | * @author James Bell <[email protected]> |
||
14 | * |
||
15 | * @version 1 |
||
16 | */ |
||
17 | class Api |
||
18 | { |
||
19 | /** |
||
20 | * Holds the provided email address for API authentication |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | public $email; |
||
25 | |||
26 | /** |
||
27 | * Holds the provided auth_key for API authentication |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | public $auth_key; |
||
32 | |||
33 | /** |
||
34 | * Holds the curl options |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | public $curl_options; |
||
39 | |||
40 | /** |
||
41 | * Make a new instance of the API client |
||
42 | * This can be done via providing the email address and api key as seperate parameters |
||
43 | * or by passing in an already instantiated object from which the details will be extracted |
||
44 | */ |
||
45 | public function __construct() |
||
46 | { |
||
47 | $num_args = func_num_args(); |
||
48 | if ($num_args === 1) { |
||
49 | $parameters = func_get_args(); |
||
50 | $client = $parameters[0]; |
||
51 | $this->email = $client->email; |
||
52 | $this->auth_key = $client->auth_key; |
||
53 | $this->curl_options = $client->curl_options; |
||
54 | } elseif ($num_args === 2) { |
||
55 | $parameters = func_get_args(); |
||
56 | $this->email = $parameters[0]; |
||
57 | $this->auth_key = $parameters[1]; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Setter to allow the setting of the email address |
||
63 | * |
||
64 | * @param string $email The email address associated with the Cloudflare account |
||
65 | */ |
||
66 | public function setEmail($email) |
||
67 | { |
||
68 | $this->email = $email; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Setter to allow the setting of the Authentication Key |
||
73 | * |
||
74 | * @param string $token Authentication key, this can be retrieve from the 'My Account' section of the Cloudflare account |
||
75 | */ |
||
76 | public function setAuthKey($token) |
||
77 | { |
||
78 | $this->auth_key = $token; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Setter to allow the adding / changing of the Curl options that will be used within the HTTP requests |
||
83 | * |
||
84 | * @param int $key The CURLOPT_XXX option to set e.g. CURLOPT_TIMEOUT |
||
85 | * @param mixed $value The value to be set on option e.g. 10 |
||
86 | */ |
||
87 | public function setCurlOption($key, $value) |
||
88 | { |
||
89 | $this->curl_options[$key] = $value; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * API call method for sending requests using GET |
||
94 | * |
||
95 | * @param string $path Path of the endpoint |
||
96 | * @param array $data Data to be sent along with the request |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function get($path, array $data = []) |
||
101 | { |
||
102 | return $this->request($path, $data, 'get'); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * API call method for sending requests using POST |
||
107 | * |
||
108 | * @param string $path Path of the endpoint |
||
109 | * @param array $data Data to be sent along with the request |
||
110 | * |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function post($path, array $data = []) |
||
114 | { |
||
115 | return $this->request($path, $data, 'post'); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * API call method for sending requests using PUT |
||
120 | * |
||
121 | * @param string $path Path of the endpoint |
||
122 | * @param array $data Data to be sent along with the request |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function put($path, array $data = []) |
||
127 | { |
||
128 | return $this->request($path, $data, 'put'); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * API call method for sending requests using DELETE |
||
133 | * |
||
134 | * @param string $path Path of the endpoint |
||
135 | * @param array $data Data to be sent along with the request |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | public function delete($path, array $data = []) |
||
140 | { |
||
141 | return $this->request($path, $data, 'delete'); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * API call method for sending requests using PATCH |
||
146 | * |
||
147 | * @param string $path Path of the endpoint |
||
148 | * @param array $data Data to be sent along with the request |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function patch($path, array $data = []) |
||
153 | { |
||
154 | return $this->request($path, $data, 'patch'); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @codeCoverageIgnore |
||
159 | * |
||
160 | * API call method for sending requests using GET, POST, PUT, DELETE OR PATCH |
||
161 | * |
||
162 | * @param string $path Path of the endpoint |
||
163 | * @param array $data Data to be sent along with the request |
||
164 | * @param string $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH') |
||
165 | * |
||
166 | * @return mixed |
||
167 | */ |
||
168 | protected function request($path, array $data = [], $method = 'get') |
||
169 | { |
||
170 | if (!isset($this->email, $this->auth_key) || false === filter_var($this->email, FILTER_VALIDATE_EMAIL)) { |
||
171 | throw new AuthenticationException('Authentication information must be provided'); |
||
172 | } |
||
173 | |||
174 | //Removes null entries |
||
175 | $data = array_filter($data, function ($val) { |
||
176 | return !is_null($val); |
||
177 | }); |
||
178 | |||
179 | $url = 'https://api.cloudflare.com/client/v4/'.$path; |
||
180 | |||
181 | $default_curl_options = [ |
||
182 | CURLOPT_VERBOSE => false, |
||
183 | CURLOPT_FORBID_REUSE => true, |
||
184 | CURLOPT_RETURNTRANSFER => 1, |
||
185 | CURLOPT_HEADER => false, |
||
186 | CURLOPT_TIMEOUT => 30, |
||
187 | CURLOPT_SSL_VERIFYPEER => true, |
||
188 | ]; |
||
189 | |||
190 | $curl_options = $default_curl_options; |
||
191 | if (isset($this->curl_options) && is_array($this->curl_options)) { |
||
192 | $curl_options = array_replace($default_curl_options, $this->curl_options); |
||
193 | } |
||
194 | |||
195 | $user_agent = __FILE__; |
||
196 | $headers = [ |
||
197 | "X-Auth-Email: {$this->email}", |
||
198 | "X-Auth-Key: {$this->auth_key}", |
||
199 | "User-Agent: {$user_agent}", |
||
200 | 'Content-type: application/json', |
||
201 | ]; |
||
202 | |||
203 | $ch = curl_init(); |
||
204 | curl_setopt_array($ch, $curl_options); |
||
205 | |||
206 | $json_data = json_encode($data); |
||
207 | |||
208 | if ($method === 'post') { |
||
209 | curl_setopt($ch, CURLOPT_POST, true); |
||
210 | curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); |
||
211 | } elseif ($method === 'put') { |
||
0 ignored issues
–
show
|
|||
212 | curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); |
||
213 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
||
214 | } elseif ($method === 'delete') { |
||
215 | curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); |
||
216 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
||
217 | } elseif ($method === 'patch') { |
||
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
218 | curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); |
||
219 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
||
220 | } else { |
||
221 | $url .= '?'.http_build_query($data); |
||
222 | } |
||
223 | |||
224 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
225 | curl_setopt($ch, CURLOPT_URL, $url); |
||
226 | |||
227 | $http_result = curl_exec($ch); |
||
228 | $error = curl_error($ch); |
||
229 | $information = curl_getinfo($ch); |
||
230 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
231 | |||
232 | if (in_array($http_code, [401, 403])) { |
||
233 | throw new UnauthorizedException('You do not have permission to perform this request'); |
||
234 | } |
||
235 | |||
236 | $response = json_decode($http_result); |
||
237 | if (!$response) { |
||
238 | $response = new \stdClass(); |
||
239 | $response->success = false; |
||
240 | } |
||
241 | |||
242 | curl_close($ch); |
||
243 | if ($response->success !== true) { |
||
244 | $response->error = $error; |
||
245 | $response->http_code = $http_code; |
||
246 | $response->method = $method; |
||
247 | $response->information = $information; |
||
248 | } |
||
249 | |||
250 | return $response; |
||
251 | } |
||
252 | } |
||
253 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.