forked from jquery/codeorigin.jquery.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge.php
96 lines (88 loc) · 2.58 KB
/
purge.php
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
<?php
/**
* This file is executed from Nginx using fastcgi.
*
* The file must be compatible with PHP 5.4 and later.
*
* See also jquery::wp::jquery in jquery/infrastructure [private].
*
* Test Plan:
*
* $ REQUEST_URI="/example" php purge.php
*/
$configFile = __DIR__ . '/config.json';
if ( !isset( $_SERVER[ 'REQUEST_URI' ] )
|| !is_readable( $configFile )
|| !function_exists( 'curl_init' )
) {
http_response_code( 500 );
echo "Context error.\n";
exit;
}
$config = json_decode( file_get_contents( $configFile ) );
$hwConfig = $config->highwinds;
if ( !$hwConfig
|| !$hwConfig->api_url
|| !$hwConfig->api_token
|| !$hwConfig->account_hash
|| !$hwConfig->file_hostname
) {
http_response_code( 500 );
echo "Configuration error.\n";
exit;
}
// The StrikeTracker Purge API is protocol-sensitive.
// HTTP and HTTPS need to be purged separately, or
// we can use a protocol-relative file url, which Highwinds
// supports as short-cut for purging both.
$file = "//{$hwConfig->file_hostname}/" . ltrim( $_SERVER[ 'REQUEST_URI' ], '/' );
/**
* Make an HTTP POST request, submitting JSON data, and receiving JSON data.
*
* @param string $url
* @param array $headers
* @param array $postData Data to be serialised using JSON as post body
* @return array|false HTTP response body decoded as JSON, or boolean false
*/
function jq_request_post_json( $url, array $headers, array $postData ) {
$ch = curl_init( $url );
curl_setopt_array( $ch, array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode( $postData ),
CURLOPT_RETURNTRANSFER => true,
) );
$response = curl_exec( $ch );
curl_close( $ch );
return $response ? json_decode( $response ) : false;
}
header( 'Content-Type: text/plain' );
header( 'X-Content-Type-Options: nosniff' );
echo "Attempting to purge:\n{$file}\n\n";
$result = jq_request_post_json(
// url
"{$hwConfig->api_url}/api/accounts/{$hwConfig->account_hash}/purge",
// headers
array(
"Authorization: Bearer {$hwConfig->api_token}",
"Content-Type: application/json",
),
// post body (will be encoded as JSON)
array(
'list' => array(
array( 'url' => $file, 'purgeAllDynamic' => true ),
),
)
);
// Successful responses contain an 'id' that identifies the purge.
// Errors should contain an 'error' string and 'code' number.
if ( !$result || !isset( $result->id ) || isset( $result->error ) || isset( $result->code ) ) {
echo "Purge may have failed.\n\n";
if ( isset( $result->code ) ) {
echo "Error code: " . $result->code . "\n";
}
if ( isset( $result->error ) ) {
echo "Error message: " . $result->error . "\n";
}
} else {
echo "Done!\n";
}