PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/Gloo/Util.php

http://webgloo.googlecode.com/
PHP | 116 lines | 76 code | 21 blank | 19 comment | 6 complexity | 0d5e798ff1cece3aad50a6089cac2d8d MD5 | raw file
  1. <?php
  2. class Gloo_Util {
  3. static function base64Encrypt($token) {
  4. $token = base64_encode($token);
  5. $token = str_rot13($token);
  6. return $token ;
  7. }
  8. static function base64Decrypt($token) {
  9. $token = str_rot13($token);
  10. $token = base64_decode($token);
  11. return $token ;
  12. }
  13. static function getBase36GUID() {
  14. $baseId = rand() ;
  15. $token = base_convert($baseId * rand(), 10, 36);
  16. return $token ;
  17. }
  18. static function getMD5GUID() {
  19. $token = md5(uniqid(mt_rand(), true));
  20. return $token ;
  21. }
  22. static function array2nl($arr) {
  23. //lambda style function
  24. // Anonymous callback @see create_function() on php.net
  25. $str = array_reduce($arr, create_function('$a,$b', 'return $a."\n".$b ;'));
  26. return $str ;
  27. }
  28. static function stringify($var) {
  29. $buffer = '' ;
  30. //var_dump($var);
  31. if(is_object($var)) {
  32. $buffer = '{object='.$var->__toString().'}' ;
  33. return $buffer;
  34. }
  35. if(is_array($var)) {
  36. $buffer= '[array=' ;
  37. foreach($var as $elem) {
  38. $buffer = $buffer.self::stringify($elem).',';
  39. }
  40. $buffer .= ']';
  41. return $buffer;
  42. }
  43. return $var ;
  44. }
  45. /**
  46. *
  47. * @param <type> $original - timestamp coming from mysql DB
  48. * @param <type> $format - output format , defaults to dd mon yyyy
  49. * @return <type> the formatted date string
  50. *
  51. * @see also http://in2.php.net/strftime
  52. * @see also http://in2.php.net/manual/en/function.strtotime.php
  53. * PHP string time functions
  54. *
  55. */
  56. static function format_date($original,$format=Gloo_Constants::DDMONYYYY) {
  57. //@todo date time operations can be expensive, need to profile!
  58. if(!isset($original) || is_null($original) || empty($original)) {
  59. return '';
  60. }
  61. $dt = strftime($format, strtotime($original));
  62. return $dt ;
  63. }
  64. static function squeeze($input) {
  65. $input = preg_replace('/\s\s+/', ' ', $input);
  66. return $input ;
  67. }
  68. static function isAlphaNumeric($input) {
  69. //Allow spaces
  70. $input = preg_replace('/\s+/', '', $input);
  71. return ctype_alnum($input);
  72. }
  73. static function isEmpty($name,$value) {
  74. if(empty($value)) {
  75. $message = 'Bad input:: '.$name. ' is empty or null!';
  76. trigger_error($message,E_USER_ERROR);
  77. }
  78. }
  79. static function startsWith($Haystack, $Needle) {
  80. // Recommended version, using strpos
  81. return strpos($Haystack, $Needle) === 0;
  82. }
  83. static function convertBytesIntoKB($bytes) {
  84. //divide bytes by 1024
  85. $kb = ceil(($bytes/1024.00));
  86. return $kb;
  87. }
  88. static function convertNameToFarmSiteDomain($name) {
  89. //convert incoming name to new farm site domain
  90. $DOT = '.' ;
  91. $config = Gloo_Config::getInstance();
  92. return $name.$DOT.$config->getFarmNewSiteDomain() ;
  93. }
  94. }
  95. ?>