-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.php
More file actions
333 lines (254 loc) · 7.77 KB
/
save.php
File metadata and controls
333 lines (254 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* Functions.
*/
function escape_sequence( $code ) {
return "\e[" . $code . 'm';
}
function format_command( $value ) {
return escape_sequence( '36' ) . $value . escape_sequence( '0' );
}
function format_error( $value ) {
return escape_sequence( '31' ) . escape_sequence( '1' ) . 'Error:' . escape_sequence( '0' ) . ' ' . $value;
}
function run_command( $command, $expected_result_code = 0 ) {
echo format_command( $command ), PHP_EOL;
passthru( $command, $result_code );
if ( null !== $expected_result_code && $expected_result_code !== $result_code ) {
exit( $result_code );
}
return $result_code;
}
function run_shell_exec( $command ) {
echo format_command( $command ), PHP_EOL;
return shell_exec( $command );
}
function start_group( $name ) {
echo '::group::', $name, PHP_EOL;
}
function end_group() {
echo '::endgroup::', PHP_EOL;
}
/**
* Get input.
*
* @link https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
* @link https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepswith
* @link https://github.com/actions/checkout/blob/cd7d8d697e10461458bc61a30d094dc601a8b017/dist/index.js#L2699-L2717
* @param string $name
* @return string|array|false
*/
function get_input( $name ) {
$env_name = 'INPUT_' . strtoupper( $name );
return getenv( $env_name );
}
function get_required_input( $name ) {
$value = get_input( $name );
if ( false === $value || '' === $value ) {
echo format_error( escape_sequence( '90' ) . 'Input required and not supplied:' . escape_sequence( '0' ) . ' ' . $name );
exit( 1 );
}
return $value;
}
/**
* Cloudflare API.
*
* @param string $url URL.
* @param string $api_token API token.
*/
function cloudflare_api( $url, $api_token ) {
$command = <<<EOT
curl --request GET --fail --silent $url \
--header "Authorization: Bearer $api_token" \
--header "Content-Type: application/json"
EOT;
$response = run_shell_exec( $command );
if ( null === $response ) {
echo format_error( 'Cloudflare API request failed' );
exit( 1 );
}
return $response;
}
/**
* Setup.
*/
$api_token = get_required_input( 'api_token' );
$zone_id = get_required_input( 'zone_id' );
$directory = get_required_input( 'directory' );
if ( '' === $directory ) {
echo format_error( escape_sequence( '90' ) . 'Directory empty' );
}
$path = getcwd() . '/' . $directory;
echo $api_token, PHP_EOL;
echo $zone_id, PHP_EOL;
$command = "rm -rf $path";
echo run_command( $command );
$command = "mkdir -p $path";
echo run_command( $command );
/**
* Test token.
*
* @link https://developers.cloudflare.com/api/resources/user/subresources/tokens/methods/verify/
*/
$command = <<<EOT
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer $api_token" \
-H "Content-Type:application/json"
EOT;
// echo run_command( $command );
/**
* Zone details.
*
* @link https://developers.cloudflare.com/api/resources/zones/methods/get/
*/
$zone_details_json = cloudflare_api( "https://api.cloudflare.com/client/v4/zones/$zone_id", $api_token );
$zone_details_object = json_decode( $zone_details_json );
$zone_name = $zone_details_object->result->name;
$zone_details_filename = $path . "/{$zone_name}-details.json";
$zone_details_object->result->development_mode = null;
file_put_contents(
$zone_details_filename,
json_encode( $zone_details_object, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
);
/**
* Zone settings.
*
* @link https://developers.cloudflare.com/api/resources/zones/subresources/settings/methods/get/
*/
$settings_ids = [
'cache_level',
'development_mode',
'security_level',
'ssl',
'webp',
'google-tag-gateway/config',
];
foreach ( $settings_ids as $setting_id ) {
$zone_setting_json = cloudflare_api( "https://api.cloudflare.com/client/v4/zones/$zone_id/settings/$setting_id", $api_token );
$zone_setting_object = json_decode( $zone_setting_json );
$setting_id_slug = str_replace( '/', '-', $setting_id );
$zone_setting_filename = $path . "/{$zone_name}-setting-{$setting_id_slug}.json";
file_put_contents(
$zone_setting_filename,
json_encode( $zone_setting_object, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
);
}
/**
* DNS-records.
*
* @ilnk https://developers.cloudflare.com/api/resources/dns/subresources/records/methods/export/
*/
$zone_dns_records = cloudflare_api( "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/export", $api_token );
/**
* Redact exported date.
*/
$zone_dns_records = preg_replace(
'/^;; Exported: \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/m',
';; Exported: ●●●●-●●-●● ●●:●●:●●',
$zone_dns_records
);
/**
* Redact SOA record serial number.
*
* Example:
* example.com 3600 IN SOA harmony.ns.cloudflare.com. dns.cloudflare.com. 2049785926 10000 2400 604800 3600
*/
$zone_dns_records = preg_replace(
'/^(\S+\s+\d+\s+IN\s+SOA\s+\S+\s+\S+\s+)(\d{10})(\s+\d+\s+\d+\s+\d+\s+\d+)$/m',
'$1●●●●●●●●●●$3',
$zone_dns_records
);
$zone_dns_records_filename = $path . "/{$zone_name}.zone";
file_put_contents(
$zone_dns_records_filename,
$zone_dns_records
);
/**
* Rulesets.
*
* @link https://developers.cloudflare.com/api/resources/rulesets/methods/list/
* @link https://developers.cloudflare.com/api/resources/rate_limits/methods/list/
*/
$zone_rulesets_json = cloudflare_api( "https://api.cloudflare.com/client/v4/zones/$zone_id/rulesets", $api_token );
$zone_rulesets_object = json_decode( $zone_rulesets_json );
foreach ( $zone_rulesets_object->result as $i => $item ) {
if ( 'managed' === $item->kind ) {
$zone_rulesets_object->result[ $i ]->last_updated = null;
$zone_rulesets_object->result[ $i ]->version = null;
}
}
usort(
$zone_rulesets_object->result,
function( $a, $b ) {
return strcmp( $a->id, $b->id );
}
);
$zone_rulesets_filename = $path . "/{$zone_name}-rulesets.json";
file_put_contents(
$zone_rulesets_filename,
json_encode( $zone_rulesets_object, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
);
/**
* Ruleset details.
*
* @link https://developers.cloudflare.com/api/resources/rulesets/methods/get/
*/
$items = $zone_rulesets_object->result;
$items = array_filter(
$items,
function ( $item ) {
if ( 'managed' === $item->kind ) {
return false;
}
return true;
}
);
foreach ( $items as $item ) {
$ruleset_id = $item->id;
$zone_ruleset_json = cloudflare_api( "https://api.cloudflare.com/client/v4/zones/$zone_id/rulesets/$ruleset_id", $api_token );
$zone_ruleset_object = json_decode( $zone_ruleset_json );
$zone_ruleset_filename = $path . "/{$zone_name}-ruleset-{$ruleset_id}.json";
file_put_contents(
$zone_ruleset_filename,
json_encode( $zone_ruleset_object, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
);
}
/**
* Check Git status.
*/
$status = trim( shell_exec( 'git status --porcelain' ) );
if ( '' === $status ) {
echo 'No changes';
exit( 0 );
}
/**
* GitHub CLI.
*
* @link https://cli.github.com/
*/
run_command( 'gh auth status' );
/**
* Git.
*/
$date = date( 'Y-m-d' );
$branch = "pronamic-cloudflare-config-exporter/$date";
$pr_title = "Cloudflare Config Update ($date)";
$pr_body = "Automated backup of Cloudflare settings on $date.";
run_command( 'git config user.name "pronamic-cloudflare-config-exporter[bot]"' );
run_command( 'git config user.email "pronamic-cloudflare-config-exporter[bot]@users.noreply.github.com"' );
run_command( "git checkout -b $branch" );
run_command( 'git add .' );
run_command( "git commit -m '$pr_title'" );
run_command( "git push origin $branch" );
/**
* GitHub PR create.
*
* @link https://cli.github.com/manual/gh_pr_create
*/
$command = <<<EOT
gh pr create \
--title "$pr_title" \
--body "$pr_body"
EOT;
run_command( $command );
run_command( 'gh pr merge --admin --merge --delete-branch' );