PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/aws-sdk/utilities/utilities.class.php

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