PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/aws-sdk/utilities/utilities.class.php

http://github.com/joebeeson/amazon
PHP | 515 lines | 185 code | 46 blank | 284 comment | 20 complexity | a0d9ea14ac220c2c6f71e44cf723ed02 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright 2010 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. * File: CFUtilities
  18. * Utilities for connecting to, and working with, AWS.
  19. *
  20. * Version:
  21. * 2010.09.30
  22. *
  23. * License and Copyright:
  24. * See the included NOTICE.md file for more information.
  25. *
  26. * See Also:
  27. * [PHP Developer Center](http://aws.amazon.com/php/)
  28. */
  29. /*%******************************************************************************************%*/
  30. // CLASS
  31. /**
  32. * Class: CFUtilities
  33. * Container for all utility-related methods.
  34. */
  35. class CFUtilities
  36. {
  37. /*%******************************************************************************************%*/
  38. // CONSTANTS
  39. /**
  40. * Constant: DATE_FORMAT_RFC2616
  41. * Define the RFC 2616-compliant date format.
  42. */
  43. const DATE_FORMAT_RFC2616 = 'D, d M Y H:i:s \G\M\T';
  44. /**
  45. * Constant: DATE_FORMAT_ISO8601
  46. * Define the ISO-8601-compliant date format.
  47. */
  48. const DATE_FORMAT_ISO8601 = 'Y-m-d\TH:i:s\Z';
  49. /**
  50. * Constant: DATE_FORMAT_MYSQL
  51. * Define the MySQL-compliant date format.
  52. */
  53. const DATE_FORMAT_MYSQL = 'Y-m-d H:i:s';
  54. /*%******************************************************************************************%*/
  55. // METHODS
  56. /**
  57. * Method: __construct()
  58. * The constructor.
  59. *
  60. * Access:
  61. * public
  62. *
  63. * Returns:
  64. * <CFUtilities> object
  65. */
  66. public function __construct()
  67. {
  68. return $this;
  69. }
  70. /**
  71. * Method: konst()
  72. * Retrieves the value of a class constant, while avoiding the `T_PAAMAYIM_NEKUDOTAYIM` error. Misspelled because `const` is a reserved word.
  73. *
  74. * Access:
  75. * public
  76. *
  77. * Parameters:
  78. * $class - _object_ (Required) An instance of the class containing the constant.
  79. * $const - _string_ (Required) The name of the constant to retrieve.
  80. *
  81. * Returns:
  82. * _mixed_ The value of the class constant.
  83. */
  84. public function konst($class, $const)
  85. {
  86. if (is_string($class))
  87. {
  88. $ref = new ReflectionClass($class);
  89. }
  90. else
  91. {
  92. $ref = new ReflectionObject($class);
  93. }
  94. return $ref->getConstant($const);
  95. }
  96. /**
  97. * Method: hex_to_base64()
  98. * Convert a HEX value to Base64.
  99. *
  100. * Access:
  101. * public
  102. *
  103. * Parameters:
  104. * $str - _string_ (Required) Value to convert.
  105. *
  106. * Returns:
  107. * _string_ Base64-encoded string.
  108. */
  109. public function hex_to_base64($str)
  110. {
  111. $raw = '';
  112. for ($i = 0; $i < strlen($str); $i += 2)
  113. {
  114. $raw .= chr(hexdec(substr($str, $i, 2)));
  115. }
  116. return base64_encode($raw);
  117. }
  118. /**
  119. * Method: to_query_string()
  120. * Convert an associative array into a query string.
  121. *
  122. * Access:
  123. * public
  124. *
  125. * Parameters:
  126. * $array - _array_ (Required) Array to convert.
  127. *
  128. * Returns:
  129. * _string_ URL-friendly query string.
  130. */
  131. public function to_query_string($array)
  132. {
  133. $temp = array();
  134. foreach ($array as $key => $value)
  135. {
  136. $temp[] = rawurlencode($key) . '=' . rawurlencode($value);
  137. }
  138. return implode('&', $temp);
  139. }
  140. /**
  141. * Method: to_signable_string()
  142. * Convert an associative array into a sign-able string.
  143. *
  144. * Access:
  145. * public
  146. *
  147. * Parameters:
  148. * $array - _array_ (Required) Array to convert.
  149. *
  150. * Returns:
  151. * _string_ URL-friendly sign-able string.
  152. */
  153. public function to_signable_string($array)
  154. {
  155. $t = array();
  156. foreach ($array as $k => $v)
  157. {
  158. $t[] = $this->encode_signature2($k) . '=' . $this->encode_signature2($v);
  159. }
  160. return implode('&', $t);
  161. }
  162. /**
  163. * Method: encode_signature2()
  164. * Encode the value according to RFC 3986.
  165. *
  166. * Access:
  167. * public
  168. *
  169. * Parameters:
  170. * $string - _string_ (Required) String to convert
  171. *
  172. * Returns:
  173. * _string_ URL-friendly sign-able string.
  174. */
  175. public function encode_signature2($string)
  176. {
  177. $string = rawurlencode($string);
  178. return str_replace('%7E', '~', $string);
  179. }
  180. /**
  181. * Method: query_to_array()
  182. * Convert a query string into an associative array. Multiple, identical keys will become an indexed array.
  183. *
  184. * Access:
  185. * public
  186. *
  187. * Parameters:
  188. * $qs - _string_ (Required) Query string to convert.
  189. *
  190. * Returns:
  191. * _array_ Associative array of keys and values.
  192. */
  193. public function query_to_array($qs)
  194. {
  195. $query = explode('&', $qs);
  196. $data = array();
  197. foreach ($query as $q)
  198. {
  199. $q = explode('=', $q);
  200. if (isset($data[$q[0]]) && is_array($data[$q[0]]))
  201. {
  202. $data[$q[0]][] = urldecode($q[1]);
  203. }
  204. else if (isset($data[$q[0]]) && !is_array($data[$q[0]]))
  205. {
  206. $data[$q[0]] = array($data[$q[0]]);
  207. $data[$q[0]][] = urldecode($q[1]);
  208. }
  209. else
  210. {
  211. $data[urldecode($q[0])] = urldecode($q[1]);
  212. }
  213. }
  214. return $data;
  215. }
  216. /**
  217. * Method: size_readable()
  218. * Return human readable file sizes.
  219. *
  220. * Author:
  221. * Aidan Lister <aidan@php.net>
  222. * Ryan Parman <ryan@getcloudfusion.com>
  223. *
  224. * Access:
  225. * public
  226. *
  227. * License:
  228. * [PHP License](http://www.php.net/license/3_01.txt)
  229. *
  230. * Parameters:
  231. * $size - _integer_ (Required) Filesize in bytes.
  232. * $unit - _string_ (Optional) The maximum unit to use. Defaults to the largest appropriate unit.
  233. * $default - _string_ (Optional) The format for the return string. Defaults to '%01.2f %s'
  234. *
  235. * Returns:
  236. * _string_ The human-readable file size.
  237. *
  238. * See Also:
  239. * Original Function - http://aidanlister.com/repos/v/function.size_readable.php
  240. */
  241. public function size_readable($size, $unit = null, $default = null)
  242. {
  243. // Units
  244. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
  245. $mod = 1024;
  246. $ii = count($sizes) - 1;
  247. // Max unit
  248. $unit = array_search((string) $unit, $sizes);
  249. if ($unit === null || $unit === false)
  250. {
  251. $unit = $ii;
  252. }
  253. // Return string
  254. if ($default === null)
  255. {
  256. $default = '%01.2f %s';
  257. }
  258. // Loop
  259. $i = 0;
  260. while ($unit != $i && $size >= 1024 && $i < $ii)
  261. {
  262. $size /= $mod;
  263. $i++;
  264. }
  265. return sprintf($default, $size, $sizes[$i]);
  266. }
  267. /**
  268. * Method: time_hms()
  269. * Convert a number of seconds into Hours:Minutes:Seconds.
  270. *
  271. * Access:
  272. * public
  273. *
  274. * Parameters:
  275. * $seconds - _integer_ (Required) The number of seconds to convert.
  276. *
  277. * Returns:
  278. * _string_ The formatted time.
  279. */
  280. public function time_hms($seconds)
  281. {
  282. $time = '';
  283. // First pass
  284. $hours = (int) ($seconds / 3600);
  285. $seconds = $seconds % 3600;
  286. $minutes = (int) ($seconds / 60);
  287. $seconds = $seconds % 60;
  288. // Cleanup
  289. $time .= ($hours) ? $hours . ':' : '';
  290. $time .= ($minutes < 10 && $hours > 0) ? '0' . $minutes : $minutes;
  291. $time .= ':';
  292. $time .= ($seconds < 10) ? '0' . $seconds : $seconds;
  293. return $time;
  294. }
  295. /**
  296. * Method: try_these()
  297. * Returns the first value that is set. Based on [Try.these()](http://api.prototypejs.org/language/try/these/) from [Prototype](http://prototypejs.org).
  298. *
  299. * Access:
  300. * public
  301. *
  302. * Parameters:
  303. * $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.
  304. * $base - _object_ (Optional) The base object to use, if any.
  305. * $default - _mixed_ (Optional) What to return if there are no matches. Defaults to null.
  306. *
  307. * Returns:
  308. * _mixed_ Either a matching property of a given object, _boolean_ false, or any other data type you might choose.
  309. */
  310. public function try_these($attrs, $base = null, $default = null)
  311. {
  312. if ($base)
  313. {
  314. foreach ($attrs as $attr)
  315. {
  316. if (isset($base->$attr))
  317. {
  318. return $base->$attr;
  319. }
  320. }
  321. }
  322. else
  323. {
  324. foreach ($attrs as $attr)
  325. {
  326. if (isset($attr))
  327. {
  328. return $attr;
  329. }
  330. }
  331. }
  332. return $default;
  333. }
  334. /**
  335. * Method: json_encode()
  336. * Can be removed once all calls are updated.
  337. *
  338. * Access:
  339. * public
  340. *
  341. * Parameters:
  342. * $obj - _mixed_ (Required) The PHP object to convert into a JSON string.
  343. *
  344. * Returns:
  345. * _string_ A JSON string.
  346. */
  347. public function json_encode($obj)
  348. {
  349. return json_encode($obj);
  350. }
  351. /**
  352. * Method: convert_response_to_array()
  353. * Converts a SimpleXML response to an array structure.
  354. *
  355. * Access:
  356. * public
  357. *
  358. * Parameters:
  359. * $response - _ResponseCore_ (Required) A response value.
  360. *
  361. * Returns:
  362. * _array_ The response value as a standard, multi-dimensional array.
  363. *
  364. * Requires:
  365. * PHP 5.2
  366. */
  367. public function convert_response_to_array(ResponseCore $response)
  368. {
  369. return json_decode(json_encode($response), true);
  370. }
  371. /**
  372. * Method: convert_date_to_iso8601()
  373. * Checks to see if a date stamp is ISO-8601 formatted, and if not, makes it so.
  374. *
  375. * Access:
  376. * public
  377. *
  378. * Parameters:
  379. * $datestamp - _string_ (Required) A date stamp, or a string that can be parsed into a date stamp.
  380. *
  381. * Returns:
  382. * _string_ An ISO-8601 formatted date stamp.
  383. */
  384. public function convert_date_to_iso8601($datestamp)
  385. {
  386. if (!preg_match('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}((\+|-)\d{2}:\d{2}|Z)/m', $datestamp))
  387. {
  388. return gmdate(self::DATE_FORMAT_ISO8601, strtotime($datestamp));
  389. }
  390. return $datestamp;
  391. }
  392. /**
  393. * Method: is_base64()
  394. * Determines whether the data is Base64 encoded or not.
  395. *
  396. * Access:
  397. * public
  398. *
  399. * License:
  400. * [PHP License](http://us.php.net/manual/en/function.base64-decode.php#81425)
  401. *
  402. * Parameters:
  403. * $s - _string_ (Required) The string to test.
  404. *
  405. * Returns:
  406. * _boolean_ Whether the string is Base64 encoded or not.
  407. */
  408. public function is_base64($s)
  409. {
  410. return (bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s);
  411. }
  412. /**
  413. * Method: decode_uhex()
  414. * Decodes \uXXXX entities into their real unicode character equivalents.
  415. *
  416. * Access:
  417. * public
  418. *
  419. * Parameters:
  420. * $s - _string_ (Required) The string to decode.
  421. *
  422. * Returns:
  423. * _string_ The decoded string.
  424. */
  425. public function decode_uhex($s)
  426. {
  427. preg_match_all('/\\\u([0-9a-f]{4})/i', $s, $matches);
  428. $matches = $matches[count($matches) - 1];
  429. $map = array();
  430. foreach ($matches as $match)
  431. {
  432. if (!isset($map[$match]))
  433. {
  434. $map['\u' . $match] = html_entity_decode('&#' . hexdec($match) . ';', ENT_NOQUOTES, 'UTF-8');
  435. }
  436. }
  437. return str_replace(array_keys($map), $map, $s);
  438. }
  439. /**
  440. * Method: generate_guid()
  441. * Generates a random GUID.
  442. *
  443. * Author:
  444. * Alix Axel <http://www.php.net/manual/en/function.com-create-guid.php#99425>
  445. *
  446. * License:
  447. * [PHP License](http://www.php.net/license/3_01.txt)
  448. *
  449. * Access:
  450. * public
  451. *
  452. * Returns:
  453. * _string_ A random GUID.
  454. */
  455. public function generate_guid()
  456. {
  457. return sprintf(
  458. '%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
  459. mt_rand(0, 65535),
  460. mt_rand(0, 65535),
  461. mt_rand(0, 65535),
  462. mt_rand(16384, 20479),
  463. mt_rand(32768, 49151),
  464. mt_rand(0, 65535),
  465. mt_rand(0, 65535),
  466. mt_rand(0, 65535)
  467. );
  468. }
  469. }