CFUtilities

Contains a set of utility methods for connecting to, and working with, AWS.

Defined (1)

The class is defined in the following location(s).

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