-
Notifications
You must be signed in to change notification settings - Fork 31
/
CloudFront.php
146 lines (125 loc) · 4.57 KB
/
CloudFront.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* A PHP5 class for invalidating Amazon CloudFront objects via its API.
*/
require_once 'HTTP/Request2.php'; // grab with "pear install --onlyreqdeps HTTP_Request2"
class CloudFront {
private $serviceUrl;
private $accessKeyId;
private $responseCode;
private $distributionId;
private $responseMessage;
/**
* 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/"){
$this->accessKeyId = $accessKeyId;
$this->secretKey = $secretKey;
$this->distributionId = $distributionId;
$this->serviceUrl = $serviceUrl;
}
/**
* Invalidates object with passed key on CloudFront
* @param $keys
* @return bool|string
* @internal param $key {String|Array} Key of object to be invalidated, or set of such keys Key of object to be invalidated, or set of such keys
*/
function invalidate($keys){
if (!is_array($keys)) {
$keys = array($keys);
}
$requestUrl = $this->serviceUrl . "2012-07-01/distribution/" . $this->distributionId . "/invalidation";
// assemble request body
$body = $this->makeRequestBody($keys);
// make and send request
$req = new HTTP_Request2($requestUrl, HTTP_Request2::METHOD_POST, array('ssl_verify_peer' => false));
$this->setRequestHeaders($req);
$req->setBody($body);
try {
$response = $req->send();
$this->responseCode = $response->getStatus();
switch ($this->responseCode) {
case 201:
$this->responseMessage = '201: Request accepted';
return true;
case 400:
$this->responseMessage = '400: Too many invalidations in progress. Retry in some time';
return false;
case 403:
$this->responseMessage = '403: Forbidden. Please check your security settings.';
return false;
default:
$this->responseMessage = $response->getStatus() . ': ' . $response->getReasonPhrase();
return false;
}
} catch (HTTP_Request2_Exception $e) {
$this->responseMessage = 'Error: ' . $e->getMessage();
return false;
}
}
/**
* Sets the common headers required by CloudFront API
* @param HTTP_Request2 $req
*/
private function setRequestHeaders(HTTP_Request2 $req)
{
$date = gmdate("D, d M Y G:i:s T");
$req->setHeader("Host", 'cloudfront.amazonaws.com');
$req->setHeader("Date", $date);
$req->setHeader("Authorization", $this->generateAuthKey($date));
$req->setHeader("Content-Type", "text/xml");
}
/**
* Makes the request body as expected by CloudFront API
* @param $objects objects to Invalidate
* @return string
*/
private function makeRequestBody($objects)
{
$body = '<?xml version="1.0" encoding="UTF-8"?>';
$body .= '<InvalidationBatch xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/">';
$body .= '<Paths>';
$body .= '<Quantity>' . count($objects) . '</Quantity>';
$body .= '<Items>';
foreach ($objects as $object) {
$object = (preg_match("/^\//", $object)) ? $object : "/" . $object;
$body .= "<Path>" . $object . "</Path>";
}
$body .= '</Items>';
$body .= '</Paths>';
$body .= "<CallerReference>" . time() . "</CallerReference>";
$body .= "</InvalidationBatch>";
return $body;
}
/**
* Returns header string containing encoded authentication key
* @param $date
* @return string
*/
private function generateAuthKey($date)
{
$signature = base64_encode(hash_hmac('sha1', $date, $this->secretKey, true));
return "AWS " . $this->accessKeyId . ":" . $signature;
}
/**
* Getter method for API response codes
* @return mixed
*/
public function getResponseCode()
{
return $this->responseCode;
}
/**
* Getter method to get the response messages generated by our class.
* @return mixed
*/
public function getResponseMessage()
{
return $this->responseMessage;
}
}
?>