helpers_default.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. header('Location: ' . $url, true, $http_code);
  42. exit;
  43. }
  44. }
  45. if (! function_exists('get')) {
  46. function get($key, $default_value=null) {
  47. return isset($_GET[$key]) ? $_GET[$key] : $default_value;
  48. }
  49. }
  50. if (! function_exists('post')) {
  51. function post($key, $default_value=null) {
  52. return isset($_POST[$key]) ? $_POST[$key] : $default_value;
  53. }
  54. }
  55. if (! function_exists('session')) {
  56. function session($key, $default_value=null) {
  57. return isset($_SESSION[$key]) ? $_SESSION[$key] : $default_value;
  58. }
  59. }
  60. if (! function_exists('cookie')) {
  61. function cookie($key, $default_value=null) {
  62. return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default_value;
  63. }
  64. }
  65. if (! function_exists('slugify')) {
  66. function slugify($text, $max_length=null) {
  67. // https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
  68. // replace non letter or digits by -
  69. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  70. // transliterate
  71. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  72. // remove unwanted characters
  73. $text = preg_replace('~[^-\w]+~', '', $text);
  74. // trim
  75. $text = trim($text, '-');
  76. // remove duplicate -
  77. $text = preg_replace('~-+~', '-', $text);
  78. // lowercase
  79. $text = strtolower($text);
  80. if (empty($text)) {
  81. return 'n-a';
  82. }
  83. if (! empty($max_length) && strlen($text) > $max_length) {
  84. $text = substr(0, $max_length);
  85. }
  86. return $text;
  87. }
  88. }
  89. if (! function_exists('geneateGuid')) {
  90. function geneateGuid() {
  91. if (function_exists('com_create_guid')) {
  92. return trim(com_create_guid(), '{}');
  93. }
  94. if (function_exists('openssl_random_pseudo_bytes') === true) {
  95. $data = openssl_random_pseudo_bytes(16);
  96. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  97. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  98. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  99. }
  100. mt_srand((double)microtime()*10000);
  101. $charid = strtoupper(md5(uniqid(rand(), true)));
  102. $uuid = sprintf(
  103. "%s-%s-%s-%s-%s",
  104. substr($charid, 0, 8),
  105. substr($charid, 8, 4),
  106. substr($charid,12, 4),
  107. substr($charid,16, 4),
  108. substr($charid,20,12)
  109. );
  110. return strtolower($uuid);
  111. }
  112. }
  113. if (! function_exists('generatePassword')) {
  114. function generatePassword($nb_chars = 8) {
  115. $ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
  116. $ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
  117. $ref = str_shuffle($ref);
  118. return substr($ref, 0, $nb_chars);
  119. }
  120. }
  121. if (! function_exists('getRouteUrl')) {
  122. function getRouteUrl($route_name, $urls_args=[]) {
  123. return Router::getRouteUrl($route_name, $urls_args);
  124. }
  125. }
  126. if (! function_exists('dateUstoFr')) {
  127. function dateUstoFr($date_us) {
  128. $date_us = substr($date_us, 0, 10);
  129. $parts = explode('-', $date_us);
  130. return implode('/', array_reverse($parts));
  131. }
  132. }