Skip to content

Commit cd0d064

Browse files
committed
purge: Improve documentation and error handling
Ref jquery/infrastructure#438.
1 parent 4ec6b0d commit cd0d064

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

purge.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,59 @@
11
<?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';
315

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';
717

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";
1224
exit;
1325
}
1426

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+
}
1648

1749
header( 'Content-Type: text/plain' );
18-
echo "Attempting to purge: $zone_id: $file.\n";
50+
echo "Attempting to purge:\n\t$file\n\n";
1951

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+
);
2257
$result = json_decode( $result, true );
2358

2459
if ( $result[ 'code' ] !== 200 ) {

0 commit comments

Comments
 (0)