PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/AWSSDKforPHP/utilities/utilities.class.php

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