|
11 | 11 | * $ REQUEST_URI="/example" php purge.php
|
12 | 12 | */
|
13 | 13 |
|
14 |
| -$configFile = __DIR__ . '/config.json'; |
15 |
| - |
16 | 14 | if ( !isset( $_SERVER[ 'REQUEST_URI' ] )
|
17 |
| - || !is_readable( $configFile ) |
18 | 15 | || !function_exists( 'curl_init' )
|
19 | 16 | ) {
|
20 | 17 | http_response_code( 500 );
|
21 | 18 | echo "Context error.\n";
|
22 | 19 | exit;
|
23 | 20 | }
|
24 | 21 |
|
25 |
| -$config = json_decode( file_get_contents( $configFile ) ); |
26 |
| -$hwConfig = $config->highwinds; |
27 |
| -if ( !$hwConfig |
28 |
| - || !$hwConfig->api_url |
29 |
| - || !$hwConfig->api_token |
30 |
| - || !$hwConfig->account_hash |
31 |
| - || !$hwConfig->file_hostname |
| 22 | +// Highwinds StrikeTracker |
| 23 | +$striketrackerUrl = getenv( 'STRIKETRACKER_URL' ) ?: 'https://striketracker.highwinds.com'; |
| 24 | +$striketrackerToken = getenv( 'STRIKETRACKER_TOKEN' ) ?: false; |
| 25 | +$striketrackerAccountHash = getenv( 'STRIKETRACKER_ACCOUNT' ) ?: false; |
| 26 | +// This is configurable because the purge script may be invoked |
| 27 | +// from a hostname different from the one canonically serving the asset, |
| 28 | +// or. e.g. from the CLI. |
| 29 | +$striketrackerPurgeHostname = getenv( 'STRIKETRACKER_PURGE_HOSTNAME' ) ?: 'code.jquery.com'; |
| 30 | + |
| 31 | +if ( !$striketrackerUrl |
| 32 | + || !$striketrackerToken |
| 33 | + || !$striketrackerAccountHash |
| 34 | + || !$striketrackerPurgeHostname |
32 | 35 | ) {
|
33 |
| - http_response_code( 500 ); |
34 |
| - echo "Configuration error.\n"; |
35 |
| - exit; |
| 36 | + $configFile = __DIR__ . '/config.json'; |
| 37 | + $configJson = @file_get_contents( $configFile ); |
| 38 | + $config = $configJson ? json_decode( $configJson ) : false; |
| 39 | + $hwConfig = $config ? $config->highwinds : false; |
| 40 | + if ( !$hwConfig |
| 41 | + || !$hwConfig->api_url |
| 42 | + || !$hwConfig->api_token |
| 43 | + || !$hwConfig->account_hash |
| 44 | + || !$hwConfig->file_hostname |
| 45 | + ) { |
| 46 | + http_response_code( 500 ); |
| 47 | + echo "Configuration error.\n"; |
| 48 | + exit; |
| 49 | + } |
| 50 | + $striketrackerUrl = $hwConfig->api_url; |
| 51 | + $striketrackerToken = $hwConfig->api_token; |
| 52 | + $striketrackerAccountHash = $hwConfig->account_hash; |
| 53 | + $striketrackerPurgeHostname = $hwConfig->file_hostname; |
36 | 54 | }
|
37 | 55 |
|
38 | 56 | // The StrikeTracker Purge API is protocol-sensitive.
|
39 | 57 | // HTTP and HTTPS need to be purged separately, or
|
40 | 58 | // we can use a protocol-relative file url, which Highwinds
|
41 | 59 | // supports as short-cut for purging both.
|
42 |
| -$file = "//{$hwConfig->file_hostname}/" . ltrim( $_SERVER[ 'REQUEST_URI' ], '/' ); |
| 60 | +$file = "//{$striketrackerPurgeHostname}/" . ltrim( $_SERVER[ 'REQUEST_URI' ], '/' ); |
43 | 61 |
|
44 | 62 | /**
|
45 | 63 | * Make an HTTP POST request, submitting JSON data, and receiving JSON data.
|
@@ -67,10 +85,10 @@ function jq_request_post_json( $url, array $headers, array $postData ) {
|
67 | 85 |
|
68 | 86 | $result = jq_request_post_json(
|
69 | 87 | // url
|
70 |
| - "{$hwConfig->api_url}/api/accounts/{$hwConfig->account_hash}/purge", |
| 88 | + "{$striketrackerUrl}/api/accounts/{$striketrackerAccountHash}/purge", |
71 | 89 | // headers
|
72 | 90 | array(
|
73 |
| - "Authorization: Bearer {$hwConfig->api_token}", |
| 91 | + "Authorization: Bearer {$striketrackerToken}", |
74 | 92 | "Content-Type: application/json",
|
75 | 93 | ),
|
76 | 94 | // post body (will be encoded as JSON)
|
|
0 commit comments