-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloudfront_invalidator.php
More file actions
175 lines (162 loc) · 6.57 KB
/
Cloudfront_invalidator.php
File metadata and controls
175 lines (162 loc) · 6.57 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*
* Cloudfront_invalidator.php
*
* Heavily inspired by: https://github.com/subchild/CloudFront-PHP-Invalidator but I wanted
* something pure PHP that didn't require additional modules to be installed.
* Also I wanted something that was CodeIgniter friendly.
*
* Example usage:
* 1. require 'Cloudfront_invalidator.php';
* 2. $invalidator = new Cloudfront_invalidator('AWS ACCESS KEY', 'AWS SECRET', 'CLOUDFRONT DIST ID');
* 3. try {
* $invalidator->invalidate('/images/*');
* } catch(Exception $e) {
* echo $e->getMessage() . "\n";
* }
*
*
* Copyright (C) 2015 KISS IT Consulting <http://www.kissitconsulting.com/>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class Cloudfront_invalidator {
private $url;
private $access_key;
private $secret_key;
private $dist_id;
private $response_code = 0;
private $response = '';
// Our standard constructor. Pass in url to override the standard cloudfront URL.
function __construct($access_key = null, $secret_key = null, $dist_id = null, $url = "https://cloudfront.amazonaws.com") {
$this->setAwsInfo($access_key, $secret_key, $dist_id, $url);
}
// Function to invalidate either a single passed in key or an array of keys.
// Returns true on success, otherwise an exception is raised.
public function invalidate($keys) {
// Validate that we have our AWS creds
if(!$this->validateAws()) {
throw new Exception("Required AWS credentials not provided");
}
// Make and send request using cURL
$return = false;
if (!is_array($keys)) {
$keys = array($keys);
}
$this->call($keys);
switch ($this->response_code) {
case 201:
$this->response = '201: Request accepted';
$return = true;
break;
case 400:
$this->response = '400: Too many invalidations in progress. Retry in some time';
break;
case 403:
$this->response = '403: Forbidden. Please check your security settings.';
break;
default:
$this->response = $this->response_code . ': Unhandled response code';
break;
}
if(!$return) {
throw new Exception($this->response);
} else {
return true;
}
}
// Getter for the response code
public function getResponseCode() {
return $this->response_code;
}
// Getter for the response (or message)
public function getResponse() {
return $this->response;
}
// Set our AWS info
public function setAwsInfo($access_key, $secret_key, $dist_id, $url = null) {
$this->access_key = $access_key;
$this->secret_key = $secret_key;
$this->dist_id = $dist_id;
if(!empty($url)) {
$this->url = $url;
}
}
// Validate that we have our information needed to process the request
private function validateAws() {
if(empty($this->access_key) || empty($this->secret_key) || empty($this->dist_id) || empty($this->url)) {
return false;
} else {
return true;
}
}
// Function to make a web request to the API using cURL
private function call($keys) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$this->url}/2012-07-01/distribution/{$this->dist_id}/invalidation");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getRequestHeaders());
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->makeRequestBody($keys));
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$this->response_code = $info['http_code'];
$this->response = $result;
curl_close($ch);
return $result;
}
// Build the headers required by AWS for the request
private function getRequestHeaders() {
$date = gmdate("D, d M Y G:i:s T");
$headers = array();
$headers[] = "Host: cloudfront.amazonaws.com";
$headers[] = "Date: $date";
$headers[] = "Authorization: " . $this->generateAuthKey($date);
$headers[] = "Content-Type: text/xml";
return $headers;
}
// Function to build a request body as per AWS API
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;
}
// Generate a header string containing encoded authentication key
private function generateAuthKey($date) {
$signature = base64_encode(hash_hmac('sha1', $date, $this->secret_key, true));
return "AWS " . $this->access_key . ":" . $signature;
}
}
?>