-
Notifications
You must be signed in to change notification settings - Fork 10
/
_utilities.class.php
475 lines (438 loc) · 10.1 KB
/
_utilities.class.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* File: CFUtilities
* Utilities for connecting to, and working with, AWS.
*
* Version:
* 2009.08.24
*
* Copyright:
* 2006-2009 Foleeo, Inc., and contributors.
*
* License:
* Simplified BSD License - http://opensource.org/licenses/bsd-license.php
*
* See Also:
* CloudFusion - http://getcloudfusion.com
*/
/*%******************************************************************************************%*/
// CLASS
/**
* Class: CFUtilities
* Container for all utility-related methods.
*/
class CFUtilities
{
/**
* Method: __construct()
* The constructor
*
* Access:
* public
*
* Returns:
* <CFUtilities> object
*/
public function __construct()
{
return $this;
}
/**
* Method: hex_to_base64()
* Convert a HEX value to Base64.
*
* Access:
* public
*
* Parameters:
* str - _string_ (Required) Value to convert.
*
* Returns:
* _string_ Base64-encoded string.
*
* Examples:
* example::utilities/hex_to_base64.phpt:
*/
public function hex_to_base64($str)
{
$raw = '';
for ($i = 0; $i < strlen($str); $i += 2)
{
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
/**
* Method: to_query_string()
* Convert an associative array into a query string.
*
* Access:
* public
*
* Parameters:
* array - _array_ (Required) Array to convert.
*
* Returns:
* _string_ URL-friendly query string.
*
* Examples:
* example::utilities/to_query_string.phpt:
*/
public function to_query_string($array)
{
return http_build_query($array);
}
/**
* Method: to_signable_string()
* Convert an associative array into a sign-able string.
*
* Access:
* public
*
* Parameters:
* array - _array_ (Required) Array to convert.
*
* Returns:
* _string_ URL-friendly sign-able string.
*
* Examples:
* example::utilities/to_signable_string.phpt:
*/
public function to_signable_string($array)
{
$t = array();
foreach ($array as $k => $v)
{
$t[] = $this->encode_signature2($k) . '=' . $this->encode_signature2($v);
}
return implode('&', $t);
}
/**
* Method: encode_signature2()
* Encode the value according to RFC 3986.
*
* Access:
* public
*
* Parameters:
* string - _string_ (Required) String to convert
*
* Returns:
* _string_ URL-friendly sign-able string.
*/
public function encode_signature2($string)
{
$string = rawurlencode($string);
return str_replace('%7E', '~', $string);
}
/**
* Method: query_to_array()
* Convert a query string into an associative array. Multiple, identical keys will become an indexed array.
*
* Access:
* public
*
* Parameters:
* qs - _string_ (Required) Query string to convert.
*
* Returns:
* _array_ Associative array of keys and values.
*
* Examples:
* example::utilities/query_to_array.phpt:
* example::utilities/query_to_array2.phpt:
*/
public function query_to_array($qs)
{
$query = explode('&', $qs);
$data = array();
foreach ($query as $q)
{
$q = explode('=', $q);
if (isset($data[$q[0]]) && is_array($data[$q[0]]))
{
$data[$q[0]][] = urldecode($q[1]);
}
else if (isset($data[$q[0]]) && !is_array($data[$q[0]]))
{
$data[$q[0]] = array($data[$q[0]]);
$data[$q[0]][] = urldecode($q[1]);
}
else
{
$data[urldecode($q[0])] = urldecode($q[1]);
}
}
return $data;
}
/**
* Method: size_readable()
* Return human readable file sizes. Original function by Aidan Lister <mailto:aidan@php.net>, modified by Ryan Parman.
*
* Access:
* public
*
* Parameters:
* size - _integer_ (Required) Filesize in bytes.
* unit - _string_ (Optional) The maximum unit to use. Defaults to the largest appropriate unit.
* retstring - _string_ (Optional) The format for the return string. Defaults to '%01.2f %s'
*
* Returns:
* _string_ The human-readable file size.
*
* Examples:
* example::utilities/size_readable.phpt:
* example::utilities/size_readable2.phpt:
* example::utilities/size_readable3.phpt:
*
* See Also:
* Original Function - http://aidanlister.com/repos/v/function.size_readable.php
*/
public function size_readable($size, $unit = null, $retstring = null)
{
// Units
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
$mod = 1024;
$ii = count($sizes) - 1;
// Max unit
$unit = array_search((string) $unit, $sizes);
if ($unit === null || $unit === false)
{
$unit = $ii;
}
// Return string
if ($retstring === null)
{
$retstring = '%01.2f %s';
}
// Loop
$i = 0;
while ($unit != $i && $size >= 1024 && $i < $ii)
{
$size /= $mod;
$i++;
}
return sprintf($retstring, $size, $sizes[$i]);
}
/**
* Method: time_hms()
* Convert a number of seconds into Hours:Minutes:Seconds.
*
* Access:
* public
*
* Parameters:
* seconds - _integer_ (Required) The number of seconds to convert.
*
* Returns:
* _string_ The formatted time.
*
* Examples:
* example::utilities/time_hms.phpt:
*/
public function time_hms($seconds)
{
$time = '';
// First pass
$hours = (int) ($seconds / 3600);
$seconds = $seconds % 3600;
$minutes = (int) ($seconds / 60);
$seconds = $seconds % 60;
// Cleanup
$time .= ($hours) ? $hours . ':' : '';
$time .= ($minutes < 10 && $hours > 0) ? '0' . $minutes : $minutes;
$time .= ':';
$time .= ($seconds < 10) ? '0' . $seconds : $seconds;
return $time;
}
/**
* Method: try_these()
* Returns the first value that is set. Based on Try.these() from Prototype <http://prototypejs.org>.
*
* Access:
* public
*
* Parameters:
* attrs - _array_ (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.
* base - _object_ (Optional) The base object to use, if any.
* default - _mixed_ (Optional) What to return if there are no matches. Defaults to null.
*
* Returns:
* _mixed_ Either a matching property of a given object, _boolean_ false, or any other data type you might choose.
*
* Examples:
* example::utilities/try_these.phpt:
* example::utilities/try_these2.phpt:
* example::utilities/try_these3.phpt:
* example::utilities/try_these4.phpt:
* example::utilities/try_these5.phpt:
*/
public function try_these($attrs, $base = null, $default = null)
{
if ($base)
{
foreach ($attrs as $attr)
{
if (isset($base->$attr))
{
return $base->$attr;
}
}
}
else
{
foreach ($attrs as $attr)
{
if (isset($attr))
{
return $attr;
}
}
}
return $default;
}
/**
* Method: json_encode()
* Replicates json_encode() for versions of PHP 5 earlier than 5.2.
*
* Access:
* public
*
* Parameters:
* obj - _mixed_ (Required) The PHP object to convert into a JSON string.
*
* Returns:
* _string_ A JSON string.
*
* Examples:
* example::utilities/json_encode2.phpt:
* example::utilities/json_encode3.phpt:
* example::utilities/json_encode4.phpt:
* example::utilities/json_encode5.phpt:
* example::utilities/json_encode6.phpt:
*/
public function json_encode($obj)
{
if (function_exists('json_encode'))
{
return json_encode($obj);
}
return $this->json_encode_php51($obj);
}
/**
* Method: json_encode_php51()
* Called by CFUtilities::json_encode() if PHP 5.2's json_encode() is unavailable. DO NOT CALL THIS METHOD DIRECTLY! Use $obj->util->json_encode() instead.
*
* Author:
* http://us2.php.net/manual/en/function.json-encode.php#82904
*
* Access:
* public
*
* Parameters:
* obj - _mixed_ (Required) The PHP object to convert into a JSON string.
*
* Returns:
* _string_ A JSON string.
*/
public function json_encode_php51($obj)
{
if (is_null($obj)) return 'null';
if ($obj === false) return 'false';
if ($obj === true) return 'true';
if (is_scalar($obj))
{
if (is_float($obj))
{
// Always use '.' for floats.
return str_replace(',', '.', strval($obj));
}
elseif (is_int($obj))
{
return strval($obj);
}
elseif (is_string($obj))
{
static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $obj) . '"';
}
return $obj;
}
$isList = true;
for ($i = 0, reset($obj); $i < count($obj); $i++, next($obj))
{
if (key($obj) !== $i)
{
$isList = false;
break;
}
}
$result = array();
if ($isList)
{
foreach ($obj as $v)
{
$result[] = json_encode($v);
}
return '[' . join(',', $result) . ']';
}
else
{
foreach ($obj as $k => $v)
{
$result[] = json_encode($k).':'.json_encode($v);
}
return '{' . join(',', $result) . '}';
}
}
/**
* Method: convert_response_to_array()
* Converts a SimpleXML response to an array structure.
*
* Author:
* Adrien Cahen <http://gaarf.info/2009/08/13/xml-string-to-php-array/>
*
* Access:
* public
*
* Parameters:
* obj - _ResponseCore_ (Required) A CloudFusion ResponseCore response value.
*
* Returns:
* _array_ The response value as a standard, multi-dimensional array.
*
* Examples:
* example::utilities/convert_response_to_array.phpt:
*
* Requirements:
* PHP 5.2 or newer.
*/
public function convert_response_to_array(ResponseCore $response)
{
return json_decode(json_encode((array) $response), true);
}
/**
* Method: convert_date_to_iso8601()
* Checks to see if a date stamp is ISO-8601 formatted, and if not, makes it so.
*
* Access:
* public
*
* Parameters:
* datestamp - _string_ (Required) A date stamp, or a string that can be parsed into a date stamp.
*
* Returns:
* _string_ An ISO-8601 formatted date stamp.
*
* Examples:
* example::utilities/convert_date_to_iso8601.phpt:
*/
public function convert_date_to_iso8601($datestamp)
{
if (!preg_match('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}((\+|-)\d{2}:\d{2}|Z)/m', $datestamp))
{
return gmdate(DATE_FORMAT_ISO8601, strtotime($datestamp));
}
return $datestamp;
}
}