1 | <?php |
||||
2 | /* |
||||
3 | ### Installing |
||||
4 | |||||
5 | Add to your _deploy.php_ |
||||
6 | |||||
7 | ```php |
||||
8 | require 'contrib/cloudflare.php'; |
||||
9 | ``` |
||||
10 | |||||
11 | ### Configuration |
||||
12 | |||||
13 | - `cloudflare` – array with configuration for cloudflare |
||||
14 | - `service_key` – Cloudflare Service Key. If this is not provided, use api_key and email. |
||||
15 | - `api_key` – Cloudflare API key generated on the "My Account" page. |
||||
16 | - `email` – Cloudflare Email address associated with your account. |
||||
17 | - `domain` – The domain you want to clear |
||||
18 | |||||
19 | ### Usage |
||||
20 | |||||
21 | Since the website should be built and some load is likely about to be applied to your server, this should be one of, |
||||
22 | if not the, last tasks before cleanup |
||||
23 | |||||
24 | */ |
||||
25 | namespace Deployer; |
||||
26 | |||||
27 | desc('Clearing Cloudflare Cache'); |
||||
28 | task('deploy:cloudflare', function () { |
||||
29 | |||||
30 | $config = get('cloudflare', []); |
||||
31 | |||||
32 | // validate config and set headers |
||||
33 | if (!empty($config['service_key'])) { |
||||
34 | $headers = [ |
||||
35 | 'X-Auth-User-Service-Key' => $config['service_key'] |
||||
36 | ]; |
||||
37 | } elseif (!empty($config['email']) && !empty($config['api_key'])) { |
||||
38 | $headers = [ |
||||
39 | 'X-Auth-Key' => $config['api_key'], |
||||
40 | 'X-Auth-Email' => $config['email'] |
||||
41 | ]; |
||||
42 | } else { |
||||
43 | throw new \RuntimeException("Set a service key or email / api key"); |
||||
44 | } |
||||
45 | |||||
46 | $headers['Content-Type'] = 'application/json'; |
||||
47 | |||||
48 | if (empty($config['domain'])) { |
||||
49 | throw new \RuntimeException("Set a domain"); |
||||
50 | } |
||||
51 | |||||
52 | $makeRequest = function ($url, $opts = []) use ($headers) { |
||||
53 | $ch = curl_init("https://api.cloudflare.com/client/v4/$url"); |
||||
54 | |||||
55 | $parsedHeaders = []; |
||||
56 | foreach($headers as $key => $value){ |
||||
57 | $parsedHeaders[] = "$key: $value"; |
||||
58 | } |
||||
59 | |||||
60 | curl_setopt_array($ch, [ |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
61 | CURLOPT_HTTPHEADER => $parsedHeaders, |
||||
62 | CURLOPT_RETURNTRANSFER => true |
||||
63 | ]); |
||||
64 | |||||
65 | curl_setopt_array($ch, $opts); |
||||
66 | |||||
67 | $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...
|
|||||
68 | |||||
69 | 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...
|
|||||
70 | throw new \RuntimeException("Error making curl request (result: $res)"); |
||||
71 | } |
||||
72 | |||||
73 | 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...
|
|||||
74 | |||||
75 | return $res; |
||||
76 | }; |
||||
77 | |||||
78 | // get the mysterious zone id from Cloud Flare |
||||
79 | $zones = json_decode($makeRequest( |
||||
80 | "zones?name={$config['domain']}" |
||||
81 | ), true); |
||||
82 | |||||
83 | if (empty($zones['success']) || !empty($zones['errors'])) { |
||||
84 | throw new \RuntimeException("Problem with zone data"); |
||||
85 | } else { |
||||
86 | $zoneId = current($zones['result'])['id']; |
||||
87 | } |
||||
88 | |||||
89 | // make purge request |
||||
90 | $makeRequest( |
||||
91 | "zones/$zoneId/purge_cache", |
||||
92 | [ |
||||
93 | CURLOPT_CUSTOMREQUEST => 'DELETE', |
||||
94 | CURLOPT_POSTFIELDS => json_encode( |
||||
95 | [ |
||||
96 | 'purge_everything' => true |
||||
97 | ] |
||||
98 | ), |
||||
99 | ] |
||||
100 | ); |
||||
101 | }); |
||||
102 |