PageRenderTime 27ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cloudfusion/_utilities.class.php

https://github.com/tinypay/KO3-Fusion
PHP | 475 lines | 232 code | 29 blank | 214 comment | 23 complexity | 53a8bbdf235a8aa516d2e41712a2711b MD5 | raw file
  1. <?php
  2. /**
  3. * File: CFUtilities
  4. * Utilities for connecting to, and working with, AWS.
  5. *
  6. * Version:
  7. * 2009.08.24
  8. *
  9. * Copyright:
  10. * 2006-2009 Foleeo, Inc., and contributors.
  11. *
  12. * License:
  13. * Simplified BSD License - http://opensource.org/licenses/bsd-license.php
  14. *
  15. * See Also:
  16. * CloudFusion - http://getcloudfusion.com
  17. */
  18. /*%******************************************************************************************%*/
  19. // CLASS
  20. /**
  21. * Class: CFUtilities
  22. * Container for all utility-related methods.
  23. */
  24. class CFUtilities
  25. {
  26. /**
  27. * Method: __construct()
  28. * The constructor
  29. *
  30. * Access:
  31. * public
  32. *
  33. * Returns:
  34. * <CFUtilities> object
  35. */
  36. public function __construct()
  37. {
  38. return $this;
  39. }
  40. /**
  41. * Method: hex_to_base64()
  42. * Convert a HEX value to Base64.
  43. *
  44. * Access:
  45. * public
  46. *
  47. * Parameters:
  48. * str - _string_ (Required) Value to convert.
  49. *
  50. * Returns:
  51. * _string_ Base64-encoded string.
  52. *
  53. * Examples:
  54. * example::utilities/hex_to_base64.phpt:
  55. */
  56. public function hex_to_base64($str)
  57. {
  58. $raw = '';
  59. for ($i = 0; $i < strlen($str); $i += 2)
  60. {
  61. $raw .= chr(hexdec(substr($str, $i, 2)));
  62. }
  63. return base64_encode($raw);
  64. }
  65. /**
  66. * Method: to_query_string()
  67. * Convert an associative array into a query string.
  68. *
  69. * Access:
  70. * public
  71. *
  72. * Parameters:
  73. * array - _array_ (Required) Array to convert.
  74. *
  75. * Returns:
  76. * _string_ URL-friendly query string.
  77. *
  78. * Examples:
  79. * example::utilities/to_query_string.phpt:
  80. */
  81. public function to_query_string($array)
  82. {
  83. return http_build_query($array);
  84. }
  85. /**
  86. * Method: to_signable_string()
  87. * Convert an associative array into a sign-able string.
  88. *
  89. * Access:
  90. * public
  91. *
  92. * Parameters:
  93. * array - _array_ (Required) Array to convert.
  94. *
  95. * Returns:
  96. * _string_ URL-friendly sign-able string.
  97. *
  98. * Examples:
  99. * example::utilities/to_signable_string.phpt:
  100. */
  101. public function to_signable_string($array)
  102. {
  103. $t = array();
  104. foreach ($array as $k => $v)
  105. {
  106. $t[] = $this->encode_signature2($k) . '=' . $this->encode_signature2($v);
  107. }
  108. return implode('&', $t);
  109. }
  110. /**
  111. * Method: encode_signature2()
  112. * Encode the value according to RFC 3986.
  113. *
  114. * Access:
  115. * public
  116. *
  117. * Parameters:
  118. * string - _string_ (Required) String to convert
  119. *
  120. * Returns:
  121. * _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. * Method: query_to_array()
  130. * Convert a query string into an associative array. Multiple, identical keys will become an indexed array.
  131. *
  132. * Access:
  133. * public
  134. *
  135. * Parameters:
  136. * qs - _string_ (Required) Query string to convert.
  137. *
  138. * Returns:
  139. * _array_ Associative array of keys and values.
  140. *
  141. * Examples:
  142. * example::utilities/query_to_array.phpt:
  143. * example::utilities/query_to_array2.phpt:
  144. */
  145. public function query_to_array($qs)
  146. {
  147. $query = explode('&', $qs);
  148. $data = array();
  149. foreach ($query as $q)
  150. {
  151. $q = explode('=', $q);
  152. if (isset($data[$q[0]]) && is_array($data[$q[0]]))
  153. {
  154. $data[$q[0]][] = urldecode($q[1]);
  155. }
  156. else if (isset($data[$q[0]]) && !is_array($data[$q[0]]))
  157. {
  158. $data[$q[0]] = array($data[$q[0]]);
  159. $data[$q[0]][] = urldecode($q[1]);
  160. }
  161. else
  162. {
  163. $data[urldecode($q[0])] = urldecode($q[1]);
  164. }
  165. }
  166. return $data;
  167. }
  168. /**
  169. * Method: size_readable()
  170. * Return human readable file sizes. Original function by Aidan Lister <mailto:aidan@php.net>, modified by Ryan Parman.
  171. *
  172. * Access:
  173. * public
  174. *
  175. * Parameters:
  176. * size - _integer_ (Required) Filesize in bytes.
  177. * unit - _string_ (Optional) The maximum unit to use. Defaults to the largest appropriate unit.
  178. * retstring - _string_ (Optional) The format for the return string. Defaults to '%01.2f %s'
  179. *
  180. * Returns:
  181. * _string_ The human-readable file size.
  182. *
  183. * Examples:
  184. * example::utilities/size_readable.phpt:
  185. * example::utilities/size_readable2.phpt:
  186. * example::utilities/size_readable3.phpt:
  187. *
  188. * See Also:
  189. * Original Function - http://aidanlister.com/repos/v/function.size_readable.php
  190. */
  191. public function size_readable($size, $unit = null, $retstring = null)
  192. {
  193. // Units
  194. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
  195. $mod = 1024;
  196. $ii = count($sizes) - 1;
  197. // Max unit
  198. $unit = array_search((string) $unit, $sizes);
  199. if ($unit === null || $unit === false)
  200. {
  201. $unit = $ii;
  202. }
  203. // Return string
  204. if ($retstring === null)
  205. {
  206. $retstring = '%01.2f %s';
  207. }
  208. // Loop
  209. $i = 0;
  210. while ($unit != $i && $size >= 1024 && $i < $ii)
  211. {
  212. $size /= $mod;
  213. $i++;
  214. }
  215. return sprintf($retstring, $size, $sizes[$i]);
  216. }
  217. /**
  218. * Method: time_hms()
  219. * Convert a number of seconds into Hours:Minutes:Seconds.
  220. *
  221. * Access:
  222. * public
  223. *
  224. * Parameters:
  225. * seconds - _integer_ (Required) The number of seconds to convert.
  226. *
  227. * Returns:
  228. * _string_ The formatted time.
  229. *
  230. * Examples:
  231. * example::utilities/time_hms.phpt:
  232. */
  233. public function time_hms($seconds)
  234. {
  235. $time = '';
  236. // First pass
  237. $hours = (int) ($seconds / 3600);
  238. $seconds = $seconds % 3600;
  239. $minutes = (int) ($seconds / 60);
  240. $seconds = $seconds % 60;
  241. // Cleanup
  242. $time .= ($hours) ? $hours . ':' : '';
  243. $time .= ($minutes < 10 && $hours > 0) ? '0' . $minutes : $minutes;
  244. $time .= ':';
  245. $time .= ($seconds < 10) ? '0' . $seconds : $seconds;
  246. return $time;
  247. }
  248. /**
  249. * Method: try_these()
  250. * Returns the first value that is set. Based on Try.these() from Prototype <http://prototypejs.org>.
  251. *
  252. * Access:
  253. * public
  254. *
  255. * Parameters:
  256. * 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.
  257. * base - _object_ (Optional) The base object to use, if any.
  258. * default - _mixed_ (Optional) What to return if there are no matches. Defaults to null.
  259. *
  260. * Returns:
  261. * _mixed_ Either a matching property of a given object, _boolean_ false, or any other data type you might choose.
  262. *
  263. * Examples:
  264. * example::utilities/try_these.phpt:
  265. * example::utilities/try_these2.phpt:
  266. * example::utilities/try_these3.phpt:
  267. * example::utilities/try_these4.phpt:
  268. * example::utilities/try_these5.phpt:
  269. */
  270. public function try_these($attrs, $base = null, $default = null)
  271. {
  272. if ($base)
  273. {
  274. foreach ($attrs as $attr)
  275. {
  276. if (isset($base->$attr))
  277. {
  278. return $base->$attr;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. foreach ($attrs as $attr)
  285. {
  286. if (isset($attr))
  287. {
  288. return $attr;
  289. }
  290. }
  291. }
  292. return $default;
  293. }
  294. /**
  295. * Method: json_encode()
  296. * Replicates json_encode() for versions of PHP 5 earlier than 5.2.
  297. *
  298. * Access:
  299. * public
  300. *
  301. * Parameters:
  302. * obj - _mixed_ (Required) The PHP object to convert into a JSON string.
  303. *
  304. * Returns:
  305. * _string_ A JSON string.
  306. *
  307. * Examples:
  308. * example::utilities/json_encode2.phpt:
  309. * example::utilities/json_encode3.phpt:
  310. * example::utilities/json_encode4.phpt:
  311. * example::utilities/json_encode5.phpt:
  312. * example::utilities/json_encode6.phpt:
  313. */
  314. public function json_encode($obj)
  315. {
  316. if (function_exists('json_encode'))
  317. {
  318. return json_encode($obj);
  319. }
  320. return $this->json_encode_php51($obj);
  321. }
  322. /**
  323. * Method: json_encode_php51()
  324. * 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.
  325. *
  326. * Author:
  327. * http://us2.php.net/manual/en/function.json-encode.php#82904
  328. *
  329. * Access:
  330. * public
  331. *
  332. * Parameters:
  333. * obj - _mixed_ (Required) The PHP object to convert into a JSON string.
  334. *
  335. * Returns:
  336. * _string_ A JSON string.
  337. */
  338. public function json_encode_php51($obj)
  339. {
  340. if (is_null($obj)) return 'null';
  341. if ($obj === false) return 'false';
  342. if ($obj === true) return 'true';
  343. if (is_scalar($obj))
  344. {
  345. if (is_float($obj))
  346. {
  347. // Always use '.' for floats.
  348. return str_replace(',', '.', strval($obj));
  349. }
  350. elseif (is_int($obj))
  351. {
  352. return strval($obj);
  353. }
  354. elseif (is_string($obj))
  355. {
  356. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  357. return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $obj) . '"';
  358. }
  359. return $obj;
  360. }
  361. $isList = true;
  362. for ($i = 0, reset($obj); $i < count($obj); $i++, next($obj))
  363. {
  364. if (key($obj) !== $i)
  365. {
  366. $isList = false;
  367. break;
  368. }
  369. }
  370. $result = array();
  371. if ($isList)
  372. {
  373. foreach ($obj as $v)
  374. {
  375. $result[] = json_encode($v);
  376. }
  377. return '[' . join(',', $result) . ']';
  378. }
  379. else
  380. {
  381. foreach ($obj as $k => $v)
  382. {
  383. $result[] = json_encode($k).':'.json_encode($v);
  384. }
  385. return '{' . join(',', $result) . '}';
  386. }
  387. }
  388. /**
  389. * Method: convert_response_to_array()
  390. * Converts a SimpleXML response to an array structure.
  391. *
  392. * Author:
  393. * Adrien Cahen <http://gaarf.info/2009/08/13/xml-string-to-php-array/>
  394. *
  395. * Access:
  396. * public
  397. *
  398. * Parameters:
  399. * obj - _ResponseCore_ (Required) A CloudFusion ResponseCore response value.
  400. *
  401. * Returns:
  402. * _array_ The response value as a standard, multi-dimensional array.
  403. *
  404. * Examples:
  405. * example::utilities/convert_response_to_array.phpt:
  406. *
  407. * Requirements:
  408. * PHP 5.2 or newer.
  409. */
  410. public function convert_response_to_array(ResponseCore $response)
  411. {
  412. return json_decode(json_encode((array) $response), true);
  413. }
  414. /**
  415. * Method: convert_date_to_iso8601()
  416. * Checks to see if a date stamp is ISO-8601 formatted, and if not, makes it so.
  417. *
  418. * Access:
  419. * public
  420. *
  421. * Parameters:
  422. * datestamp - _string_ (Required) A date stamp, or a string that can be parsed into a date stamp.
  423. *
  424. * Returns:
  425. * _string_ An ISO-8601 formatted date stamp.
  426. *
  427. * Examples:
  428. * example::utilities/convert_date_to_iso8601.phpt:
  429. */
  430. public function convert_date_to_iso8601($datestamp)
  431. {
  432. if (!preg_match('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}((\+|-)\d{2}:\d{2}|Z)/m', $datestamp))
  433. {
  434. return gmdate(DATE_FORMAT_ISO8601, strtotime($datestamp));
  435. }
  436. return $datestamp;
  437. }
  438. }