/applications/client/helpers/common_helper.php

https://bitbucket.org/amitholkar/zenfile-18-05 · PHP · 225 lines · 136 code · 21 blank · 68 comment · 31 complexity · 09a871a6ec854598c1521f7b087975c2 MD5 · raw file

  1. <?php
  2. /**
  3. * Checks an array: exist and has more than 0 elements
  4. *
  5. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  6. * @param array
  7. * @return bool
  8. */
  9. function check_array($array = array())
  10. {
  11. if (is_array($array) && (count($array) > 0))
  12. {
  13. return TRUE;
  14. }
  15. return FALSE;
  16. }
  17. /**
  18. * Checks is current http request AJAX or not
  19. *
  20. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  21. * @return bool
  22. */
  23. function is_ajax()
  24. {
  25. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest");
  26. }
  27. /**
  28. * Formats bytes to more appealing look
  29. *
  30. * @author Sergey Koshkarev
  31. * @param int size in bytes
  32. * @return string
  33. *
  34. */
  35. function format_bytes($size)
  36. {
  37. $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  38. for ($i = 0; $size >= 1024 && $i < 4; $i++)
  39. {
  40. $size /= 1024;
  41. }
  42. return round($size, 2).$units[$i];
  43. }
  44. /**
  45. * Removes folder recursively
  46. *
  47. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  48. * @param string folder to be removed
  49. * @return void
  50. */
  51. function rrmdir($dir)
  52. {
  53. if (is_dir($dir))
  54. {
  55. $objects = scandir($dir);
  56. foreach ($objects as $object)
  57. {
  58. if ($object != "." && $object != "..")
  59. {
  60. if (filetype($dir."/".$object) == "dir")
  61. {
  62. rrmdir($dir."/".$object);
  63. }
  64. else
  65. {
  66. unlink($dir."/".$object);
  67. }
  68. }
  69. }
  70. reset($objects);
  71. rmdir($dir);
  72. }
  73. }
  74. function create_password($pw_length = 8, $use_caps = TRUE, $use_numeric = TRUE, $use_specials = TRUE)
  75. {
  76. $caps = array();
  77. $numbers = array();
  78. $num_specials = 0;
  79. $reg_length = $pw_length;
  80. $pws = array();
  81. $chars = range(97, 122); // create a-z
  82. if ($use_caps) $caps = range(65, 90); // create A-Z
  83. if ($use_numeric) $numbers = range(48, 57); // create 0-9
  84. $all = array_merge($chars, $caps, $numbers);
  85. if ($use_specials) {
  86. $reg_length = ceil($pw_length*0.75);
  87. $num_specials = $pw_length - $reg_length;
  88. if ($num_specials > 5) $num_specials = 5;
  89. $signs = range(33, 47);
  90. $rs_keys = array_rand($signs, $num_specials);
  91. if (count($rs_keys) > 1)
  92. {
  93. foreach ($rs_keys as $rs) {
  94. $pws[] = chr($signs[$rs]);
  95. }
  96. }
  97. else
  98. {
  99. $pws[] = chr($signs[$rs_keys]);
  100. }
  101. }
  102. $rand_keys = array_rand($all, $reg_length);
  103. foreach ($rand_keys as $rand) {
  104. $pw[] = chr($all[$rand]);
  105. }
  106. $compl = array_merge($pw, $pws);
  107. shuffle($compl);
  108. return implode('', $compl);
  109. }
  110. /**
  111. * @params : $a array the recursion array
  112. * : $s array storage array
  113. * : $l integer the depth level
  114. *
  115. */
  116. if( ! function_exists( 'array_flat' ) )
  117. {
  118. function array_flat( $a, $s = array( ), $l = 0 )
  119. {
  120. # check if this is an array
  121. if( !is_array( $a ) ) return $s;
  122. # go through the array values
  123. foreach( $a as $k => $v )
  124. {
  125. # check if the contained values are arrays
  126. if( !is_array( $v ) )
  127. {
  128. # store the value
  129. $s[ ] = $v;
  130. # move to the next node
  131. continue;
  132. }
  133. # increment depth level
  134. $l++;
  135. # replace the content of stored values
  136. $s = array_flat( $v, $s, $l );
  137. # decrement depth level
  138. $l--;
  139. }
  140. # get only unique values
  141. if( $l == 0 ) $s = array_values( array_unique( $s ) );
  142. # return stored values
  143. return $s;
  144. } # end of function array_flat( ...
  145. }
  146. /**
  147. * Rounds up float/integer value. Used in estimate calculations
  148. *
  149. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  150. * @param float initial value
  151. * @return int
  152. */
  153. function round_up($value)
  154. {
  155. $num_decimals = strlen(substr(strrchr($value, "."), 1));
  156. if (!$num_decimals && $value%10 === 0) {
  157. $a = $value;
  158. } else {
  159. $a = ceil($value / 10) * 10;
  160. }
  161. return $a;
  162. }
  163. /**
  164. * Add business days to date
  165. *
  166. * @author Sergey Koshkarev
  167. * @param date initial date
  168. * @param int how many days to add
  169. * @param array holidays
  170. * @param string date format
  171. * @return date new date with adjustments
  172. */
  173. function add_business_days($startdate, $buisnessdays = 0, $holidays = array(), $dateformat = 'Y-m-d')
  174. {
  175. $enddate = strtotime($startdate);
  176. $day = date('N',$enddate);
  177. while($buisnessdays > 0)
  178. { // compatible with 1 businessday if I'll need it
  179. $enddate = strtotime(date('Y-m-d',$enddate).' +1 day');
  180. $day = date('N',$enddate);
  181. if($day < 6 && !in_array(date('Y-m-d',$enddate), $holidays))$buisnessdays--;
  182. }
  183. return date($dateformat, $enddate);
  184. }
  185. /**
  186. * Get the time of last modification for selected file
  187. *
  188. * @author Sergey Koshkarev
  189. * @param string
  190. * @return int time of modification
  191. */
  192. function get_file_modification_time($file_path = '')
  193. {
  194. if (empty($file_path))
  195. {
  196. return time();
  197. }
  198. if (file_exists($file_path))
  199. {
  200. return filemtime($file_path);
  201. }
  202. return time();
  203. }
  204. ?>