GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 103d4f...4eef74 )
by James
13s
created

src/CloudFlare/Api.php (5 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

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|null $data Data to be sent along with the request
97
     *
98
     * @return mixed
99
     */
100
    public function get($path, array $data = null)
101
    {
102
        return $this->request($path, $data, 'get');
0 ignored issues
show
It seems like $data defined by parameter $data on line 100 can also be of type null; however, Cloudflare\Api::request() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
    }
104
105
    /**
106
     * API call method for sending requests using POST
107
     *
108
     * @param string     $path Path of the endpoint
109
     * @param array|null $data Data to be sent along with the request
110
     *
111
     * @return mixed
112
     */
113
    public function post($path, array $data = null)
114
    {
115
        return $this->request($path, $data, 'post');
0 ignored issues
show
It seems like $data defined by parameter $data on line 113 can also be of type null; however, Cloudflare\Api::request() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
116
    }
117
118
    /**
119
     * API call method for sending requests using PUT
120
     *
121
     * @param string     $path Path of the endpoint
122
     * @param array|null $data Data to be sent along with the request
123
     *
124
     * @return mixed
125
     */
126
    public function put($path, array $data = null)
127
    {
128
        return $this->request($path, $data, 'put');
0 ignored issues
show
It seems like $data defined by parameter $data on line 126 can also be of type null; however, Cloudflare\Api::request() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
129
    }
130
131
    /**
132
     * API call method for sending requests using DELETE
133
     *
134
     * @param string     $path Path of the endpoint
135
     * @param array|null $data Data to be sent along with the request
136
     *
137
     * @return mixed
138
     */
139
    public function delete($path, array $data = null)
140
    {
141
        return $this->request($path, $data, 'delete');
0 ignored issues
show
It seems like $data defined by parameter $data on line 139 can also be of type null; however, Cloudflare\Api::request() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
142
    }
143
144
    /**
145
     * API call method for sending requests using PATCH
146
     *
147
     * @param string     $path Path of the endpoint
148
     * @param array|null $data Data to be sent along with the request
149
     *
150
     * @return mixed
151
     */
152
    public function patch($path, array $data = null)
153
    {
154
        return $this->request($path, $data, 'patch');
0 ignored issues
show
It seems like $data defined by parameter $data on line 152 can also be of type null; however, Cloudflare\Api::request() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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|null  $data   Data to be sent along with the request
164
     * @param string|null $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 = array(), $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 = ["X-Auth-Email: {$this->email}", "X-Auth-Key: {$this->auth_key}", "User-Agent: {$user_agent}"];
197
198
        $ch = curl_init();
199
        curl_setopt_array($ch, $curl_options);
200
201
        $headers[] = 'Content-type: application/json';
202
        $json_data = json_encode($data);
203
204
        if ($method === 'post') {
205
            curl_setopt($ch, CURLOPT_POST, true);
206
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
207
        } elseif ($method === 'put') {
208
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
209
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
210
        } elseif ($method === 'delete') {
211
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
212
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
213
        } elseif ($method === 'patch') {
214
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
215
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
216
        } else {
217
            $url .= '?'.http_build_query($data);
218
        }
219
220
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
221
        curl_setopt($ch, CURLOPT_URL, $url);
222
223
        $http_result = curl_exec($ch);
224
        $error = curl_error($ch);
225
        $information = curl_getinfo($ch);
226
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
227
228
        if (in_array($http_code, [401, 403])) {
229
            throw new UnauthorizedException('You do not have permission to perform this request');
230
        }
231
232
        $response = json_decode($http_result);
233
        if (!$response) {
234
            $response = new \stdClass();
235
            $response->success = false;
236
        }
237
238
        curl_close($ch);
239
        if ($response->success !== true) {
240
            $response->error = $error;
241
            $response->http_code = $http_code;
242
            $response->method = $method;
243
            $response->information = $information;
244
        }
245
246
        return $response;
247
    }
248
}
249