Skip to content

Commit 3cd498b

Browse files
scottgonzalezgnarf
authored andcommitted
Coding standards / simplify purge script. Closes gh-3
1 parent 813aab6 commit 3cd498b

File tree

2 files changed

+26
-46
lines changed

2 files changed

+26
-46
lines changed

config-sample.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"username": "admin",
44
"password": "secret",
55
"cdn": {
6-
"alias": "jquery",
76
"consumer_key": "key",
8-
"consumer_secret": "secret",
9-
"zone_id": 1
7+
"consumer_secret": "secret"
108
}
119
}

purge.php

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,35 @@
11
<?php
2-
//requires https://github.com/netdna/netdnarws-php
3-
require_once('netdnarws-php/NetDNA.php');
2+
require_once( 'netdnarws-php/NetDNA.php' );
43

5-
$config = json_decode(file_get_contents('./config.json'), true);
6-
$config = $config["cdn"];
4+
$config = json_decode( file_get_contents( './config.json' ), true );
5+
$config = $config[ 'cdn' ];
6+
$zone_id = 1;
77

8-
//place your alias, key, secret into this constructor
9-
$api = new NetDNA($config["alias"], $config["consumer_key"], $config["consumer_secret"]);
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.';
12+
exit;
13+
}
1014

11-
function purgeCacheFileFromCDN($id, $files = null) {
15+
$file = $parts[ 0 ];
1216

13-
global $api;
14-
//3 options for purge
15-
$result = null;
16-
17-
if ($files == null){
18-
$result = $api->delete('/zones/pull.json/'.$id.'/cache');
19-
} else if (!is_array($files)){
20-
//Purge single file
21-
$params = array('file'=>$files);
22-
$result = $api->delete('/zones/pull.json/'.$id.'/cache',$params);
23-
} else if (is_array($files)){
24-
//Purge multiple files
25-
$params = array();
26-
foreach ($files as $k=>$v) $params[$k] = $v;
27-
$result = $api->delete('/zones/pull.json/'.$id.'/cache',$params);
28-
}
17+
header( 'Content-Type: text/plain' );
18+
echo "Attempting to purge: $zone_id: $file.\n";
2919

30-
$result = json_decode($result,true);
31-
if ($result['code'] != 200) {
32-
echo 'Error reported: '.print_r( $result, 1 );
33-
} else {
34-
echo "Done\n";
35-
}
20+
$api = new NetDNA( 'jquery', $config[ 'consumer_key' ], $config[ 'consumer_secret' ] );
3621

22+
if ( empty( $file ) ) {
23+
// Purge all files
24+
$result = $api->delete( '/zones/pull.json/' . $zone_id . '/cache' );
25+
} else {
26+
// Purge single file
27+
$result = $api->delete( '/zones/pull.json/' . $zone_id . '/cache', array( 'file' => $file ) );
3728
}
3829

39-
//set zone id that you want to purge
40-
$zone_id = $config["zone_id"];
41-
42-
$parts = preg_split( "/\?reload=?/", $_SERVER["REQUEST_URI"] );
43-
if ( !$parts ) {
44-
header("400 Bad Request");
45-
echo $_SERVER["REQUEST_URI"]." is not a valid URI";
46-
exit;
30+
$result = json_decode( $result, true );
31+
if ( $result[ 'code' ] !== 200 ) {
32+
echo 'Error reported: ' . print_r( $result, true );
33+
} else {
34+
echo 'Done\n';
4735
}
48-
49-
$file = $parts[0];
50-
51-
header( "Content-Type: text/plain" );
52-
echo "Attempting to purge: ".$zone_id.": ".$file."\n";
53-
purgeCacheFileFromCDN( $zone_id, $file );

0 commit comments

Comments
 (0)