helpers_default.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. use \KarmaFW\Routing\Router;
  3. if (! function_exists('pre')) {
  4. function pre($var, $exit = false, $prefix = '') {
  5. echo "<pre>";
  6. if (!empty($prefix)) {
  7. echo $prefix;
  8. }
  9. if (is_null($var)) {
  10. echo "NULL";
  11. } else if ($var === true) {
  12. echo "TRUE";
  13. } else if ($var === false) {
  14. echo "FALSE";
  15. } else if (is_string($var)) {
  16. echo '"' . $var . '"';
  17. } else {
  18. print_r($var);
  19. }
  20. echo "</pre>";
  21. if ($exit) {
  22. exit;
  23. }
  24. }
  25. }
  26. if (! function_exists('ifEmpty')) {
  27. function ifEmpty($val, $default_value='-1') {
  28. return empty($val) ? $default_value : $val;
  29. }
  30. }
  31. if (! function_exists('errorHttp')) {
  32. function errorHttp($error_code, $message='An error has occured', $title='Error') {
  33. header("HTTP/1.0 " . $error_code . " " . $title);
  34. echo '<h1>' . $title . '</h1>';
  35. echo '<p>' . $message . '</p>';
  36. exit;
  37. }
  38. }
  39. if (! function_exists('redirect')) {
  40. function redirect($url, $http_code=302) {
  41. if ($http_code == 'link' || $http_code == 'debug') {
  42. echo 'continue to <a href="' . $url . '">' . $url . '</a>';
  43. exit;
  44. }
  45. header('Location: ' . $url, true, $http_code);
  46. exit;
  47. }
  48. }
  49. if (! function_exists('get')) {
  50. function get($key, $default_value=null) {
  51. return isset($_GET[$key]) ? $_GET[$key] : $default_value;
  52. }
  53. }
  54. if (! function_exists('post')) {
  55. function post($key, $default_value=null) {
  56. return isset($_POST[$key]) ? $_POST[$key] : $default_value;
  57. }
  58. }
  59. if (! function_exists('session')) {
  60. function session($key, $default_value=null) {
  61. return isset($_SESSION[$key]) ? $_SESSION[$key] : $default_value;
  62. }
  63. }
  64. if (! function_exists('cookie')) {
  65. function cookie($key, $default_value=null) {
  66. return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default_value;
  67. }
  68. }
  69. if (! function_exists('slugify')) {
  70. function slugify($text, $max_length=null) {
  71. // https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
  72. // https://stackoverflow.com/questions/3371697/replacing-accented-characters-php
  73. $unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  74. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  75. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  76. 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
  77. 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
  78. $text = strtr( $text, $unwanted_array );
  79. // replace non letter or digits by -
  80. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  81. // transliterate
  82. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  83. // remove unwanted characters
  84. $text = preg_replace('~[^-\w]+~', '', $text);
  85. // trim
  86. $text = trim($text, '-');
  87. // remove duplicate -
  88. $text = preg_replace('~-+~', '-', $text);
  89. // lowercase
  90. $text = strtolower($text);
  91. if (empty($text)) {
  92. return 'n-a';
  93. }
  94. if (! empty($max_length) && strlen($text) > $max_length) {
  95. $text = substr(0, $max_length);
  96. }
  97. return $text;
  98. }
  99. }
  100. if (! function_exists('generate_uid')) {
  101. function generate_uid() {
  102. if (function_exists('com_create_guid')) {
  103. return trim(com_create_guid(), '{}');
  104. }
  105. if (function_exists('openssl_random_pseudo_bytes') === true) {
  106. $data = openssl_random_pseudo_bytes(16);
  107. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  108. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  109. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  110. }
  111. mt_srand((double)microtime()*10000);
  112. $charid = strtoupper(md5(uniqid(rand(), true)));
  113. $uuid = sprintf(
  114. "%s-%s-%s-%s-%s",
  115. substr($charid, 0, 8),
  116. substr($charid, 8, 4),
  117. substr($charid,12, 4),
  118. substr($charid,16, 4),
  119. substr($charid,20,12)
  120. );
  121. return strtolower($uuid);
  122. }
  123. }
  124. if (! function_exists('generate_password')) {
  125. function generate_password($nb_chars = 8) {
  126. $ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
  127. $ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
  128. $ref = str_shuffle($ref);
  129. return substr($ref, 0, $nb_chars);
  130. }
  131. }
  132. if (! function_exists('getRouteUrl')) {
  133. function getRouteUrl($route_name, $urls_args=[]) {
  134. return Router::getRouteUrl($route_name, $urls_args);
  135. }
  136. }
  137. if (! function_exists('date_us_to_fr')) {
  138. function date_us_to_fr($date_us, $include_time=false) {
  139. if (empty($date_us)) {
  140. return null;
  141. }
  142. $time = ($include_time) ? substr($date_us, 10) : "";
  143. $date_us = substr($date_us, 0, 10);
  144. $parts = explode('-', $date_us);
  145. return implode('/', array_reverse($parts)) . $time;
  146. }
  147. }
  148. if (! function_exists('date_us2_to_fr')) {
  149. function date_us2_to_fr($date_us, $include_time=false) {
  150. if (empty($date_us)) {
  151. return null;
  152. }
  153. $time = ($include_time) ? substr($date_us, 10) : "";
  154. $date_us = substr($date_us, 0, 10);
  155. $parts = explode('/', $date_us);
  156. $parts2 = [
  157. substr('00' . $parts[1], -2),
  158. substr('00' . $parts[0], -2),
  159. $parts[2],
  160. ];
  161. return implode('/', $parts2) . $time;
  162. }
  163. }
  164. if (! function_exists('date_fr_to_us')) {
  165. function date_fr_to_us($date_fr, $include_time=false) {
  166. if (empty($date_fr)) {
  167. return null;
  168. }
  169. $time = ($include_time) ? substr($date_fr, 10) : "";
  170. $date_fr = substr($date_fr, 0, 10);
  171. $parts = explode('/', $date_fr);
  172. return implode('-', array_reverse($parts)) . $time;
  173. }
  174. }
  175. if (! function_exists('truncate_str')) {
  176. function truncate_str($str, $max_length) {
  177. if (strlen($str) > $max_length) {
  178. $str = substr($str, 0, $max_length-1) . '…';
  179. }
  180. return $str;
  181. }
  182. }
  183. if (! function_exists('get_url_path')) {
  184. function get_url_path($url, $with_querystring=true, $with_url_hash=false) {
  185. $url_parts = parse_url($url);
  186. $url = $url_parts['path'];
  187. if ($with_querystring && ! empty($url_parts['query'])) {
  188. $url .= '?' . $url_parts['query'];
  189. }
  190. if ($with_url_hash && ! empty($url_parts['fragment'])) {
  191. $url .= '#' . $url_parts['fragment'];
  192. }
  193. return $url;
  194. }
  195. }
  196. if (! function_exists('rrmdir')) {
  197. function rrmdir($src) {
  198. // https://www.php.net/manual/fr/function.rmdir.php
  199. $dir = opendir($src);
  200. while(false !== ( $file = readdir($dir)) ) {
  201. if (( $file != '.' ) && ( $file != '..' )) {
  202. $full = $src . '/' . $file;
  203. if ( is_dir($full) ) {
  204. rrmdir($full);
  205. }
  206. else {
  207. unlink($full);
  208. }
  209. }
  210. }
  211. closedir($dir);
  212. rmdir($src);
  213. }
  214. }