PageRenderTime 33ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/dist/webpagetest/www/ec2/utilities/utilities.class.php

http://webpagetest.googlecode.com/
PHP | 394 lines | 192 code | 46 blank | 156 comment | 22 complexity | c01e7f25362e8c7c46456ab7dbffad48 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-3.0, LGPL-3.0, MIT, BSD-3-Clause, ISC, LGPL-2.1
  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. if (is_string($key) && !is_array($value))
  99. {
  100. $temp[] = rawurlencode($key) . '=' . rawurlencode($value);
  101. }
  102. }
  103. return implode('&', $temp);
  104. }
  105. /**
  106. * Convert an associative array into a sign-able string.
  107. *
  108. * @param array $array (Required) Array to convert.
  109. * @return string URL-friendly sign-able string.
  110. */
  111. public function to_signable_string($array)
  112. {
  113. $t = array();
  114. foreach ($array as $k => $v)
  115. {
  116. $t[] = $this->encode_signature2($k) . '=' . $this->encode_signature2($v);
  117. }
  118. return implode('&', $t);
  119. }
  120. /**
  121. * Encode the value according to RFC 3986.
  122. *
  123. * @param string $string (Required) String to convert.
  124. * @return string URL-friendly sign-able string.
  125. */
  126. public function encode_signature2($string)
  127. {
  128. $string = rawurlencode($string);
  129. return str_replace('%7E', '~', $string);
  130. }
  131. /**
  132. * Convert a query string into an associative array. Multiple, identical keys will become an indexed array.
  133. *
  134. * @param string $qs (Required) Query string to convert.
  135. * @return array Associative array of keys and values.
  136. */
  137. public function query_to_array($qs)
  138. {
  139. $query = explode('&', $qs);
  140. $data = array();
  141. foreach ($query as $q)
  142. {
  143. $q = explode('=', $q);
  144. if (isset($data[$q[0]]) && is_array($data[$q[0]]))
  145. {
  146. $data[$q[0]][] = urldecode($q[1]);
  147. }
  148. else if (isset($data[$q[0]]) && !is_array($data[$q[0]]))
  149. {
  150. $data[$q[0]] = array($data[$q[0]]);
  151. $data[$q[0]][] = urldecode($q[1]);
  152. }
  153. else
  154. {
  155. $data[urldecode($q[0])] = urldecode($q[1]);
  156. }
  157. }
  158. return $data;
  159. }
  160. /**
  161. * Return human readable file sizes.
  162. *
  163. * @author Aidan Lister <aidan@php.net>
  164. * @author Ryan Parman <ryan@getcloudfusion.com>
  165. * @license http://www.php.net/license/3_01.txt PHP License
  166. * @param integer $size (Required) Filesize in bytes.
  167. * @param string $unit (Optional) The maximum unit to use. Defaults to the largest appropriate unit.
  168. * @param string $default (Optional) The format for the return string. Defaults to `%01.2f %s`.
  169. * @return string The human-readable file size.
  170. * @link http://aidanlister.com/repos/v/function.size_readable.php Original Function
  171. */
  172. public function size_readable($size, $unit = null, $default = null)
  173. {
  174. // Units
  175. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
  176. $mod = 1024;
  177. $ii = count($sizes) - 1;
  178. // Max unit
  179. $unit = array_search((string) $unit, $sizes);
  180. if ($unit === null || $unit === false)
  181. {
  182. $unit = $ii;
  183. }
  184. // Return string
  185. if ($default === null)
  186. {
  187. $default = '%01.2f %s';
  188. }
  189. // Loop
  190. $i = 0;
  191. while ($unit != $i && $size >= 1024 && $i < $ii)
  192. {
  193. $size /= $mod;
  194. $i++;
  195. }
  196. return sprintf($default, $size, $sizes[$i]);
  197. }
  198. /**
  199. * Convert a number of seconds into Hours:Minutes:Seconds.
  200. *
  201. * @param integer $seconds (Required) The number of seconds to convert.
  202. * @return string The formatted time.
  203. */
  204. public function time_hms($seconds)
  205. {
  206. $time = '';
  207. // First pass
  208. $hours = (int) ($seconds / 3600);
  209. $seconds = $seconds % 3600;
  210. $minutes = (int) ($seconds / 60);
  211. $seconds = $seconds % 60;
  212. // Cleanup
  213. $time .= ($hours) ? $hours . ':' : '';
  214. $time .= ($minutes < 10 && $hours > 0) ? '0' . $minutes : $minutes;
  215. $time .= ':';
  216. $time .= ($seconds < 10) ? '0' . $seconds : $seconds;
  217. return $time;
  218. }
  219. /**
  220. * Returns the first value that is set. Based on [Try.these()](http://api.prototypejs.org/language/Try/these/) from [Prototype](http://prototypejs.org).
  221. *
  222. * @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.
  223. * @param object $base (Optional) The base object to use, if any.
  224. * @param mixed $default (Optional) What to return if there are no matches. Defaults to `null`.
  225. * @return mixed Either a matching property of a given object, boolean `false`, or any other data type you might choose.
  226. */
  227. public function try_these($attrs, $base = null, $default = null)
  228. {
  229. if ($base)
  230. {
  231. foreach ($attrs as $attr)
  232. {
  233. if (isset($base->$attr))
  234. {
  235. return $base->$attr;
  236. }
  237. }
  238. }
  239. else
  240. {
  241. foreach ($attrs as $attr)
  242. {
  243. if (isset($attr))
  244. {
  245. return $attr;
  246. }
  247. }
  248. }
  249. return $default;
  250. }
  251. /**
  252. * Can be removed once all calls are updated.
  253. *
  254. * @deprecated Use <php:json_encode()> instead.
  255. * @param mixed $obj (Required) The PHP object to convert into a JSON string.
  256. * @return string A JSON string.
  257. */
  258. public function json_encode($obj)
  259. {
  260. return json_encode($obj);
  261. }
  262. /**
  263. * Converts a SimpleXML response to an array structure.
  264. *
  265. * @param ResponseCore $response (Required) A response value.
  266. * @return array The response value as a standard, multi-dimensional array.
  267. */
  268. public function convert_response_to_array(ResponseCore $response)
  269. {
  270. return json_decode(json_encode($response), true);
  271. }
  272. /**
  273. * Checks to see if a date stamp is ISO-8601 formatted, and if not, makes it so.
  274. *
  275. * @param string $datestamp (Required) A date stamp, or a string that can be parsed into a date stamp.
  276. * @return string An ISO-8601 formatted date stamp.
  277. */
  278. public function convert_date_to_iso8601($datestamp)
  279. {
  280. if (!preg_match('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}((\+|-)\d{2}:\d{2}|Z)/m', $datestamp))
  281. {
  282. return gmdate(self::DATE_FORMAT_ISO8601, strtotime($datestamp));
  283. }
  284. return $datestamp;
  285. }
  286. /**
  287. * Determines whether the data is Base64 encoded or not.
  288. *
  289. * @license http://us.php.net/manual/en/function.base64-decode.php#81425 PHP License
  290. * @param string $s (Required) The string to test.
  291. * @return boolean Whether the string is Base64 encoded or not.
  292. */
  293. public function is_base64($s)
  294. {
  295. return (bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s);
  296. }
  297. /**
  298. * Determines whether the data is a JSON string or not.
  299. *
  300. * @param string $s (Required) The string to test.
  301. * @return boolean Whether the string is a JSON string or not.
  302. */
  303. public function is_json($s)
  304. {
  305. return json_decode($s) ? true : false;
  306. }
  307. /**
  308. * Decodes `\uXXXX` entities into their real unicode character equivalents.
  309. *
  310. * @param string $s (Required) The string to decode.
  311. * @return string The decoded string.
  312. */
  313. public function decode_uhex($s)
  314. {
  315. preg_match_all('/\\\u([0-9a-f]{4})/i', $s, $matches);
  316. $matches = $matches[count($matches) - 1];
  317. $map = array();
  318. foreach ($matches as $match)
  319. {
  320. if (!isset($map[$match]))
  321. {
  322. $map['\u' . $match] = html_entity_decode('&#' . hexdec($match) . ';', ENT_NOQUOTES, 'UTF-8');
  323. }
  324. }
  325. return str_replace(array_keys($map), $map, $s);
  326. }
  327. /**
  328. * Generates a random GUID.
  329. *
  330. * @author Alix Axel <http://www.php.net/manual/en/function.com-create-guid.php#99425>
  331. * @license http://www.php.net/license/3_01.txt PHP License
  332. * @return string A random GUID.
  333. */
  334. public function generate_guid()
  335. {
  336. return sprintf(
  337. '%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
  338. mt_rand(0, 65535),
  339. mt_rand(0, 65535),
  340. mt_rand(0, 65535),
  341. mt_rand(16384, 20479),
  342. mt_rand(32768, 49151),
  343. mt_rand(0, 65535),
  344. mt_rand(0, 65535),
  345. mt_rand(0, 65535)
  346. );
  347. }
  348. }