| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
4 |
*
|
|
|
5 |
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
|
6 |
* You may not use this file except in compliance with the License.
|
|
|
7 |
* A copy of the License is located at
|
|
|
8 |
*
|
|
|
9 |
* http://aws.amazon.com/apache2.0
|
|
|
10 |
*
|
|
|
11 |
* or in the "license" file accompanying this file. This file is distributed
|
|
|
12 |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
|
13 |
* express or implied. See the License for the specific language governing
|
|
|
14 |
* permissions and limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
/*%******************************************************************************************%*/
|
|
|
19 |
// CLASS
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Contains a set of utility methods for connecting to, and working with, AWS.
|
|
|
23 |
*
|
|
|
24 |
* @version 2010.09.30
|
|
|
25 |
* @license See the included NOTICE.md file for more information.
|
|
|
26 |
* @copyright See the included NOTICE.md file for more information.
|
|
|
27 |
* @link http://aws.amazon.com/php/ PHP Developer Center
|
|
|
28 |
*/
|
|
|
29 |
class CFUtilities
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
/*%******************************************************************************************%*/
|
|
|
33 |
// CONSTANTS
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Define the RFC 2616-compliant date format.
|
|
|
37 |
*/
|
|
|
38 |
const DATE_FORMAT_RFC2616 = 'D, d M Y H:i:s \G\M\T';
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Define the ISO-8601-compliant date format.
|
|
|
42 |
*/
|
|
|
43 |
const DATE_FORMAT_ISO8601 = 'Y-m-d\TH:i:s\Z';
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Define the MySQL-compliant date format.
|
|
|
47 |
*/
|
|
|
48 |
const DATE_FORMAT_MYSQL = 'Y-m-d H:i:s';
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Define the Signature v4 date format.
|
|
|
52 |
*/
|
|
|
53 |
const DATE_FORMAT_SIGV4 = 'Ymd\THis\Z';
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
/*%******************************************************************************************%*/
|
|
|
57 |
// METHODS
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Constructs a new instance of this class.
|
|
|
61 |
*
|
|
|
62 |
* @return $this A reference to the current instance.
|
|
|
63 |
*/
|
|
|
64 |
public function __construct()
|
|
|
65 |
{
|
|
|
66 |
return $this;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Retrieves the value of a class constant, while avoiding the `T_PAAMAYIM_NEKUDOTAYIM` error. Misspelled because `const` is a reserved word.
|
|
|
71 |
*
|
|
|
72 |
* @param object $class (Required) An instance of the class containing the constant.
|
|
|
73 |
* @param string $const (Required) The name of the constant to retrieve.
|
|
|
74 |
* @return mixed The value of the class constant.
|
|
|
75 |
*/
|
|
|
76 |
public function konst($class, $const)
|
|
|
77 |
{
|
|
|
78 |
if (is_string($class))
|
|
|
79 |
{
|
|
|
80 |
$ref = new ReflectionClass($class);
|
|
|
81 |
}
|
|
|
82 |
else
|
|
|
83 |
{
|
|
|
84 |
$ref = new ReflectionObject($class);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return $ref->getConstant($const);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Convert a HEX value to Base64.
|
|
|
92 |
*
|
|
|
93 |
* @param string $str (Required) Value to convert.
|
|
|
94 |
* @return string Base64-encoded string.
|
|
|
95 |
*/
|
|
|
96 |
public function hex_to_base64($str)
|
|
|
97 |
{
|
|
|
98 |
$raw = '';
|
|
|
99 |
|
|
|
100 |
for ($i = 0; $i < strlen($str); $i += 2)
|
|
|
101 |
{
|
|
|
102 |
$raw .= chr(hexdec(substr($str, $i, 2)));
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return base64_encode($raw);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Convert an associative array into a query string.
|
|
|
110 |
*
|
|
|
111 |
* @param array $array (Required) Array to convert.
|
|
|
112 |
* @return string URL-friendly query string.
|
|
|
113 |
*/
|
|
|
114 |
public function to_query_string($array)
|
|
|
115 |
{
|
|
|
116 |
$temp = array();
|
|
|
117 |
|
|
|
118 |
foreach ($array as $key => $value)
|
|
|
119 |
{
|
|
|
120 |
if (is_string($key) && !is_array($value))
|
|
|
121 |
{
|
|
|
122 |
$temp[] = rawurlencode($key) . '=' . rawurlencode($value);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
return implode('&', $temp);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Convert an associative array into a sign-able string.
|
|
|
131 |
*
|
|
|
132 |
* @param array $array (Required) Array to convert.
|
|
|
133 |
* @return string URL-friendly sign-able string.
|
|
|
134 |
*/
|
|
|
135 |
public function to_signable_string($array)
|
|
|
136 |
{
|
|
|
137 |
$t = array();
|
|
|
138 |
|
|
|
139 |
foreach ($array as $k => $v)
|
|
|
140 |
{
|
|
|
141 |
$t[] = $this->encode_signature2($k) . '=' . $this->encode_signature2($v);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
return implode('&', $t);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Encode the value according to RFC 3986.
|
|
|
149 |
*
|
|
|
150 |
* @param string $string (Required) String to convert.
|
|
|
151 |
* @return string URL-friendly sign-able string.
|
|
|
152 |
*/
|
|
|
153 |
public function encode_signature2($string)
|
|
|
154 |
{
|
|
|
155 |
$string = rawurlencode($string);
|
|
|
156 |
return str_replace('%7E', '~', $string);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Convert a query string into an associative array. Multiple, identical keys will become an indexed array.
|
|
|
161 |
*
|
|
|
162 |
* @param string $qs (Required) Query string to convert.
|
|
|
163 |
* @return array Associative array of keys and values.
|
|
|
164 |
*/
|
|
|
165 |
public function query_to_array($qs)
|
|
|
166 |
{
|
|
|
167 |
$query = explode('&', $qs);
|
|
|
168 |
$data = array();
|
|
|
169 |
|
|
|
170 |
foreach ($query as $q)
|
|
|
171 |
{
|
|
|
172 |
$q = explode('=', $q);
|
|
|
173 |
|
|
|
174 |
if (isset($data[$q[0]]) && is_array($data[$q[0]]))
|
|
|
175 |
{
|
|
|
176 |
$data[$q[0]][] = urldecode($q[1]);
|
|
|
177 |
}
|
|
|
178 |
else if (isset($data[$q[0]]) && !is_array($data[$q[0]]))
|
|
|
179 |
{
|
|
|
180 |
$data[$q[0]] = array($data[$q[0]]);
|
|
|
181 |
$data[$q[0]][] = urldecode($q[1]);
|
|
|
182 |
}
|
|
|
183 |
else
|
|
|
184 |
{
|
|
|
185 |
$data[urldecode($q[0])] = urldecode($q[1]);
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
return $data;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Return human readable file sizes.
|
|
|
193 |
*
|
|
|
194 |
* @author Aidan Lister <aidan@php.net>
|
|
|
195 |
* @author Ryan Parman <ryan@getcloudfusion.com>
|
|
|
196 |
* @license http://www.php.net/license/3_01.txt PHP License
|
|
|
197 |
* @param integer $size (Required) Filesize in bytes.
|
|
|
198 |
* @param string $unit (Optional) The maximum unit to use. Defaults to the largest appropriate unit.
|
|
|
199 |
* @param string $default (Optional) The format for the return string. Defaults to `%01.2f %s`.
|
|
|
200 |
* @return string The human-readable file size.
|
|
|
201 |
* @link http://aidanlister.com/repos/v/function.size_readable.php Original Function
|
|
|
202 |
*/
|
|
|
203 |
public function size_readable($size, $unit = null, $default = null)
|
|
|
204 |
{
|
|
|
205 |
// Units
|
|
|
206 |
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
|
|
|
207 |
$mod = 1024;
|
|
|
208 |
$ii = count($sizes) - 1;
|
|
|
209 |
|
|
|
210 |
// Max unit
|
|
|
211 |
$unit = array_search((string) $unit, $sizes);
|
|
|
212 |
if ($unit === null || $unit === false)
|
|
|
213 |
{
|
|
|
214 |
$unit = $ii;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
// Return string
|
|
|
218 |
if ($default === null)
|
|
|
219 |
{
|
|
|
220 |
$default = '%01.2f %s';
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// Loop
|
|
|
224 |
$i = 0;
|
|
|
225 |
while ($unit != $i && $size >= 1024 && $i < $ii)
|
|
|
226 |
{
|
|
|
227 |
$size /= $mod;
|
|
|
228 |
$i++;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
return sprintf($default, $size, $sizes[$i]);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Convert a number of seconds into Hours:Minutes:Seconds.
|
|
|
236 |
*
|
|
|
237 |
* @param integer $seconds (Required) The number of seconds to convert.
|
|
|
238 |
* @return string The formatted time.
|
|
|
239 |
*/
|
|
|
240 |
public function time_hms($seconds)
|
|
|
241 |
{
|
|
|
242 |
$time = '';
|
|
|
243 |
|
|
|
244 |
// First pass
|
|
|
245 |
$hours = (int) ($seconds / 3600);
|
|
|
246 |
$seconds = $seconds % 3600;
|
|
|
247 |
$minutes = (int) ($seconds / 60);
|
|
|
248 |
$seconds = $seconds % 60;
|
|
|
249 |
|
|
|
250 |
// Cleanup
|
|
|
251 |
$time .= ($hours) ? $hours . ':' : '';
|
|
|
252 |
$time .= ($minutes < 10 && $hours > 0) ? '0' . $minutes : $minutes;
|
|
|
253 |
$time .= ':';
|
|
|
254 |
$time .= ($seconds < 10) ? '0' . $seconds : $seconds;
|
|
|
255 |
|
|
|
256 |
return $time;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* Returns the first value that is set. Based on [Try.these()](http://api.prototypejs.org/language/Try/these/) from [Prototype](http://prototypejs.org).
|
|
|
261 |
*
|
|
|
262 |
* @param array $attrs (Required) The attributes to test, as strings. Intended for testing properties of the $base object, but also works with variables if you place an @ symbol at the beginning of the command.
|
|
|
263 |
* @param object $base (Optional) The base object to use, if any.
|
|
|
264 |
* @param mixed $default (Optional) What to return if there are no matches. Defaults to `null`.
|
|
|
265 |
* @return mixed Either a matching property of a given object, boolean `false`, or any other data type you might choose.
|
|
|
266 |
*/
|
|
|
267 |
public function try_these($attrs, $base = null, $default = null)
|
|
|
268 |
{
|
|
|
269 |
if ($base)
|
|
|
270 |
{
|
|
|
271 |
foreach ($attrs as $attr)
|
|
|
272 |
{
|
|
|
273 |
if (isset($base->$attr))
|
|
|
274 |
{
|
|
|
275 |
return $base->$attr;
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
else
|
|
|
280 |
{
|
|
|
281 |
foreach ($attrs as $attr)
|
|
|
282 |
{
|
|
|
283 |
if (isset($attr))
|
|
|
284 |
{
|
|
|
285 |
return $attr;
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
return $default;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
/**
|
|
|
294 |
* Can be removed once all calls are updated.
|
|
|
295 |
*
|
|
|
296 |
* @deprecated Use <php:json_encode()> instead.
|
|
|
297 |
* @param mixed $obj (Required) The PHP object to convert into a JSON string.
|
|
|
298 |
* @return string A JSON string.
|
|
|
299 |
*/
|
|
|
300 |
public function json_encode($obj)
|
|
|
301 |
{
|
|
|
302 |
return json_encode($obj);
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Converts a SimpleXML response to an array structure.
|
|
|
307 |
*
|
|
|
308 |
* @param ResponseCore $response (Required) A response value.
|
|
|
309 |
* @return array The response value as a standard, multi-dimensional array.
|
|
|
310 |
*/
|
|
|
311 |
public function convert_response_to_array(ResponseCore $response)
|
|
|
312 |
{
|
|
|
313 |
return json_decode(json_encode($response), true);
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Checks to see if a date stamp is ISO-8601 formatted, and if not, makes it so.
|
|
|
318 |
*
|
|
|
319 |
* @param string $datestamp (Required) A date stamp, or a string that can be parsed into a date stamp.
|
|
|
320 |
* @return string An ISO-8601 formatted date stamp.
|
|
|
321 |
*/
|
|
|
322 |
public function convert_date_to_iso8601($datestamp)
|
|
|
323 |
{
|
|
|
324 |
if (!preg_match('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}((\+|-)\d{2}:\d{2}|Z)/m', $datestamp))
|
|
|
325 |
{
|
|
|
326 |
return gmdate(self::DATE_FORMAT_ISO8601, strtotime($datestamp));
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
return $datestamp;
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Determines whether the data is Base64 encoded or not.
|
|
|
334 |
*
|
|
|
335 |
* @license http://us.php.net/manual/en/function.base64-decode.php#81425 PHP License
|
|
|
336 |
* @param string $s (Required) The string to test.
|
|
|
337 |
* @return boolean Whether the string is Base64 encoded or not.
|
|
|
338 |
*/
|
|
|
339 |
public function is_base64($s)
|
|
|
340 |
{
|
|
|
341 |
return (bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s);
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Determines whether the data is a JSON string or not.
|
|
|
346 |
*
|
|
|
347 |
* @param string $s (Required) The string to test.
|
|
|
348 |
* @return boolean Whether the string is a valid JSON object or not.
|
|
|
349 |
*/
|
|
|
350 |
public function is_json($s)
|
|
|
351 |
{
|
|
|
352 |
return !!(json_decode($s) instanceof stdClass);
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Decodes `\uXXXX` entities into their real unicode character equivalents.
|
|
|
357 |
*
|
|
|
358 |
* @param string $s (Required) The string to decode.
|
|
|
359 |
* @return string The decoded string.
|
|
|
360 |
*/
|
|
|
361 |
public function decode_uhex($s)
|
|
|
362 |
{
|
|
|
363 |
preg_match_all('/\\\u([0-9a-f]{4})/i', $s, $matches);
|
|
|
364 |
$matches = $matches[count($matches) - 1];
|
|
|
365 |
$map = array();
|
|
|
366 |
|
|
|
367 |
foreach ($matches as $match)
|
|
|
368 |
{
|
|
|
369 |
if (!isset($map[$match]))
|
|
|
370 |
{
|
|
|
371 |
$map['\u' . $match] = html_entity_decode('&#' . hexdec($match) . ';', ENT_NOQUOTES, 'UTF-8');
|
|
|
372 |
}
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
return str_replace(array_keys($map), $map, $s);
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
/**
|
|
|
379 |
* Generates a random GUID.
|
|
|
380 |
*
|
|
|
381 |
* @author Alix Axel <http://www.php.net/manual/en/function.com-create-guid.php#99425>
|
|
|
382 |
* @license http://www.php.net/license/3_01.txt PHP License
|
|
|
383 |
* @return string A random GUID.
|
|
|
384 |
*/
|
|
|
385 |
public function generate_guid()
|
|
|
386 |
{
|
|
|
387 |
return sprintf(
|
|
|
388 |
'%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
|
|
|
389 |
mt_rand(0, 65535),
|
|
|
390 |
mt_rand(0, 65535),
|
|
|
391 |
mt_rand(0, 65535),
|
|
|
392 |
mt_rand(16384, 20479),
|
|
|
393 |
mt_rand(32768, 49151),
|
|
|
394 |
mt_rand(0, 65535),
|
|
|
395 |
mt_rand(0, 65535),
|
|
|
396 |
mt_rand(0, 65535)
|
|
|
397 |
);
|
|
|
398 |
}
|
|
|
399 |
}
|