Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 65 additions & 37 deletions CloudFront.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@

/**
* A PHP5 class for invalidating Amazon CloudFront objects via its API.
*
* Original source:
* https://github.com/subchild/CloudFront-PHP-Invalidator
*
* codebynumbers - Remove dependencies by replacing PEAR/Http_request with Curl
* RobFerrer - fixed the debug functionality to bring it in line with the master branch
*/

require_once 'HTTP/Request.php'; // grab with "pear install --onlyreqdeps HTTP_Request"
class CloudFront {

protected $serviceUrl;
protected $accessKeyId;
protected $responseBody;
protected $responseCode;
protected $distributionId;
protected $debug;


class CloudFront {

var $serviceUrl;
var $accessKeyId;
var $responseBody;
var $responseCode;
var $distributionId;


/**
* Constructs a CloudFront object and assigns required account values
* @param $accessKeyId {String} AWS access key id
* @param $secretKey {String} AWS secret key
* @param $distributionId {String} CloudFront distribution id
* @param $serviceUrl {String} Optional parameter for overriding cloudfront api URL
*/
function __construct($accessKeyId, $secretKey, $distributionId, $serviceUrl="https://cloudfront.amazonaws.com/"){
public function __construct($accessKeyId, $secretKey, $distributionId, $serviceUrl="https://cloudfront.amazonaws.com/"){
$this->accessKeyId = $accessKeyId;
$this->secretKey = $secretKey;
$this->distributionId = $distributionId;
$this->serviceUrl = $serviceUrl;
$this->serviceUrl = $serviceUrl;
}


/**
* Invalidates object with passed key on CloudFront
* @param $key {String|Array} Key of object to be invalidated, or set of such keys
*/
function invalidate($keys, $debug=false){
*/
public function invalidate($keys, $debug=false){
if (!is_array($keys)){
$keys = array($keys);
}
Expand All @@ -49,50 +53,65 @@ function invalidate($keys, $debug=false){
}
$body .= "<CallerReference>".time()."</CallerReference>";
$body .= "</InvalidationBatch>";
// make and send request
$req = & new HTTP_Request($requestUrl);
$req->setMethod("POST");
$req->addHeader("Date", $date);
$req->addHeader("Authorization", $this->makeKey($date));
$req->addHeader("Content-Type", "text/xml");
$req->setBody($body);
$response = $req->sendRequest();
$this->responseCode = $req->getResponseCode();
// make and send request

$cURL_Session = curl_init();
curl_setopt($cURL_Session, CURLOPT_URL, $requestUrl);
curl_setopt($cURL_Session, CURLOPT_HTTPHEADER,
array(
"Date: $date",
'Authorization: '.$this->makeKey($date),
"Content-Type: text/xml"
)
);


curl_setopt($cURL_Session, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($cURL_Session, CURLOPT_POST, 1);
curl_setopt($cURL_Session, CURLOPT_POSTFIELDS, $body);
curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);

$this->responseBody = curl_exec($cURL_Session);
$this->responseCode = curl_getinfo($cURL_Session, CURLINFO_HTTP_CODE);

if ($debug==true){
$er = array();
array_push($er, "CloudFront: Invalidating Object: $key");
array_push($er, $requestUrl);
array_push($er, "body: $body");
array_push($er, "response: $response");
array_push($er, "response string: " . $req->getResponseBody());
array_push($er, "response: $this->responseBody");
array_push($er, "");
array_push($er, "response code: " . $this->responseCode);
array_push($er, "");
return implode("\n",$er);
$this->debug = implode("\n",$er);

return $this->debug;
}
else {
else
{
return ($this->responseCode === 201);
}
}


/**
* Returns header string containing encoded authentication key
* @param $date {Date}
* @return {String}
*/
function makeKey($date){
public function makeKey($date){
return "AWS " . $this->accessKeyId . ":" . base64_encode($this->hmacSha1($this->secretKey, $date));
}


/**
* Returns HMAC string
* @param $key {String}
* @param $date {Date}
* @return {String}
*/
function hmacSha1($key, $date){
*/
public function hmacSha1($key, $date){
$blocksize = 64;
$hashfunc = 'sha1';
if (strlen($key)>$blocksize){
Expand All @@ -104,5 +123,14 @@ function hmacSha1($key, $date){
$hmac = pack('H*', $hashfunc( ($key^$opad).pack('H*',$hashfunc(($key^$ipad).$date)) ));
return $hmac;
}

/**
* Returns debugging info
* @return {String}
*/
public function get_debug() {
return $this->debug;
}

}
?>
?>
26 changes: 14 additions & 12 deletions example.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?
require 'CloudFront.php';

// your AWS/CloudFront keys go here
$keyId = "";
$keyId = "";
$secretKey = "";
$distributionId = "";
$key = ""; // String representing the existing CloudFront object to invalidate
$cf = new CloudFront($keyId, $secretKey, $distributionId);

$key = ""; // String representing the existing CloudFront object to invalidate, no leading slash

$cf = new CloudFront($keyId, $secretKey, $distributionId); // cloudfront distribution id for S3 bucket, not the bucket name
?>
<html>
<head>
Expand All @@ -18,10 +18,12 @@
Key: <?=$key?><br/>
<hr/>
CF call:<br/>
<?/*
Passing "true" to enable debugging for the purpose of this example.
This will render the XML response.
*/?>
<textarea><?=$cf->invalidate($key, true)?></textarea>
<textarea>
<?
if (!$cf->invalidate($key) ) {
echo $cf->debug();
}
?>
</textarea>
</body>
</html>
</html>