helpers_default.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // https://stackoverflow.com/questions/3371697/replacing-accented-characters-php
  69. $unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  70. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  71. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  72. 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
  73. 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
  74. $text = strtr( $text, $unwanted_array );
  75. // replace non letter or digits by -
  76. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  77. // transliterate
  78. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  79. // remove unwanted characters
  80. $text = preg_replace('~[^-\w]+~', '', $text);
  81. // trim
  82. $text = trim($text, '-');
  83. // remove duplicate -
  84. $text = preg_replace('~-+~', '-', $text);
  85. // lowercase
  86. $text = strtolower($text);
  87. if (empty($text)) {
  88. return 'n-a';
  89. }
  90. if (! empty($max_length) && strlen($text) > $max_length) {
  91. $text = substr(0, $max_length);
  92. }
  93. return $text;
  94. }
  95. }
  96. if (! function_exists('generate_uid')) {
  97. function generate_uid() {
  98. if (function_exists('com_create_guid')) {
  99. return trim(com_create_guid(), '{}');
  100. }
  101. if (function_exists('openssl_random_pseudo_bytes') === true) {
  102. $data = openssl_random_pseudo_bytes(16);
  103. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  104. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  105. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  106. }
  107. mt_srand((double)microtime()*10000);
  108. $charid = strtoupper(md5(uniqid(rand(), true)));
  109. $uuid = sprintf(
  110. "%s-%s-%s-%s-%s",
  111. substr($charid, 0, 8),
  112. substr($charid, 8, 4),
  113. substr($charid,12, 4),
  114. substr($charid,16, 4),
  115. substr($charid,20,12)
  116. );
  117. return strtolower($uuid);
  118. }
  119. }
  120. if (! function_exists('generate_password')) {
  121. function generate_password($nb_chars = 8) {
  122. $ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
  123. $ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
  124. $ref = str_shuffle($ref);
  125. return substr($ref, 0, $nb_chars);
  126. }
  127. }
  128. if (! function_exists('getRouteUrl')) {
  129. function getRouteUrl($route_name, $urls_args=[]) {
  130. return Router::getRouteUrl($route_name, $urls_args);
  131. }
  132. }
  133. if (! function_exists('date_us_to_fr')) {
  134. function date_us_to_fr($date_us) {
  135. $date_us = substr($date_us, 0, 10);
  136. $parts = explode('-', $date_us);
  137. return implode('/', array_reverse($parts));
  138. }
  139. }
  140. if (! function_exists('truncate_str')) {
  141. function truncate_str($str, $max_length) {
  142. if (strlen($str) > $max_length) {
  143. $str = substr($str, 0, $max_length-1) . '…';
  144. }
  145. return $str;
  146. }
  147. }
  148. if (! function_exists('get_url_path')) {
  149. function get_url_path($url, $with_querystring=true, $with_url_hash=false) {
  150. $url_parts = parse_url($url);
  151. $url = $url_parts['path'];
  152. if ($with_querystring && ! empty($url_parts['query'])) {
  153. $url .= '?' . $url_parts['query'];
  154. }
  155. if ($with_url_hash && ! empty($url_parts['fragment'])) {
  156. $url .= '#' . $url_parts['fragment'];
  157. }
  158. return $url;
  159. }
  160. }