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

/core/components/phpthumbof/model/aws/utilities/utilities.class.php

http://github.com/splittingred/phpThumbOf
PHP | 500 lines | 180 code | 44 blank | 276 comment | 20 complexity | c289452213784786777201f360d5db0d MD5 | raw file
Possible License(s): Apache-2.0, 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. // Explicitly pass a third parameter.
  134. // <http://php.net/manual/en/function.http-build-query.php#74781>
  135. return http_build_query($array, '', '&');
  136. }
  137. /**
  138. * Method: to_signable_string()
  139. * Convert an associative array into a sign-able string.
  140. *
  141. * Access:
  142. * public
  143. *
  144. * Parameters:
  145. * $array - _array_ (Required) Array to convert.
  146. *
  147. * Returns:
  148. * _string_ URL-friendly sign-able string.
  149. */
  150. public function to_signable_string($array)
  151. {
  152. $t = array();
  153. foreach ($array as $k => $v)
  154. {
  155. $t[] = $this->encode_signature2($k) . '=' . $this->encode_signature2($v);
  156. }
  157. return implode('&', $t);
  158. }
  159. /**
  160. * Method: encode_signature2()
  161. * Encode the value according to RFC 3986.
  162. *
  163. * Access:
  164. * public
  165. *
  166. * Parameters:
  167. * $string - _string_ (Required) String to convert
  168. *
  169. * Returns:
  170. * _string_ URL-friendly sign-able string.
  171. */
  172. public function encode_signature2($string)
  173. {
  174. $string = rawurlencode($string);
  175. return str_replace('%7E', '~', $string);
  176. }
  177. /**
  178. * Method: query_to_array()
  179. * Convert a query string into an associative array. Multiple, identical keys will become an indexed array.
  180. *
  181. * Access:
  182. * public
  183. *
  184. * Parameters:
  185. * $qs - _string_ (Required) Query string to convert.
  186. *
  187. * Returns:
  188. * _array_ Associative array of keys and values.
  189. */
  190. public function query_to_array($qs)
  191. {
  192. $query = explode('&', $qs);
  193. $data = array();
  194. foreach ($query as $q)
  195. {
  196. $q = explode('=', $q);
  197. if (isset($data[$q[0]]) && is_array($data[$q[0]]))
  198. {
  199. $data[$q[0]][] = urldecode($q[1]);
  200. }
  201. else if (isset($data[$q[0]]) && !is_array($data[$q[0]]))
  202. {
  203. $data[$q[0]] = array($data[$q[0]]);
  204. $data[$q[0]][] = urldecode($q[1]);
  205. }
  206. else
  207. {
  208. $data[urldecode($q[0])] = urldecode($q[1]);
  209. }
  210. }
  211. return $data;
  212. }
  213. /**
  214. * Method: size_readable()
  215. * Return human readable file sizes. Original function by Aidan Lister <aidan@php.net>, modified by Ryan Parman.
  216. *
  217. * Access:
  218. * public
  219. *
  220. * License:
  221. * [PHP License](http://www.php.net/license/3_01.txt)
  222. *
  223. * Parameters:
  224. * $size - _integer_ (Required) Filesize in bytes.
  225. * $unit - _string_ (Optional) The maximum unit to use. Defaults to the largest appropriate unit.
  226. * $default - _string_ (Optional) The format for the return string. Defaults to '%01.2f %s'
  227. *
  228. * Returns:
  229. * _string_ The human-readable file size.
  230. *
  231. * See Also:
  232. * Original Function - http://aidanlister.com/repos/v/function.size_readable.php
  233. */
  234. public function size_readable($size, $unit = null, $default = null)
  235. {
  236. // Units
  237. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
  238. $mod = 1024;
  239. $ii = count($sizes) - 1;
  240. // Max unit
  241. $unit = array_search((string) $unit, $sizes);
  242. if ($unit === null || $unit === false)
  243. {
  244. $unit = $ii;
  245. }
  246. // Return string
  247. if ($default === null)
  248. {
  249. $default = '%01.2f %s';
  250. }
  251. // Loop
  252. $i = 0;
  253. while ($unit != $i && $size >= 1024 && $i < $ii)
  254. {
  255. $size /= $mod;
  256. $i++;
  257. }
  258. return sprintf($default, $size, $sizes[$i]);
  259. }
  260. /**
  261. * Method: time_hms()
  262. * Convert a number of seconds into Hours:Minutes:Seconds.
  263. *
  264. * Access:
  265. * public
  266. *
  267. * Parameters:
  268. * $seconds - _integer_ (Required) The number of seconds to convert.
  269. *
  270. * Returns:
  271. * _string_ The formatted time.
  272. */
  273. public function time_hms($seconds)
  274. {
  275. $time = '';
  276. // First pass
  277. $hours = (int) ($seconds / 3600);
  278. $seconds = $seconds % 3600;
  279. $minutes = (int) ($seconds / 60);
  280. $seconds = $seconds % 60;
  281. // Cleanup
  282. $time .= ($hours) ? $hours . ':' : '';
  283. $time .= ($minutes < 10 && $hours > 0) ? '0' . $minutes : $minutes;
  284. $time .= ':';
  285. $time .= ($seconds < 10) ? '0' . $seconds : $seconds;
  286. return $time;
  287. }
  288. /**
  289. * Method: try_these()
  290. * Returns the first value that is set. Based on [Try.these()](http://api.prototypejs.org/language/try/these/) from [Prototype](http://prototypejs.org).
  291. *
  292. * Access:
  293. * public
  294. *
  295. * Parameters:
  296. * $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.
  297. * $base - _object_ (Optional) The base object to use, if any.
  298. * $default - _mixed_ (Optional) What to return if there are no matches. Defaults to null.
  299. *
  300. * Returns:
  301. * _mixed_ Either a matching property of a given object, _boolean_ false, or any other data type you might choose.
  302. */
  303. public function try_these($attrs, $base = null, $default = null)
  304. {
  305. if ($base)
  306. {
  307. foreach ($attrs as $attr)
  308. {
  309. if (isset($base->$attr))
  310. {
  311. return $base->$attr;
  312. }
  313. }
  314. }
  315. else
  316. {
  317. foreach ($attrs as $attr)
  318. {
  319. if (isset($attr))
  320. {
  321. return $attr;
  322. }
  323. }
  324. }
  325. return $default;
  326. }
  327. /**
  328. * Method: json_encode()
  329. * Can be removed once all calls are updated.
  330. *
  331. * Access:
  332. * public
  333. *
  334. * Parameters:
  335. * $obj - _mixed_ (Required) The PHP object to convert into a JSON string.
  336. *
  337. * Returns:
  338. * _string_ A JSON string.
  339. */
  340. public function json_encode($obj)
  341. {
  342. return json_encode($obj);
  343. }
  344. /**
  345. * Method: convert_response_to_array()
  346. * Converts a SimpleXML response to an array structure.
  347. *
  348. * Access:
  349. * public
  350. *
  351. * Parameters:
  352. * $response - _ResponseCore_ (Required) A response value.
  353. *
  354. * Returns:
  355. * _array_ The response value as a standard, multi-dimensional array.
  356. *
  357. * Requires:
  358. * PHP 5.2
  359. */
  360. public function convert_response_to_array(ResponseCore $response)
  361. {
  362. return json_decode(json_encode($response), true);
  363. }
  364. /**
  365. * Method: convert_date_to_iso8601()
  366. * Checks to see if a date stamp is ISO-8601 formatted, and if not, makes it so.
  367. *
  368. * Access:
  369. * public
  370. *
  371. * Parameters:
  372. * $datestamp - _string_ (Required) A date stamp, or a string that can be parsed into a date stamp.
  373. *
  374. * Returns:
  375. * _string_ An ISO-8601 formatted date stamp.
  376. */
  377. public function convert_date_to_iso8601($datestamp)
  378. {
  379. if (!preg_match('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}((\+|-)\d{2}:\d{2}|Z)/m', $datestamp))
  380. {
  381. return gmdate(self::DATE_FORMAT_ISO8601, strtotime($datestamp));
  382. }
  383. return $datestamp;
  384. }
  385. /**
  386. * Method: is_base64()
  387. * Determines whether the data is Base64 encoded or not.
  388. *
  389. * Access:
  390. * public
  391. *
  392. * License:
  393. * [PHP License](http://us.php.net/manual/en/function.base64-decode.php#81425)
  394. *
  395. * Parameters:
  396. * $s - _string_ (Required) The string to test.
  397. *
  398. * Returns:
  399. * _boolean_ Whether the string is Base64 encoded or not.
  400. */
  401. public function is_base64($s)
  402. {
  403. return (bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s);
  404. }
  405. /**
  406. * Method: decode_uhex()
  407. * Decodes \uXXXX entities into their real unicode character equivalents.
  408. *
  409. * Access:
  410. * public
  411. *
  412. * Parameters:
  413. * $s - _string_ (Required) The string to decode.
  414. *
  415. * Returns:
  416. * _string_ The decoded string.
  417. */
  418. public function decode_uhex($s)
  419. {
  420. preg_match_all('/\\\u([0-9a-f]{4})/i', $s, $matches);
  421. $matches = $matches[count($matches) - 1];
  422. $map = array();
  423. foreach ($matches as $match)
  424. {
  425. if (!isset($map[$match]))
  426. {
  427. $map['\u' . $match] = html_entity_decode('&#' . hexdec($match) . ';', ENT_NOQUOTES, 'UTF-8');
  428. }
  429. }
  430. return str_replace(array_keys($map), $map, $s);
  431. }
  432. /**
  433. * Method: generate_guid()
  434. * Generates a random GUID.
  435. *
  436. * Access:
  437. * public
  438. *
  439. * Returns:
  440. * _string_ A random GUID.
  441. */
  442. public function generate_guid()
  443. {
  444. return sprintf(
  445. '%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
  446. mt_rand(0, 65535),
  447. mt_rand(0, 65535),
  448. mt_rand(0, 65535),
  449. mt_rand(16384, 20479),
  450. mt_rand(32768, 49151),
  451. mt_rand(0, 65535),
  452. mt_rand(0, 65535),
  453. mt_rand(0, 65535)
  454. );
  455. }
  456. }