|
1 | 1 | <?php
|
2 |
| -require_once( 'netdnarws-php/NetDNA.php' ); |
| 2 | +/** |
| 3 | + * This file is executed from Nginx using fastcgi. |
| 4 | + * |
| 5 | + * The file must be compatible with PHP 5.4 and later. |
| 6 | + * |
| 7 | + * See also jquery::wp::jquery in jquery/infrastructure [private]. |
| 8 | + * |
| 9 | + * Test Plan: |
| 10 | + * |
| 11 | + * $ REQUEST_URI="/example" php purge.php |
| 12 | + * |
| 13 | + */ |
| 14 | +require_once __DIR__ . '/netdnarws-php/NetDNA.php'; |
3 | 15 |
|
4 |
| -$config = json_decode( file_get_contents( './config.json' ), true ); |
5 |
| -$config = $config[ 'cdn' ]; |
6 |
| -$zone_id = $config[ 'zone_id' ]; |
| 16 | +$configFile = __DIR__ . '/config.json'; |
7 | 17 |
|
8 |
| -$parts = preg_split( '/\?reload=?/', $_SERVER[ 'REQUEST_URI' ] ); |
9 |
| -if ( !$parts ) { |
10 |
| - header( '400 Bad Request' ); |
11 |
| - echo $_SERVER[ 'REQUEST_URI' ] . ' is not a valid URI.'; |
| 18 | +if ( !isset( $_SERVER[ 'REQUEST_URI' ] ) |
| 19 | + || !is_readable( $configFile ) |
| 20 | + || !function_exists( 'curl_init' ) |
| 21 | +) { |
| 22 | + http_response_code( 500 ); |
| 23 | + echo "Context error.\n"; |
12 | 24 | exit;
|
13 | 25 | }
|
14 | 26 |
|
15 |
| -$file = $parts[ 0 ]; |
| 27 | +$config = json_decode( file_get_contents( $configFile ), true ); |
| 28 | +$cdnConfig = $config[ 'cdn' ]; |
| 29 | +$zoneId = $cdnConfig[ 'zone_id' ]; |
| 30 | +$consumerKey = $cdnConfig[ 'consumer_key' ]; |
| 31 | +$consumerSecret = $cdnConfig[ 'consumer_secret' ]; |
| 32 | +if ( !$zoneId |
| 33 | + || !$consumerKey |
| 34 | + || !$consumerSecret |
| 35 | +) { |
| 36 | + http_response_code( 500 ); |
| 37 | + echo "Configuration error.\n"; |
| 38 | + exit; |
| 39 | +} |
| 40 | + |
| 41 | +$parts = explode( '?reload', $_SERVER[ 'REQUEST_URI' ], 2 ); |
| 42 | +$file = $parts ? $parts[ 0 ] : null; |
| 43 | +if ( !$file ) { |
| 44 | + http_response_code( 400 ); |
| 45 | + echo "Bad Request: Invalid REQUEST_URI.\n"; |
| 46 | + exit; |
| 47 | +} |
16 | 48 |
|
17 | 49 | header( 'Content-Type: text/plain' );
|
18 |
| -echo "Attempting to purge: $zone_id: $file.\n"; |
| 50 | +echo "Attempting to purge:\n\t$file\n\n"; |
19 | 51 |
|
20 |
| -$api = new NetDNA( 'jquery', $config[ 'consumer_key' ], $config[ 'consumer_secret' ] ); |
21 |
| -$result = $api->delete( '/zones/pull.json/' . $zone_id . '/cache', array( 'file' => $file ) ); |
| 52 | +$api = new NetDNA( 'jquery', $consumerKey, $consumerSecret ); |
| 53 | +$result = $api->delete( |
| 54 | + '/zones/pull.json/' . $zoneId . '/cache', |
| 55 | + array( 'file' => $file ) |
| 56 | +); |
22 | 57 | $result = json_decode( $result, true );
|
23 | 58 |
|
24 | 59 | if ( $result[ 'code' ] !== 200 ) {
|
|
0 commit comments