1 | <?php |
||||
2 | /* (c) David Jordan / CyberDuck <[email protected]> |
||||
3 | * |
||||
4 | * For the full copyright and license information, please view the LICENSE |
||||
5 | * file that was distributed with this source code. |
||||
6 | */ |
||||
7 | |||||
8 | namespace Deployer; |
||||
9 | |||||
10 | desc('Clearing Cloudflare Cache'); |
||||
11 | task('deploy:cloudflare', function () { |
||||
12 | |||||
13 | $config = get('cloudflare', []); |
||||
14 | |||||
15 | // validate config and set headers |
||||
16 | if (!empty($config['service_key'])) { |
||||
17 | $headers = [ |
||||
18 | 'X-Auth-User-Service-Key' => $config['service_key'] |
||||
19 | ]; |
||||
20 | } elseif (!empty($config['email']) && !empty($config['api_key'])) { |
||||
21 | $headers = [ |
||||
22 | 'X-Auth-Key' => $config['api_key'], |
||||
23 | 'X-Auth-Email' => $config['email'] |
||||
24 | ]; |
||||
25 | } else { |
||||
26 | throw new \RuntimeException("Set a service key or email / api key"); |
||||
27 | } |
||||
28 | |||||
29 | $headers['Content-Type'] = 'application/json'; |
||||
30 | |||||
31 | if (empty($config['domain'])) { |
||||
32 | throw new \RuntimeException("Set a domain"); |
||||
33 | } |
||||
34 | |||||
35 | $makeRequest = function ($url, $opts = []) use ($headers) { |
||||
36 | $ch = curl_init("https://api.cloudflare.com/client/v4/$url"); |
||||
37 | |||||
38 | $parsedHeaders = []; |
||||
39 | foreach($headers as $key => $value){ |
||||
40 | $parsedHeaders[] = "$key: $value"; |
||||
41 | } |
||||
42 | |||||
43 | curl_setopt_array($ch, [ |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
44 | CURLOPT_HTTPHEADER => $parsedHeaders, |
||||
45 | CURLOPT_RETURNTRANSFER => true |
||||
46 | ]); |
||||
47 | |||||
48 | curl_setopt_array($ch, $opts); |
||||
49 | |||||
50 | $res = curl_exec($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
51 | |||||
52 | if (curl_errno($ch)) { |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_errno() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
53 | throw new \RuntimeException("Error making curl request (result: $res)"); |
||||
54 | } |
||||
55 | |||||
56 | curl_close($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
57 | |||||
58 | return $res; |
||||
59 | }; |
||||
60 | |||||
61 | // get the mysterious zone id from Cloud Flare |
||||
62 | $zones = json_decode($makeRequest( |
||||
63 | "zones?name={$config['domain']}" |
||||
64 | ), true); |
||||
65 | |||||
66 | if (empty($zones['success']) || !empty($zones['errors'])) { |
||||
67 | throw new \RuntimeException("Problem with zone data"); |
||||
68 | } else { |
||||
69 | $zoneId = current($zones['result'])['id']; |
||||
70 | } |
||||
71 | |||||
72 | // make purge request |
||||
73 | $makeRequest( |
||||
74 | "zones/$zoneId/purge_cache", |
||||
75 | [ |
||||
76 | CURLOPT_CUSTOMREQUEST => 'DELETE', |
||||
77 | CURLOPT_POSTFIELDS => json_encode( |
||||
78 | [ |
||||
79 | 'purge_everything' => true |
||||
80 | ] |
||||
81 | ), |
||||
82 | ] |
||||
83 | ); |
||||
84 | }); |
||||
85 |